public class LruBlockCache extends SynchronousLoadingBlockCache implements BlockCache, HeapSize
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(java.lang.String, byte[], boolean)
and getBlock(java.lang.String)
operations.
Contains three levels of block priority to allow for scan-resistance and in-memory families. A block is added with an inMemory flag if necessary, otherwise a block becomes a single access priority. Once a blocked is accessed again, it changes to multiple access. This 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 | Class and Description |
---|---|
static class |
LruBlockCache.CacheStats |
BlockCache.Loader, BlockCache.Stats
Modifier and Type | Field and Description |
---|---|
static long |
CACHE_FIXED_OVERHEAD |
Constructor and Description |
---|
LruBlockCache(LruBlockCacheConfiguration conf)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
CacheEntry |
cacheBlock(String blockName,
byte[] buf)
Cache the block with the specified name and buffer.
|
CacheEntry |
cacheBlock(String blockName,
byte[] buf,
boolean inMemory)
Cache the block with the specified name and buffer.
|
static long |
calculateOverhead(long maxSize,
long blockSize,
int concurrency) |
protected long |
evictBlock(CachedBlock block) |
CacheEntry |
getBlock(String blockName)
Get the buffer of the block with the specified name.
|
protected CacheEntry |
getBlockNoStats(String blockName)
Get a block from the cache without changing any stats the cache is keeping.
|
long |
getCurrentSize()
Get the current size of this cache.
|
long |
getEvictedCount()
Get the number of blocks that have been evicted during the lifetime of this cache.
|
long |
getEvictionCount()
Get the number of eviction runs that have occurred
|
long |
getFreeSize()
Get the current size of this cache.
|
int |
getMaxEntrySize()
Get the maximum size of an individual cache entry.
|
long |
getMaxHeapSize()
Get the maximum amount of on heap memory this cache will use.
|
long |
getMaxSize()
Get the maximum size of this cache.
|
long |
getOverhead() |
LruBlockCache.CacheStats |
getStats()
Get counter statistics for this cache.
|
long |
heapSize() |
void |
logStats() |
void |
shutdown() |
long |
size()
Get the size of this cache (number of cached blocks)
|
getBlock
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getBlock
public LruBlockCache(LruBlockCacheConfiguration conf)
All other factors will be calculated based on defaults specified in this class.
conf
- block cache configurationpublic long getOverhead()
public CacheEntry cacheBlock(String blockName, byte[] buf, boolean inMemory)
It is assumed this will NEVER be called on an already cached block. If that is done, it is assumed that you are reinserting the same exact block due to a race condition and will update the buffer but not modify the size of the cache.
blockName
- block namebuf
- block bufferinMemory
- if block is in-memorypublic CacheEntry cacheBlock(String blockName, byte[] buf)
It is assumed this will NEVER be called on an already cached block. If that is done, it is assumed that you are reinserting the same exact block due to a race condition and will update the buffer but not modify the size of the cache.
cacheBlock
in interface BlockCache
blockName
- block namebuf
- block bufferpublic CacheEntry getBlock(String blockName)
getBlock
in interface BlockCache
blockName
- block nameprotected CacheEntry getBlockNoStats(String blockName)
SynchronousLoadingBlockCache
getBlockNoStats
in class SynchronousLoadingBlockCache
protected long evictBlock(CachedBlock block)
public long getMaxHeapSize()
BlockCache
getMaxHeapSize
in interface BlockCache
public long getMaxSize()
BlockCache
getMaxSize
in interface BlockCache
public int getMaxEntrySize()
SynchronousLoadingBlockCache
getMaxEntrySize
in class SynchronousLoadingBlockCache
public long getCurrentSize()
public long getFreeSize()
public long size()
public long getEvictionCount()
public long getEvictedCount()
public void logStats()
public LruBlockCache.CacheStats getStats()
Includes: total accesses, hits, misses, evicted blocks, and runs of the eviction processes.
getStats
in interface BlockCache
public long heapSize()
public static long calculateOverhead(long maxSize, long blockSize, int concurrency)
public void shutdown()
Copyright © 2011–2018 The Apache Software Foundation. All rights reserved.