ehcache / ehcache3

Ehcache 3.x line
http://www.ehcache.org
Apache License 2.0
2.02k stars 580 forks source link

Error while cache.iterator() in ehcache-3.10.8.jar #3209

Open sathishkumar08082001 opened 10 months ago

sathishkumar08082001 commented 10 months ago

Here is my configuration

LocalPersistenceService persistenceService= new DefaultLocalPersistenceService(new DefaultPersistenceConfiguration(new File(cacheDirectory)));
PersistentUserManagedCache cache = UserManagedCacheBuilder.newUserManagedCacheBuilder(keyClass, valueClass)
                .with(new UserManagedPersistenceContext<>(cacheName, persistenceService))
                .withResourcePools(ResourcePoolsBuilder.newResourcePoolsBuilder()
                        .heap(100L, MemoryUnit.MB)
                        .disk(1024L, MemoryUnit.MB, true)).build();
cache.init();

cache.putAll(hashMap); // hashMap is loaded with data
Iterator iterator = cache.iterator();

Find the stack trace below,

[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: java.lang.NullPointerException|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.findChunk(FileBackedStorageEngine.java:363)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.readKeyBuffer(FileBackedStorageEngine.java:264)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.storage.PortabilityBasedStorageEngine.readKey(PortabilityBasedStorageEngine.java:119)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$DirectEntry.<init>(OffHeapHashMap.java:1562)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$EntryIterator.create(OffHeapHashMap.java:1540)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:133)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:130)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$HashIterator.<init>(OffHeapHashMap.java:1400)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$EntryIterator.<init>(OffHeapHashMap.java:1535)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.AbstractLockedOffHeapHashMap$LockedEntryIterator.<init>(AbstractLockedOffHeapHashMap.java:385)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.<init>(EhcachePersistentSegmentFactory.java:130)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet.iterator(EhcachePersistentSegmentFactory.java:130)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateEntrySet$1.getNextIterator(AbstractConcurrentOffHeapMap.java:454)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateIterator.<init>(AbstractConcurrentOffHeapMap.java:520)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateEntrySet$1.<init>(AbstractConcurrentOffHeapMap.java:450)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateEntrySet.iterator(AbstractConcurrentOffHeapMap.java:450)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore$1.<init>(AbstractOffHeapStore.java:501)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.iterator(AbstractOffHeapStore.java:500)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.impl.internal.store.tiering.TieredStore.iterator(TieredStore.java:234)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.core.EhcacheBase$CacheEntryIterator.<init>(EhcacheBase.java:727)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.core.EhcacheBase.iterator(EhcacheBase.java:321)|
[08:21:08:300]|[01-02-2024]|[SYSERR]|[INFO]|[139]: at org.ehcache.core.PersistentUserManagedEhcache.iterator(PersistentUserManagedEhcache.java:245)|

The data is being stored in cache directory. When i try to iterate over the data , the above issue reproduces, but not always. Once i faced this issue, I have manually deleted the cached data from cache directory. Again i have executed the same code,, the data is loaded in to disk and used iterator,, then its working fine... Hence this issue is not reproducing always.. Please help me with the solution as soon as possible.

chrisdennis commented 10 months ago

The first thing that springs to mind here is some kind of issue with either not shutting the cache down properly, or with the logic that detects this. There have been improvements to the "dirty shutdown" detection logic recently but they're not in any official release yet. As a quick fix you could try using one of the internal releases available via the Terracotta Maven repository to see if that solves the problem: https://mvnrepository.com/artifact/org.ehcache/ehcache/3.10.9-internal17

If not we're probably going to need more information about how the cache is lifecycled, when is it shutdown, when is it created, and could anything else be messing with the contents of the persistence directory other than Ehcache.

sathishkumar08082001 commented 10 months ago

Thank you so much for your valuable comments, chris. Let me once again look in to my cache shutdown flow. Other than shut logic, is there any possibilities of this issue related to cache configuration. If so, Please suggest me the better cache configuration.

sathishkumar08082001 commented 7 months ago

Hello Chris, My previous was resolved by rectifying the control flow. Now facing the new issue,

org.ehcache.spi.resilience.StoreAccessException: java.lang.RuntimeException: java.io.EOFException at org.ehcache.core.EhcacheBase$CacheEntryIterator.advance(EhcacheBase.java:741) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.core.EhcacheBase$CacheEntryIterator.next(EhcacheBase.java:765) [ehcache-3.10.8.jar:3.10.8] at org.ehcache.core.EhcacheBase$CacheEntryIterator.next(EhcacheBase.java:719) [ehcache-3.10.8.jar:3.10.8] at mypackage.DiskCache.getAll(DiskCache.java:86)

