HBASE-5672 TestLruBlockCache#testBackgroundEvictionThread fails occasionally

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1330971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2012-04-26 17:03:51 +00:00
parent c8b61833f7
commit 5f01fcd4fe
2 changed files with 29 additions and 5 deletions

View File

@ -572,15 +572,21 @@ public class LruBlockCache implements BlockCache, HeapSize {
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.<p>
*
* Thread is triggered into action by {@link LruBlockCache#runEviction()}
*/
private static class EvictionThread extends HasThread {
static class EvictionThread extends HasThread {
private WeakReference<LruBlockCache> 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 @@ public class LruBlockCache implements BlockCache, HeapSize {
@Override
public void run() {
enteringRun = true;
while (this.go) {
synchronized(this) {
try {
@ -612,6 +619,13 @@ public class LruBlockCache implements BlockCache, HeapSize {
this.go = false;
interrupt();
}
/**
* Used for the test.
*/
boolean isEnteringRun() {
return this.enteringRun;
}
}
/*

View File

@ -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 java.util.Random;
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.<p>
*
@ -73,7 +75,6 @@ public class TestLruBlockCache {
@Test
public void testBackgroundEvictionThread() throws Exception {
long maxSize = 100000;
long blockSize = calculateBlockSizeDefault(maxSize, 9); // room for 9, will evict
@ -81,6 +82,15 @@ public class TestLruBlockCache {
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);