2222import static org .apache .hadoop .hbase .io .ByteBuffAllocator .BUFFER_SIZE_KEY ;
2323import static org .apache .hadoop .hbase .io .ByteBuffAllocator .MAX_BUFFER_COUNT_KEY ;
2424import static org .apache .hadoop .hbase .io .ByteBuffAllocator .MIN_ALLOCATE_SIZE_KEY ;
25+ import static org .apache .hadoop .hbase .io .hfile .BlockCacheFactory .BLOCKCACHE_POLICY_KEY ;
2526import static org .apache .hadoop .hbase .io .hfile .CacheConfig .EVICT_BLOCKS_ON_CLOSE_KEY ;
2627import static org .junit .Assert .assertEquals ;
2728import static org .junit .Assert .assertFalse ;
@@ -205,10 +206,11 @@ public void testReaderWithLRUBlockCache() throws Exception {
205206 lru .shutdown ();
206207 }
207208
208- private BlockCache initCombinedBlockCache () {
209+ private BlockCache initCombinedBlockCache (final String l1CachePolicy ) {
209210 Configuration that = HBaseConfiguration .create (conf );
210211 that .setFloat (BUCKET_CACHE_SIZE_KEY , 32 ); // 32MB for bucket cache.
211212 that .set (BUCKET_CACHE_IOENGINE_KEY , "offheap" );
213+ that .set (BLOCKCACHE_POLICY_KEY , l1CachePolicy );
212214 BlockCache bc = BlockCacheFactory .createBlockCache (that );
213215 Assert .assertNotNull (bc );
214216 Assert .assertTrue (bc instanceof CombinedBlockCache );
@@ -225,7 +227,7 @@ public void testReaderWithCombinedBlockCache() throws Exception {
225227 fillByteBuffAllocator (alloc , bufCount );
226228 Path storeFilePath = writeStoreFile ();
227229 // Open the file reader with CombinedBlockCache
228- BlockCache combined = initCombinedBlockCache ();
230+ BlockCache combined = initCombinedBlockCache ("LRU" );
229231 conf .setBoolean (EVICT_BLOCKS_ON_CLOSE_KEY , true );
230232 CacheConfig cacheConfig = new CacheConfig (conf , null , combined , alloc );
231233 HFile .Reader reader = HFile .createReader (fs , storeFilePath , cacheConfig , true , conf );
@@ -890,4 +892,72 @@ public void testDBEShipped() throws IOException {
890892 writer .close ();
891893 }
892894 }
895+
896+ /**
897+ * Test case for CombinedBlockCache with TinyLfu as L1 cache
898+ */
899+ @ Test
900+ public void testReaderWithTinyLfuCombinedBlockCache () throws Exception {
901+ testReaderCombinedCache ("TinyLfu" );
902+ }
903+
904+ /**
905+ * Test case for CombinedBlockCache with AdaptiveLRU as L1 cache
906+ */
907+ @ Test
908+ public void testReaderWithAdaptiveLruCombinedBlockCache () throws Exception {
909+ testReaderCombinedCache ("AdaptiveLRU" );
910+ }
911+
912+ /**
913+ * Test case for CombinedBlockCache with AdaptiveLRU as L1 cache
914+ */
915+ @ Test
916+ public void testReaderWithLruCombinedBlockCache () throws Exception {
917+ testReaderCombinedCache ("LRU" );
918+ }
919+
920+ private void testReaderCombinedCache (final String l1CachePolicy ) throws Exception {
921+ int bufCount = 1024 ;
922+ int blockSize = 64 * 1024 ;
923+ ByteBuffAllocator alloc = initAllocator (true , bufCount , blockSize , 0 );
924+ fillByteBuffAllocator (alloc , bufCount );
925+ Path storeFilePath = writeStoreFile ();
926+ // Open the file reader with CombinedBlockCache
927+ BlockCache combined = initCombinedBlockCache (l1CachePolicy );
928+ conf .setBoolean (EVICT_BLOCKS_ON_CLOSE_KEY , true );
929+ CacheConfig cacheConfig = new CacheConfig (conf , null , combined , alloc );
930+ HFile .Reader reader = HFile .createReader (fs , storeFilePath , cacheConfig , true , conf );
931+ long offset = 0 ;
932+ Cacheable cachedBlock = null ;
933+ while (offset < reader .getTrailer ().getLoadOnOpenDataOffset ()) {
934+ BlockCacheKey key = new BlockCacheKey (storeFilePath .getName (), offset );
935+ HFileBlock block = reader .readBlock (offset , -1 , true , true , false , true , null , null );
936+ offset += block .getOnDiskSizeWithHeader ();
937+ // Read the cached block.
938+ cachedBlock = combined .getBlock (key , false , false , true );
939+ try {
940+ Assert .assertNotNull (cachedBlock );
941+ Assert .assertTrue (cachedBlock instanceof HFileBlock );
942+ HFileBlock hfb = (HFileBlock ) cachedBlock ;
943+ // Data block will be cached in BucketCache, so it should be an off-heap block.
944+ if (hfb .getBlockType ().isData ()) {
945+ Assert .assertTrue (hfb .isSharedMem ());
946+ } else if (!l1CachePolicy .equals ("TinyLfu" )) {
947+ Assert .assertFalse (hfb .isSharedMem ());
948+ }
949+ } finally {
950+ cachedBlock .release ();
951+ }
952+ block .release (); // return back the ByteBuffer back to allocator.
953+ }
954+ reader .close ();
955+ combined .shutdown ();
956+ if (cachedBlock != null ) {
957+ Assert .assertEquals (0 , cachedBlock .refCnt ());
958+ }
959+ Assert .assertEquals (bufCount , alloc .getFreeBufferCount ());
960+ alloc .clean ();
961+ }
962+
893963}
0 commit comments