HBASE-8564 TestMetricsRegionServer depends on test order; ADDENDUM

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1484052 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2013-05-18 03:23:13 +00:00
parent 6b46196c31
commit 998296008a
6 changed files with 45 additions and 23 deletions

View File

@ -23,6 +23,12 @@ import org.apache.hadoop.hbase.metrics.BaseSource;
/** Interface of a class to make assertions about metrics values. */ /** Interface of a class to make assertions about metrics values. */
public interface MetricsAssertHelper { public interface MetricsAssertHelper {
/**
* Init helper. This method will make sure that the metrics system is set
* up for tests.
*/
public void init();
/** /**
* Assert that a tag exists and has a given value. * Assert that a tag exists and has a given value.
* *

View File

@ -25,6 +25,7 @@ import org.apache.hadoop.metrics2.MetricsBuilder;
import org.apache.hadoop.metrics2.MetricsRecordBuilder; import org.apache.hadoop.metrics2.MetricsRecordBuilder;
import org.apache.hadoop.metrics2.MetricsSource; import org.apache.hadoop.metrics2.MetricsSource;
import org.apache.hadoop.metrics2.MetricsTag; import org.apache.hadoop.metrics2.MetricsTag;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -110,6 +111,11 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper {
} }
} }
@Override
public void init() {
// In hadoop 1 there's no minicluster mode so there's nothing to do here.
}
@Override @Override
public void assertTag(String name, String expected, BaseSource source) { public void assertTag(String name, String expected, BaseSource source) {
getMetrics(source); getMetrics(source);

View File

@ -25,8 +25,6 @@ public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServer
public static enum FactoryStorage { public static enum FactoryStorage {
INSTANCE; INSTANCE;
private Object aggLock = new Object(); private Object aggLock = new Object();
private Object serverLock = new Object();
private MetricsRegionServerSource serverSource;
private MetricsRegionAggregateSourceImpl aggImpl; private MetricsRegionAggregateSourceImpl aggImpl;
} }
@ -42,13 +40,7 @@ public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServer
@Override @Override
public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) { public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) {
synchronized (FactoryStorage.INSTANCE.serverLock) { return new MetricsRegionServerSourceImpl(regionServerWrapper);
if (FactoryStorage.INSTANCE.serverSource == null) {
FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl(
regionServerWrapper);
}
return FactoryStorage.INSTANCE.serverSource;
}
} }
@Override @Override

View File

@ -26,6 +26,7 @@ import org.apache.hadoop.metrics2.MetricsInfo;
import org.apache.hadoop.metrics2.MetricsRecordBuilder; import org.apache.hadoop.metrics2.MetricsRecordBuilder;
import org.apache.hadoop.metrics2.MetricsSource; import org.apache.hadoop.metrics2.MetricsSource;
import org.apache.hadoop.metrics2.MetricsTag; import org.apache.hadoop.metrics2.MetricsTag;
import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -36,7 +37,6 @@ import static org.junit.Assert.*;
* A helper class that will allow tests to get into hadoop2's metrics2 values. * A helper class that will allow tests to get into hadoop2's metrics2 values.
*/ */
public class MetricsAssertHelperImpl implements MetricsAssertHelper { public class MetricsAssertHelperImpl implements MetricsAssertHelper {
private Map<String, String> tags = new HashMap<String, String>(); private Map<String, String> tags = new HashMap<String, String>();
private Map<String, Number> gauges = new HashMap<String, Number>(); private Map<String, Number> gauges = new HashMap<String, Number>();
private Map<String, Long> counters = new HashMap<String, Long>(); private Map<String, Long> counters = new HashMap<String, Long>();
@ -129,6 +129,13 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper {
} }
} }
@Override
public void init() {
// Make sure that the metrics system doesn't throw an exception when
// registering a source with the same name
DefaultMetricsSystem.setMiniClusterMode(true);
}
@Override @Override
public void assertTag(String name, String expected, BaseSource source) { public void assertTag(String name, String expected, BaseSource source) {
getMetrics(source); getMetrics(source);

View File

@ -39,6 +39,7 @@ import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.Regio
import org.apache.hadoop.hbase.regionserver.HRegion; import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.hadoop.hbase.regionserver.HRegionServer; import org.apache.hadoop.hbase.regionserver.HRegionServer;
import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.test.MetricsAssertHelper;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.JVMClusterUtil; import org.apache.hadoop.hbase.util.JVMClusterUtil;
import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
@ -88,6 +89,10 @@ public class MiniHBaseCluster extends HBaseCluster {
throws IOException, InterruptedException { throws IOException, InterruptedException {
super(conf); super(conf);
conf.set(HConstants.MASTER_PORT, "0"); conf.set(HConstants.MASTER_PORT, "0");
// Hadoop 2
CompatibilityFactory.getInstance(MetricsAssertHelper.class).init();
init(numMasters, numRegionServers, masterClass, regionserverClass); init(numMasters, numRegionServers, masterClass, regionserverClass);
this.initialClusterStatus = getClusterStatus(); this.initialClusterStatus = getClusterStatus();
} }

View File

@ -19,10 +19,9 @@ package org.apache.hadoop.hbase.regionserver;
import org.apache.hadoop.hbase.CompatibilityFactory; import org.apache.hadoop.hbase.CompatibilityFactory;
import org.apache.hadoop.hbase.SmallTests; import org.apache.hadoop.hbase.SmallTests;
import org.apache.hadoop.hbase.regionserver.MetricsRegionServer;
import org.apache.hadoop.hbase.regionserver.MetricsRegionServerWrapperStub;
import org.apache.hadoop.hbase.regionserver.MetricsRegionServerSource;
import org.apache.hadoop.hbase.test.MetricsAssertHelper; import org.apache.hadoop.hbase.test.MetricsAssertHelper;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@ -33,17 +32,27 @@ import static org.junit.Assert.assertNotNull;
*/ */
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestMetricsRegionServer { public class TestMetricsRegionServer {
public static MetricsAssertHelper HELPER =
CompatibilityFactory.getInstance(MetricsAssertHelper.class);
public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class); private MetricsRegionServerWrapperStub wrapper;
private MetricsRegionServer rsm;
private MetricsRegionServerSource serverSource;
@BeforeClass
public static void classSetUp() {
HELPER.init();
}
@Before
public void setUp() {
wrapper = new MetricsRegionServerWrapperStub();
rsm = new MetricsRegionServer(wrapper);
serverSource = rsm.getMetricsSource();
}
@Test @Test
public void testWrapperSource() { public void testWrapperSource() {
MetricsRegionServerWrapperStub wrapper = new MetricsRegionServerWrapperStub();
MetricsRegionServerSource source =
CompatibilityFactory.getInstance(MetricsRegionServerSourceFactory.class)
.createServer(wrapper);
MetricsRegionServer rsm = new MetricsRegionServer(wrapper, source);
MetricsRegionServerSource serverSource = rsm.getMetricsSource();
HELPER.assertTag("serverName", "test", serverSource); HELPER.assertTag("serverName", "test", serverSource);
HELPER.assertTag("clusterId", "tClusterId", serverSource); HELPER.assertTag("clusterId", "tClusterId", serverSource);
HELPER.assertTag("zookeeperQuorum", "zk", serverSource); HELPER.assertTag("zookeeperQuorum", "zk", serverSource);
@ -79,15 +88,12 @@ public class TestMetricsRegionServer {
@Test @Test
public void testConstuctor() { public void testConstuctor() {
MetricsRegionServer rsm = new MetricsRegionServer(new MetricsRegionServerWrapperStub());
assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource() ); assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource() );
assertNotNull("The RegionServerMetricsWrapper should be accessable", rsm.getRegionServerWrapper()); assertNotNull("The RegionServerMetricsWrapper should be accessable", rsm.getRegionServerWrapper());
} }
@Test @Test
public void testSlowCount() { public void testSlowCount() {
MetricsRegionServer rsm = new MetricsRegionServer(new MetricsRegionServerWrapperStub());
MetricsRegionServerSource serverSource = rsm.getMetricsSource();
for (int i=0; i < 12; i ++) { for (int i=0; i < 12; i ++) {
rsm.updateAppend(12); rsm.updateAppend(12);
rsm.updateAppend(1002); rsm.updateAppend(1002);