use of org.apache.accumulo.core.file.blockfile.cache.lru.LruBlockCache in project accumulo by apache.
the class TestLruBlockCache method testCacheEvictionThreePriorities.
public void testCacheEvictionThreePriorities() throws Exception {
long maxSize = 100000;
long blockSize = calculateBlockSize(maxSize, 10);
DefaultConfiguration dc = DefaultConfiguration.getInstance();
ConfigurationCopy cc = new ConfigurationCopy(dc);
cc.set(Property.TSERV_CACHE_MANAGER_IMPL, LruBlockCacheManager.class.getName());
BlockCacheManager manager = BlockCacheManagerFactory.getInstance(cc);
cc.set(Property.TSERV_DEFAULT_BLOCKSIZE, Long.toString(blockSize));
cc.set(Property.TSERV_INDEXCACHE_SIZE, Long.toString(maxSize));
LruBlockCacheConfiguration.builder(CacheType.INDEX).useEvictionThread(false).minFactor(0.98f).acceptableFactor(0.99f).singleFactor(0.33f).multiFactor(0.33f).memoryFactor(0.34f).buildMap().forEach(cc::set);
manager.start(new BlockCacheConfiguration(cc));
LruBlockCache cache = (LruBlockCache) manager.getBlockCache(CacheType.INDEX);
Block[] singleBlocks = generateFixedBlocks(5, blockSize, "single");
Block[] multiBlocks = generateFixedBlocks(5, blockSize, "multi");
Block[] memoryBlocks = generateFixedBlocks(5, blockSize, "memory");
long expectedCacheSize = cache.heapSize();
// Add 3 blocks from each priority
for (int i = 0; i < 3; i++) {
// Just add single blocks
cache.cacheBlock(singleBlocks[i].blockName, singleBlocks[i].buf);
expectedCacheSize += singleBlocks[i].heapSize();
// Add and get multi blocks
cache.cacheBlock(multiBlocks[i].blockName, multiBlocks[i].buf);
expectedCacheSize += multiBlocks[i].heapSize();
cache.getBlock(multiBlocks[i].blockName);
// Add memory blocks as such
cache.cacheBlock(memoryBlocks[i].blockName, memoryBlocks[i].buf, true);
expectedCacheSize += memoryBlocks[i].heapSize();
}
// Do not expect any evictions yet
assertEquals(0, cache.getEvictionCount());
// Verify cache size
assertEquals(expectedCacheSize, cache.heapSize());
// Insert a single block, oldest single should be evicted
cache.cacheBlock(singleBlocks[3].blockName, singleBlocks[3].buf);
// Single eviction, one thing evicted
assertEquals(1, cache.getEvictionCount());
assertEquals(1, cache.getEvictedCount());
// Verify oldest single block is the one evicted
assertEquals(null, cache.getBlock(singleBlocks[0].blockName));
// Change the oldest remaining single block to a multi
cache.getBlock(singleBlocks[1].blockName);
// Insert another single block
cache.cacheBlock(singleBlocks[4].blockName, singleBlocks[4].buf);
// Two evictions, two evicted.
assertEquals(2, cache.getEvictionCount());
assertEquals(2, cache.getEvictedCount());
// Oldest multi block should be evicted now
assertEquals(null, cache.getBlock(multiBlocks[0].blockName));
// Insert another memory block
cache.cacheBlock(memoryBlocks[3].blockName, memoryBlocks[3].buf, true);
// Three evictions, three evicted.
assertEquals(3, cache.getEvictionCount());
assertEquals(3, cache.getEvictedCount());
// Oldest memory block should be evicted now
assertEquals(null, cache.getBlock(memoryBlocks[0].blockName));
// Add a block that is twice as big (should force two evictions)
Block[] bigBlocks = generateFixedBlocks(3, blockSize * 3, "big");
cache.cacheBlock(bigBlocks[0].blockName, bigBlocks[0].buf);
// Four evictions, six evicted (inserted block 3X size, expect +3 evicted)
assertEquals(4, cache.getEvictionCount());
assertEquals(6, cache.getEvictedCount());
// Expect three remaining singles to be evicted
assertEquals(null, cache.getBlock(singleBlocks[2].blockName));
assertEquals(null, cache.getBlock(singleBlocks[3].blockName));
assertEquals(null, cache.getBlock(singleBlocks[4].blockName));
// Make the big block a multi block
cache.getBlock(bigBlocks[0].blockName);
// Cache another single big block
cache.cacheBlock(bigBlocks[1].blockName, bigBlocks[1].buf);
// Five evictions, nine evicted (3 new)
assertEquals(5, cache.getEvictionCount());
assertEquals(9, cache.getEvictedCount());
// Expect three remaining multis to be evicted
assertEquals(null, cache.getBlock(singleBlocks[1].blockName));
assertEquals(null, cache.getBlock(multiBlocks[1].blockName));
assertEquals(null, cache.getBlock(multiBlocks[2].blockName));
// Cache a big memory block
cache.cacheBlock(bigBlocks[2].blockName, bigBlocks[2].buf, true);
// Six evictions, twelve evicted (3 new)
assertEquals(6, cache.getEvictionCount());
assertEquals(12, cache.getEvictedCount());
// Expect three remaining in-memory to be evicted
assertEquals(null, cache.getBlock(memoryBlocks[1].blockName));
assertEquals(null, cache.getBlock(memoryBlocks[2].blockName));
assertEquals(null, cache.getBlock(memoryBlocks[3].blockName));
manager.stop();
}
Aggregations