You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/03/01 18:44:58 UTC

[GitHub] sijie commented on a change in pull request #832: Issue 620: Close the fileChannels for read when they are idle

sijie 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_r171654510
 
 

 ##########
 File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
 ##########
 @@ -330,54 +350,47 @@ 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, EntryLogBufferedReadChannel>> logid2ReadChannel =
+            new ThreadLocal<Cache<Long, EntryLogBufferedReadChannel>>() {
         @Override
-        public Map<Long, BufferedReadChannel> initialValue() {
+        public Cache<Long, EntryLogBufferedReadChannel> 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 -> ((EntryLogBufferedReadChannel) removal.getValue()).release())
 
 Review comment:
   @ArvinDevel 
   
   a better way here is not to cast the object itself, but assigning the type for the lambda closure.
   
   ```
   CacheBuilder.newBuilder()
               .removalListener((RemovalListener<String, EntryLogBufferedReadChannel>) notification -> notification.getValue.release)
               .build();
   ```
   
   @ivankelly because it is not pure RemovalListener<K, V>. it is RemovalListener<K1 extends K, V1 extends V>, you need type to do the inference. I believe oracle jdk and open jdk have different ways on interpreting this, even different java versions as well. so a safer approach is to have the interface type for lambda function.

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


With regards,
Apache Git Services