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 ;
@@ -204,10 +205,11 @@ public void testReaderWithLRUBlockCache() throws Exception {
204205 lru .shutdown ();
205206 }
206207
207- private BlockCache initCombinedBlockCache () {
208+ private BlockCache initCombinedBlockCache (final String l1CachePolicy ) {
208209 Configuration that = HBaseConfiguration .create (conf );
209210 that .setFloat (BUCKET_CACHE_SIZE_KEY , 32 ); // 32MB for bucket cache.
210211 that .set (BUCKET_CACHE_IOENGINE_KEY , "offheap" );
212+ that .set (BLOCKCACHE_POLICY_KEY , l1CachePolicy );
211213 BlockCache bc = BlockCacheFactory .createBlockCache (that );
212214 Assert .assertNotNull (bc );
213215 Assert .assertTrue (bc instanceof CombinedBlockCache );
@@ -224,7 +226,7 @@ public void testReaderWithCombinedBlockCache() throws Exception {
224226 fillByteBuffAllocator (alloc , bufCount );
225227 Path storeFilePath = writeStoreFile ();
226228 // Open the file reader with CombinedBlockCache
227- BlockCache combined = initCombinedBlockCache ();
229+ BlockCache combined = initCombinedBlockCache ("LRU" );
228230 conf .setBoolean (EVICT_BLOCKS_ON_CLOSE_KEY , true );
229231 CacheConfig cacheConfig = new CacheConfig (conf , null , combined , alloc );
230232 HFile .Reader reader = HFile .createReader (fs , storeFilePath , cacheConfig , true , conf );
@@ -854,4 +856,72 @@ public void testDBEShipped() throws IOException {
854856 writer .close ();
855857 }
856858 }
859+
860+ /**
861+ * Test case for CombinedBlockCache with TinyLfu as L1 cache
862+ */
863+ @ Test
864+ public void testReaderWithTinyLfuCombinedBlockCache () throws Exception {
865+ testReaderCombinedCache ("TinyLfu" );
866+ }
867+
868+ /**
869+ * Test case for CombinedBlockCache with AdaptiveLRU as L1 cache
870+ */
871+ @ Test
872+ public void testReaderWithAdaptiveLruCombinedBlockCache () throws Exception {
873+ testReaderCombinedCache ("AdaptiveLRU" );
874+ }
875+
876+ /**
877+ * Test case for CombinedBlockCache with AdaptiveLRU as L1 cache
878+ */
879+ @ Test
880+ public void testReaderWithLruCombinedBlockCache () throws Exception {
881+ testReaderCombinedCache ("LRU" );
882+ }
883+
884+ private void testReaderCombinedCache (final String l1CachePolicy ) throws Exception {
885+ int bufCount = 1024 ;
886+ int blockSize = 64 * 1024 ;
887+ ByteBuffAllocator alloc = initAllocator (true , bufCount , blockSize , 0 );
888+ fillByteBuffAllocator (alloc , bufCount );
889+ Path storeFilePath = writeStoreFile ();
890+ // Open the file reader with CombinedBlockCache
891+ BlockCache combined = initCombinedBlockCache (l1CachePolicy );
892+ conf .setBoolean (EVICT_BLOCKS_ON_CLOSE_KEY , true );
893+ CacheConfig cacheConfig = new CacheConfig (conf , null , combined , alloc );
894+ HFile .Reader reader = HFile .createReader (fs , storeFilePath , cacheConfig , true , conf );
895+ long offset = 0 ;
896+ Cacheable cachedBlock = null ;
897+ while (offset < reader .getTrailer ().getLoadOnOpenDataOffset ()) {
898+ BlockCacheKey key = new BlockCacheKey (storeFilePath .getName (), offset );
899+ HFileBlock block = reader .readBlock (offset , -1 , true , true , false , true , null , null );
900+ offset += block .getOnDiskSizeWithHeader ();
901+ // Read the cached block.
902+ cachedBlock = combined .getBlock (key , false , false , true );
903+ try {
904+ Assert .assertNotNull (cachedBlock );
905+ Assert .assertTrue (cachedBlock instanceof HFileBlock );
906+ HFileBlock hfb = (HFileBlock ) cachedBlock ;
907+ // Data block will be cached in BucketCache, so it should be an off-heap block.
908+ if (hfb .getBlockType ().isData ()) {
909+ Assert .assertTrue (hfb .isSharedMem ());
910+ } else if (!l1CachePolicy .equals ("TinyLfu" )) {
911+ Assert .assertFalse (hfb .isSharedMem ());
912+ }
913+ } finally {
914+ cachedBlock .release ();
915+ }
916+ block .release (); // return back the ByteBuffer back to allocator.
917+ }
918+ reader .close ();
919+ combined .shutdown ();
920+ if (cachedBlock != null ) {
921+ Assert .assertEquals (0 , cachedBlock .refCnt ());
922+ }
923+ Assert .assertEquals (bufCount , alloc .getFreeBufferCount ());
924+ alloc .clean ();
925+ }
926+
857927}
0 commit comments