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/24 21:41:53 UTC

[GitHub] sijie closed pull request #1284: Improve FileInfoBackingCache

sijie closed pull request #1284: Improve FileInfoBackingCache
URL: https://github.com/apache/bookkeeper/pull/1284
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileInfoBackingCache.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileInfoBackingCache.java
index e4cb18392..31fd077a5 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileInfoBackingCache.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileInfoBackingCache.java
@@ -22,24 +22,35 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import java.io.UncheckedIOException;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.bookkeeper.util.collections.ConcurrentLongHashMap;
 
 @Slf4j
 class FileInfoBackingCache {
     static final int DEAD_REF = -0xdead;
 
     final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
-    final ConcurrentHashMap<Long, CachedFileInfo> fileInfos = new ConcurrentHashMap<>();
+    final ConcurrentLongHashMap<CachedFileInfo> fileInfos = new ConcurrentLongHashMap<>();
     final FileLoader fileLoader;
 
     FileInfoBackingCache(FileLoader fileLoader) {
         this.fileLoader = fileLoader;
     }
 
+    /**
+     * This method should be under `lock` of FileInfoBackingCache.
+     */
+    private static CachedFileInfo tryRetainFileInfo(CachedFileInfo fi) throws IOException {
+        boolean retained = fi.tryRetain();
+        if (!retained) {
+            throw new IOException("FileInfo " + fi + " is already marked dead");
+        }
+        return fi;
+    }
+
     CachedFileInfo loadFileInfo(long ledgerId, byte[] masterKey) throws IOException {
         lock.readLock().lock();
         try {
@@ -52,25 +63,29 @@ CachedFileInfo loadFileInfo(long ledgerId, byte[] masterKey) throws IOException
                 // have been able to get a reference to it here.
                 // The caller of loadFileInfo owns the refence, and is
                 // responsible for calling the corresponding #release().
-                boolean retained = fi.tryRetain();
-                assert(retained);
-                return fi;
+                return tryRetainFileInfo(fi);
             }
         } finally {
             lock.readLock().unlock();
         }
 
+        File backingFile = fileLoader.load(ledgerId, masterKey != null);
+        CachedFileInfo newFi = new CachedFileInfo(ledgerId, backingFile, masterKey);
+
         // else FileInfo not found, create it under write lock
         lock.writeLock().lock();
         try {
-            File backingFile = fileLoader.load(ledgerId, masterKey != null);
-            CachedFileInfo fi = new CachedFileInfo(ledgerId, backingFile, masterKey);
-            fileInfos.put(ledgerId, fi);
+            CachedFileInfo fi = fileInfos.get(ledgerId);
+            if (fi != null) {
+                // someone is already putting a fileinfo here, so use the existing one and recycle the new one
+                newFi.recycle();
+            } else {
+                fileInfos.put(ledgerId, newFi);
+                fi = newFi;
+            }
 
             // see comment above for why we assert
-            boolean retained = fi.tryRetain();
-            assert(retained);
-            return fi;
+            return tryRetainFileInfo(fi);
         } finally {
             lock.writeLock().unlock();
         }
@@ -92,8 +107,16 @@ private void releaseFileInfo(long ledgerId, CachedFileInfo fileInfo) {
     }
 
     void closeAllWithoutFlushing() throws IOException {
-        for (Map.Entry<Long, CachedFileInfo> entry : fileInfos.entrySet()) {
-            entry.getValue().close(false);
+        try {
+            fileInfos.forEach((key, fileInfo) -> {
+                try {
+                    fileInfo.close(false);
+                } catch (IOException e) {
+                    throw new UncheckedIOException(e);
+                }
+            });
+        } catch (UncheckedIOException uioe) {
+            throw uioe.getCause();
         }
     }
 


 

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