@InterfaceAudience.Private public class LruBlockCache extends Object implements FirstLevelBlockCache
HeapSize, memory-bound using an
LRU eviction algorithm, and concurrent: backed by a ConcurrentHashMap and with a
non-blocking eviction thread giving constant-time cacheBlock(org.apache.hadoop.hbase.io.hfile.BlockCacheKey, org.apache.hadoop.hbase.io.hfile.Cacheable, boolean) and getBlock(org.apache.hadoop.hbase.io.hfile.BlockCacheKey, boolean, boolean, boolean)
operations.
Contains three levels of block priority to allow for scan-resistance and in-memory families
HColumnDescriptor.setInMemory(boolean) (An in-memory column
family is a column family that should be served from memory if possible): single-access,
multiple-accesses, and in-memory priority. A block is added with an in-memory priority flag if
HColumnDescriptor.isInMemory(), otherwise a block becomes a
single access priority the first time it is read into this block cache. If a block is accessed
again while in cache, it is marked as a multiple access priority block. This delineation of
blocks is used to prevent scans from thrashing the cache adding a least-frequently-used element
to the eviction algorithm.
Each priority is given its own chunk of the total cache to ensure fairness during eviction. Each priority will retain close to its maximum size, however, if any priority is not using its entire chunk the others are able to grow beyond their chunk size.
Instantiated at a minimum with the total size and average block size. All sizes are in bytes. The block size is not especially important as this cache is fully dynamic in its sizing of blocks. It is only used for pre-allocating data structures and in initial heap estimation of the map.
The detailed constructor defines the sizes for the three priorities (they should total to the
maximum size defined). It also sets the levels that trigger and control the eviction
thread.
The acceptable size is the cache size level which triggers the eviction process to
start. It evicts enough blocks to get the size below the minimum size specified.
Eviction happens in a separate thread and involves a single full-scan of the map. It determines how many bytes must be freed to reach the minimum size, and then while scanning determines the fewest least-recently-used blocks necessary from each of the three priorities (would be 3 times bytes to free). It then uses the priority chunk sizes to evict fairly according to the relative sizes and usage.
| Modifier and Type | Field and Description |
|---|---|
static long |
CACHE_FIXED_OVERHEAD |
| Constructor and Description |
|---|
LruBlockCache(long maxSize,
long blockSize)
Default constructor.
|
LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread)
Constructor used for testing.
|
LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread,
org.apache.hadoop.conf.Configuration conf) |
LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread,
int mapInitialSize,
float mapLoadFactor,
int mapConcurrencyLevel,
float minFactor,
float acceptableFactor,
float singleFactor,
float multiFactor,
float memoryFactor,
float hardLimitFactor,
boolean forceInMemory,
long maxBlockSize)
Configurable constructor.
|
LruBlockCache(long maxSize,
long blockSize,
org.apache.hadoop.conf.Configuration conf) |
| Modifier and Type | Method and Description |
|---|---|
void |
cacheBlock(BlockCacheKey cacheKey,
Cacheable buf)
Cache the block with the specified name and buffer.
|
void |
cacheBlock(BlockCacheKey cacheKey,
Cacheable buf,
boolean inMemory)
Cache the block with the specified name and buffer.
|
void |
clearCache()
Clears the cache.
|
boolean |
containsBlock(BlockCacheKey cacheKey)
Whether the cache contains block with specified cacheKey
|
boolean |
evictBlock(BlockCacheKey cacheKey)
Evict block from cache.
|
protected long |
evictBlock(LruCachedBlock block,
boolean evictedByEvictionProcess)
Evict the block, and it will be cached by the victim handler if exists && block may be
read again later
|
int |
evictBlocksByHfileName(String hfileName)
Evicts all blocks for a specific HFile.
|
Cacheable |
getBlock(BlockCacheKey cacheKey,
boolean caching,
boolean repeat,
boolean updateCacheMetrics)
Get the buffer of the block with the specified name.
|
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 |
getBloomBlockCount() |
long |
getCurrentBloomSize() |
long |
getCurrentDataSize()
Returns the occupied size of data blocks, in bytes.
|
long |
getCurrentIndexSize() |
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.
|
Map<DataBlockEncoding,Integer> |
getEncodingCountsForTest() |
long |
getFreeSize()
Returns the free size of the block cache, in bytes.
|
long |
getIndexBlockCount() |
long |
getMaxSize()
Get the maximum size of this cache.
|
CacheStats |
getStats()
Get counter statistics for this cache.
|
long |
heapSize() |
Iterator<CachedBlock> |
iterator()
Returns Iterator over the blocks in the cache.
|
void |
logStats() |
void |
setMaxSize(long maxSize)
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, waitcacheBlock, isMetaBlockforEach, spliteratorpublic LruBlockCache(long maxSize,
long blockSize)
All other factors will be calculated based on defaults specified in this class.
maxSize - maximum size of cache, in bytesblockSize - approximate size of each block, in bytespublic LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread)
public LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread,
org.apache.hadoop.conf.Configuration conf)
public LruBlockCache(long maxSize,
long blockSize,
org.apache.hadoop.conf.Configuration conf)
public LruBlockCache(long maxSize,
long blockSize,
boolean evictionThread,
int mapInitialSize,
float mapLoadFactor,
int mapConcurrencyLevel,
float minFactor,
float acceptableFactor,
float singleFactor,
float multiFactor,
float memoryFactor,
float hardLimitFactor,
boolean forceInMemory,
long maxBlockSize)
maxSize - maximum size of this cache, in bytesblockSize - expected average size of blocks, in bytesevictionThread - whether to run evictions in a bg thread or notmapInitialSize - initial size of backing ConcurrentHashMapmapLoadFactor - initial load factor of backing ConcurrentHashMapmapConcurrencyLevel - initial concurrency factor for backing CHMminFactor - percentage of total size that eviction will evict untilacceptableFactor - percentage of total size that triggers evictionsingleFactor - percentage of total size for single-access blocksmultiFactor - percentage of total size for multiple-access blocksmemoryFactor - percentage of total size for in-memory blockspublic void setVictimCache(BlockCache victimCache)
FirstLevelBlockCachesetVictimCache in interface FirstLevelBlockCachevictimCache - the second level cachepublic void setMaxSize(long maxSize)
ResizableBlockCachesetMaxSize in interface ResizableBlockCachemaxSize - The max heap size.public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory)
It is assumed this will NOT be called on an already cached block. In rare cases (HBASE-8547) this can happen, for which we compare the buffer contents.
cacheBlock in interface BlockCachecacheKey - block's cache keybuf - block bufferinMemory - if block is in-memorypublic void cacheBlock(BlockCacheKey cacheKey, Cacheable buf)
TODO after HBASE-22005, we may cache an block which allocated from off-heap, but our LRU cache sizing is based on heap size, so we should handle this in HBASE-22127. It will introduce an switch whether make the LRU on-heap or not, if so we may need copy the memory to on-heap, otherwise the caching size is based on off-heap.
cacheBlock in interface BlockCachecacheKey - block's cache keybuf - block bufferpublic Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics)
getBlock in interface BlockCachecacheKey - block's cache keycaching - true if the caller caches blocks on cache missesrepeat - 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 boolean containsBlock(BlockCacheKey cacheKey)
containsBlock in interface FirstLevelBlockCachecacheKey - cache key for the blockpublic boolean evictBlock(BlockCacheKey cacheKey)
BlockCacheevictBlock in interface BlockCachecacheKey - Block to evictpublic int evictBlocksByHfileName(String hfileName)
This is used for evict-on-close to remove all blocks of a specific HFile.
evictBlocksByHfileName in interface BlockCacheprotected long evictBlock(LruCachedBlock block, boolean evictedByEvictionProcess)
evictedByEvictionProcess - true if the given block is evicted by EvictionThreadpublic long getMaxSize()
getMaxSize in interface BlockCachepublic long getCurrentSize()
BlockCachegetCurrentSize in interface BlockCachepublic long getCurrentDataSize()
BlockCachegetCurrentDataSize in interface BlockCachepublic long getCurrentIndexSize()
public long getCurrentBloomSize()
public long getFreeSize()
BlockCachegetFreeSize in interface BlockCachepublic long size()
BlockCachesize in interface BlockCachepublic long getBlockCount()
BlockCachegetBlockCount in interface BlockCachepublic long getDataBlockCount()
BlockCachegetDataBlockCount in interface BlockCachepublic long getIndexBlockCount()
public long getBloomBlockCount()
public void logStats()
public CacheStats getStats()
Includes: total accesses, hits, misses, evicted blocks, and runs of the eviction processes.
getStats in interface BlockCachepublic Iterator<CachedBlock> iterator()
BlockCacheiterator in interface Iterable<CachedBlock>iterator in interface BlockCachepublic void shutdown()
BlockCacheshutdown in interface BlockCachepublic void clearCache()
public Map<DataBlockEncoding,Integer> getEncodingCountsForTest()
public BlockCache[] getBlockCaches()
BlockCachegetBlockCaches in interface BlockCacheCopyright © 2007–2020 The Apache Software Foundation. All rights reserved.