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..4f6ced1 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,30 @@ 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()) { + /** + * Read cache needs conditions below: + * 1. blockCache is open + * 2. if expectedBlockType is null + * just read cache + * else + * 2.1 the request cache is open + * 2.2 table's column family cache is open + * 2.3 check the blockType.category + * **/ + if (cacheConf.isBlockCacheEnabled() && ( + expectedBlockType == null || ( + expectedBlockType != null + && cacheBlock + && cacheConf.shouldCacheBlockOnRead(expectedBlockType.getCategory()) + ) + ) + ) { + // Only lock when blockCacheEnabled + 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, @@ -1449,12 +1467,13 @@ public class HFileReaderImpl implements HFile.Reader, Configurable { return cachedBlock; } // Carry on, please load. + if (!useLock) { + // check cache again with lock + useLock = true; + continue; + } } - if (!useLock) { - // check cache again with lock - useLock = true; - continue; - } + if (Trace.isTracing()) { traceScope.getSpan().addTimelineAnnotation("blockCacheMiss"); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java index 3e988d6..2a6a46c 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java @@ -5183,7 +5183,7 @@ public class TestFromClientSide { assertEquals(1, store.getStorefilesCount()); expectedBlockCount -= 2; // evicted two blocks, cached none assertEquals(expectedBlockCount, cache.getBlockCount()); - expectedBlockHits += 2; + //expectedBlockHits += 2; assertEquals(expectedBlockMiss, cache.getStats().getMissCount()); assertEquals(expectedBlockHits, cache.getStats().getHitCount()); // read the row, this should be a cache miss because we don't cache data