diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java index 0ed3cbd..1098bdc 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CacheConfig.java @@ -431,6 +431,43 @@ public class CacheConfig { return isBlockCacheEnabled() && this.prefetchOnOpen; } + /** + * We consider all conditions block be cached + */ + public boolean shouldReadBlockFromCache(BlockType blockType) { + if (!isBlockCacheEnabled()) { + return false; + } + if (cacheDataOnRead) { + return true; + } + if (prefetchOnOpen) { + return true; + } + if (cacheDataOnWrite) { + return true; + } + if (blockType == null) { + return true; + } + if (blockType.getCategory() == BlockCategory.BLOOM || + blockType.getCategory() == BlockCategory.INDEX) { + return true; + } + return false; + } + + /** + * If we make sure the block could not be cached, we will not acquire the lock + * otherwise we will acquire lock + */ + public boolean shouldLockOnCacheMiss(BlockType blockType) { + if (blockType == null) { + return true; + } + return shouldCacheBlockOnRead(blockType.getCategory()); + } + @Override public String toString() { if (!isBlockCacheEnabled()) { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java index 82f5366..c16c653 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileReaderImpl.java @@ -1418,12 +1418,11 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { TraceScope traceScope = Trace.startSpan("HFileReaderImpl.readBlock"); try { while (true) { - if (useLock) { - lockEntry = offsetLock.getLockEntry(dataBlockOffset); - } - // Check cache for block. If found return. - if (cacheConf.isBlockCacheEnabled()) { + if (cacheConf.shouldReadBlockFromCache(expectedBlockType)) { + if (useLock) { + lockEntry = offsetLock.getLockEntry(dataBlockOffset); + } // Try and get the block from the block cache. If the useLock variable is true then this // is the second time through the loop and it should not be counted as a block cache miss. HFileBlock cachedBlock = getCachedBlock(cacheKey, cacheBlock, useLock, isCompaction, @@ -1448,13 +1447,15 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { // Cache-hit. Return! return cachedBlock; } + + if (!useLock && cacheBlock && cacheConf.shouldLockOnCacheMiss(expectedBlockType)) { + // check cache again with lock + useLock = true; + continue; + } // Carry on, please load. } - if (!useLock) { - // check cache again with lock - useLock = true; - continue; - } + if (Trace.isTracing()) { traceScope.getSpan().addTimelineAnnotation("blockCacheMiss"); }