-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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-27370 Avoid decompressing blocks when reading from bucket cache… #4781
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,13 +19,15 @@ | |
|
||
import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasName; | ||
import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasParentSpanId; | ||
import static org.apache.hadoop.hbase.io.hfile.CacheConfig.CACHE_DATA_BLOCKS_COMPRESSED_KEY; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.allOf; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasItems; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
|
@@ -34,6 +36,8 @@ | |
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiFunction; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.fs.FileSystem; | ||
import org.apache.hadoop.fs.Path; | ||
|
@@ -47,6 +51,7 @@ | |
import org.apache.hadoop.hbase.client.trace.StringTraceRenderer; | ||
import org.apache.hadoop.hbase.fs.HFileSystem; | ||
import org.apache.hadoop.hbase.io.ByteBuffAllocator; | ||
import org.apache.hadoop.hbase.io.compress.Compression; | ||
import org.apache.hadoop.hbase.regionserver.StoreFileWriter; | ||
import org.apache.hadoop.hbase.testclassification.IOTests; | ||
import org.apache.hadoop.hbase.testclassification.MediumTests; | ||
|
@@ -148,36 +153,88 @@ private void readStoreFileLikeScanner(Path storeFilePath) throws Exception { | |
} | ||
|
||
private void readStoreFile(Path storeFilePath) throws Exception { | ||
readStoreFile(storeFilePath, (r, o) -> { | ||
HFileBlock block = null; | ||
try { | ||
block = r.readBlock(o, -1, false, true, false, true, null, null); | ||
} catch (IOException e) { | ||
fail(e.getMessage()); | ||
} | ||
return block; | ||
}, (key, block) -> { | ||
boolean isCached = blockCache.getBlock(key, true, false, true) != null; | ||
if ( | ||
block.getBlockType() == BlockType.DATA || block.getBlockType() == BlockType.ROOT_INDEX | ||
|| block.getBlockType() == BlockType.INTERMEDIATE_INDEX | ||
) { | ||
assertTrue(isCached); | ||
} | ||
}); | ||
} | ||
|
||
private void readStoreFileCacheOnly(Path storeFilePath) throws Exception { | ||
readStoreFile(storeFilePath, (r, o) -> { | ||
HFileBlock block = null; | ||
try { | ||
block = r.readBlock(o, -1, false, true, false, true, null, null, true); | ||
} catch (IOException e) { | ||
fail(e.getMessage()); | ||
} | ||
return block; | ||
}, (key, block) -> { | ||
boolean isCached = blockCache.getBlock(key, true, false, true) != null; | ||
if (block.getBlockType() == BlockType.DATA) { | ||
assertFalse(block.isUnpacked()); | ||
} else if ( | ||
block.getBlockType() == BlockType.ROOT_INDEX | ||
|| block.getBlockType() == BlockType.INTERMEDIATE_INDEX | ||
) { | ||
assertTrue(block.isUnpacked()); | ||
} | ||
assertTrue(isCached); | ||
}); | ||
} | ||
|
||
private void readStoreFile(Path storeFilePath, | ||
BiFunction<HFile.Reader, Long, HFileBlock> readFunction, | ||
BiConsumer<BlockCacheKey, HFileBlock> validationFunction) throws Exception { | ||
// Open the file | ||
HFile.Reader reader = HFile.createReader(fs, storeFilePath, cacheConf, true, conf); | ||
|
||
while (!reader.prefetchComplete()) { | ||
// Sleep for a bit | ||
Thread.sleep(1000); | ||
} | ||
|
||
// Check that all of the data blocks were preloaded | ||
BlockCache blockCache = cacheConf.getBlockCache().get(); | ||
long offset = 0; | ||
while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) { | ||
HFileBlock block = reader.readBlock(offset, -1, false, true, false, true, null, null); | ||
HFileBlock block = readFunction.apply(reader, offset); | ||
BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset); | ||
boolean isCached = blockCache.getBlock(blockCacheKey, true, false, true) != null; | ||
if ( | ||
block.getBlockType() == BlockType.DATA || block.getBlockType() == BlockType.ROOT_INDEX | ||
|| block.getBlockType() == BlockType.INTERMEDIATE_INDEX | ||
) { | ||
assertTrue(isCached); | ||
} | ||
validationFunction.accept(blockCacheKey, block); | ||
offset += block.getOnDiskSizeWithHeader(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPrefetchCompressed() throws Exception { | ||
conf.setBoolean(CACHE_DATA_BLOCKS_COMPRESSED_KEY, true); | ||
cacheConf = new CacheConfig(conf, blockCache); | ||
HFileContext context = new HFileContextBuilder().withCompression(Compression.Algorithm.GZ) | ||
.withBlockSize(DATA_BLOCK_SIZE).build(); | ||
Path storeFile = writeStoreFile("TestPrefetchCompressed", context); | ||
readStoreFileCacheOnly(storeFile); | ||
conf.setBoolean(CACHE_DATA_BLOCKS_COMPRESSED_KEY, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] is it for resetting configuration to avoid error for other tests? should we use a cleanup? but it's not a blocker There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, don't want to interfere on other tests. |
||
|
||
} | ||
|
||
private Path writeStoreFile(String fname) throws IOException { | ||
Path storeFileParentDir = new Path(TEST_UTIL.getDataTestDir(), fname); | ||
HFileContext meta = new HFileContextBuilder().withBlockSize(DATA_BLOCK_SIZE).build(); | ||
return writeStoreFile(fname, meta); | ||
} | ||
|
||
private Path writeStoreFile(String fname, HFileContext context) throws IOException { | ||
Path storeFileParentDir = new Path(TEST_UTIL.getDataTestDir(), fname); | ||
StoreFileWriter sfw = new StoreFileWriter.Builder(conf, cacheConf, fs) | ||
.withOutputDir(storeFileParentDir).withFileContext(meta).build(); | ||
.withOutputDir(storeFileParentDir).withFileContext(context).build(); | ||
Random rand = ThreadLocalRandom.current(); | ||
final int rowLen = 32; | ||
for (int i = 0; i < NUM_KV; ++i) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] this new flag
cacheOnly=true
should just skip reading blocks from the local cache, and it does not have any caller other in the unit tests, are you planning to introduce a new behavior in the future ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We call this on line #60 of HFilePreadReader. That line is executed once we set CACHE_DATA_BLOCKS_COMPRESSED_KEY to true, which is how we are testing it further down on the UT.