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_r172122421
 
 

 ##########
 File path: 
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileChannelBackingCache.java
 ##########
 @@ -0,0 +1,190 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.bookkeeper.bookie;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.apache.bookkeeper.util.IOUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * FileChannelBackingCache used to cache RefCntFileChannels for read.
+ * In order to avoid get released file, adopt design of FileInfoBackingCache.
+ * @see FileInfoBackingCache
+ */
+class FileChannelBackingCache {
+    private static final Logger LOG = 
LoggerFactory.getLogger(FileChannelBackingCache.class);
+    static final int DEAD_REF = -0xdead;
+    final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+    final FileLoader fileLoader;
+
+    FileChannelBackingCache(FileLoader fileLoader) {
+        this.fileLoader = fileLoader;
+    }
+
+    final ConcurrentHashMap<Long, CachedFileChannel> fileChannels = new 
ConcurrentHashMap<>();
+
+    CachedFileChannel loadFileChannel(long logId) throws IOException {
+        lock.readLock().lock();
+        try {
+            CachedFileChannel cachedFileChannel = fileChannels.get(logId);
+            if (cachedFileChannel != null) {
+                boolean retained = cachedFileChannel.tryRetain();
+                checkArgument(retained);
+                return cachedFileChannel;
+            }
+        } finally {
+            lock.readLock().unlock();
+        }
+
+        lock.writeLock().lock();
+        try {
+            File file = fileLoader.load(logId);
+            // 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();
+            CachedFileChannel cachedFileChannel = new CachedFileChannel(logId, 
newFc);
+            fileChannels.put(logId, cachedFileChannel);
+            boolean retained = cachedFileChannel.tryRetain();
+            checkArgument(retained);
+            return cachedFileChannel;
+        } finally {
+            lock.writeLock().unlock();
+        }
+    }
+
+    /**
+     * close FileChannel and remove from cache when possible.
+     * @param logId
+     * @param cachedFileChannel
+     */
+    private void releaseFileChannel(long logId, CachedFileChannel 
cachedFileChannel) {
+        lock.writeLock().lock();
 
 Review comment:
   The lock can guarantee the fileChannel being loading not closed by another 
thread.
   Below is a race condition.
   Thread A holds one refCnt for the specific fileChannel and release it, so 
the refCnt is 0 and being closed.
   Thread B load the fileChannel with ReadLock, so it can't continue if thread 
A call `releaseFileChannel ` firstly as the Thread A holds writeLock. This case 
is fine.
   If thread B add refCnt before thread A get writeLock, then 
`releaseFileChannel `'s logic guarantee the fileChannel was not closed by check 
`markDead()` under writeLock. So it still works fine under this case.

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