Package org.apache.hadoop.hbase.io.hfile
Interface BlockCache
- All Superinterfaces:
Iterable<CachedBlock>
- All Known Subinterfaces:
FirstLevelBlockCache
,ResizableBlockCache
- All Known Implementing Classes:
BucketCache
,CombinedBlockCache
,InclusiveCombinedBlockCache
,IndexOnlyLruBlockCache
,LruAdaptiveBlockCache
,LruBlockCache
,MemcachedBlockCache
,TinyLfuBlockCache
Block cache interface. Anything that implements the
Cacheable
interface can be put in the
cache.-
Method Summary
Modifier and TypeMethodDescriptionblockFitsIntoTheCache
(HFileBlock block) Checks whether there's enough space left in the cache to accommodate the passed block.void
cacheBlock
(BlockCacheKey cacheKey, Cacheable buf) Add block to cache (defaults to not in-memory).void
cacheBlock
(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) Add block to cache.default void
cacheBlock
(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory, boolean waitWhenCache) Add block to cache.boolean
evictBlock
(BlockCacheKey cacheKey) Evict block from cache.int
evictBlocksByHfileName
(String hfileName) Evicts all blocks for the given HFile.default int
evictBlocksRangeByHfileName
(String hfileName, long initOffset, long endOffset) Evict all blocks for the given file name between the passed offset values.getBlock
(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) Fetch block from cache.default Cacheable
getBlock
(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics, BlockType blockType) 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.Returns an Optional containing the size of the block related to the passed key.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.Returns an Optional containing the map of files that have been fully cached (all its blocks are present in the cache.long
Returns the Max size of the block cache, in bytes.Returns an Optional containing a map of regions and the percentage of how much of it has been cached so far.getStats()
Get the statistics for this block cache.Checks whether the block for the passed key is already cached.default boolean
API to check whether or not, the cache is enabled.default boolean
isMetaBlock
(BlockType blockType) Check if block type is meta or index blockiterator()
Returns Iterator over the blocks in the cache.default void
notifyFileCachingCompleted
(org.apache.hadoop.fs.Path fileName, int totalBlockCount, int dataBlockCount, long size) Notifies the cache implementation that the given file has been fully cached (all its blocks made into the cache).shouldCacheFile
(String fileName) Checks whether blocks for the passed file should be cached or not.void
shutdown()
Shutdown the cache.long
size()
Returns the total size of the block cache, in bytes.default boolean
waitForCacheInitialization
(long timeout) Wait for the block cache implementation to be completely enabled.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
cacheBlock
Add block to cache.- Parameters:
cacheKey
- The block's cache key.buf
- The block contents wrapped in a ByteBuffer.inMemory
- Whether block should be treated as in-memory
-
cacheBlock
default void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory, boolean waitWhenCache) Add block to cache.- Parameters:
cacheKey
- The block's cache key.buf
- The block contents wrapped in a ByteBuffer.inMemory
- Whether block should be treated as in-memorywaitWhenCache
- Whether to wait for the cache to be flushed mainly when BucketCache is configured.
-
cacheBlock
Add block to cache (defaults to not in-memory).- Parameters:
cacheKey
- The block's cache key.buf
- The object to cache.
-
getBlock
Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics) Fetch block from cache.- 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.
-
getBlock
default Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat, boolean updateCacheMetrics, BlockType blockType) Fetch block from cache.- 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 notblockType
- BlockType- Returns:
- Block or null if block is not in 2 cache.
-
evictBlock
Evict block from cache.- Parameters:
cacheKey
- Block to evict- Returns:
- true if block existed and was evicted, false if not
-
evictBlocksByHfileName
Evicts all blocks for the given HFile.- Returns:
- the number of blocks evicted
-
getStats
Get the statistics for this block cache. -
shutdown
void shutdown()Shutdown the cache. -
size
long size()Returns the total size of the block cache, in bytes.- Returns:
- size of cache, in bytes
-
getMaxSize
long getMaxSize()Returns the Max size of the block cache, in bytes.- Returns:
- size of cache, in bytes
-
getFreeSize
long getFreeSize()Returns the free size of the block cache, in bytes.- Returns:
- free space in cache, in bytes
-
getCurrentSize
long getCurrentSize()Returns the occupied size of the block cache, in bytes.- Returns:
- occupied space in cache, in bytes
-
getCurrentDataSize
long getCurrentDataSize()Returns the occupied size of data blocks, in bytes.- Returns:
- occupied space in cache, in bytes
-
getBlockCount
long getBlockCount()Returns the number of blocks currently cached in the block cache.- Returns:
- number of blocks in the cache
-
getDataBlockCount
long getDataBlockCount()Returns the number of data blocks currently cached in the block cache.- Returns:
- number of blocks in the cache
-
iterator
Returns Iterator over the blocks in the cache.- Specified by:
iterator
in interfaceIterable<CachedBlock>
-
getBlockCaches
Returns The list of sub blockcaches that make up this one; returns null if no sub caches. -
isMetaBlock
Check if block type is meta or index block- Parameters:
blockType
- block type of a given HFile block- Returns:
- true if block type is non-data block
-
notifyFileCachingCompleted
default void notifyFileCachingCompleted(org.apache.hadoop.fs.Path fileName, int totalBlockCount, int dataBlockCount, long size) Notifies the cache implementation that the given file has been fully cached (all its blocks made into the cache).- Parameters:
fileName
- the file that has been completely cached.totalBlockCount
- the total of blocks cached for this file.dataBlockCount
- number of DATA block type cached.size
- the size, in bytes, cached.
-
blockFitsIntoTheCache
Checks whether there's enough space left in the cache to accommodate the passed block. This method may not be overridden by all implementing classes. In such cases, the returned Optional will be empty. For subclasses implementing this logic, the returned Optional would contain the boolean value reflecting if the passed block fits into the remaining cache space available.- Parameters:
block
- the block we want to check if fits into the cache.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains the boolean value informing if the block fits into the cache available space.
-
shouldCacheFile
Checks whether blocks for the passed file should be cached or not. This method may not be overridden by all implementing classes. In such cases, the returned Optional will be empty. For subclasses implementing this logic, the returned Optional would contain the boolean value reflecting if the passed file should indeed be cached.- Parameters:
fileName
- to check if it should be cached.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains the boolean value informing if the file should be cached.
-
isAlreadyCached
Checks whether the block for the passed key is already cached. This method may not be overridden by all implementing classes. In such cases, the returned Optional will be empty. For subclasses implementing this logic, the returned Optional would contain the boolean value reflecting if the block for the passed key is already cached or not.- Parameters:
key
- for the block we want to check if it's already in the cache.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains the boolean value informing if the block is already cached.
-
getBlockSize
Returns an Optional containing the size of the block related to the passed key. If the block is not in the cache, returned optional will be empty. Also, this method may not be overridden by all implementing classes. In such cases, the returned Optional will be empty.- Parameters:
key
- for the block we want to check if it's already in the cache.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains the boolean value informing if the block is already cached.
-
getFullyCachedFiles
Returns an Optional containing the map of files that have been fully cached (all its blocks are present in the cache. This method may not be overridden by all implementing classes. In such cases, the returned Optional will be empty.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains a map of all files that have been fully cached.
-
getRegionCachedInfo
Returns an Optional containing a map of regions and the percentage of how much of it has been cached so far.- Returns:
- empty optional if this method is not supported, otherwise the returned optional contains a map of current regions caching percentage.
-
evictBlocksRangeByHfileName
Evict all blocks for the given file name between the passed offset values.- Parameters:
hfileName
- The file for which blocks should be evicted.initOffset
- the initial offset for the range of blocks to be evicted.endOffset
- the end offset for the range of blocks to be evicted.- Returns:
- number of blocks evicted.
-
isCacheEnabled
API to check whether or not, the cache is enabled.- Returns:
- returns true if the cache is enabled, false otherwise.
-
waitForCacheInitialization
Wait for the block cache implementation to be completely enabled. Some block cache implementations may take longer to initialise, and this initialisation may be asynchronous.- Parameters:
timeout
- time to wait for the cache to become enabled.- Returns:
- boolean true if the cache is enabled, false otherwise.
-