sijie 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_r171943988
##########
File path:
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogger.java
##########
@@ -326,59 +353,45 @@ private int readFromLogChannel(long entryLogId,
BufferedReadChannel channel, Byt
return channel.read(buff, pos);
}
+ final ConcurrentLinkedQueue<Cache<Long, EntryLogBufferedReadChannel>>
readChannelCaches =
+ new ConcurrentLinkedQueue<>();
+
/**
* A thread-local variable that wraps a mapping of log ids to
bufferedchannels
* These channels should be used only for reading. logChannel is the one
* that is used for writes.
+ * We use this Guava cache to store the BufferedReadChannel.
+ * When the BufferedReadChannel is removed, the underlying fileChannel's
refCnt decrease 1,
+ * temporally use 1h to relax replace after reading.
*/
- private final ThreadLocal<Map<Long, BufferedReadChannel>> logid2Channel =
- new ThreadLocal<Map<Long, BufferedReadChannel>>() {
+ final ThreadLocal<Cache<Long, EntryLogBufferedReadChannel>>
logid2ReadChannel =
+ new ThreadLocal<Cache<Long, EntryLogBufferedReadChannel>>() {
@Override
- public Map<Long, BufferedReadChannel> initialValue() {
+ public Cache<Long, EntryLogBufferedReadChannel> initialValue() {
// Since this is thread local there only one modifier
// We dont really need the concurrency, but we need to use
// the weak values. Therefore using the concurrency level of 1
- return new MapMaker().concurrencyLevel(1)
- .weakValues()
- .makeMap();
+ Cache<Long, EntryLogBufferedReadChannel> cache =
+ CacheBuilder.newBuilder().concurrencyLevel(1)
+ .expireAfterAccess(readChannelCacheExpireTimeMs,
TimeUnit.MILLISECONDS)
+ //decrease the refCnt
+ .removalListener(( RemovalListener<Long,
EntryLogBufferedReadChannel>) removal
+ ->
removal.getValue().release()).ticker(getTicker()).build();
+ readChannelCaches.add(cache);
+ return cache;
}
};
- /**
- * Each thread local buffered read channel can share the same file handle
because reads are not relative
- * and don't cause a change in the channel's position. We use this map to
store the file channels. Each
- * file channel is mapped to a log id which represents an open log file.
- */
- private final ConcurrentMap<Long, FileChannel> logid2FileChannel = new
ConcurrentHashMap<Long, FileChannel>();
-
- /**
- * Put the logId, bc pair in the map responsible for the current thread.
- * @param logId
- * @param bc
- */
- public BufferedReadChannel putInReadChannels(long logId,
BufferedReadChannel bc) {
- Map<Long, BufferedReadChannel> threadMap = logid2Channel.get();
- return threadMap.put(logId, bc);
+ Ticker getTicker() {
+ return Ticker.systemTicker();
}
/**
- * Remove all entries for this log file in each thread's cache.
- * @param logId
+ * Each thread local buffered read channel can share the same file handle
because reads are not relative
+ * and don't cause a change in the channel's position.
+ * Each file channel is mapped to a log id which represents an open log
file.
*/
- public void removeFromChannelsAndClose(long logId) {
- FileChannel fileChannel = logid2FileChannel.remove(logId);
- if (null != fileChannel) {
- try {
- fileChannel.close();
- } catch (IOException e) {
- LOG.warn("Exception while closing channel for log file:" +
logId);
- }
- }
- }
-
- public BufferedReadChannel getFromChannels(long logId) {
- return logid2Channel.get().get(logId);
- }
+ FileChannelBackingCache fileChannelBackingCache = new
FileChannelBackingCache(this::findFile);
Review comment:
nit: make it final
----------------------------------------------------------------
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