使用Ehcache时出现ClosedChannelException异常
ClosedChannelException exception when using Ehcache
我在使用 Ehcache 时遇到问题。它通常工作正常,但偶尔我在尝试访问缓存时会遇到 ClosedChannelException,一旦发生这种情况,我只能通过重新创建缓存来解决问题
这是我的 class
package com.jthink.songkong.cache;
import com.jthink.acoustid.acoustidschema.Result;
import com.jthink.songkong.analyse.general.Errors;
import org.ehcache.CachePersistenceException;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.statistics.CacheStatistics;
import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* AcoustIdResult EhCache
*
*/
public class AcoustIdResultCache
{
protected org.ehcache.Cache<String, Result> cache;
public static final String ACOUSTID_RESULT_CACHE = "AcoustidResultCache";
private static AcoustIdResultCache apc;
private AcoustIdResultCache()
{
createCache();
}
static
{
apc = new AcoustIdResultCache();
}
public static AcoustIdResultCache getInstanceOf()
{
if(apc==null)
{
apc = new AcoustIdResultCache();
}
return apc;
}
/**
* Create the Cache
*/
private void createCache()
{
cache = SongKongCacheManager.getCacheManager().createCache(getCacheName(),
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
Result.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(300, EntryUnit.ENTRIES)
.disk(500, MemoryUnit.MB, true)
)
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(10)) //Since Acoustid is Live Db
)
);
}
protected AcoustIdResultCache(String cacheName)
{
createCache();
}
public void init()
{
}
/**
* Close Cache Manager
*/
public static void close()
{
SongKongCacheManager.getCacheManager().close();
}
public void emptyCache()
{
try
{
SongKongCacheManager.getCacheManager().destroyCache(getCacheName());
createCache();
}
catch(CachePersistenceException cpe)
{
Errors.addError("Problem emptying cache for"+getCacheName()+":"+cpe.getMessage(), cpe);
}
}
public void clearCacheStatistics()
{
SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName()).clear();
}
/**
*
* @return cache name
*/
protected String getCacheName()
{
return ACOUSTID_RESULT_CACHE;
}
/**
*
* @return cache
*/
protected org.ehcache.Cache<String, Result> getCache()
{
return cache;
}
/**
*
* @return statistics of cache
*/
public String statusCache()
{
CacheStatistics ehCacheStat = SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName());
return "CacheName:" + getCacheName()
+ ":Put:" + ehCacheStat.getCachePuts()
+ ":Hit:" + ehCacheStat.getCacheHits()
+ ":Miss:" + ehCacheStat.getCacheMisses()
+ ":Evictions:" + ehCacheStat.getCacheEvictions()
+ ":Removals:" + ehCacheStat.getCacheRemovals();
}
/**
* Add an item to cache
*
* @param item
*/
public void add(Result item)
{
addToCache(item);
}
/**
* Get Item from cache
*
* @param id
* @return the item or null
*/
public Result get(String id)
{
return getFromCache(id);
}
/**
* Is there an item with this id in cache
* @param id
* @return
*/
public boolean isInCache(String id)
{
return getCache().containsKey(id);
}
/**
*
* @param id
* @return item for this id or null if doesn't not exist
*/
protected Result getFromCache(String id)
{
return getCache().get(id);
}
/**
* For adding multiple items to cache
*
* @param items
* @return
*/
public boolean addToCache(Collection<Result> items)
{
try
{
Map<String, Result> map = new HashMap<String, Result>();
for(Result acoustidResult:items)
{
map.put(acoustidResult.getId(), acoustidResult);
}
getCache().putAll(map);
return true;
}
catch(Exception e)
{
Errors.addError("Failed AddResultToCache:" + e.getMessage(), e);
return false;
}
}
/**
* Add item to cache
*
* @param item
*/
protected boolean addToCache(Result item)
{
try
{
getCache().putIfAbsent(item.getId(), item);
return true;
}
catch(Exception e)
{
Errors.addError("Failed AddAcoustidToCache:"+item.getId()+ ':' +e.getMessage(),e);
return false;
}
}
}
这是堆栈跟踪
java.lang.RuntimeException: java.nio.channels.ClosedChannelException
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.setLong(FileBackedStorageEngine.java:677)
at org.ehcache.impl.internal.store.offheap.LazyOffHeapValueHolder.writeBack(LazyOffHeapValueHolder.java:82)
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.lambda$flush(AbstractOffHeapStore.java:865)
at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.lambda$computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:213)
at org.terracotta.offheapstore.OffHeapHashMap.computeIfPresentWithMetadata(OffHeapHashMap.java:2096)
at org.terracotta.offheapstore.AbstractLockedOffHeapHashMap.computeIfPresentWithMetadata(AbstractLockedOffHeapHashMap.java:604)
at org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap.computeIfPresentWithMetadata(AbstractConcurrentOffHeapMap.java:781)
at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:210)
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.flush(AbstractOffHeapStore.java:858)
at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$setInvalidationListener(OnHeapStore.java:914)
at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$evict(OnHeapStore.java:1552)
at org.ehcache.impl.internal.concurrent.ConcurrentHashMap.computeIfPresent(ConcurrentHashMap.java:1848)
at org.ehcache.impl.internal.store.heap.SimpleBackend.computeIfPresent(SimpleBackend.java:130)
at org.ehcache.impl.internal.store.heap.OnHeapStore.evict(OnHeapStore.java:1547)
at org.ehcache.impl.internal.store.heap.OnHeapStore.enforceCapacity(OnHeapStore.java:1514)
at org.ehcache.impl.internal.store.heap.OnHeapStore.resolveFault(OnHeapStore.java:745)
at org.ehcache.impl.internal.store.heap.OnHeapStore.getOrComputeIfAbsent(OnHeapStore.java:692)
at org.ehcache.impl.internal.store.tiering.TieredStore.get(TieredStore.java:88)
at org.ehcache.core.Ehcache.doGet(Ehcache.java:90)
at org.ehcache.core.EhcacheBase.get(EhcacheBase.java:127)
at com.jthink.songkong.cache.AcoustIdResultCache.getFromCache(AcoustIdResultCache.java:177)
at com.jthink.songkong.cache.AcoustIdResultCache.get(AcoustIdResultCache.java:157)
at com.jthink.songkong.analyse.acoustid.AcoustidHelper.getMusicBrainzAcoustidResultsFromCacheOrDb(AcoustidHelper.java:136)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.getReleasesByAcoustid(MusicBrainzSingleSongMatcher.java:99)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.queryForCandidateReleasesByArtistTitleAndReleaseForSingleSong(MusicBrainzSingleSongMatcher.java:335)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.matchSingleSongToReleaseUsingExistingFileMetadata(MusicBrainzSingleSongMatcher.java:396)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.match(MusicBrainzSingleSongMatcher.java:72)
at com.jthink.songkong.analyse.analyser.AbstractMusicBrainzGroupMatcher.matchSongs(AbstractMusicBrainzGroupMatcher.java:146)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.doTask(MusicBrainzSongGroupMatcher1.java:420)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher.call(MusicBrainzSongGroupMatcher.java:469)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:97)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:40)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:758)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeBufferToChannel(FileBackedStorageEngine.java:393)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeLongToChannel(FileBackedStorageEngine.java:388)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.access0(FileBackedStorageEngine.java:60)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.setLong(FileBackedStorageEngine.java:675)
... 35 more
我不明白为什么会这样,我的第一个想法是我应该修改
protected org.ehcache.Cache<String, Result> getCache()
{
return cache;
}
到
protected org.ehcache.Cache<String, Result> getCache()
{
if(cache==null)
{
cache = createCache();
}
return cache;
}
但为什么缓存永远为空?
我的第二个想法是存在一些多线程问题,但我假设 Ehcache 是线程安全的?
这方面有几个错误。我认为您正在成为以下变体的受害者:https://github.com/Terracotta-OSS/offheap-store/pull/53
如果您可以更新到包含这些问题修复程序的 Ehcache 3.7.1,看看您是否可以重现。如果这不能解决问题,请在 https://github.com/ehcache/ehcache3
提交错误
我在使用 Ehcache 时遇到问题。它通常工作正常,但偶尔我在尝试访问缓存时会遇到 ClosedChannelException,一旦发生这种情况,我只能通过重新创建缓存来解决问题
这是我的 class
package com.jthink.songkong.cache;
import com.jthink.acoustid.acoustidschema.Result;
import com.jthink.songkong.analyse.general.Errors;
import org.ehcache.CachePersistenceException;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.ehcache.core.statistics.CacheStatistics;
import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* AcoustIdResult EhCache
*
*/
public class AcoustIdResultCache
{
protected org.ehcache.Cache<String, Result> cache;
public static final String ACOUSTID_RESULT_CACHE = "AcoustidResultCache";
private static AcoustIdResultCache apc;
private AcoustIdResultCache()
{
createCache();
}
static
{
apc = new AcoustIdResultCache();
}
public static AcoustIdResultCache getInstanceOf()
{
if(apc==null)
{
apc = new AcoustIdResultCache();
}
return apc;
}
/**
* Create the Cache
*/
private void createCache()
{
cache = SongKongCacheManager.getCacheManager().createCache(getCacheName(),
CacheConfigurationBuilder.newCacheConfigurationBuilder(
String.class,
Result.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(300, EntryUnit.ENTRIES)
.disk(500, MemoryUnit.MB, true)
)
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofDays(10)) //Since Acoustid is Live Db
)
);
}
protected AcoustIdResultCache(String cacheName)
{
createCache();
}
public void init()
{
}
/**
* Close Cache Manager
*/
public static void close()
{
SongKongCacheManager.getCacheManager().close();
}
public void emptyCache()
{
try
{
SongKongCacheManager.getCacheManager().destroyCache(getCacheName());
createCache();
}
catch(CachePersistenceException cpe)
{
Errors.addError("Problem emptying cache for"+getCacheName()+":"+cpe.getMessage(), cpe);
}
}
public void clearCacheStatistics()
{
SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName()).clear();
}
/**
*
* @return cache name
*/
protected String getCacheName()
{
return ACOUSTID_RESULT_CACHE;
}
/**
*
* @return cache
*/
protected org.ehcache.Cache<String, Result> getCache()
{
return cache;
}
/**
*
* @return statistics of cache
*/
public String statusCache()
{
CacheStatistics ehCacheStat = SongKongCacheManager.getStatistics().getCacheStatistics(getCacheName());
return "CacheName:" + getCacheName()
+ ":Put:" + ehCacheStat.getCachePuts()
+ ":Hit:" + ehCacheStat.getCacheHits()
+ ":Miss:" + ehCacheStat.getCacheMisses()
+ ":Evictions:" + ehCacheStat.getCacheEvictions()
+ ":Removals:" + ehCacheStat.getCacheRemovals();
}
/**
* Add an item to cache
*
* @param item
*/
public void add(Result item)
{
addToCache(item);
}
/**
* Get Item from cache
*
* @param id
* @return the item or null
*/
public Result get(String id)
{
return getFromCache(id);
}
/**
* Is there an item with this id in cache
* @param id
* @return
*/
public boolean isInCache(String id)
{
return getCache().containsKey(id);
}
/**
*
* @param id
* @return item for this id or null if doesn't not exist
*/
protected Result getFromCache(String id)
{
return getCache().get(id);
}
/**
* For adding multiple items to cache
*
* @param items
* @return
*/
public boolean addToCache(Collection<Result> items)
{
try
{
Map<String, Result> map = new HashMap<String, Result>();
for(Result acoustidResult:items)
{
map.put(acoustidResult.getId(), acoustidResult);
}
getCache().putAll(map);
return true;
}
catch(Exception e)
{
Errors.addError("Failed AddResultToCache:" + e.getMessage(), e);
return false;
}
}
/**
* Add item to cache
*
* @param item
*/
protected boolean addToCache(Result item)
{
try
{
getCache().putIfAbsent(item.getId(), item);
return true;
}
catch(Exception e)
{
Errors.addError("Failed AddAcoustidToCache:"+item.getId()+ ':' +e.getMessage(),e);
return false;
}
}
}
这是堆栈跟踪
java.lang.RuntimeException: java.nio.channels.ClosedChannelException
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.setLong(FileBackedStorageEngine.java:677)
at org.ehcache.impl.internal.store.offheap.LazyOffHeapValueHolder.writeBack(LazyOffHeapValueHolder.java:82)
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.lambda$flush(AbstractOffHeapStore.java:865)
at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.lambda$computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:213)
at org.terracotta.offheapstore.OffHeapHashMap.computeIfPresentWithMetadata(OffHeapHashMap.java:2096)
at org.terracotta.offheapstore.AbstractLockedOffHeapHashMap.computeIfPresentWithMetadata(AbstractLockedOffHeapHashMap.java:604)
at org.terracotta.offheapstore.concurrent.AbstractConcurrentOffHeapMap.computeIfPresentWithMetadata(AbstractConcurrentOffHeapMap.java:781)
at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.computeIfPinned(EhcachePersistentConcurrentOffHeapClockCache.java:210)
at org.ehcache.impl.internal.store.offheap.AbstractOffHeapStore.flush(AbstractOffHeapStore.java:858)
at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$setInvalidationListener(OnHeapStore.java:914)
at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$evict(OnHeapStore.java:1552)
at org.ehcache.impl.internal.concurrent.ConcurrentHashMap.computeIfPresent(ConcurrentHashMap.java:1848)
at org.ehcache.impl.internal.store.heap.SimpleBackend.computeIfPresent(SimpleBackend.java:130)
at org.ehcache.impl.internal.store.heap.OnHeapStore.evict(OnHeapStore.java:1547)
at org.ehcache.impl.internal.store.heap.OnHeapStore.enforceCapacity(OnHeapStore.java:1514)
at org.ehcache.impl.internal.store.heap.OnHeapStore.resolveFault(OnHeapStore.java:745)
at org.ehcache.impl.internal.store.heap.OnHeapStore.getOrComputeIfAbsent(OnHeapStore.java:692)
at org.ehcache.impl.internal.store.tiering.TieredStore.get(TieredStore.java:88)
at org.ehcache.core.Ehcache.doGet(Ehcache.java:90)
at org.ehcache.core.EhcacheBase.get(EhcacheBase.java:127)
at com.jthink.songkong.cache.AcoustIdResultCache.getFromCache(AcoustIdResultCache.java:177)
at com.jthink.songkong.cache.AcoustIdResultCache.get(AcoustIdResultCache.java:157)
at com.jthink.songkong.analyse.acoustid.AcoustidHelper.getMusicBrainzAcoustidResultsFromCacheOrDb(AcoustidHelper.java:136)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.getReleasesByAcoustid(MusicBrainzSingleSongMatcher.java:99)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.queryForCandidateReleasesByArtistTitleAndReleaseForSingleSong(MusicBrainzSingleSongMatcher.java:335)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.matchSingleSongToReleaseUsingExistingFileMetadata(MusicBrainzSingleSongMatcher.java:396)
at com.jthink.songkong.analyse.analyser.MusicBrainzSingleSongMatcher.match(MusicBrainzSingleSongMatcher.java:72)
at com.jthink.songkong.analyse.analyser.AbstractMusicBrainzGroupMatcher.matchSongs(AbstractMusicBrainzGroupMatcher.java:146)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.doTask(MusicBrainzSongGroupMatcher1.java:420)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher.call(MusicBrainzSongGroupMatcher.java:469)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:97)
at com.jthink.songkong.analyse.analyser.MusicBrainzSongGroupMatcher1.call(MusicBrainzSongGroupMatcher1.java:40)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.ClosedChannelException
at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:110)
at sun.nio.ch.FileChannelImpl.write(FileChannelImpl.java:758)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeBufferToChannel(FileBackedStorageEngine.java:393)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.writeLongToChannel(FileBackedStorageEngine.java:388)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine.access0(FileBackedStorageEngine.java:60)
at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk.setLong(FileBackedStorageEngine.java:675)
... 35 more
我不明白为什么会这样,我的第一个想法是我应该修改
protected org.ehcache.Cache<String, Result> getCache()
{
return cache;
}
到
protected org.ehcache.Cache<String, Result> getCache()
{
if(cache==null)
{
cache = createCache();
}
return cache;
}
但为什么缓存永远为空?
我的第二个想法是存在一些多线程问题,但我假设 Ehcache 是线程安全的?
这方面有几个错误。我认为您正在成为以下变体的受害者:https://github.com/Terracotta-OSS/offheap-store/pull/53
如果您可以更新到包含这些问题修复程序的 Ehcache 3.7.1,看看您是否可以重现。如果这不能解决问题,请在 https://github.com/ehcache/ehcache3
提交错误