@InterfaceAudience.Private public class BucketCache extends Object implements BlockCache, HeapSize
BucketAllocator to allocate/free blocks, and uses BucketCache#ramCache
and BucketCache#backingMap in order to determine if a given element is in the cache. The bucket
cache can use off-heap memory ByteBufferIOEngine or mmap
ExclusiveMemoryMmapIOEngine or pmem SharedMemoryMmapIOEngine or local files
FileIOEngine to store/read the block data.
Eviction is via a similar algorithm as used in
LruBlockCache
BucketCache can be used as mainly a block cache (see
CombinedBlockCache), combined with a BlockCache to
decrease CMS GC and heap fragmentation.
It also can be used as a secondary cache (e.g. using a file on ssd/fusionio to store blocks) to enlarge cache space via a victim cache.
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_ERROR_TOLERATION_DURATION |
| Constructor and Description |
|---|
BucketCache(String ioEngineName,
long capacity,
int blockSize,
int[] bucketSizes,
int writerThreadNum,
int writerQLen,
String persistencePath) |
BucketCache(String ioEngineName,
long capacity,
int blockSize,
int[] bucketSizes,
int writerThreadNum,
int writerQLen,
String persistencePath,
int ioErrorsTolerationDuration,
org.apache.hadoop.conf.Configuration conf) |
| Modifier and Type | Method and Description |
|---|---|
long |
acceptableSize() |
void |
cacheBlock(BlockCacheKey cacheKey,
Cacheable buf)
Cache the block with the specified name and buffer.
|
void |
cacheBlock(BlockCacheKey cacheKey,
Cacheable cachedItem,
boolean inMemory)
Cache the block with the specified name and buffer.
|
void |
cacheBlock(BlockCacheKey cacheKey,
Cacheable cachedItem,
boolean inMemory,
boolean waitWhenCache)
Cache the block with the specified name and buffer.
|
void |
cacheBlockWithWait(BlockCacheKey cacheKey,
Cacheable cachedItem,
boolean inMemory,
boolean wait)
Cache the block to ramCache
|
protected void |
cacheBlockWithWaitInternal(BlockCacheKey cacheKey,
Cacheable cachedItem,
boolean inMemory,
boolean wait) |
boolean |
evictBlock(BlockCacheKey cacheKey)
Try to evict the block from
BlockCache by force. |
boolean |
evictBlockIfNoRpcReferenced(BlockCacheKey blockCacheKey)
NOTE: This method is only for test.
|
int |
evictBlocksByHfileName(String hfileName)
Evicts all blocks for a specific HFile.
|
protected String |
getAlgorithm() |
BucketAllocator |
getAllocator() |
Cacheable |
getBlock(BlockCacheKey key,
boolean caching,
boolean repeat,
boolean updateCacheMetrics)
Get the buffer of the block with the specified key.
|
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.
|
String |
getIoEngine() |
long |
getMaxSize()
Returns the Max size of the block cache, in bytes.
|
long |
getRealCacheSize() |
int |
getRpcRefCount(BlockCacheKey cacheKey) |
CacheStats |
getStats()
Get the statistics for this block cache.
|
long |
heapSize() |
Iterator<CachedBlock> |
iterator()
Returns Iterator over the blocks in the cache.
|
void |
logStats() |
protected void |
putIntoBackingMap(BlockCacheKey key,
org.apache.hadoop.hbase.io.hfile.bucket.BucketEntry bucketEntry)
Put the new bucket entry into backingMap.
|
protected boolean |
removeFromRamCache(BlockCacheKey cacheKey) |
protected boolean |
shouldReplaceExistingCacheBlock(BlockCacheKey cacheKey,
Cacheable newBlock) |
void |
shutdown()
Shutdown the cache.
|
long |
size()
Returns the total size of the block cache, in bytes.
|
protected void |
startWriterThreads()
Called by the constructor to start the writer threads.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitisMetaBlockforEach, spliteratorpublic static final int DEFAULT_ERROR_TOLERATION_DURATION
public BucketCache(String ioEngineName, long capacity, int blockSize, int[] bucketSizes, int writerThreadNum, int writerQLen, String persistencePath) throws IOException
IOExceptionpublic BucketCache(String ioEngineName, long capacity, int blockSize, int[] bucketSizes, int writerThreadNum, int writerQLen, String persistencePath, int ioErrorsTolerationDuration, org.apache.hadoop.conf.Configuration conf) throws IOException
IOExceptionprotected void startWriterThreads()
public long getMaxSize()
BlockCachegetMaxSize in interface BlockCachepublic String getIoEngine()
public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf)
cacheBlock in interface BlockCachecacheKey - block's cache keybuf - block bufferpublic void cacheBlock(BlockCacheKey cacheKey, Cacheable cachedItem, boolean inMemory)
cacheBlock in interface BlockCachecacheKey - block's cache keycachedItem - block bufferinMemory - if block is in-memorypublic void cacheBlock(BlockCacheKey cacheKey, Cacheable cachedItem, boolean inMemory, boolean waitWhenCache)
cacheBlock in interface BlockCachecacheKey - block's cache keycachedItem - block bufferinMemory - if block is in-memorywaitWhenCache - Whether to wait for the cache to be flushed mainly when BucketCache is
configured.public void cacheBlockWithWait(BlockCacheKey cacheKey, Cacheable cachedItem, boolean inMemory, boolean wait)
cacheKey - block's cache keycachedItem - block bufferinMemory - if block is in-memorywait - if true, blocking wait when queue is fullprotected boolean shouldReplaceExistingCacheBlock(BlockCacheKey cacheKey, Cacheable newBlock)
protected void cacheBlockWithWaitInternal(BlockCacheKey cacheKey, Cacheable cachedItem, boolean inMemory, boolean wait)
public Cacheable getBlock(BlockCacheKey key, boolean caching, boolean repeat, boolean updateCacheMetrics)
getBlock in interface BlockCachekey - block's cache keycaching - true if the caller caches blocks on cache missesrepeat - Whether this is a repeat lookup for the same blockupdateCacheMetrics - Whether we should update cache metrics or notpublic boolean evictBlock(BlockCacheKey cacheKey)
BlockCache by force. We'll call this in few cases:Admin.clearBlockCache(TableName) to clear all blocks for a given table.
Firstly, we'll try to remove the block from RAMCache,and then try to evict from backingMap.
Here we evict the block from backingMap immediately, but only free the reference from bucket
cache by calling BucketEntry.markedAsEvicted. If there're still some RPC referring this
block, block can only be de-allocated when all of them release the block.
NOTICE: we need to grab the write offset lock firstly before releasing the reference from
bucket cache. if we don't, we may read an BucketEntry with refCnt = 0 when
getBlock(BlockCacheKey, boolean, boolean, boolean), it's a memory leak.
evictBlock in interface BlockCachecacheKey - Block to evictpublic boolean evictBlockIfNoRpcReferenced(BlockCacheKey blockCacheKey)
protected boolean removeFromRamCache(BlockCacheKey cacheKey)
public void logStats()
public long getRealCacheSize()
public long acceptableSize()
protected void putIntoBackingMap(BlockCacheKey key, org.apache.hadoop.hbase.io.hfile.bucket.BucketEntry bucketEntry)
key - Block cache keybucketEntry - Bucket entry to put into backingMap.BlockCacheUtil.shouldReplaceExistingCacheBlock(BlockCache blockCache,BlockCacheKey
cacheKey, Cacheable newBlock)public void shutdown()
BlockCacheshutdown in interface BlockCachepublic CacheStats getStats()
BlockCachegetStats in interface BlockCachepublic BucketAllocator getAllocator()
public long size()
BlockCachesize in interface BlockCachepublic long getCurrentDataSize()
BlockCachegetCurrentDataSize in interface BlockCachepublic long getFreeSize()
BlockCachegetFreeSize in interface BlockCachepublic long getBlockCount()
BlockCachegetBlockCount in interface BlockCachepublic long getDataBlockCount()
BlockCachegetDataBlockCount in interface BlockCachepublic long getCurrentSize()
BlockCachegetCurrentSize in interface BlockCacheprotected String getAlgorithm()
public int evictBlocksByHfileName(String hfileName)
This is used for evict-on-close to remove all blocks of a specific HFile.
evictBlocksByHfileName in interface BlockCachepublic Iterator<CachedBlock> iterator()
BlockCacheiterator in interface Iterable<CachedBlock>iterator in interface BlockCachepublic BlockCache[] getBlockCaches()
BlockCachegetBlockCaches in interface BlockCachepublic int getRpcRefCount(BlockCacheKey cacheKey)
Copyright © 2007–2020 The Apache Software Foundation. All rights reserved.