Index: src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java (revision 1330082) +++ src/main/java/org/apache/hadoop/hbase/io/hfile/LruBlockCache.java (working copy) @@ -572,15 +572,21 @@ 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; private boolean go = true; + // flag set after enter the run method, used for test + private boolean enteringRun = false; public EvictionThread(LruBlockCache cache) { super(Thread.currentThread().getName() + ".LruBlockCache.EvictionThread"); @@ -590,6 +596,7 @@ @Override public void run() { + enteringRun = true; while (this.go) { synchronized(this) { try { @@ -612,6 +619,13 @@ this.go = false; interrupt(); } + + /** + * Used for the test. + */ + 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 1330082) +++ 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);