Исключение ClosedChannelException при использовании Ehcache - PullRequest
0 голосов
/ 30 мая 2019

У меня проблема с Ehcache. Обычно он работает нормально, но иногда я получаю исключение ClosedChannelException при попытке получить доступ к кешу, и как только это произойдет, я могу обойти проблему только путем воссоздания кеша

Вот мой класс

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$1.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$16(AbstractOffHeapStore.java:865)
    at org.ehcache.impl.internal.store.disk.EhcachePersistentConcurrentOffHeapClockCache.lambda$computeIfPinned$3(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$19(OnHeapStore.java:914)
    at org.ehcache.impl.internal.store.heap.OnHeapStore.lambda$evict$28(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.access$700(FileBackedStorageEngine.java:60)
    at org.terracotta.offheapstore.disk.storage.FileBackedStorageEngine$FileChunk$1.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 безопасен для потоков?

1 Ответ

1 голос
/ 30 мая 2019

В этой области было несколько ошибок. Я думаю, что вы стали жертвой варианта: https://github.com/Terracotta-OSS/offheap-store/pull/53

Если бы вы могли обновить Ehcache 3.7.1, в котором исправлены эти проблемы, и посмотреть, сможете ли вы воспроизвести. Если это не решит проблему, пожалуйста, отправьте сообщение об ошибке на https://github.com/ehcache/ehcache3

...