From 5b515de7924367dcdf1d0ac5d1217b4265b7657a Mon Sep 17 00:00:00 2001 From: 1996fanrui <1996fanrui@gmail.com> Date: Sat, 22 Aug 2020 00:31:39 +0800 Subject: [PATCH] HBASE-24915 Improve BlockCache read performance by specifying BlockType (#2288) Signed-off-by: Viraj Jasani Signed-off-by: stack --- .../hadoop/hbase/io/hfile/BlockCache.java | 15 ++++++++++++++ .../hbase/io/hfile/CombinedBlockCache.java | 20 ++++++++++++++++++- .../hbase/io/hfile/HFileReaderImpl.java | 3 ++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java index 6849a9780ed..f54edae31e1 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/BlockCache.java @@ -55,6 +55,21 @@ public interface BlockCache extends Iterable { Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics); + /** + * Fetch block from cache. + * @param cacheKey Block to fetch. + * @param caching Whether this request has caching enabled (used for stats) + * @param repeat Whether this is a repeat lookup for the same block + * (used to avoid double counting cache misses when doing double-check locking) + * @param updateCacheMetrics Whether to update cache metrics or not + * @param blockType BlockType + * @return Block or null if block is not in 2 cache. + */ + default Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, + boolean updateCacheMetrics, BlockType blockType) { + return getBlock(cacheKey, caching, repeat, updateCacheMetrics); + } + /** * Evict block from cache. * @param cacheKey Block to evict diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java index 3691635902a..5544ecef374 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/CombinedBlockCache.java @@ -61,7 +61,7 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize { @Override public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) { - boolean metaBlock = buf.getBlockType().getCategory() != BlockCategory.DATA; + boolean metaBlock = isMetaBlock(buf.getBlockType()); if (metaBlock) { l1Cache.cacheBlock(cacheKey, buf, inMemory); } else { @@ -74,6 +74,10 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize { cacheBlock(cacheKey, buf, false); } + private boolean isMetaBlock(BlockType blockType) { + return blockType.getCategory() != BlockCategory.DATA; + } + @Override public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) { @@ -86,6 +90,20 @@ public class CombinedBlockCache implements ResizableBlockCache, HeapSize { l2Cache.getBlock(cacheKey, caching, repeat, updateCacheMetrics); } + @Override + public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, + boolean updateCacheMetrics, BlockType blockType) { + if (blockType == null) { + return getBlock(cacheKey, caching, repeat, updateCacheMetrics); + } + boolean metaBlock = isMetaBlock(blockType); + if (metaBlock) { + return l1Cache.getBlock(cacheKey, caching, repeat, updateCacheMetrics); + } else { + return l2Cache.getBlock(cacheKey, caching, repeat, updateCacheMetrics); + } + } + @Override public boolean evictBlock(BlockCacheKey cacheKey) { return l1Cache.evictBlock(cacheKey) || l2Cache.evictBlock(cacheKey); 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 5c51fe0060c..de0b15feebb 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 @@ -1115,7 +1115,8 @@ public abstract class HFileReaderImpl implements HFile.Reader, Configurable { BlockCache cache = cacheConf.getBlockCache().orElse(null); if (cache != null) { HFileBlock cachedBlock = - (HFileBlock) cache.getBlock(cacheKey, cacheBlock, useLock, updateCacheMetrics); + (HFileBlock) cache.getBlock(cacheKey, cacheBlock, useLock, + updateCacheMetrics, expectedBlockType); if (cachedBlock != null) { if (cacheConf.shouldCacheCompressed(cachedBlock.getBlockType().getCategory())) { HFileBlock compressedBlock = cachedBlock;