Skip to content

HBASE-29035: Amount of region cached in the region metrics not updated for a region immediately after it is flushed with cacheOnWrite turned on #6549

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

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,23 @@ private void writeInlineBlocks(boolean closing) throws IOException {
private void doCacheOnWrite(long offset) {
cacheConf.getBlockCache().ifPresent(cache -> {
HFileBlock cacheFormatBlock = blockWriter.getBlockForCaching(cacheConf);
BlockCacheKey key = buildCacheBlockKey(offset, cacheFormatBlock.getBlockType());
try {
cache.cacheBlock(new BlockCacheKey(name, offset, true, cacheFormatBlock.getBlockType()),
cacheFormatBlock, cacheConf.isInMemory(), true);
cache.cacheBlock(key, cacheFormatBlock, cacheConf.isInMemory(), true);
} finally {
// refCnt will auto increase when block add to Cache, see RAMCache#putIfAbsent
cacheFormatBlock.release();
}
});
}

private BlockCacheKey buildCacheBlockKey(long offset, BlockType blockType) {
if (path != null) {
return new BlockCacheKey(path, offset, true, blockType);
}
return new BlockCacheKey(name, offset, true, blockType);
}

/**
* Ready a new block for writing.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.regionserver.HStoreFile;
import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.hadoop.hbase.util.Bytes;
Expand Down Expand Up @@ -67,6 +68,7 @@ public static void setUp() throws Exception {
UTIL.getConfiguration().setBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, true);
UTIL.getConfiguration().set(BUCKET_CACHE_IOENGINE_KEY, "offheap");
UTIL.getConfiguration().setInt(BUCKET_CACHE_SIZE_KEY, 200);
UTIL.getConfiguration().set(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
}

@Test
Expand Down Expand Up @@ -103,7 +105,7 @@ private void doTestEvictOnSplit(String table, boolean evictOnSplit,
UTIL.startMiniCluster(1);
try {
TableName tableName = TableName.valueOf(table);
createAndCacheTable(tableName);
createTable(tableName, true);
Collection<HStoreFile> files =
UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles();
checkCacheForBlocks(tableName, files, predicateBeforeSplit);
Expand All @@ -125,7 +127,7 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
UTIL.startMiniCluster(1);
try {
TableName tableName = TableName.valueOf(table);
createAndCacheTable(tableName);
createTable(tableName, true);
Collection<HStoreFile> files =
UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles();
checkCacheForBlocks(tableName, files, predicateBeforeClose);
Expand All @@ -139,7 +141,8 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
}
}

private void createAndCacheTable(TableName tableName) throws IOException, InterruptedException {
private void createTable(TableName tableName, boolean shouldFlushTable)
throws IOException, InterruptedException {
byte[] family = Bytes.toBytes("CF");
TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
Expand All @@ -153,7 +156,10 @@ private void createAndCacheTable(TableName tableName) throws IOException, Interr
puts.add(p);
}
tbl.put(puts);
UTIL.getAdmin().flush(tableName);
if (shouldFlushTable) {
UTIL.getAdmin().flush(tableName);
Thread.sleep(5000);
}
}

private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> files,
Expand All @@ -167,4 +173,44 @@ private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> fil
});
});
}

@Test
public void testNoCacheWithoutFlush() throws Exception {
UTIL.startMiniCluster(1);
try {
TableName tableName = TableName.valueOf("tableNoCache");
createTable(tableName, false);
checkRegionCached(tableName, false);
} finally {
UTIL.shutdownMiniCluster();
}
}

@Test
public void testCacheWithFlush() throws Exception {
UTIL.startMiniCluster(1);
try {
TableName tableName = TableName.valueOf("tableWithFlush");
createTable(tableName, true);
checkRegionCached(tableName, true);
} finally {
UTIL.shutdownMiniCluster();
}
}

private void checkRegionCached(TableName tableName, boolean isCached) throws IOException {
UTIL.getMiniHBaseCluster().getRegions(tableName).forEach(r -> {
try {
UTIL.getMiniHBaseCluster().getClusterMetrics().getLiveServerMetrics().forEach((sn, sm) -> {
for (Map.Entry<byte[], RegionMetrics> rm : sm.getRegionMetrics().entrySet()) {
if (rm.getValue().getNameAsString().equals(r.getRegionInfo().getRegionNameAsString())) {
assertTrue(isCached == (rm.getValue().getCurrentRegionCachedRatio() > 0.0f));
}
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
}