Index: src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java (revision 1310151) +++ src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java (working copy) @@ -571,14 +571,20 @@ return this.stats.getEvictedCount(); } + EvictionThread getEvictionThread() { + return this.evictionThread; + } + /* * Eviction thread. Sits in waiting state until an eviction is triggered * when the cache size grows above the acceptable level.

* * Thread is triggered into action by {@link LruBlockCache#runEviction()} */ - private static class EvictionThread extends HasThread { + static class EvictionThread extends HasThread { private WeakReference cache; + // flag set after enter the run method, used for test + private boolean enteringRun = false; public EvictionThread(LruBlockCache cache) { super("LruBlockCache.EvictionThread"); @@ -588,6 +594,7 @@ @Override public void run() { + enteringRun = true; while(true) { synchronized(this) { try { @@ -604,6 +611,13 @@ this.notify(); // FindBugs NN_NAKED_NOTIFY } } + + /** + * Used for the test. + */ + public boolean isEnteringRun() { + return this.enteringRun; + } } /* Index: src/test/java/org/apache/hadoop/hbase/io/hfile/TestLruBlockCache.java =================================================================== --- src/test/java/org/apache/hadoop/hbase/io/hfile/TestLruBlockCache.java (revision 1310151) +++ src/test/java/org/apache/hadoop/hbase/io/hfile/TestLruBlockCache.java (working copy) @@ -19,6 +19,9 @@ */ package org.apache.hadoop.hbase.io.hfile; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import java.nio.ByteBuffer; import java.util.Collection; import java.util.Map; @@ -26,19 +29,18 @@ import org.apache.hadoop.hbase.MediumTests; import org.apache.hadoop.hbase.io.HeapSize; +import org.apache.hadoop.hbase.io.hfile.LruBlockCache.EvictionThread; import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics; import org.apache.hadoop.hbase.regionserver.metrics.TestSchemaMetrics; import org.apache.hadoop.hbase.util.ClassSize; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; -import org.junit.experimental.categories.Category; -import static org.junit.Assert.*; - /** * Tests the concurrent LruBlockCache.

* @@ -73,7 +75,6 @@ @Test public void testBackgroundEvictionThread() throws Exception { - long maxSize = 100000; long blockSize = calculateBlockSizeDefault(maxSize, 9); // room for 9, will evict @@ -81,6 +82,15 @@ CachedItem [] blocks = generateFixedBlocks(10, blockSize, "block"); + EvictionThread evictionThread = cache.getEvictionThread(); + assertTrue(evictionThread != null); + + // Make sure eviction thread has entered run method + while (!evictionThread.isEnteringRun()) { + Thread.sleep(1); + } + + // Add all the blocks for (CachedItem block : blocks) { cache.cacheBlock(block.cacheKey, block);