From 1afea63673603b0737dd304ea81c024f35c6a0b9 Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Wed, 19 Sep 2012 23:44:59 +0000 Subject: [PATCH] HBASE-6501 Integrate with unit-testing tools of hadoop's metrics2 framework git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1387820 13f79535-47bb-0310-9956-ffa450edef68 --- .../hbase/test/MetricsAssertHelper.java | 113 +++++++++++++++++- .../metrics/MasterMetricsSourceImpl.java | 16 +-- .../hbase/test/MetricsAssertHelperImpl.java | 65 +++++----- .../metrics/MasterMetricsSourceImpl.java | 17 ++- .../hbase/test/MetricsAssertHelperImpl.java | 68 ++++++----- .../master/metrics/MasterStatistics.java | 44 ------- .../hbase/master/TestMasterMetrics.java | 112 +++++++++++++++++ 7 files changed, 305 insertions(+), 130 deletions(-) delete mode 100644 hbase-server/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterStatistics.java create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java 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 6cec9090870..fc668bf8398 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 @@ -20,29 +20,136 @@ package org.apache.hadoop.hbase.test; import org.apache.hadoop.hbase.metrics.BaseMetricsSource; -/** - * - */ +/** Interface of a class to make assertions about metrics values. */ public interface MetricsAssertHelper { + /** + * Assert that a tag exists and has a given value. + * + * @param name The name of the tag. + * @param expected The expected value + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertTag(String name, String expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and that it's value is equal to the expected value. + * + * @param name The name of the gauge + * @param expected The expected value of the gauge. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGauge(String name, long expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and it's value is greater than a given value + * + * @param name The name of the gauge + * @param expected Value that the gauge is expected to be greater than + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGaugeGt(String name, long expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and it's value is less than a given value + * + * @param name The name of the gauge + * @param expected Value that the gauge is expected to be less than + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGaugeLt(String name, long expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and that it's value is equal to the expected value. + * + * @param name The name of the gauge + * @param expected The expected value of the gauge. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGauge(String name, double expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and it's value is greater than a given value + * + * @param name The name of the gauge + * @param expected Value that the gauge is expected to be greater than + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGaugeGt(String name, double expected, BaseMetricsSource source); + /** + * Assert that a gauge exists and it's value is less than a given value + * + * @param name The name of the gauge + * @param expected Value that the gauge is expected to be less than + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertGaugeLt(String name, double expected, BaseMetricsSource source); + /** + * Assert that a counter exists and that it's value is equal to the expected value. + * + * @param name The name of the counter. + * @param expected The expected value + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertCounter(String name, long expected, BaseMetricsSource source); + /** + * Assert that a counter exists and that it's value is greater than the given value. + * + * @param name The name of the counter. + * @param expected The value the counter is expected to be greater than. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertCounterGt(String name, long expected, BaseMetricsSource source); + /** + * Assert that a counter exists and that it's value is less than the given value. + * + * @param name The name of the counter. + * @param expected The value the counter is expected to be less than. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + */ public void assertCounterLt(String name, long expected, BaseMetricsSource source); + /** + * Get the value of a counter. + * + * @param name name of the counter. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + * @return long value of the counter. + */ + public long getCounter(String name, BaseMetricsSource source); + + /** + * Get the value of a gauge as a double. + * + * @param name name of the gauge. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + * @return double value of the gauge. + */ + public double getGaugeDouble(String name, BaseMetricsSource source); + + /** + * Get the value of a gauge as a long. + * + * @param name name of the gauge. + * @param source The BaseMetricsSource{@link BaseMetricsSource} that will provide the tags, + * gauges, and counters. + * @return long value of the gauge. + */ + public long getGaugeLong(String name, BaseMetricsSource source); } diff --git a/hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java b/hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java index 94b18e3f1a5..906ae98463d 100644 --- a/hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java +++ b/hbase-hadoop1-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java @@ -53,6 +53,11 @@ public class MasterMetricsSourceImpl MasterMetricsWrapper masterWrapper) { super(metricsName, metricsDescription, metricsContext, metricsJmxContext); this.masterWrapper = masterWrapper; + } + + @Override + public void init() { + super.init(); clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l); ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l); ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_NAME, "", 0l); @@ -61,15 +66,6 @@ public class MasterMetricsSourceImpl splitSizeHisto = metricsRegistry.newHistogram(SPLIT_TIME_NAME, SPLIT_TIME_DESC); } - @Override - public void init() { - this.metricsRegistry.clearMetrics(); - clusterRequestsCounter = getMetricsRegistry().getLongCounter("cluster_requests", 0); - ritGauge = getMetricsRegistry().getLongGauge("ritCount", 0); - ritCountOverThresholdGauge = getMetricsRegistry().getLongGauge("ritCountOverThreshold", 0); - ritOldestAgeGauge = getMetricsRegistry().getLongGauge("ritOldestAge", 0); - } - public void incRequests(final int inc) { this.clusterRequestsCounter.incr(inc); } @@ -112,7 +108,7 @@ public class MasterMetricsSourceImpl if (masterWrapper != null) { metricsRecordBuilder .addGauge(MASTER_ACTIVE_TIME_NAME, - MASTER_ACTIVE_TIME_DESC, masterWrapper.getMasterStartTime()) + MASTER_ACTIVE_TIME_DESC, masterWrapper.getMasterActiveTime()) .addGauge(MASTER_START_TIME_NAME, MASTER_START_TIME_DESC, masterWrapper.getMasterStartTime()) .addGauge(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC, masterWrapper.getAverageLoad()) 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 8556f2c6283..346047c623d 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 @@ -118,77 +118,80 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper { @Override public void assertGauge(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Should be equal", Long.valueOf(expected), gauges.get(cName)); + long found = getGaugeLong(name, source); + assertEquals("Metrics Should be equal", (long) Long.valueOf(expected), found); } @Override public void assertGaugeGt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - long found = gauges.get(cName).longValue(); + double found = getGaugeDouble(name, source); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); } @Override public void assertGaugeLt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - long found = gauges.get(cName).longValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); } @Override public void assertGauge(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Should be equal", Double.valueOf(expected), gauges.get(cName)); + double found = getGaugeDouble(name, source); + assertEquals("Metrics Should be equal", (double) Double.valueOf(expected), found); } @Override public void assertGaugeGt(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - double found = gauges.get(cName).doubleValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected); } @Override public void assertGaugeLt(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - double found = gauges.get(cName).doubleValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); } @Override public void assertCounter(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Counters should be equal", Long.valueOf(expected), counters.get(cName)); + long found = getCounter(name, source); + assertEquals("Metrics Counters should be equal", (long) Long.valueOf(expected), found); } @Override public void assertCounterGt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(counters.get(cName)); - long found = counters.get(cName).longValue(); + long found = getCounter(name, source); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); } @Override public void assertCounterLt(String name, long expected, BaseMetricsSource source) { + long found = getCounter(name, source); + assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); + } + + @Override + public long getCounter(String name, BaseMetricsSource source) { getMetrics(source); String cName = canonicalizeMetricName(name); assertNotNull(counters.get(cName)); - long found = counters.get(cName).longValue(); - assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); + return counters.get(cName).longValue(); + } + + @Override + public double getGaugeDouble(String name, BaseMetricsSource source) { + getMetrics(source); + String cName = canonicalizeMetricName(name); + assertNotNull(gauges.get(cName)); + return gauges.get(cName).doubleValue(); + } + + @Override + public long getGaugeLong(String name, BaseMetricsSource source) { + getMetrics(source); + String cName = canonicalizeMetricName(name); + assertNotNull(gauges.get(cName)); + return gauges.get(cName).longValue(); } private void reset() { diff --git a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java index a5ca74f3bf3..9c9d124bb85 100644 --- a/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java +++ b/hbase-hadoop2-compat/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterMetricsSourceImpl.java @@ -54,6 +54,12 @@ public class MasterMetricsSourceImpl MasterMetricsWrapper masterWrapper) { super(metricsName, metricsDescription, metricsContext, metricsJmxContext); this.masterWrapper = masterWrapper; + + } + + @Override + public void init() { + super.init(); clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l); ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l); ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_NAME, "", 0l); @@ -62,15 +68,6 @@ public class MasterMetricsSourceImpl splitSizeHisto = metricsRegistry.newHistogram(SPLIT_TIME_NAME, SPLIT_TIME_DESC); } - @Override - public void init() { - super.init(); - clusterRequestsCounter = getMetricsRegistry().getLongCounter("cluster_requests", 0); - ritGauge = getMetricsRegistry().getLongGauge("ritCount", 0); - ritCountOverThresholdGauge = getMetricsRegistry().getLongGauge("ritCountOverThreshold", 0); - ritOldestAgeGauge = getMetricsRegistry().getLongGauge("ritOldestAge", 0); - } - public void incRequests(final int inc) { this.clusterRequestsCounter.incr(inc); } @@ -107,7 +104,7 @@ public class MasterMetricsSourceImpl if (masterWrapper != null) { metricsRecordBuilder .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME, - MASTER_ACTIVE_TIME_DESC), masterWrapper.getMasterStartTime()) + MASTER_ACTIVE_TIME_DESC), masterWrapper.getMasterActiveTime()) .addGauge(Interns.info(MASTER_START_TIME_NAME, MASTER_START_TIME_DESC), masterWrapper.getMasterStartTime()) .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC), 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 408850ba4ab..b8b06ab4504 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 @@ -65,7 +65,7 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper { @Override public MetricsRecordBuilder tag(MetricsInfo metricsInfo, String s) { - tags.put(metricsInfo.name(), s); + tags.put(canonicalizeMetricName(metricsInfo.name()), s); return this; } @@ -137,79 +137,83 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper { @Override public void assertGauge(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Should be equal", Long.valueOf(expected), gauges.get(cName)); + long found = getGaugeLong(name, source); + assertEquals("Metrics Should be equal", (long) Long.valueOf(expected), found); } @Override public void assertGaugeGt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - long found = gauges.get(cName).longValue(); + double found = getGaugeDouble(name, source); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); } @Override public void assertGaugeLt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - long found = gauges.get(cName).longValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); } @Override public void assertGauge(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Should be equal", Double.valueOf(expected), gauges.get(cName)); + double found = getGaugeDouble(name, source); + assertEquals("Metrics Should be equal", (double) Double.valueOf(expected), found); } @Override public void assertGaugeGt(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - double found = gauges.get(cName).doubleValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected); } @Override public void assertGaugeLt(String name, double expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(gauges.get(cName)); - double found = gauges.get(cName).doubleValue(); + double found = getGaugeDouble(name, source); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); } @Override public void assertCounter(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertEquals("Metrics Counters should be equal", Long.valueOf(expected), counters.get(cName)); + long found = getCounter(name, source); + assertEquals("Metrics Counters should be equal", (long) Long.valueOf(expected), found); } @Override public void assertCounterGt(String name, long expected, BaseMetricsSource source) { - getMetrics(source); - String cName = canonicalizeMetricName(name); - assertNotNull(counters.get(cName)); - long found = gauges.get(cName).longValue(); + long found = getCounter(name, source); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); } @Override public void assertCounterLt(String name, long expected, BaseMetricsSource source) { + long found = getCounter(name, source); + assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); + } + + @Override + public long getCounter(String name, BaseMetricsSource source) { getMetrics(source); String cName = canonicalizeMetricName(name); assertNotNull(counters.get(cName)); - long found = gauges.get(cName).longValue(); - assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); + return counters.get(cName).longValue(); } + @Override + public double getGaugeDouble(String name, BaseMetricsSource source) { + getMetrics(source); + String cName = canonicalizeMetricName(name); + assertNotNull(gauges.get(cName)); + return gauges.get(cName).doubleValue(); + } + + @Override + public long getGaugeLong(String name, BaseMetricsSource source) { + getMetrics(source); + String cName = canonicalizeMetricName(name); + assertNotNull(gauges.get(cName)); + return gauges.get(cName).longValue(); + } + + private void reset() { tags.clear(); gauges.clear(); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterStatistics.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterStatistics.java deleted file mode 100644 index ced1d7e2690..00000000000 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/metrics/MasterStatistics.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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.metrics; - -import javax.management.ObjectName; - -import org.apache.hadoop.classification.InterfaceAudience; -import org.apache.hadoop.hbase.metrics.MetricsMBeanBase; -import org.apache.hadoop.metrics.util.MBeanUtil; -import org.apache.hadoop.metrics.util.MetricsRegistry; - -/** - * Exports the {@link MasterMetrics} statistics as an MBean - * for JMX. - */ -@InterfaceAudience.Private -public class MasterStatistics extends MetricsMBeanBase { - private final ObjectName mbeanName; - - public MasterStatistics(MetricsRegistry registry) { - super(registry, "MasterStatistics"); - mbeanName = MBeanUtil.registerMBean("Master", "MasterStatistics", this); - } - - public void shutdown() { - if (mbeanName != null) - MBeanUtil.unregisterMBean(mbeanName); - } -} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java new file mode 100644 index 00000000000..73294977062 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterMetrics.java @@ -0,0 +1,112 @@ +/** + * 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 org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.CompatibilityFactory; +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.MasterMetricsSource; +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.test.MetricsAssertHelper; +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.Before; +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); + private static final MetricsAssertHelper metricsHelper = CompatibilityFactory + .getInstance(MetricsAssertHelper.class); + + private static MiniHBaseCluster cluster; + private static HMaster master; + private static HBaseTestingUtility TEST_UTIL; + + + @BeforeClass + public static void startCluster() throws Exception { + LOG.info("Starting cluster"); + TEST_UTIL = new HBaseTestingUtility(); + TEST_UTIL.startMiniCluster(1, 1); + cluster = TEST_UTIL.getHBaseCluster(); + LOG.info("Waiting for active/ready master"); + cluster.waitForActiveAndReadyMaster(); + master = cluster.getMaster(); + } + + @AfterClass + public static void after() throws Exception { + if (TEST_UTIL != null) { + TEST_UTIL.shutdownMiniCluster(); + } + } + + @Test(timeout = 300000) + public void testClusterRequests() throws Exception { + + // 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(10000) + .build(); + master.getMetrics().getMetricsSource().init(); + request.setLoad(sl); + master.regionServerReport(null, request.build()); + + metricsHelper.assertCounter("cluster_requests", 10000, + master.getMetrics().getMetricsSource()); + master.stopMaster(); + } + + @Test + public void testDefaultMasterMetrics() throws Exception { + MasterMetricsSource source = master.getMetrics().getMetricsSource(); + metricsHelper.assertGauge( "numRegionServers", 1, source); + metricsHelper.assertGauge( "averageLoad", 2, source); + metricsHelper.assertGauge( "numDeadRegionServers", 0, source); + + metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), source); + metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), source); + + metricsHelper.assertTag("isActiveMaster", "true", source); + metricsHelper.assertTag("serverName", master.getServerName().toString(), source); + metricsHelper.assertTag("clusterId", master.getClusterId(), source); + metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), source); + + } +}