ivankelly commented on a change in pull request #832: Issue 620: Close the 
fileChannels for read when they are idle
URL: https://github.com/apache/bookkeeper/pull/832#discussion_r164785989
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
 ##########
 @@ -330,54 +334,118 @@ private int readFromLogChannel(long entryLogId, 
BufferedReadChannel channel, Byt
      * A thread-local variable that wraps a mapping of log ids to 
bufferedchannels
      * These channels should be used only for reading. logChannel is the one
      * that is used for writes.
+     * We use this Guava cache to store the BufferedReadChannel.
+     * When the BufferedReadChannel is removed, the underlying fileChannel's 
refCnt decrease 1,
+     * temporally use 1h to relax replace after reading.
      */
-    private final ThreadLocal<Map<Long, BufferedReadChannel>> logid2Channel =
-            new ThreadLocal<Map<Long, BufferedReadChannel>>() {
+    private final ThreadLocal<Cache<Long, BufferedReadChannel>> logid2Channel =
+            new ThreadLocal<Cache<Long, BufferedReadChannel>>() {
         @Override
-        public Map<Long, BufferedReadChannel> initialValue() {
+        public Cache<Long, BufferedReadChannel> initialValue() {
             // Since this is thread local there only one modifier
             // We dont really need the concurrency, but we need to use
             // the weak values. Therefore using the concurrency level of 1
-            return new MapMaker().concurrencyLevel(1)
-                .weakValues()
-                .makeMap();
+            return CacheBuilder.newBuilder().concurrencyLevel(1)
+                    .expireAfterAccess(readChannelCacheExpireTimeMs, 
TimeUnit.MILLISECONDS)
+                    //decrease the refCnt
+                    .removalListener(removal -> 
logid2FileChannel.get(removal.getKey()).release())
+                    .build(readChannelLoader);
+        }
+    };
+
+    @VisibleForTesting
+    long getReadChannelCacheExpireTimeMs() {
+        return readChannelCacheExpireTimeMs;
+    }
+
+    @VisibleForTesting
+    CacheLoader<Long, BufferedReadChannel> getReadChannelLoader() {
+        return readChannelLoader;
+    }
+
+    private final  CacheLoader<Long, BufferedReadChannel> readChannelLoader =
+            new CacheLoader<Long, BufferedReadChannel> () {
+        public BufferedReadChannel load(Long entryLogId) throws Exception {
+
+            return getChannelForLogId(entryLogId);
+
         }
     };
 
+    static class ReferenceCountedFileChannel extends AbstractReferenceCounted {
+        private final FileChannel fc;
+        AtomicBoolean dead;
+
+        public ReferenceCountedFileChannel(FileChannel fileChannel) {
+            this.fc = fileChannel;
+            dead = new AtomicBoolean(false);
+        }
+
+        @VisibleForTesting
+        FileChannel getFileChannel(){
+            return fc;
+        }
+
+        @Override
+        public ReferenceCounted touch(Object hint) {
+            return this;
+        }
+
+        // when the refCnt decreased to 0 or force deallocate
+        @Override
+        protected void deallocate() {
+            dead.compareAndSet(false, true);
+            try {
+                fc.close();
+            } catch (IOException e) {
+                LOG.warn("Exception occurred in ReferenceCountedFileChannel"
+                        + " while closing channel for log file: {}", fc);
+            } finally {
+                IOUtils.close(LOG, fc);
+            }
+        }
+
+    }
+
     /**
      * Each thread local buffered read channel can share the same file handle 
because reads are not relative
-     * and don't cause a change in the channel's position. We use this map to 
store the file channels. Each
-     * file channel is mapped to a log id which represents an open log file.
+     * and don't cause a change in the channel's position.
+     * Each file channel is mapped to a log id which represents an open log 
file.
+     * when ReferenceCountedFileChannel's refCnt decrease to 0, close the 
fileChannel.
      */
-    private final ConcurrentMap<Long, FileChannel> logid2FileChannel = new 
ConcurrentHashMap<Long, FileChannel>();
+    private ConcurrentMap<Long, ReferenceCountedFileChannel>
+            logid2FileChannel = new ConcurrentHashMap<>();
+    @VisibleForTesting
+    ConcurrentMap<Long, ReferenceCountedFileChannel> getLogid2FileChannel() {
+        return logid2FileChannel;
+    }
 
     /**
      * Put the logId, bc pair in the map responsible for the current thread.
      * @param logId
      * @param bc
      */
-    public BufferedReadChannel putInReadChannels(long logId, 
BufferedReadChannel bc) {
-        Map<Long, BufferedReadChannel> threadMap = logid2Channel.get();
-        return threadMap.put(logId, bc);
+    public void putInReadChannels(long logId, BufferedReadChannel bc) {
+        Cache<Long, BufferedReadChannel> threadCahe = logid2Channel.get();
 
 Review comment:
   typo

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to