Skip to content

Commit c3d0cc7

Browse files
authored
HBASE-26830 Rewrite TestLruBlockCache to make it more stable (#4212)
Signed-off-by: Xiaolin Ha <[email protected]>
1 parent fd301ad commit c3d0cc7

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestLruBlockCache.java

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class TestLruBlockCache {
6666

6767
private static final Logger LOG = LoggerFactory.getLogger(TestLruBlockCache.class);
6868

69+
private static final Configuration CONF = HBaseConfiguration.create();
70+
6971
@Test
7072
public void testCacheEvictionThreadSafe() throws Exception {
7173
long maxSize = 100000;
@@ -74,13 +76,10 @@ public void testCacheEvictionThreadSafe() throws Exception {
7476
final long blockSize = calculateBlockSizeDefault(maxSize, numBlocks);
7577
assertTrue("calculateBlockSize appears broken.", blockSize * numBlocks <= maxSize);
7678

77-
final Configuration conf = HBaseConfiguration.create();
7879
final LruBlockCache cache = new LruBlockCache(maxSize, blockSize);
7980
EvictionThread evictionThread = cache.getEvictionThread();
8081
assertTrue(evictionThread != null);
81-
while (!evictionThread.isEnteringRun()) {
82-
Thread.sleep(1);
83-
}
82+
Waiter.waitFor(CONF, 10000, 100, () -> evictionThread.isEnteringRun());
8483
final String hfileName = "hfile";
8584
int threads = 10;
8685
final int blocksPerThread = 5 * numBlocks;
@@ -103,7 +102,7 @@ public void run() {
103102
service.shutdown();
104103
// The test may fail here if the evict thread frees the blocks too fast
105104
service.awaitTermination(10, TimeUnit.MINUTES);
106-
Waiter.waitFor(conf, 10000, 100, new ExplainingPredicate<Exception>() {
105+
Waiter.waitFor(CONF, 10000, 100, new ExplainingPredicate<Exception>() {
107106
@Override
108107
public boolean evaluate() throws Exception {
109108
return cache.getBlockCount() == 0;
@@ -132,21 +131,26 @@ public void testBackgroundEvictionThread() throws Exception {
132131
CachedItem[] blocks = generateFixedBlocks(numBlocks + 1, blockSize, "block");
133132

134133
// Make sure eviction thread has entered run method
135-
while (!evictionThread.isEnteringRun()) {
136-
Thread.sleep(10);
137-
}
134+
Waiter.waitFor(CONF, 10000, 10, () -> evictionThread.isEnteringRun());
138135

139136
// Add all the blocks
140137
for (CachedItem block : blocks) {
141138
cache.cacheBlock(block.cacheKey, block);
142139
}
143140

144141
// wait until at least one eviction has run
145-
int n = 0;
146-
while(cache.getStats().getEvictionCount() == 0) {
147-
Thread.sleep(200);
148-
assertTrue("Eviction never happened.", n++ < 20);
149-
}
142+
Waiter.waitFor(CONF, 30000, 200, new ExplainingPredicate<Exception>() {
143+
144+
@Override
145+
public boolean evaluate() throws Exception {
146+
return cache.getStats().getEvictionCount() > 0;
147+
}
148+
149+
@Override
150+
public String explainFailure() throws Exception {
151+
return "Eviction never happened.";
152+
}
153+
});
150154

151155
// let cache stabilize
152156
// On some systems, the cache will run multiple evictions before it attains
@@ -155,22 +159,20 @@ public void testBackgroundEvictionThread() throws Exception {
155159
// evicts another. I think this is due to the delta between minSize and
156160
// acceptableSize, combined with variance between object overhead on
157161
// different environments.
158-
n = 0;
159-
for (long prevCnt = 0 /* < number of blocks added */,
160-
curCnt = cache.getBlockCount();
161-
prevCnt != curCnt; prevCnt = curCnt, curCnt = cache.getBlockCount()) {
162+
int n = 0;
163+
for (long prevCnt = 0 /* < number of blocks added */, curCnt =
164+
cache.getBlockCount(); prevCnt != curCnt; prevCnt = curCnt, curCnt = cache.getBlockCount()) {
162165
Thread.sleep(200);
163-
assertTrue("Cache never stabilized.", n++ < 20);
166+
assertTrue("Cache never stabilized.", n++ < 100);
164167
}
165168

166169
long evictionCount = cache.getStats().getEvictionCount();
167170
assertTrue(evictionCount >= 1);
168-
System.out.println("Background Evictions run: " + evictionCount);
171+
LOG.info("Background Evictions run: {}", evictionCount);
169172
}
170173

171174
@Test
172175
public void testCacheSimple() throws Exception {
173-
174176
long maxSize = 1000000;
175177
long blockSize = calculateBlockSizeDefault(maxSize, 101);
176178

@@ -229,7 +231,6 @@ public void testCacheSimple() throws Exception {
229231

230232
@Test
231233
public void testCacheEvictionSimple() throws Exception {
232-
233234
long maxSize = 100000;
234235
long blockSize = calculateBlockSizeDefault(maxSize, 10);
235236

@@ -269,14 +270,13 @@ public void testCacheEvictionSimple() throws Exception {
269270

270271
@Test
271272
public void testCacheEvictionTwoPriorities() throws Exception {
272-
273273
long maxSize = 100000;
274274
long blockSize = calculateBlockSizeDefault(maxSize, 10);
275275

276276
LruBlockCache cache = new LruBlockCache(maxSize,blockSize,false);
277277

278-
CachedItem [] singleBlocks = generateFixedBlocks(5, 10000, "single");
279-
CachedItem [] multiBlocks = generateFixedBlocks(5, 10000, "multi");
278+
CachedItem[] singleBlocks = generateFixedBlocks(5, 10000, "single");
279+
CachedItem[] multiBlocks = generateFixedBlocks(5, 10000, "multi");
280280

281281
long expectedCacheSize = cache.heapSize();
282282

@@ -328,7 +328,6 @@ public void testCacheEvictionTwoPriorities() throws Exception {
328328

329329
@Test
330330
public void testCacheEvictionThreePriorities() throws Exception {
331-
332331
long maxSize = 100000;
333332
long blockSize = calculateBlockSize(maxSize, 10);
334333

@@ -345,9 +344,9 @@ public void testCacheEvictionThreePriorities() throws Exception {
345344
false,
346345
16 * 1024 * 1024);
347346

348-
CachedItem [] singleBlocks = generateFixedBlocks(5, blockSize, "single");
349-
CachedItem [] multiBlocks = generateFixedBlocks(5, blockSize, "multi");
350-
CachedItem [] memoryBlocks = generateFixedBlocks(5, blockSize, "memory");
347+
CachedItem[] singleBlocks = generateFixedBlocks(5, blockSize, "single");
348+
CachedItem[] multiBlocks = generateFixedBlocks(5, blockSize, "multi");
349+
CachedItem[] memoryBlocks = generateFixedBlocks(5, blockSize, "memory");
351350

352351
long expectedCacheSize = cache.heapSize();
353352

@@ -574,8 +573,8 @@ public void testScanResistance() throws Exception {
574573
false,
575574
16 * 1024 * 1024);
576575

577-
CachedItem [] singleBlocks = generateFixedBlocks(20, blockSize, "single");
578-
CachedItem [] multiBlocks = generateFixedBlocks(5, blockSize, "multi");
576+
CachedItem[] singleBlocks = generateFixedBlocks(20, blockSize, "single");
577+
CachedItem[] multiBlocks = generateFixedBlocks(5, blockSize, "multi");
579578

580579
// Add 5 multi blocks
581580
for (CachedItem block : multiBlocks) {
@@ -584,7 +583,7 @@ public void testScanResistance() throws Exception {
584583
}
585584

586585
// Add 5 single blocks
587-
for(int i=0;i<5;i++) {
586+
for (int i = 0; i < 5; i++) {
588587
cache.cacheBlock(singleBlocks[i].cacheKey, singleBlocks[i]);
589588
}
590589

@@ -607,7 +606,7 @@ public void testScanResistance() throws Exception {
607606
// blocks evicted. Inserting 13 blocks should yield 3 more evictions and
608607
// 12 more evicted.
609608

610-
for(int i=5;i<18;i++) {
609+
for (int i = 5; i < 18; i++) {
611610
cache.cacheBlock(singleBlocks[i].cacheKey, singleBlocks[i]);
612611
}
613612

@@ -637,8 +636,8 @@ public void testMaxBlockSize() throws Exception {
637636
1.2f, // limit
638637
false,
639638
1024);
640-
CachedItem [] tooLong = generateFixedBlocks(10, 1024+5, "long");
641-
CachedItem [] small = generateFixedBlocks(15, 600, "small");
639+
CachedItem[] tooLong = generateFixedBlocks(10, 1024+5, "long");
640+
CachedItem[] small = generateFixedBlocks(15, 600, "small");
642641

643642

644643
for (CachedItem i:tooLong) {
@@ -661,7 +660,6 @@ public void testMaxBlockSize() throws Exception {
661660
// test setMaxSize
662661
@Test
663662
public void testResizeBlockCache() throws Exception {
664-
665663
long maxSize = 300000;
666664
long blockSize = calculateBlockSize(maxSize, 31);
667665

@@ -678,13 +676,12 @@ public void testResizeBlockCache() throws Exception {
678676
false,
679677
16 * 1024 * 1024);
680678

681-
CachedItem [] singleBlocks = generateFixedBlocks(10, blockSize, "single");
682-
CachedItem [] multiBlocks = generateFixedBlocks(10, blockSize, "multi");
683-
CachedItem [] memoryBlocks = generateFixedBlocks(10, blockSize, "memory");
679+
CachedItem[] singleBlocks = generateFixedBlocks(10, blockSize, "single");
680+
CachedItem[] multiBlocks = generateFixedBlocks(10, blockSize, "multi");
681+
CachedItem[] memoryBlocks = generateFixedBlocks(10, blockSize, "memory");
684682

685683
// Add all blocks from all priorities
686-
for(int i=0;i<10;i++) {
687-
684+
for (int i = 0; i < 10; i++) {
688685
// Just add single blocks
689686
cache.cacheBlock(singleBlocks[i].cacheKey, singleBlocks[i]);
690687

@@ -726,7 +723,7 @@ public void testResizeBlockCache() throws Exception {
726723
// test metricsPastNPeriods
727724
@Test
728725
public void testPastNPeriodsMetrics() throws Exception {
729-
double delta = 0.01;
726+
double delta = 0.01;
730727

731728
// 3 total periods
732729
CacheStats stats = new CacheStats("test", 3);
@@ -874,11 +871,11 @@ public void testCacheBlockNextBlockMetadataMissing() {
874871
return blocks;
875872
}
876873

877-
private CachedItem [] generateFixedBlocks(int numBlocks, long size, String pfx) {
874+
private CachedItem[] generateFixedBlocks(int numBlocks, long size, String pfx) {
878875
return generateFixedBlocks(numBlocks, (int)size, pfx);
879876
}
880877

881-
private CachedItem [] generateRandomBlocks(int numBlocks, long maxSize) {
878+
private CachedItem[] generateRandomBlocks(int numBlocks, long maxSize) {
882879
CachedItem [] blocks = new CachedItem[numBlocks];
883880
Random rand = ThreadLocalRandom.current();
884881
for(int i=0;i<numBlocks;i++) {

0 commit comments

Comments
 (0)