| 
21 | 21 | import static org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.DEFAULT_ERROR_TOLERATION_DURATION;  | 
22 | 22 | import static org.junit.Assert.assertEquals;  | 
23 | 23 | import static org.junit.Assert.assertFalse;  | 
 | 24 | +import static org.junit.Assert.assertNotNull;  | 
24 | 25 | import static org.junit.Assert.assertNull;  | 
25 | 26 | import static org.junit.Assert.assertTrue;  | 
26 | 27 | import static org.junit.Assert.fail;  | 
 | 
50 | 51 | import org.apache.hadoop.hbase.client.TableDescriptor;  | 
51 | 52 | import org.apache.hadoop.hbase.client.TableDescriptorBuilder;  | 
52 | 53 | import org.apache.hadoop.hbase.fs.HFileSystem;  | 
 | 54 | +import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;  | 
53 | 55 | import org.apache.hadoop.hbase.io.hfile.BlockCache;  | 
54 | 56 | import org.apache.hadoop.hbase.io.hfile.BlockCacheFactory;  | 
55 | 57 | import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;  | 
56 | 58 | import org.apache.hadoop.hbase.io.hfile.BlockType;  | 
 | 59 | +import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;  | 
57 | 60 | import org.apache.hadoop.hbase.io.hfile.CacheConfig;  | 
58 | 61 | import org.apache.hadoop.hbase.io.hfile.CacheTestUtils;  | 
 | 62 | +import org.apache.hadoop.hbase.io.hfile.HFileBlock;  | 
59 | 63 | import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;  | 
60 | 64 | import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache;  | 
61 | 65 | import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTracker;  | 
@@ -565,6 +569,66 @@ public void testFeatureKeyDisabled() throws Exception {  | 
565 | 569 |     }  | 
566 | 570 |   }  | 
567 | 571 | 
 
  | 
 | 572 | +  @Test  | 
 | 573 | +  public void testCacheConfigShouldCacheFile() throws Exception {  | 
 | 574 | +    // Evict the files from cache.  | 
 | 575 | +    for (HStoreFile file : hStoreFiles) {  | 
 | 576 | +      file.closeStoreFile(true);  | 
 | 577 | +    }  | 
 | 578 | +    // Verify that the API shouldCacheFileBlock returns the result correctly.  | 
 | 579 | +    // hStoreFiles[0], hStoreFiles[1], hStoreFiles[2] are hot files.  | 
 | 580 | +    // hStoreFiles[3] is a cold file.  | 
 | 581 | +    try {  | 
 | 582 | +      assertTrue(cacheConf.shouldCacheBlockOnRead(BlockCategory.DATA,  | 
 | 583 | +        hStoreFiles.get(0).getFileInfo().getHFileInfo(),  | 
 | 584 | +        hStoreFiles.get(0).getFileInfo().getConf()));  | 
 | 585 | +      assertTrue(cacheConf.shouldCacheBlockOnRead(BlockCategory.DATA,  | 
 | 586 | +        hStoreFiles.get(1).getFileInfo().getHFileInfo(),  | 
 | 587 | +        hStoreFiles.get(1).getFileInfo().getConf()));  | 
 | 588 | +      assertTrue(cacheConf.shouldCacheBlockOnRead(BlockCategory.DATA,  | 
 | 589 | +        hStoreFiles.get(2).getFileInfo().getHFileInfo(),  | 
 | 590 | +        hStoreFiles.get(2).getFileInfo().getConf()));  | 
 | 591 | +      assertFalse(cacheConf.shouldCacheBlockOnRead(BlockCategory.DATA,  | 
 | 592 | +        hStoreFiles.get(3).getFileInfo().getHFileInfo(),  | 
 | 593 | +        hStoreFiles.get(3).getFileInfo().getConf()));  | 
 | 594 | +    } finally {  | 
 | 595 | +      for (HStoreFile file : hStoreFiles) {  | 
 | 596 | +        file.initReader();  | 
 | 597 | +      }  | 
 | 598 | +    }  | 
 | 599 | +  }  | 
 | 600 | + | 
 | 601 | +  @Test  | 
 | 602 | +  public void testCacheOnReadColdFile() throws Exception {  | 
 | 603 | +    // hStoreFiles[3] is a cold file. the blocks should not get loaded after a readBlock call.  | 
 | 604 | +    HStoreFile hStoreFile = hStoreFiles.get(3);  | 
 | 605 | +    BlockCacheKey cacheKey = new BlockCacheKey(hStoreFile.getPath(), 0, true, BlockType.DATA);  | 
 | 606 | +    testCacheOnRead(hStoreFile, cacheKey, 23025, false);  | 
 | 607 | +  }  | 
 | 608 | + | 
 | 609 | +  @Test  | 
 | 610 | +  public void testCacheOnReadHotFile() throws Exception {  | 
 | 611 | +    // hStoreFiles[0] is a hot file. the blocks should get loaded after a readBlock call.  | 
 | 612 | +    HStoreFile hStoreFile = hStoreFiles.get(0);  | 
 | 613 | +    BlockCacheKey cacheKey =  | 
 | 614 | +      new BlockCacheKey(hStoreFiles.get(0).getPath(), 0, true, BlockType.DATA);  | 
 | 615 | +    testCacheOnRead(hStoreFile, cacheKey, 23025, true);  | 
 | 616 | +  }  | 
 | 617 | + | 
 | 618 | +  private void testCacheOnRead(HStoreFile hStoreFile, BlockCacheKey key, long onDiskBlockSize,  | 
 | 619 | +    boolean expectedCached) throws Exception {  | 
 | 620 | +    // Execute the read block API which will try to cache the block if the block is a hot block.  | 
 | 621 | +    hStoreFile.getReader().getHFileReader().readBlock(key.getOffset(), onDiskBlockSize, true, false,  | 
 | 622 | +      false, false, key.getBlockType(), DataBlockEncoding.NONE);  | 
 | 623 | +    // Validate that the hot block gets cached and cold block is not cached.  | 
 | 624 | +    HFileBlock block = (HFileBlock) blockCache.getBlock(key, false, false, false, BlockType.DATA);  | 
 | 625 | +    if (expectedCached) {  | 
 | 626 | +      assertNotNull(block);  | 
 | 627 | +    } else {  | 
 | 628 | +      assertNull(block);  | 
 | 629 | +    }  | 
 | 630 | +  }  | 
 | 631 | + | 
568 | 632 |   private void validateBlocks(Set<BlockCacheKey> keys, int expectedTotalKeys, int expectedHotBlocks,  | 
569 | 633 |     int expectedColdBlocks) {  | 
570 | 634 |     int numHotBlocks = 0, numColdBlocks = 0;  | 
 | 
0 commit comments