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_r171480143
##########
File path:
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
##########
@@ -1090,28 +1106,46 @@ private Header getHeaderForLogId(long entryLogId)
throws IOException {
return new Header(headerVersion, ledgersMapOffset, ledgersCount);
} finally {
headers.release();
+ if (bc != null) {
+ bc.release();
+ }
}
}
- private BufferedReadChannel getChannelForLogId(long entryLogId) throws
IOException {
- BufferedReadChannel fc = getFromChannels(entryLogId);
- if (fc != null) {
- return fc;
- }
- File file = findFile(entryLogId);
- // 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();
- FileChannel oldFc = logid2FileChannel.putIfAbsent(entryLogId, newFc);
- if (null != oldFc) {
- newFc.close();
- newFc = oldFc;
- }
- // We set the position of the write buffer of this buffered channel to
Long.MAX_VALUE
- // so that there are no overlaps with the write buffer while reading
- fc = new BufferedReadChannel(newFc, conf.getReadBufferBytes());
- putInReadChannels(entryLogId, fc);
- return fc;
+ /**
+ * Add one refCnt for BufferedReadChannel and return it, caller need to
subtract one refCnt.
+ * @param entryLogId
+ * @return
+ * @throws IOException
+ */
+ private EntryLogBufferedReadChannel getChannelForLogId(long entryLogId)
throws IOException {
+ try {
+ EntryLogBufferedReadChannel brc;
+ Callable<EntryLogBufferedReadChannel> loader = () -> {
+ CachedFileChannel cachedFileChannel =
fileChannelBackingCache.loadFileChannel(entryLogId);
+ // We set the position of the write buffer of this buffered
channel to Long.MAX_VALUE
+ // so that there are no overlaps with the write buffer while
reading
+ return new EntryLogBufferedReadChannel(cachedFileChannel,
conf.getReadBufferBytes());
+ };
+ do {
+ // Put the logId, bc pair in the cache responsible for the
current thread.
+ brc = getThreadLocalCacheForReadChannel().get(entryLogId,
loader);
+ if (!brc.cachedFileChannel.tryRetain()) {
Review comment:
I think your method is not suitable because the refCnt of
BufferedReadChannel to fileChannel will be two when it was loaded first time,
in this situation, we don't need tryRetain() again. But when the
BufferedReadChannel is already in the cache, we definitely need to tryRetain()
to add refCnt.
----------------------------------------------------------------
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