Caused by: java.lang.RuntimeException: java.io.EOFException at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.readBuffer(FileBackedStorageEngine.java:574) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.readKeyBuffer(FileBackedStorageEngine.java:507) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.readKeyBuffer(FileBackedStorageEngine.java:265) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.storage.PortabilityBasedStorageEngine.readKey(PortabilityBasedStorageEngine.java:119) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$DirectEntry.(OffHeapHashMap.java:1562) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$EntryIterator.create(OffHeapHashMap.java:1540) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:133) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:130) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$HashIterator.next(OffHeapHashMap.java:1429) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.AbstractLockedOffHeapHashMap$LockedEntryIterator.next(AbstractLockedOffHeapHashMap.java:392) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.AbstractLockedOffHeapHashMap$LockedEntryIterator.next(AbstractLockedOffHeapHashMap.java:385) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateIterator.next(AbstractConcurrentOffHeapMap.java:553) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore$1.next(AbstractOffHeapStore.java:510) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore$1.next(AbstractOffHeapStore.java:500) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.advance(TieredStore.java:279) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.next(TieredStore.java:261) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.next(TieredStore.java:236) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.core.EhcacheBase$CacheEntryIterator.advance(EhcacheBase.java:734) ~[ehcache-3.10.8.jar:3.10.8] ... 16 more Caused by: java.io.EOFException at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.readBuffer(FileBackedStorageEngine.java:567) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.readKeyBuffer(FileBackedStorageEngine.java:507) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.readKeyBuffer(FileBackedStorageEngine.java:265) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.storage.PortabilityBasedStorageEngine.readKey(PortabilityBasedStorageEngine.java:119) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$DirectEntry.(OffHeapHashMap.java:1562) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$EntryIterator.create(OffHeapHashMap.java:1540) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:133) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.disk.factories.EhcachePersistentSegmentFactory$EhcachePersistentSegment$EntrySet$1.create(EhcachePersistentSegmentFactory.java:130) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.OffHeapHashMap$HashIterator.next(OffHeapHashMap.java:1429) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.AbstractLockedOffHeapHashMap$LockedEntryIterator.next(AbstractLockedOffHeapHashMap.java:392) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.AbstractLockedOffHeapHashMap$LockedEntryIterator.next(AbstractLockedOffHeapHashMap.java:385) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.shadow.org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap$AggregateIterator.next(AbstractConcurrentOffHeapMap.java:553) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore$1.next(AbstractOffHeapStore.java:510) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore$1.next(AbstractOffHeapStore.java:500) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.advance(TieredStore.java:279) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.next(TieredStore.java:261) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.impl.internal.store.tiering.TieredStore$2.next(TieredStore.java:236) ~[ehcache-3.10.8.jar:3.10.8] at org.ehcache.core.EhcacheBase$CacheEntryIterator.advance(EhcacheBase.java:734) ~[ehcache-3.10.8.jar:3.10.8] ... 16 more | [15:20:01:927]|[04-09-2024]|[SYSERR]|[INFO]|[299]: org.ehcache.CacheIterationException: org.ehcache.spi.resilience.StoreAccessException: java.lang.RuntimeException: java.io.EOFException| [15:20:01:927]|[04-09-2024]|[SYSERR]|[INFO]|[299]: at org.ehcache.impl.internal.resilience.AbstractResilienceStrategy.iteratorFailure(AbstractResilienceStrategy.java:59)| [15:20:01:927]|[04-09-2024]|[SYSERR]|[INFO]|[299]: at org.ehcache.core.EhcacheBase$CacheEntryIterator.next(EhcacheBase.java:771)| [15:20:01:927]|[04-09-2024]|[SYSERR]|[INFO]|[299]: at org.ehcache.core.EhcacheBase$CacheEntryIterator.next(EhcacheBase.java:719)| [15:20:01:927]|[04-09-2024]|[SYSERR]|[INFO]|[299]: at mypackage.DiskCache.getAll(DiskCache.java:86)|,

For, DiskCache.java public Map<K,V> getAll(){ initCache(); Map<K,V> values= new HashMap<>(); Iterator iterator = cache.iterator(); while (iterator.hasNext()) { Entry<K, V> entry = (Cache.Entry) iterator.next(); //line 86 values.put(entry.getKey(), entry.getValue()); } return values; } - Please help me to resolve the issue