ArvinDevel 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_r163567948
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
 ##########
 @@ -330,54 +334,115 @@ 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;
+
+        public ReferenceCountedFileChannel(FileChannel fileChannel) {
+            this.fc = fileChannel;
+        }
+
+        @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() {
+            try {
+                fc.close();
 
 Review comment:
   @ivankelly I have fixed your comment. I'm not sure whether it still has race 
condition, please has a review. If it still has race condition, I'll try use 
your design in #913. And the last resort can be removing hashMap and use 
multiple fileChannel directly.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to