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_r174512220
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileChannelBackingCache.java
 ##########
 @@ -52,36 +53,33 @@
     final ConcurrentHashMap<Long, CachedFileChannel> fileChannels = new 
ConcurrentHashMap<>();
 
     CachedFileChannel loadFileChannel(long logId) throws IOException {
-        lock.readLock().lock();
-        try {
-            CachedFileChannel cachedFileChannel = fileChannels.get(logId);
+        CachedFileChannel cachedFileChannel = null;
+        do {
+            fileChannels.computeIfAbsent(logId, logFileId -> {
+                CachedFileChannel cfc = null;
+                try {
+                    File file = fileLoader.load(logFileId);
+                    // get channel is used to open an existing entry log file
+                    // it would be better to open using read mode
+                    FileChannel newFc = new RandomAccessFile(file, 
"r").getChannel();
+                    cfc = new CachedFileChannel(logFileId, newFc);
+                } catch (IOException ioe){
+                    throw new UncheckedIOException(ioe);
 
 Review comment:
   This will end up doing throwing a RuntimeException. Rather than using a call 
to computeIfAbsent(), do:
   ```
   CachedFileChannel cachedFileChannel = null;
   do {
       CachedFileChannel c = fileChannels.get(logId);
       if (c != null) {
           if (c.tryRetain()) {
               cachedFileChannel = c;
           } else {
               // this isn't strictly necessary, but it's good defensively to 
avoid infinite loop
               fileChannels.remove(logId, c);
           }
       } else {
           c = // the construction stuff
           CachedFileChannel existing = fileChannels.putIfAbsent(logId, c);
           if (existing != null) {
               // cleanup c
               if (existing.tryRetain()) {
                   cachedFileChannel = existing;
               } else {
                   fileChannels.remove(logId, existing);
               }
           }
       }
   } while (cachedFileChannel == null);
   ```

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