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_r172119053
 
 

 ##########
 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);
 
 Review comment:
   This guarantees the refCnt's correctness.
   Imagine the scenario: thread A and thread B both want to load the same 
fileChannel without lock:
   1.A and B both run to the line after created fileChannel respectively;
   2.A execute: 
   `fileChannels.put(logId, cachedFileChannel);
     boolean retained = cachedFileChannel.tryRetain();`;
   3.A was switched and B execute these line again;
   Then the FileChannelBacking cache will hold the last fileChannel with one 
refCnt, but the refCnt should be two actually.

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