@InterfaceAudience.Private public final class TinyLfuBlockCache extends Object implements FirstLevelBlockCache
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.
| Modifier and Type | Class and Description |
|---|---|
private static class |
TinyLfuBlockCache.CachedBlockView |
private class |
TinyLfuBlockCache.EvictionListener
A removal listener to asynchronously record evictions and populate the victim cache.
|
| Modifier and Type | Field and Description |
|---|---|
(package private) com.github.benmanes.caffeine.cache.Cache<BlockCacheKey,Cacheable> |
cache |
private static long |
DEFAULT_MAX_BLOCK_SIZE |
private static org.slf4j.Logger |
LOG |
private static String |
MAX_BLOCK_SIZE |
private long |
maxBlockSize |
private com.github.benmanes.caffeine.cache.Policy.Eviction<BlockCacheKey,Cacheable> |
policy |
private static int |
STAT_THREAD_PERIOD_SECONDS |
private CacheStats |
stats |
private ScheduledExecutorService |
statsThreadPool |
private BlockCache |
victimCache |
| Constructor and Description |
|---|
TinyLfuBlockCache(long maximumSizeInBytes,
long avgBlockSize,
Executor executor,
org.apache.hadoop.conf.Configuration conf)
Creates a block cache.
|
TinyLfuBlockCache(long maximumSizeInBytes,
long avgBlockSize,
long maxBlockSize,
Executor executor)
Creates a block cache.
|
| Modifier and Type | Method and Description |
|---|---|
private Cacheable |
asReferencedHeapBlock(Cacheable buf)
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 cacheKey
|
boolean |
evictBlock(BlockCacheKey cacheKey)
Evict block from cache.
|
int |
evictBlocksByHfileName(String hfileName)
Evicts all blocks for the given HFile.
|
Cacheable |
getBlock(BlockCacheKey cacheKey,
boolean caching,
boolean repeat,
boolean updateCacheMetrics)
Fetch block from cache.
|
BlockCache[] |
getBlockCaches()
Returns The list of sub blockcaches that make up this one; returns null if no sub caches.
|
long |
getBlockCount()
Returns the number of blocks currently cached in the block cache.
|
long |
getCurrentDataSize()
Returns the occupied size of data blocks, in bytes.
|
long |
getCurrentSize()
Returns the occupied size of the block cache, in bytes.
|
long |
getDataBlockCount()
Returns the number of data blocks currently cached in the block cache.
|
long |
getFreeSize()
Returns the free size of the block cache, in bytes.
|
long |
getMaxSize()
Returns the Max size of the block cache, in bytes.
|
CacheStats |
getStats()
Get the statistics for this block cache.
|
long |
heapSize()
Return the approximate 'exclusive deep size' of implementing object.
|
Iterator<CachedBlock> |
iterator()
Returns Iterator over the blocks in the cache.
|
private void |
logStats() |
private void |
recordEviction()
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.
|
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitisMetaBlockforEach, spliteratorprivate static final org.slf4j.Logger LOG
private static final String MAX_BLOCK_SIZE
private static final long DEFAULT_MAX_BLOCK_SIZE
private static final int STAT_THREAD_PERIOD_SECONDS
private final transient com.github.benmanes.caffeine.cache.Policy.Eviction<BlockCacheKey,Cacheable> policy
private final transient ScheduledExecutorService statsThreadPool
private final long maxBlockSize
private final CacheStats stats
private transient BlockCache victimCache
final transient com.github.benmanes.caffeine.cache.Cache<BlockCacheKey,Cacheable> cache
public TinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, Executor executor, org.apache.hadoop.conf.Configuration conf)
maximumSizeInBytes - maximum size of this cache, in bytesavgBlockSize - expected average size of blocks, in bytesexecutor - the cache's executorconf - additional configurationpublic TinyLfuBlockCache(long maximumSizeInBytes, long avgBlockSize, long maxBlockSize, Executor executor)
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 executorpublic void setVictimCache(BlockCache victimCache)
FirstLevelBlockCachesetVictimCache in interface FirstLevelBlockCachevictimCache - the second level cachepublic long size()
BlockCachesize in interface BlockCachepublic long getFreeSize()
BlockCachegetFreeSize in interface BlockCachepublic long getCurrentSize()
BlockCachegetCurrentSize in interface BlockCachepublic long getBlockCount()
BlockCachegetBlockCount in interface BlockCachepublic long heapSize()
HeapSizepublic void setMaxSize(long size)
ResizableBlockCachesetMaxSize in interface ResizableBlockCachesize - The max heap size.public boolean containsBlock(BlockCacheKey cacheKey)
FirstLevelBlockCachecontainsBlock in interface FirstLevelBlockCachecacheKey - cache key for the blockpublic Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics)
BlockCachegetBlock in interface BlockCachecacheKey - 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 notpublic void cacheBlock(BlockCacheKey cacheKey, Cacheable value, boolean inMemory)
BlockCachecacheBlock in interface BlockCachecacheKey - The block's cache key.value - The block contents wrapped in a ByteBuffer.inMemory - Whether block should be treated as in-memorypublic void cacheBlock(BlockCacheKey key, Cacheable value)
BlockCachecacheBlock in interface BlockCachekey - The block's cache key.value - The object to cache.private Cacheable asReferencedHeapBlock(Cacheable buf)
buf - the original blockpublic boolean evictBlock(BlockCacheKey cacheKey)
BlockCacheevictBlock in interface BlockCachecacheKey - Block to evictpublic int evictBlocksByHfileName(String hfileName)
BlockCacheevictBlocksByHfileName in interface BlockCachepublic CacheStats getStats()
BlockCachegetStats in interface BlockCachepublic void shutdown()
BlockCacheshutdown in interface BlockCachepublic BlockCache[] getBlockCaches()
BlockCachegetBlockCaches in interface BlockCachepublic Iterator<CachedBlock> iterator()
BlockCacheiterator in interface Iterable<CachedBlock>iterator in interface BlockCacheprivate void logStats()
private void recordEviction()
public long getMaxSize()
BlockCachegetMaxSize in interface BlockCachepublic long getCurrentDataSize()
BlockCachegetCurrentDataSize in interface BlockCachepublic long getDataBlockCount()
BlockCachegetDataBlockCount in interface BlockCacheCopyright © 2007–2020 The Apache Software Foundation. All rights reserved.