diff --git a/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelper.java b/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelper.java index 968ab83ac1b..1d4d2f62ecc 100644 --- a/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelper.java +++ b/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelper.java @@ -23,6 +23,12 @@ import org.apache.hadoop.hbase.metrics.BaseSource; /** Interface of a class to make assertions about metrics values. */ 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. * diff --git a/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java b/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java index 8ac38af1551..e557afe611b 100644 --- a/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java +++ b/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java @@ -25,6 +25,7 @@ import org.apache.hadoop.metrics2.MetricsBuilder; import org.apache.hadoop.metrics2.MetricsRecordBuilder; import org.apache.hadoop.metrics2.MetricsSource; import org.apache.hadoop.metrics2.MetricsTag; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; import java.util.HashMap; 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 public void assertTag(String name, String expected, BaseSource source) { getMetrics(source); diff --git a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSourceFactoryImpl.java b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSourceFactoryImpl.java index f6cb79bf427..c5cc8b4fdd6 100644 --- a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSourceFactoryImpl.java +++ b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/regionserver/MetricsRegionServerSourceFactoryImpl.java @@ -25,8 +25,6 @@ public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServer public static enum FactoryStorage { INSTANCE; private Object aggLock = new Object(); - private Object serverLock = new Object(); - private MetricsRegionServerSource serverSource; private MetricsRegionAggregateSourceImpl aggImpl; } @@ -42,13 +40,7 @@ public class MetricsRegionServerSourceFactoryImpl implements MetricsRegionServer @Override public synchronized MetricsRegionServerSource createServer(MetricsRegionServerWrapper regionServerWrapper) { - synchronized (FactoryStorage.INSTANCE.serverLock) { - if (FactoryStorage.INSTANCE.serverSource == null) { - FactoryStorage.INSTANCE.serverSource = new MetricsRegionServerSourceImpl( - regionServerWrapper); - } - return FactoryStorage.INSTANCE.serverSource; - } + return new MetricsRegionServerSourceImpl(regionServerWrapper); } @Override diff --git a/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java b/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java index e4cfafb55c8..3481cc2a224 100644 --- a/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java +++ b/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/test/MetricsAssertHelperImpl.java @@ -26,6 +26,7 @@ import org.apache.hadoop.metrics2.MetricsInfo; import org.apache.hadoop.metrics2.MetricsRecordBuilder; import org.apache.hadoop.metrics2.MetricsSource; import org.apache.hadoop.metrics2.MetricsTag; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; import java.util.HashMap; 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. */ public class MetricsAssertHelperImpl implements MetricsAssertHelper { - private Map tags = new HashMap(); private Map gauges = new HashMap(); private Map counters = new HashMap(); @@ -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 public void assertTag(String name, String expected, BaseSource source) { getMetrics(source); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/MiniHBaseCluster.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/MiniHBaseCluster.java index 477cc9cd7b8..75cca706850 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/MiniHBaseCluster.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/MiniHBaseCluster.java @@ -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.HRegionServer; 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.JVMClusterUtil; import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread; @@ -88,6 +89,10 @@ public class MiniHBaseCluster extends HBaseCluster { throws IOException, InterruptedException { super(conf); conf.set(HConstants.MASTER_PORT, "0"); + + // Hadoop 2 + CompatibilityFactory.getInstance(MetricsAssertHelper.class).init(); + init(numMasters, numRegionServers, masterClass, regionserverClass); this.initialClusterStatus = getClusterStatus(); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionServer.java index 81436a73664..fb4abb1835d 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionServer.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestMetricsRegionServer.java @@ -19,10 +19,9 @@ package org.apache.hadoop.hbase.regionserver; import org.apache.hadoop.hbase.CompatibilityFactory; 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.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; @@ -33,17 +32,27 @@ import static org.junit.Assert.assertNotNull; */ @Category(SmallTests.class) 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 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("clusterId", "tClusterId", serverSource); HELPER.assertTag("zookeeperQuorum", "zk", serverSource); @@ -79,15 +88,12 @@ public class TestMetricsRegionServer { @Test public void testConstuctor() { - MetricsRegionServer rsm = new MetricsRegionServer(new MetricsRegionServerWrapperStub()); assertNotNull("There should be a hadoop1/hadoop2 metrics source", rsm.getMetricsSource() ); assertNotNull("The RegionServerMetricsWrapper should be accessable", rsm.getRegionServerWrapper()); } @Test public void testSlowCount() { - MetricsRegionServer rsm = new MetricsRegionServer(new MetricsRegionServerWrapperStub()); - MetricsRegionServerSource serverSource = rsm.getMetricsSource(); for (int i=0; i < 12; i ++) { rsm.updateAppend(12); rsm.updateAppend(1002);