yzang commented on a change in pull request #913: Refactor FileInfo locking and refcounting out IndexPersistenceMgr URL: https://github.com/apache/bookkeeper/pull/913#discussion_r158851397
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/FileInfoBackingCache.java ########## @@ -0,0 +1,125 @@ +/** + * + * 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 java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +class FileInfoBackingCache { + final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + final ConcurrentHashMap<Long, CachedFileInfo> fileInfos = new ConcurrentHashMap<>(); + final FileLoader fileLoader; + + FileInfoBackingCache(FileLoader fileLoader) { + this.fileLoader = fileLoader; + } + + CachedFileInfo loadFileInfo(long ledgerId, byte[] masterKey) throws IOException { + lock.readLock().lock(); + try { + CachedFileInfo fi = fileInfos.get(ledgerId); + if (fi != null) { + fi.retain(); // caller of loadFileInfo owns this reference Review comment: If we use another ConcurrentHashMap to store FileInfo, it will cause issues. This is based on the assumption that you fix the comment for line 219 in IndexPersistenceMgr.java, because we need to put FileInfo into both read cache and write cache when it's first created for write. Now let's talk about the issue: 1. We created a new ledger, so we create a new FileInfo and put it into both read cache and write cache. 2. However, here since it's already in the hashmap, we only increment the refCount by one although we store the ref in 2 different caches. 3. Now the fileInfo is evicted from write cache and refCount becomes 0, so we decide to close the file. 4. After that, we try to get that fileInfo for read, and we can find it from cache, however, when we get the file from cache, the file is already closed. ---------------------------------------------------------------- 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
