@Test
  public void testRetrieveFromFile() throws Exception {
    HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
    Path testDir = TEST_UTIL.getDataTestDir();
    TEST_UTIL.getTestFileSystem().mkdirs(testDir);

    Configuration conf = HBaseConfiguration.create();
    // Disables the persister thread by setting its interval to MAX_VALUE
    conf.setLong(BUCKETCACHE_PERSIST_INTERVAL_KEY, Long.MAX_VALUE);

    BucketCache bucketCache = null;
    BucketCache recoveredBucketCache = null;
    try {
      bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize,
        constructedBlockSize, constructedBlockSizes, writeThreads, writerQLen,
        testDir + "/bucket.persistence", DEFAULT_ERROR_TOLERATION_DURATION, conf);
      assertTrue(bucketCache.waitForCacheInitialization(10000));
      long usedSize = bucketCache.getAllocator().getUsedSize();
      assertEquals(0, usedSize);
      CacheTestUtils.HFileBlockPair[] blocks =
        CacheTestUtils.generateHFileBlocks(constructedBlockSize, 1);
      // Add blocks
      for (CacheTestUtils.HFileBlockPair block : blocks) {
        cacheAndWaitUntilFlushedToBucket(bucketCache, block.getBlockName(), block.getBlock());
      }
      usedSize = bucketCache.getAllocator().getUsedSize();
      assertNotEquals(0, usedSize);
      // 1.persist cache to file
      bucketCache.shutdown();
      // restore cache from file
      bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize,
        constructedBlockSize, constructedBlockSizes, writeThreads, writerQLen,
        testDir + "/bucket.persistence", DEFAULT_ERROR_TOLERATION_DURATION, conf);
      assertTrue(bucketCache.waitForCacheInitialization(10000));
      waitPersistentCacheValidation(conf, bucketCache);
      assertEquals(usedSize, bucketCache.getAllocator().getUsedSize());
      // persist cache to file
      bucketCache.shutdown();

      // 2.delete bucket cache file
      final java.nio.file.Path cacheFile =
        FileSystems.getDefault().getPath(testDir.toString(), "bucket.cache");
      assertTrue(Files.deleteIfExists(cacheFile));
      // can't restore cache from file
      recoveredBucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize,
        constructedBlockSize, constructedBlockSizes, writeThreads, writerQLen,
        testDir + "/bucket.persistence", DEFAULT_ERROR_TOLERATION_DURATION, conf);
      assertTrue(recoveredBucketCache.waitForCacheInitialization(10000));
      waitPersistentCacheValidation(conf, recoveredBucketCache);
      assertEquals(0, recoveredBucketCache.getAllocator().getUsedSize());
      assertEquals(0, recoveredBucketCache.backingMap.size());
      // Add blocks
      for (CacheTestUtils.HFileBlockPair block : blocks) {
        cacheAndWaitUntilFlushedToBucket(recoveredBucketCache, block.getBlockName(),
          block.getBlock());
      }
      usedSize = recoveredBucketCache.getAllocator().getUsedSize();
      assertNotEquals(0, usedSize);
      // persist cache to file
      recoveredBucketCache.shutdown();

      // 3.delete backingMap persistence file
      final java.nio.file.Path mapFile =
        FileSystems.getDefault().getPath(testDir.toString(), "bucket.persistence");
      assertTrue(Files.deleteIfExists(mapFile));
      // can't restore cache from file
      bucketCache = new BucketCache("file:" + testDir + "/bucket.cache", capacitySize,
        constructedBlockSize, constructedBlockSizes, writeThreads, writerQLen,
        testDir + "/bucket.persistence", DEFAULT_ERROR_TOLERATION_DURATION, conf);
      assertTrue(bucketCache.waitForCacheInitialization(10000));
      waitPersistentCacheValidation(conf, bucketCache);
      assertEquals(0, bucketCache.getAllocator().getUsedSize());
      assertEquals(0, bucketCache.backingMap.size());
    } finally {
      if (bucketCache != null) {
        bucketCache.shutdown();
      }
      if (recoveredBucketCache != null) {
        recoveredBucketCache.shutdown();
      }
    }
    TEST_UTIL.cleanupTestDir();
  }
Test cache file or persistence file does not exist whether BucketCache starts normally (1)
Start BucketCache and add some blocks, then shutdown BucketCache and persist cache to file.
Restart BucketCache and it can restore cache from file. (2) Delete bucket cache file after
shutdown BucketCache. Restart BucketCache and it can't restore cache from file, the cache file
and persistence file would be deleted before BucketCache start normally. (3) Delete persistence
file after shutdown BucketCache. Restart BucketCache and it can't restore cache from file, the
cache file and persistence file would be deleted before BucketCache start normally.
@throws Exception the exception
hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/bucket/TestVerifyBucketCacheFile.java