Package org.apache.hadoop.hbase.io.hfile
Class TinyLfuBlockCache
java.lang.Object
org.apache.hadoop.hbase.io.hfile.TinyLfuBlockCache
- All Implemented Interfaces:
Iterable<CachedBlock>
,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
Modifier and TypeClassDescriptionprivate static final class
private final class
A removal listener to asynchronously record evictions and populate the victim cache. -
Field Summary
Modifier and TypeFieldDescription(package private) final com.github.benmanes.caffeine.cache.Cache<BlockCacheKey,
Cacheable> private static final long
private static final org.slf4j.Logger
private static final String
private final long
private final com.github.benmanes.caffeine.cache.Policy.Eviction<BlockCacheKey,
Cacheable> private static final int
private final CacheStats
private final ScheduledExecutorService
private BlockCache
-
Constructor Summary
ConstructorDescriptionTinyLfuBlockCache
(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 Cacheable
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.void
cacheBlock
(BlockCacheKey key, Cacheable value) Add block to cache (defaults to not in-memory).void
cacheBlock
(BlockCacheKey cacheKey, Cacheable value, boolean inMemory) Add block to cache.boolean
containsBlock
(BlockCacheKey cacheKey) Whether the cache contains the block with specified cacheKeyboolean
evictBlock
(BlockCacheKey cacheKey) Evict block from cache.int
evictBlocksByHfileName
(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.long
Returns the number of blocks currently cached in the block cache.long
Returns the occupied size of data blocks, in bytes.long
Returns the occupied size of the block cache, in bytes.long
Returns the number of data blocks currently cached in the block cache.long
Returns the free size of the block cache, in bytes.long
Returns the Max size of the block cache, in bytes.getStats()
Get the statistics for this block cache.long
heapSize()
Return the approximate 'exclusive deep size' of implementing object.iterator()
Returns Iterator over the blocks in the cache.private void
logStats()
private void
Records an eviction.void
setMaxSize
(long size) Sets the max heap size that can be used by the BlockCache.void
setVictimCache
(BlockCache victimCache) Specifies the secondary cache.void
shutdown()
Shutdown the cache.long
size()
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, wait
Methods inherited from interface org.apache.hadoop.hbase.io.hfile.BlockCache
blockFitsIntoTheCache, cacheBlock, evictBlocksRangeByHfileName, getBlock, getBlockSize, getFullyCachedFiles, getRegionCachedInfo, isAlreadyCached, isCacheEnabled, isMetaBlock, notifyFileCachingCompleted, shouldCacheFile, waitForCacheInitialization
Methods 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:FirstLevelBlockCache
Specifies 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:
setVictimCache
in interfaceFirstLevelBlockCache
- Parameters:
victimCache
- the second level cache
-
size
Description copied from interface:BlockCache
Returns the total size of the block cache, in bytes.- Specified by:
size
in interfaceBlockCache
- Returns:
- size of cache, in bytes
-
getFreeSize
Description copied from interface:BlockCache
Returns the free size of the block cache, in bytes.- Specified by:
getFreeSize
in interfaceBlockCache
- Returns:
- free space in cache, in bytes
-
getCurrentSize
Description copied from interface:BlockCache
Returns the occupied size of the block cache, in bytes.- Specified by:
getCurrentSize
in interfaceBlockCache
- Returns:
- occupied space in cache, in bytes
-
getBlockCount
Description copied from interface:BlockCache
Returns the number of blocks currently cached in the block cache.- Specified by:
getBlockCount
in interfaceBlockCache
- Returns:
- number of blocks in the cache
-
heapSize
Description copied from interface:HeapSize
Return the approximate 'exclusive deep size' of implementing object. Includes count of payload and hosting object sizings. -
setMaxSize
Description copied from interface:ResizableBlockCache
Sets the max heap size that can be used by the BlockCache.- Specified by:
setMaxSize
in interfaceResizableBlockCache
- Parameters:
size
- The max heap size.
-
containsBlock
Description copied from interface:FirstLevelBlockCache
Whether the cache contains the block with specified cacheKey- Specified by:
containsBlock
in 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:BlockCache
Fetch block from cache.- Specified by:
getBlock
in 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:BlockCache
Add block to cache.- Specified by:
cacheBlock
in 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:BlockCache
Add block to cache (defaults to not in-memory).- Specified by:
cacheBlock
in 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:BlockCache
Evict block from cache.- Specified by:
evictBlock
in interfaceBlockCache
- Parameters:
cacheKey
- Block to evict- Returns:
- true if block existed and was evicted, false if not
-
evictBlocksByHfileName
Description copied from interface:BlockCache
Evicts all blocks for the given HFile.- Specified by:
evictBlocksByHfileName
in interfaceBlockCache
- Returns:
- the number of blocks evicted
-
getStats
Description copied from interface:BlockCache
Get the statistics for this block cache.- Specified by:
getStats
in interfaceBlockCache
-
shutdown
Description copied from interface:BlockCache
Shutdown the cache.- Specified by:
shutdown
in interfaceBlockCache
-
getBlockCaches
Description copied from interface:BlockCache
Returns The list of sub blockcaches that make up this one; returns null if no sub caches.- Specified by:
getBlockCaches
in interfaceBlockCache
-
iterator
Description copied from interface:BlockCache
Returns Iterator over the blocks in the cache.- Specified by:
iterator
in interfaceBlockCache
- Specified by:
iterator
in 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:BlockCache
Returns the Max size of the block cache, in bytes.- Specified by:
getMaxSize
in interfaceBlockCache
- Returns:
- size of cache, in bytes
-
getCurrentDataSize
Description copied from interface:BlockCache
Returns the occupied size of data blocks, in bytes.- Specified by:
getCurrentDataSize
in interfaceBlockCache
- Returns:
- occupied space in cache, in bytes
-
getDataBlockCount
Description copied from interface:BlockCache
Returns the number of data blocks currently cached in the block cache.- Specified by:
getDataBlockCount
in interfaceBlockCache
- Returns:
- number of blocks in the cache
-