Skip to content

Commit 689010e

Browse files
author
Prathyusha Garre
committed
Merge remote-tracking branch 'upstream/master' into HBASE-28969
2 parents a988178 + ac22a22 commit 689010e

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFileWriterImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,16 +556,23 @@ private void writeInlineBlocks(boolean closing) throws IOException {
556556
private void doCacheOnWrite(long offset) {
557557
cacheConf.getBlockCache().ifPresent(cache -> {
558558
HFileBlock cacheFormatBlock = blockWriter.getBlockForCaching(cacheConf);
559+
BlockCacheKey key = buildCacheBlockKey(offset, cacheFormatBlock.getBlockType());
559560
try {
560-
cache.cacheBlock(new BlockCacheKey(name, offset, true, cacheFormatBlock.getBlockType()),
561-
cacheFormatBlock, cacheConf.isInMemory(), true);
561+
cache.cacheBlock(key, cacheFormatBlock, cacheConf.isInMemory(), true);
562562
} finally {
563563
// refCnt will auto increase when block add to Cache, see RAMCache#putIfAbsent
564564
cacheFormatBlock.release();
565565
}
566566
});
567567
}
568568

569+
private BlockCacheKey buildCacheBlockKey(long offset, BlockType blockType) {
570+
if (path != null) {
571+
return new BlockCacheKey(path, offset, true, blockType);
572+
}
573+
return new BlockCacheKey(name, offset, true, blockType);
574+
}
575+
569576
/**
570577
* Ready a new block for writing.
571578
*/

hbase-server/src/test/java/org/apache/hadoop/hbase/TestCacheEviction.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.apache.hadoop.hbase.client.TableDescriptor;
3838
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
3939
import org.apache.hadoop.hbase.regionserver.HStoreFile;
40+
import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
4041
import org.apache.hadoop.hbase.testclassification.MediumTests;
4142
import org.apache.hadoop.hbase.testclassification.MiscTests;
4243
import org.apache.hadoop.hbase.util.Bytes;
@@ -67,6 +68,7 @@ public static void setUp() throws Exception {
6768
UTIL.getConfiguration().setBoolean(PREFETCH_BLOCKS_ON_OPEN_KEY, true);
6869
UTIL.getConfiguration().set(BUCKET_CACHE_IOENGINE_KEY, "offheap");
6970
UTIL.getConfiguration().setInt(BUCKET_CACHE_SIZE_KEY, 200);
71+
UTIL.getConfiguration().set(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
7072
}
7173

7274
@Test
@@ -103,7 +105,7 @@ private void doTestEvictOnSplit(String table, boolean evictOnSplit,
103105
UTIL.startMiniCluster(1);
104106
try {
105107
TableName tableName = TableName.valueOf(table);
106-
createAndCacheTable(tableName);
108+
createTable(tableName, true);
107109
Collection<HStoreFile> files =
108110
UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles();
109111
checkCacheForBlocks(tableName, files, predicateBeforeSplit);
@@ -125,7 +127,7 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
125127
UTIL.startMiniCluster(1);
126128
try {
127129
TableName tableName = TableName.valueOf(table);
128-
createAndCacheTable(tableName);
130+
createTable(tableName, true);
129131
Collection<HStoreFile> files =
130132
UTIL.getMiniHBaseCluster().getRegions(tableName).get(0).getStores().get(0).getStorefiles();
131133
checkCacheForBlocks(tableName, files, predicateBeforeClose);
@@ -139,7 +141,8 @@ private void doTestEvictOnClose(String table, boolean evictOnClose,
139141
}
140142
}
141143

142-
private void createAndCacheTable(TableName tableName) throws IOException, InterruptedException {
144+
private void createTable(TableName tableName, boolean shouldFlushTable)
145+
throws IOException, InterruptedException {
143146
byte[] family = Bytes.toBytes("CF");
144147
TableDescriptor td = TableDescriptorBuilder.newBuilder(tableName)
145148
.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
@@ -153,7 +156,10 @@ private void createAndCacheTable(TableName tableName) throws IOException, Interr
153156
puts.add(p);
154157
}
155158
tbl.put(puts);
156-
UTIL.getAdmin().flush(tableName);
159+
if (shouldFlushTable) {
160+
UTIL.getAdmin().flush(tableName);
161+
Thread.sleep(5000);
162+
}
157163
}
158164

159165
private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> files,
@@ -167,4 +173,44 @@ private void checkCacheForBlocks(TableName tableName, Collection<HStoreFile> fil
167173
});
168174
});
169175
}
176+
177+
@Test
178+
public void testNoCacheWithoutFlush() throws Exception {
179+
UTIL.startMiniCluster(1);
180+
try {
181+
TableName tableName = TableName.valueOf("tableNoCache");
182+
createTable(tableName, false);
183+
checkRegionCached(tableName, false);
184+
} finally {
185+
UTIL.shutdownMiniCluster();
186+
}
187+
}
188+
189+
@Test
190+
public void testCacheWithFlush() throws Exception {
191+
UTIL.startMiniCluster(1);
192+
try {
193+
TableName tableName = TableName.valueOf("tableWithFlush");
194+
createTable(tableName, true);
195+
checkRegionCached(tableName, true);
196+
} finally {
197+
UTIL.shutdownMiniCluster();
198+
}
199+
}
200+
201+
private void checkRegionCached(TableName tableName, boolean isCached) throws IOException {
202+
UTIL.getMiniHBaseCluster().getRegions(tableName).forEach(r -> {
203+
try {
204+
UTIL.getMiniHBaseCluster().getClusterMetrics().getLiveServerMetrics().forEach((sn, sm) -> {
205+
for (Map.Entry<byte[], RegionMetrics> rm : sm.getRegionMetrics().entrySet()) {
206+
if (rm.getValue().getNameAsString().equals(r.getRegionInfo().getRegionNameAsString())) {
207+
assertTrue(isCached == (rm.getValue().getCurrentRegionCachedRatio() > 0.0f));
208+
}
209+
}
210+
});
211+
} catch (IOException e) {
212+
throw new RuntimeException(e);
213+
}
214+
});
215+
}
170216
}

0 commit comments

Comments
 (0)