Package org.apache.hadoop.hbase.io.hfile
Class TinyLfuBlockCache
java.lang.Object
org.apache.hadoop.hbase.io.hfile.TinyLfuBlockCache
- All Implemented Interfaces:
Iterable<CachedBlock>,ConfigurationObserver,HeapSize,BlockCache,FirstLevelBlockCache,ResizableBlockCache
A block cache that is memory-aware using
HeapSize, memory bounded using the W-TinyLFU
eviction algorithm, and concurrent. This implementation delegates to a Caffeine cache to provide
O(1) read and write operations.
- W-TinyLFU: http://arxiv.org/pdf/1512.00727.pdf
- Caffeine: https://github.com/ben-manes/caffeine
- Cache design: http://highscalability.com/blog/2016/1/25/design-of-a-modern-cache.html
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate final classA removal listener to asynchronously record evictions and populate the victim cache. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final com.github.benmanes.caffeine.cache.Cache<BlockCacheKey,Cacheable> private static final longprivate static final org.slf4j.Loggerprivate static final Stringprivate final longprivate final com.github.benmanes.caffeine.cache.Policy.Eviction<BlockCacheKey,Cacheable> private static final intprivate final CacheStatsprivate final ScheduledExecutorServiceprivate BlockCache -
Constructor Summary
ConstructorsConstructorDescriptionTinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, long maxBlockSize, Executor executor) Creates a block cache.TinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, Executor executor, org.apache.hadoop.conf.Configuration conf) Creates a block cache. -
Method Summary
Modifier and TypeMethodDescriptionprivate CacheableThe block cached in TinyLfuBlockCache will always be an heap block: on the one side, the heap access will be more faster then off-heap, the small index block or meta block cached in CombinedBlockCache will benefit a lot.voidcacheBlock(BlockCacheKey key, Cacheable value) Add block to cache (defaults to not in-memory).voidcacheBlock(BlockCacheKey cacheKey, Cacheable value, boolean inMemory) Add block to cache.booleancontainsBlock(BlockCacheKey cacheKey) Whether the cache contains the block with specified cacheKeybooleanevictBlock(BlockCacheKey cacheKey) Evict block from cache.intevictBlocksByHfileName(String hfileName) Evicts all blocks for the given HFile.getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) Fetch block from cache.Returns The list of sub blockcaches that make up this one; returns null if no sub caches.longReturns the number of blocks currently cached in the block cache.longReturns the occupied size of data blocks, in bytes.longReturns the occupied size of the block cache, in bytes.longReturns the number of data blocks currently cached in the block cache.longReturns the free size of the block cache, in bytes.longReturns the Max size of the block cache, in bytes.getStats()Get the statistics for this block cache.longheapSize()Return the approximate 'exclusive deep size' of implementing object.iterator()Returns Iterator over the blocks in the cache.private voidlogStats()private voidRecords an eviction.voidsetMaxSize(long size) Sets the max heap size that can be used by the BlockCache.voidsetVictimCache(BlockCache victimCache) Specifies the secondary cache.voidshutdown()Shutdown the cache.longsize()Returns the total size of the block cache, in bytes.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.hadoop.hbase.io.hfile.BlockCache
blockFitsIntoTheCache, cacheBlock, evictBlocksRangeByHfileName, getBlock, getBlockSize, getFullyCachedFiles, getRegionCachedInfo, isAlreadyCached, isCacheEnabled, isMetaBlock, notifyFileCachingCompleted, onConfigurationChange, shouldCacheBlock, shouldCacheFile, waitForCacheInitializationMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
LOG
-
MAX_BLOCK_SIZE
- See Also:
-
DEFAULT_MAX_BLOCK_SIZE
- See Also:
-
STAT_THREAD_PERIOD_SECONDS
- See Also:
-
policy
private final transient com.github.benmanes.caffeine.cache.Policy.Eviction<BlockCacheKey,Cacheable> policy -
statsThreadPool
-
maxBlockSize
-
stats
-
victimCache
-
cache
-
-
Constructor Details
-
TinyLfuBlockCache
public TinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, Executor executor, org.apache.hadoop.conf.Configuration conf) Creates a block cache.- Parameters:
maximumSizeInBytes- maximum size of this cache, in bytesavgBlockSize- expected average size of blocks, in bytesexecutor- the cache's executorconf- additional configuration
-
TinyLfuBlockCache
public TinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, long maxBlockSize, Executor executor) Creates a block cache.- Parameters:
maximumSizeInBytes- maximum size of this cache, in bytesavgBlockSize- expected average size of blocks, in bytesmaxBlockSize- maximum size of a block, in bytesexecutor- the cache's executor
-
-
Method Details
-
setVictimCache
Description copied from interface:FirstLevelBlockCacheSpecifies the secondary cache. An entry that is evicted from this cache due to a size constraint will be inserted into the victim cache.- Specified by:
setVictimCachein interfaceFirstLevelBlockCache- Parameters:
victimCache- the second level cache
-
size
Description copied from interface:BlockCacheReturns the total size of the block cache, in bytes.- Specified by:
sizein interfaceBlockCache- Returns:
- size of cache, in bytes
-
getFreeSize
Description copied from interface:BlockCacheReturns the free size of the block cache, in bytes.- Specified by:
getFreeSizein interfaceBlockCache- Returns:
- free space in cache, in bytes
-
getCurrentSize
Description copied from interface:BlockCacheReturns the occupied size of the block cache, in bytes.- Specified by:
getCurrentSizein interfaceBlockCache- Returns:
- occupied space in cache, in bytes
-
getBlockCount
Description copied from interface:BlockCacheReturns the number of blocks currently cached in the block cache.- Specified by:
getBlockCountin interfaceBlockCache- Returns:
- number of blocks in the cache
-
heapSize
Description copied from interface:HeapSizeReturn the approximate 'exclusive deep size' of implementing object. Includes count of payload and hosting object sizings. -
setMaxSize
Description copied from interface:ResizableBlockCacheSets the max heap size that can be used by the BlockCache.- Specified by:
setMaxSizein interfaceResizableBlockCache- Parameters:
size- The max heap size.
-
containsBlock
Description copied from interface:FirstLevelBlockCacheWhether the cache contains the block with specified cacheKey- Specified by:
containsBlockin interfaceFirstLevelBlockCache- Parameters:
cacheKey- cache key for the block- Returns:
- true if it contains the block
-
getBlock
public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) Description copied from interface:BlockCacheFetch block from cache.- Specified by:
getBlockin interfaceBlockCache- Parameters:
cacheKey- Block to fetch.caching- Whether this request has caching enabled (used for stats)repeat- Whether this is a repeat lookup for the same block (used to avoid double counting cache misses when doing double-check locking)updateCacheMetrics- Whether to update cache metrics or not- Returns:
- Block or null if block is not in 2 cache.
-
cacheBlock
Description copied from interface:BlockCacheAdd block to cache.- Specified by:
cacheBlockin interfaceBlockCache- Parameters:
cacheKey- The block's cache key.value- The block contents wrapped in a ByteBuffer.inMemory- Whether block should be treated as in-memory
-
cacheBlock
Description copied from interface:BlockCacheAdd block to cache (defaults to not in-memory).- Specified by:
cacheBlockin interfaceBlockCache- Parameters:
key- The block's cache key.value- The object to cache.
-
asReferencedHeapBlock
The block cached in TinyLfuBlockCache will always be an heap block: on the one side, the heap access will be more faster then off-heap, the small index block or meta block cached in CombinedBlockCache will benefit a lot. on other side, the TinyLfuBlockCache size is always calculated based on the total heap size, if caching an off-heap block in TinyLfuBlockCache, the heap size will be messed up. Here we will clone the block into an heap block if it's an off-heap block, otherwise just use the original block. The key point is maintain the refCnt of the block (HBASE-22127):
1. if cache the cloned heap block, its refCnt is an totally new one, it's easy to handle;
2. if cache the original heap block, we're sure that it won't be tracked in ByteBuffAllocator's reservoir, if both RPC and TinyLfuBlockCache release the block, then it can be garbage collected by JVM, so need a retain here.- Parameters:
buf- the original block- Returns:
- an block with an heap memory backend.
-
evictBlock
Description copied from interface:BlockCacheEvict block from cache.- Specified by:
evictBlockin interfaceBlockCache- Parameters:
cacheKey- Block to evict- Returns:
- true if block existed and was evicted, false if not
-
evictBlocksByHfileName
Description copied from interface:BlockCacheEvicts all blocks for the given HFile.- Specified by:
evictBlocksByHfileNamein interfaceBlockCache- Returns:
- the number of blocks evicted
-
getStats
Description copied from interface:BlockCacheGet the statistics for this block cache.- Specified by:
getStatsin interfaceBlockCache
-
shutdown
Description copied from interface:BlockCacheShutdown the cache.- Specified by:
shutdownin interfaceBlockCache
-
getBlockCaches
Description copied from interface:BlockCacheReturns The list of sub blockcaches that make up this one; returns null if no sub caches.- Specified by:
getBlockCachesin interfaceBlockCache
-
iterator
Description copied from interface:BlockCacheReturns Iterator over the blocks in the cache.- Specified by:
iteratorin interfaceBlockCache- Specified by:
iteratorin interfaceIterable<CachedBlock>
-
logStats
-
toString
-
recordEviction
Records an eviction. The number of eviction operations and evicted blocks are identical, as an eviction is triggered immediately when the capacity has been exceeded. An eviction is performed asynchronously. See the library's documentation for details on write buffers, batching, and maintenance behavior. -
getMaxSize
Description copied from interface:BlockCacheReturns the Max size of the block cache, in bytes.- Specified by:
getMaxSizein interfaceBlockCache- Returns:
- size of cache, in bytes
-
getCurrentDataSize
Description copied from interface:BlockCacheReturns the occupied size of data blocks, in bytes.- Specified by:
getCurrentDataSizein interfaceBlockCache- Returns:
- occupied space in cache, in bytes
-
getDataBlockCount
Description copied from interface:BlockCacheReturns the number of data blocks currently cached in the block cache.- Specified by:
getDataBlockCountin interfaceBlockCache- Returns:
- number of blocks in the cache
-