Index: hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts
===================================================================
--- hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision )
+++ hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision )
@@ -0,0 +1,1 @@
+org.apache.hadoop.hbase.metrics.MetricsAssertsImpl
Index: hbase-hadoop2-compat/pom.xml
===================================================================
--- hbase-hadoop2-compat/pom.xml (revision 1377167)
+++ hbase-hadoop2-compat/pom.xml (revision )
@@ -129,5 +129,14 @@
hadoop-minicluster
${hadoop-two.version}
+
+
+ org.apache.hbase
+ hbase-hadoop-compat
+ ${project.version}
+ test-jar
+ true
+ test
+
Index: hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java
===================================================================
--- hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java (revision )
+++ hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java (revision )
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.master;
+
+import junit.framework.Assert;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.MediumTests;
+import org.apache.hadoop.hbase.MiniHBaseCluster;
+import org.apache.hadoop.hbase.master.metrics.MXBeanImpl;
+import org.apache.hadoop.hbase.metrics.MetricsAsserts;
+import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
+import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
+import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos;
+import org.apache.hadoop.hbase.regionserver.HRegionServer;
+import org.apache.hadoop.hbase.util.Threads;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(MediumTests.class)
+public class TestMasterMetrics {
+ private static final Log LOG = LogFactory.getLog(TestMasterMetrics.class);
+
+ static {
+ Logger.getLogger("org.apache.hadoop.hbase").setLevel(Level.DEBUG);
+ }
+
+ private MiniHBaseCluster cluster;
+ private HMaster master;
+ private Configuration conf;
+ private HBaseTestingUtility TEST_UTIL;
+
+ private void startCluster() throws Exception{
+ LOG.info("Starting cluster");
+ conf = HBaseConfiguration.create();
+ conf.getLong("hbase.splitlog.max.resubmit", 0);
+ // Make the failure test faster
+ conf.setInt("zookeeper.recovery.retry", 0);
+ conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
+ conf.setFloat(HConstants.LOAD_BALANCER_SLOP_KEY, (float) 100.0); // no load balancing
+ conf.setBoolean(HConstants.DISTRIBUTED_LOG_SPLITTING_KEY, true);
+ TEST_UTIL = new HBaseTestingUtility(conf);
+ TEST_UTIL.startMiniCluster(1, 1);
+ cluster = TEST_UTIL.getHBaseCluster();
+ LOG.info("Waiting for active/ready master");
+ cluster.waitForActiveAndReadyMaster();
+ master = cluster.getMaster();
+
+ while (cluster.getLiveRegionServerThreads().size() < 1) {
+ Threads.sleep(1);
+ }
+ }
+
+ @After
+ public void after() throws Exception {
+ if (TEST_UTIL != null) {
+ TEST_UTIL.shutdownMiniCluster();
+ }
+ }
+
+ @Test(timeout=300000)
+ public void testClusterRequests() throws Exception {
+ MetricsAsserts.getInstance();
+
+ startCluster();
+
+ long val = MetricsAsserts.getInstance()
+ .getCounterValue("cluster_requests", master.getMetrics().getMetricsSource());
+
+ // sending fake request to master to see how metric value has changed
+ RegionServerStatusProtos.RegionServerReportRequest.Builder request =
+ RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
+ HRegionServer rs = cluster.getRegionServer(0);
+ request.setServer(ProtobufUtil.toServerName(rs.getServerName()));
+
+ HBaseProtos.ServerLoad sl = HBaseProtos.ServerLoad.newBuilder()
+ .setTotalNumberOfRequests(10)
+ .build();
+ request.setLoad(sl);
+ master.regionServerReport(null, request.build());
+
+ MetricsAsserts.getInstance().assertCounter("cluster_requests", val + 10,
+ master.getMetrics().getMetricsSource());
+ master.stopMaster();
+ }
+}
Index: hbase-hadoop1-compat/pom.xml
===================================================================
--- hbase-hadoop1-compat/pom.xml (revision 1377167)
+++ hbase-hadoop1-compat/pom.xml (revision )
@@ -86,10 +86,19 @@
+
org.apache.hadoop
hadoop-test
${hadoop-one.version}
+ true
+ test
+
+
+ org.apache.hbase
+ hbase-hadoop-compat
+ ${project.version}
+ test-jar
true
test
Index: hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts
===================================================================
--- hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision )
+++ hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision )
@@ -0,0 +1,1 @@
+org.apache.hadoop.hbase.metrics.MetricsAssertsImpl
Index: hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java
===================================================================
--- hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision )
+++ hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision )
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.metrics;
+
+import org.apache.hadoop.metrics2.MetricsBuilder;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+import org.apache.hadoop.metrics2.lib.MetricMutableGaugeLong;
+import org.mockito.Mockito;
+import org.mockito.internal.stubbing.defaultanswers.ReturnsMocks;
+import org.mockito.invocation.InvocationOnMock;
+
+/**
+ * Helpers for metrics source tests
+ */
+public class MetricsAssertsImpl extends MetricsAsserts {
+ @Override
+ public void assertCounter(String name, long value, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ org.apache.hadoop.test.MetricsAsserts.assertCounter(name, value, bms);
+ }
+
+ @Override
+ public void assertGauge(String name, long value, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ org.apache.hadoop.test.MetricsAsserts.assertGauge(name, value, bms);
+ }
+
+ @Override
+ public long getCounterValue(final String name, BaseMetricsSource ms) {
+ return getLongMetricValue(name, ms);
+ }
+
+ @Override
+ public long getGaugeValue(final String name, BaseMetricsSource ms) {
+ return getLongMetricValue(name, ms);
+ }
+
+ private long getLongMetricValue(final String name, BaseMetricsSource ms) {
+ // This looks ugly, but this is is the only usable way I found
+ // to get metrics value in hadoop 1.0...
+ final long[] value = new long[1];
+ final MetricsRecordBuilder mrb = Mockito.mock(MetricsRecordBuilder.class, new ReturnsMocks() {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws Throwable {
+ Object[] args = invocation.getArguments();
+ if (args[0].equals(name)) { // TODO: remove check? We do it in MetricsBuilder impl below
+ value[0] = (Long) args[2]; // args are: name, description, value
+ }
+ return invocation.getMock();
+ }
+ });
+
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ MetricsBuilder mb = new MetricsBuilder() {
+ @Override
+ public MetricsRecordBuilder addRecord(String s) {
+ return mrb;
+ }
+ };
+
+ bms.getMetrics(mb, true);
+
+ return value[0];
+ }
+}
Index: hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java
===================================================================
--- hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java (revision )
+++ hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java (revision )
@@ -0,0 +1,110 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.metrics;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.metrics.BaseMetricsSource;
+
+/**
+ * Class to load MetricsAsserts from the class path. Will only return a singleton
+ * instance.
+ * Contains helpers for metrics source tests.
+ */
+public abstract class MetricsAsserts {
+ private static final Log LOG = LogFactory.getLog(MetricsAsserts.class);
+
+ private static MetricsAsserts factory = null;
+ public static final String EXCEPTION_STRING =
+ "Could not create a MetricsAsserts metrics source. " +
+ "Is the hadoop compatibility jar on the classpath?";
+
+
+ /**
+ * Verifies counter value
+ * @param name name of counter
+ * @param value value to verify against
+ * @param ms metrics source
+ */
+ public abstract void assertCounter(String name, long value, BaseMetricsSource ms);
+
+ /**
+ * Verifies gauge value
+ * @param name name of gauge
+ * @param value value to verify against
+ * @param ms metrics source
+ */
+ public abstract void assertGauge(String name, long value, BaseMetricsSource ms);
+
+ /**
+ * Gets value of a counter. Handy in unit-tests
+ * @param name name of counter
+ * @param ms metrics source
+ * @return
+ */
+ public abstract long getCounterValue(String name, BaseMetricsSource ms);
+
+ /**
+ * Gets value of a gauge. Handy in unit-tests
+ * @param name name of counter
+ * @param ms metrics source
+ * @return
+ */
+ public abstract long getGaugeValue(String name, BaseMetricsSource ms);
+
+ /**
+ * Get the singleton instance of ReplicationMetricsSource
+ *
+ * @return the singleton
+ */
+ public static synchronized MetricsAsserts getInstance() {
+ if (factory == null) {
+ try {
+ ServiceLoader loader = ServiceLoader.load(MetricsAsserts.class);
+ Iterator it = loader.iterator();
+ factory = it.next();
+ if (it.hasNext()) {
+ StringBuilder msg = new StringBuilder();
+ msg.append("ServiceLoader provided more than one implementation for class: ")
+ .append(MetricsAsserts.class)
+ .append(", using implementation: ").append(factory.getClass())
+ .append(", other implementations: {");
+ while (it.hasNext()) {
+ msg.append(it.next()).append(" ");
+ }
+ msg.append("}");
+ LOG.warn(msg);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(EXCEPTION_STRING, e);
+ } catch (Error e) {
+ throw new RuntimeException(EXCEPTION_STRING, e);
+ }
+
+ // If there was nothing returned and no exception then throw an exception.
+ if (factory == null) {
+ throw new RuntimeException(EXCEPTION_STRING);
+ }
+ }
+ return factory;
+ }
+}
Index: hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java
===================================================================
--- hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision )
+++ hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision )
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hbase.metrics;
+
+import org.apache.hadoop.metrics2.lib.MutableCounterLong;
+import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
+
+/**
+ * Helpers for metrics source tests
+ */
+public class MetricsAssertsImpl extends MetricsAsserts {
+ @Override
+ public void assertCounter(String name, long value, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ org.apache.hadoop.test.MetricsAsserts.assertCounter(name, value, bms);
+ }
+
+ @Override
+ public void assertGauge(String name, long value, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ org.apache.hadoop.test.MetricsAsserts.assertGauge(name, value, bms);
+ }
+
+ @Override
+ public long getCounterValue(String name, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ return ((MutableCounterLong) bms.metricsRegistry.get(name)).value();
+ }
+
+ @Override
+ public long getGaugeValue(String name, BaseMetricsSource ms) {
+ BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms;
+ return ((MutableGaugeLong) bms.metricsRegistry.get(name)).value();
+ }
+}
Index: hbase-server/pom.xml
===================================================================
--- hbase-server/pom.xml (revision 1377167)
+++ hbase-server/pom.xml (revision )
@@ -451,6 +451,22 @@
jettison
test
+
+ org.apache.hbase
+ hbase-hadoop-compat
+ ${project.version}
+ test-jar
+ true
+ test
+
+
+ org.apache.hbase
+ ${compat.module}
+ ${project.version}
+ test-jar
+ true
+ test
+
Index: hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java
===================================================================
--- hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java (revision )
+++ hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRegionServerMetrics2.java (revision )
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.regionserver;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.MediumTests;
+import org.apache.hadoop.hbase.MiniHBaseCluster;
+import org.apache.hadoop.hbase.metrics.MetricsAsserts;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.Threads;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(MediumTests.class)
+public class TestRegionServerMetrics2 {
+ private static final Log LOG = LogFactory.getLog(TestRegionServerMetrics2.class);
+
+ static {
+ Logger.getLogger("org.apache.hadoop.hbase").setLevel(Level.DEBUG);
+ }
+
+ private MiniHBaseCluster cluster;
+ private HRegionServer rs;
+ private Configuration conf;
+ private HBaseTestingUtility TEST_UTIL;
+
+ private void startCluster() throws Exception{
+ LOG.info("Starting cluster");
+ conf = HBaseConfiguration.create();
+ conf.getLong("hbase.splitlog.max.resubmit", 0);
+ // Make the failure test faster
+ conf.setInt("zookeeper.recovery.retry", 0);
+ conf.setInt(HConstants.REGIONSERVER_INFO_PORT, -1);
+ conf.setFloat(HConstants.LOAD_BALANCER_SLOP_KEY, (float) 100.0); // no load balancing
+ conf.setBoolean(HConstants.DISTRIBUTED_LOG_SPLITTING_KEY, true);
+ TEST_UTIL = new HBaseTestingUtility(conf);
+ TEST_UTIL.startMiniCluster(1, 1);
+ cluster = TEST_UTIL.getHBaseCluster();
+ LOG.info("Waiting for active/ready master");
+ cluster.waitForActiveAndReadyMaster();
+
+ while (cluster.getLiveRegionServerThreads().size() < 1) {
+ Threads.sleep(1);
+ }
+
+ rs = cluster.getRegionServer(0);
+ }
+
+ @After
+ public void after() throws Exception {
+ if (TEST_UTIL != null) {
+ TEST_UTIL.shutdownMiniCluster();
+ }
+ }
+
+ @Test(timeout=300000)
+ public void testClusterRequests() throws Exception {
+ startCluster();
+
+ long regions = MetricsAsserts.getInstance()
+ .getGaugeValue("regions", rs.getMetrics2().getMetricsSource());
+
+ // Creating a table should add one region
+ TEST_UTIL.createTable(Bytes.toBytes("table"), Bytes.toBytes("cf"));
+ MetricsAsserts.getInstance().assertGauge("regions",
+ regions + 1, rs.getMetrics2().getMetricsSource());
+ }
+}