Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HBASE-23976 Use nio for file manipulation in TestVerifyBucketCacheFile #1342

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,4 @@
/**
/*
* Copyright The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand All @@ -19,14 +19,16 @@
package org.apache.hadoop.hbase.io.hfile.bucket;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Arrays;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseTestingUtility;
Expand Down Expand Up @@ -90,15 +92,15 @@ public void testRetrieveFromFile() throws Exception {
new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, constructedBlockSize,
constructedBlockSizes, writeThreads, writerQLen, testDir + "/bucket.persistence");
long usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize == 0);
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();
assertTrue(usedSize != 0);
assertNotEquals(0, usedSize);
// 1.persist cache to file
bucketCache.shutdown();
// restore cache from file
Expand All @@ -110,8 +112,9 @@ public void testRetrieveFromFile() throws Exception {
bucketCache.shutdown();

// 2.delete bucket cache file
File cacheFile = new File(testDir + "/bucket.cache");
assertTrue(cacheFile.delete());
final java.nio.file.Path cacheFile =
FileSystems.getDefault().getPath(testDir.toString(), "bucket.cache");
assertTrue(Files.deleteIfExists(cacheFile));
// can't restore cache from file
bucketCache =
new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, constructedBlockSize,
Expand All @@ -123,13 +126,14 @@ public void testRetrieveFromFile() throws Exception {
cacheAndWaitUntilFlushedToBucket(bucketCache, block.getBlockName(), block.getBlock());
}
usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize != 0);
assertNotEquals(0, usedSize);
// persist cache to file
bucketCache.shutdown();

// 3.delete backingMap persistence file
File mapFile = new File(testDir + "/bucket.persistence");
assertTrue(mapFile.delete());
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,
Expand Down Expand Up @@ -157,7 +161,7 @@ public void testModifiedBucketCacheFileData() throws Exception {
new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, constructedBlockSize,
constructedBlockSizes, writeThreads, writerQLen, testDir + "/bucket.persistence");
long usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize == 0);
assertEquals(0, usedSize);

CacheTestUtils.HFileBlockPair[] blocks =
CacheTestUtils.generateHFileBlocks(constructedBlockSize, 1);
Expand All @@ -166,7 +170,7 @@ public void testModifiedBucketCacheFileData() throws Exception {
cacheAndWaitUntilFlushedToBucket(bucketCache, block.getBlockName(), block.getBlock());
}
usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize != 0);
assertNotEquals(0, usedSize);
// persist cache to file
bucketCache.shutdown();

Expand Down Expand Up @@ -204,7 +208,7 @@ public void testModifiedBucketCacheFileTime() throws Exception {
new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, constructedBlockSize,
constructedBlockSizes, writeThreads, writerQLen, testDir + "/bucket.persistence");
long usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize == 0);
assertEquals(0, usedSize);

CacheTestUtils.HFileBlockPair[] blocks =
CacheTestUtils.generateHFileBlocks(constructedBlockSize, 1);
Expand All @@ -213,13 +217,14 @@ public void testModifiedBucketCacheFileTime() throws Exception {
cacheAndWaitUntilFlushedToBucket(bucketCache, block.getBlockName(), block.getBlock());
}
usedSize = bucketCache.getAllocator().getUsedSize();
assertTrue(usedSize != 0);
assertNotEquals(0, usedSize);
// persist cache to file
bucketCache.shutdown();

// modified bucket cache file LastModifiedTime
File file = new File(testDir + "/bucket.cache");
assertTrue(file.setLastModified(System.currentTimeMillis() + 1000));
final java.nio.file.Path file =
FileSystems.getDefault().getPath(testDir.toString(), "bucket.cache");
Files.setLastModifiedTime(file, FileTime.from(Instant.now().plusMillis(1_000)));
// can't restore cache from file
bucketCache =
new BucketCache("file:" + testDir + "/bucket.cache", capacitySize, constructedBlockSize,
Expand Down