@@ -66,6 +66,8 @@ public class TestLruAdaptiveBlockCache {
6666
6767 private static final Logger LOG = LoggerFactory .getLogger (TestLruAdaptiveBlockCache .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 LruAdaptiveBlockCache cache = new LruAdaptiveBlockCache (maxSize , blockSize );
7980 EvictionThread evictionThread = cache .getEvictionThread ();
8081 assertNotNull (evictionThread );
81- while (!evictionThread .isEnteringRun ()) {
82- Thread .sleep (1000 );
83- }
82+ Waiter .waitFor (CONF , 10000 , 100 , () -> evictionThread .isEnteringRun ());
8483 final String hfileName = "hfile" ;
8584 int threads = 10 ;
8685 final int blocksPerThread = 5 * numBlocks ;
@@ -102,7 +101,7 @@ public void testCacheEvictionThreadSafe() throws Exception {
102101 service .shutdown ();
103102 // The test may fail here if the evict thread frees the blocks too fast
104103 service .awaitTermination (10 , TimeUnit .MINUTES );
105- Waiter .waitFor (conf , 10000 , 100 , new ExplainingPredicate <Exception >() {
104+ Waiter .waitFor (CONF , 10000 , 100 , new ExplainingPredicate <Exception >() {
106105 @ Override
107106 public boolean evaluate () throws Exception {
108107 return cache .getBlockCount () == 0 ;
@@ -133,21 +132,26 @@ public void testBackgroundEvictionThread() throws Exception {
133132 CachedItem [] blocks = generateFixedBlocks (numBlocks + 1 , blockSize , "block" );
134133
135134 // Make sure eviction thread has entered run method
136- while (!evictionThread .isEnteringRun ()) {
137- Thread .sleep (1 );
138- }
135+ Waiter .waitFor (CONF , 10000 , 10 , () -> evictionThread .isEnteringRun ());
139136
140137 // Add all the blocks
141138 for (CachedItem block : blocks ) {
142139 cache .cacheBlock (block .cacheKey , block );
143140 }
144141
145142 // wait until at least one eviction has run
146- int n = 0 ;
147- while (cache .getStats ().getEvictionCount () == 0 ) {
148- Thread .sleep (200 );
149- assertTrue ("Eviction never happened." , n ++ < 20 );
150- }
143+ Waiter .waitFor (CONF , 30000 , 200 , new ExplainingPredicate <Exception >() {
144+
145+ @ Override
146+ public boolean evaluate () throws Exception {
147+ return cache .getStats ().getEvictionCount () > 0 ;
148+ }
149+
150+ @ Override
151+ public String explainFailure () throws Exception {
152+ return "Eviction never happened." ;
153+ }
154+ });
151155
152156 // let cache stabilize
153157 // On some systems, the cache will run multiple evictions before it attains
@@ -156,22 +160,20 @@ public void testBackgroundEvictionThread() throws Exception {
156160 // evicts another. I think this is due to the delta between minSize and
157161 // acceptableSize, combined with variance between object overhead on
158162 // different environments.
159- n = 0 ;
160- for (long prevCnt = 0 /* < number of blocks added */ ,
161- curCnt = cache .getBlockCount ();
162- prevCnt != curCnt ; prevCnt = curCnt , curCnt = cache .getBlockCount ()) {
163+ int n = 0 ;
164+ for (long prevCnt = 0 /* < number of blocks added */ , curCnt =
165+ cache .getBlockCount (); prevCnt != curCnt ; prevCnt = curCnt , curCnt = cache .getBlockCount ()) {
163166 Thread .sleep (200 );
164- assertTrue ("Cache never stabilized." , n ++ < 20 );
167+ assertTrue ("Cache never stabilized." , n ++ < 100 );
165168 }
166169
167170 long evictionCount = cache .getStats ().getEvictionCount ();
168171 assertTrue (evictionCount >= 1 );
169- System . out . println ("Background Evictions run: " + evictionCount );
172+ LOG . info ("Background Evictions run: {}" , evictionCount );
170173 }
171174
172175 @ Test
173176 public void testCacheSimple () throws Exception {
174-
175177 long maxSize = 1000000 ;
176178 long blockSize = calculateBlockSizeDefault (maxSize , 101 );
177179
@@ -233,7 +235,6 @@ public void testCacheSimple() throws Exception {
233235
234236 @ Test
235237 public void testCacheEvictionSimple () throws Exception {
236-
237238 long maxSize = 100000 ;
238239 long blockSize = calculateBlockSizeDefault (maxSize , 10 );
239240
@@ -275,14 +276,13 @@ public void testCacheEvictionSimple() throws Exception {
275276
276277 @ Test
277278 public void testCacheEvictionTwoPriorities () throws Exception {
278-
279279 long maxSize = 100000 ;
280280 long blockSize = calculateBlockSizeDefault (maxSize , 10 );
281281
282282 LruAdaptiveBlockCache cache = new LruAdaptiveBlockCache (maxSize ,blockSize ,false );
283283
284- CachedItem [] singleBlocks = generateFixedBlocks (5 , 10000 , "single" );
285- CachedItem [] multiBlocks = generateFixedBlocks (5 , 10000 , "multi" );
284+ CachedItem [] singleBlocks = generateFixedBlocks (5 , 10000 , "single" );
285+ CachedItem [] multiBlocks = generateFixedBlocks (5 , 10000 , "multi" );
286286
287287 long expectedCacheSize = cache .heapSize ();
288288
@@ -339,7 +339,6 @@ public void testCacheEvictionTwoPriorities() throws Exception {
339339
340340 @ Test
341341 public void testCacheEvictionThreePriorities () throws Exception {
342-
343342 long maxSize = 100000 ;
344343 long blockSize = calculateBlockSize (maxSize , 10 );
345344
@@ -359,9 +358,9 @@ public void testCacheEvictionThreePriorities() throws Exception {
359358 500 ,
360359 0.01f );
361360
362- CachedItem [] singleBlocks = generateFixedBlocks (5 , blockSize , "single" );
363- CachedItem [] multiBlocks = generateFixedBlocks (5 , blockSize , "multi" );
364- CachedItem [] memoryBlocks = generateFixedBlocks (5 , blockSize , "memory" );
361+ CachedItem [] singleBlocks = generateFixedBlocks (5 , blockSize , "single" );
362+ CachedItem [] multiBlocks = generateFixedBlocks (5 , blockSize , "multi" );
363+ CachedItem [] memoryBlocks = generateFixedBlocks (5 , blockSize , "memory" );
365364
366365 long expectedCacheSize = cache .heapSize ();
367366
@@ -617,8 +616,8 @@ public void testScanResistance() throws Exception {
617616 500 ,
618617 0.01f );
619618
620- CachedItem [] singleBlocks = generateFixedBlocks (20 , blockSize , "single" );
621- CachedItem [] multiBlocks = generateFixedBlocks (5 , blockSize , "multi" );
619+ CachedItem [] singleBlocks = generateFixedBlocks (20 , blockSize , "single" );
620+ CachedItem [] multiBlocks = generateFixedBlocks (5 , blockSize , "multi" );
622621
623622 // Add 5 multi blocks
624623 for (CachedItem block : multiBlocks ) {
@@ -627,7 +626,7 @@ public void testScanResistance() throws Exception {
627626 }
628627
629628 // Add 5 single blocks
630- for (int i = 0 ; i < 5 ; i ++) {
629+ for (int i = 0 ; i < 5 ; i ++) {
631630 cache .cacheBlock (singleBlocks [i ].cacheKey , singleBlocks [i ]);
632631 }
633632
@@ -654,7 +653,7 @@ public void testScanResistance() throws Exception {
654653 // blocks evicted. Inserting 13 blocks should yield 3 more evictions and
655654 // 12 more evicted.
656655
657- for (int i = 5 ; i < 18 ;i ++) {
656+ for (int i = 5 ; i < 18 ; i ++) {
658657 cache .cacheBlock (singleBlocks [i ].cacheKey , singleBlocks [i ]);
659658 }
660659
@@ -688,9 +687,8 @@ public void testMaxBlockSize() throws Exception {
688687 500 ,
689688 0.01f );
690689
691- CachedItem [] tooLong = generateFixedBlocks (10 , 1024 +5 , "long" );
692- CachedItem [] small = generateFixedBlocks (15 , 600 , "small" );
693-
690+ CachedItem [] tooLong = generateFixedBlocks (10 , 1024 +5 , "long" );
691+ CachedItem [] small = generateFixedBlocks (15 , 600 , "small" );
694692
695693 for (CachedItem i :tooLong ) {
696694 cache .cacheBlock (i .cacheKey , i );
@@ -712,7 +710,6 @@ public void testMaxBlockSize() throws Exception {
712710 // test setMaxSize
713711 @ Test
714712 public void testResizeBlockCache () throws Exception {
715-
716713 long maxSize = 300000 ;
717714 long blockSize = calculateBlockSize (maxSize , 31 );
718715
@@ -732,13 +729,12 @@ public void testResizeBlockCache() throws Exception {
732729 500 ,
733730 0.01f );
734731
735- CachedItem [] singleBlocks = generateFixedBlocks (10 , blockSize , "single" );
736- CachedItem [] multiBlocks = generateFixedBlocks (10 , blockSize , "multi" );
737- CachedItem [] memoryBlocks = generateFixedBlocks (10 , blockSize , "memory" );
732+ CachedItem [] singleBlocks = generateFixedBlocks (10 , blockSize , "single" );
733+ CachedItem [] multiBlocks = generateFixedBlocks (10 , blockSize , "multi" );
734+ CachedItem [] memoryBlocks = generateFixedBlocks (10 , blockSize , "memory" );
738735
739736 // Add all blocks from all priorities
740- for (int i =0 ;i <10 ;i ++) {
741-
737+ for (int i = 0 ; i < 10 ; i ++) {
742738 // Just add single blocks
743739 cache .cacheBlock (singleBlocks [i ].cacheKey , singleBlocks [i ]);
744740
@@ -939,11 +935,11 @@ public void testCacheBlockNextBlockMetadataMissing() {
939935 return blocks ;
940936 }
941937
942- private CachedItem [] generateFixedBlocks (int numBlocks , long size , String pfx ) {
938+ private CachedItem [] generateFixedBlocks (int numBlocks , long size , String pfx ) {
943939 return generateFixedBlocks (numBlocks , (int )size , pfx );
944940 }
945941
946- private CachedItem [] generateRandomBlocks (int numBlocks , long maxSize ) {
942+ private CachedItem [] generateRandomBlocks (int numBlocks , long maxSize ) {
947943 CachedItem [] blocks = new CachedItem [numBlocks ];
948944 Random rand = ThreadLocalRandom .current ();
949945 for (int i =0 ;i <numBlocks ;i ++) {
@@ -1126,9 +1122,7 @@ public void testSkipCacheDataBlocksInteral(int heavyEvictionCountLimit) throws E
11261122
11271123 EvictionThread evictionThread = cache .getEvictionThread ();
11281124 assertNotNull (evictionThread );
1129- while (!evictionThread .isEnteringRun ()) {
1130- Thread .sleep (1 );
1131- }
1125+ Waiter .waitFor (CONF , 10000 , 10 , () -> evictionThread .isEnteringRun ());
11321126
11331127 final String hfileName = "hfile" ;
11341128 for (int blockIndex = 0 ; blockIndex <= numBlocks * 3000 ; ++blockIndex ) {
0 commit comments