Index: hbase-it/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java =================================================================== --- hbase-it/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java (revision 0) +++ hbase-it/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java (revision 0) @@ -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-it/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java =================================================================== --- hbase-it/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java (revision 0) +++ hbase-it/hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAsserts.java (revision 0) @@ -0,0 +1,94 @@ +/** + * 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); + + /** + * 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); + + /** + * 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-it/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java =================================================================== --- hbase-it/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision 0) +++ hbase-it/hbase-hadoop1-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision 0) @@ -0,0 +1,65 @@ +/** + * 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.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 long getCounterValue(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-it/hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts =================================================================== --- hbase-it/hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision 0) +++ hbase-it/hbase-hadoop1-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision 0) @@ -0,0 +1 @@ +org.apache.hadoop.hbase.metrics.MetricsAssertsImpl Index: hbase-it/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java =================================================================== --- hbase-it/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision 0) +++ hbase-it/hbase-hadoop2-compat/src/test/java/org/apache/hadoop/hbase/metrics/MetricsAssertsImpl.java (revision 0) @@ -0,0 +1,38 @@ +/** + * 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; + +/** + * 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 long getCounterValue(String name, BaseMetricsSource ms) { + BaseMetricsSourceImpl bms = (BaseMetricsSourceImpl) ms; + return ((MutableCounterLong) bms.metricsRegistry.get(name)).value(); + } +} Index: hbase-it/hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts =================================================================== --- hbase-it/hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision 0) +++ hbase-it/hbase-hadoop2-compat/src/test/resources/META-INF/services/org.apache.hadoop.hbase.metrics.MetricsAsserts (revision 0) @@ -0,0 +1 @@ +org.apache.hadoop.hbase.metrics.MetricsAssertsImpl