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
This commit is contained in:
Michael Stack 2012-09-19 23:44:59 +00:00
parent d46c899468
commit 1afea63673
7 changed files with 305 additions and 130 deletions

View File

@ -20,29 +20,136 @@ package org.apache.hadoop.hbase.test;
import org.apache.hadoop.hbase.metrics.BaseMetricsSource; import org.apache.hadoop.hbase.metrics.BaseMetricsSource;
/** /** Interface of a class to make assertions about metrics values. */
*
*/
public interface MetricsAssertHelper { 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); 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); 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); 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); 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); 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); 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); 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); 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); 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); 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);
} }

View File

@ -53,6 +53,11 @@ public class MasterMetricsSourceImpl
MasterMetricsWrapper masterWrapper) { MasterMetricsWrapper masterWrapper) {
super(metricsName, metricsDescription, metricsContext, metricsJmxContext); super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
this.masterWrapper = masterWrapper; this.masterWrapper = masterWrapper;
}
@Override
public void init() {
super.init();
clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l); clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l); ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l);
ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_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); 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) { public void incRequests(final int inc) {
this.clusterRequestsCounter.incr(inc); this.clusterRequestsCounter.incr(inc);
} }
@ -112,7 +108,7 @@ public class MasterMetricsSourceImpl
if (masterWrapper != null) { if (masterWrapper != null) {
metricsRecordBuilder metricsRecordBuilder
.addGauge(MASTER_ACTIVE_TIME_NAME, .addGauge(MASTER_ACTIVE_TIME_NAME,
MASTER_ACTIVE_TIME_DESC, masterWrapper.getMasterStartTime()) MASTER_ACTIVE_TIME_DESC, masterWrapper.getMasterActiveTime())
.addGauge(MASTER_START_TIME_NAME, .addGauge(MASTER_START_TIME_NAME,
MASTER_START_TIME_DESC, masterWrapper.getMasterStartTime()) MASTER_START_TIME_DESC, masterWrapper.getMasterStartTime())
.addGauge(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC, masterWrapper.getAverageLoad()) .addGauge(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC, masterWrapper.getAverageLoad())

View File

@ -118,77 +118,80 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper {
@Override @Override
public void assertGauge(String name, long expected, BaseMetricsSource source) { public void assertGauge(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getGaugeLong(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Should be equal", (long) Long.valueOf(expected), found);
assertEquals("Metrics Should be equal", Long.valueOf(expected), gauges.get(cName));
} }
@Override @Override
public void assertGaugeGt(String name, long expected, BaseMetricsSource source) { public void assertGaugeGt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
long found = gauges.get(cName).longValue();
assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertGaugeLt(String name, long expected, BaseMetricsSource source) { public void assertGaugeLt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
long found = gauges.get(cName).longValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected);
} }
@Override @Override
public void assertGauge(String name, double expected, BaseMetricsSource source) { public void assertGauge(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Should be equal", (double) Double.valueOf(expected), found);
assertEquals("Metrics Should be equal", Double.valueOf(expected), gauges.get(cName));
} }
@Override @Override
public void assertGaugeGt(String name, double expected, BaseMetricsSource source) { public void assertGaugeGt(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
double found = gauges.get(cName).doubleValue();
assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected); assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertGaugeLt(String name, double expected, BaseMetricsSource source) { public void assertGaugeLt(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
double found = gauges.get(cName).doubleValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected);
} }
@Override @Override
public void assertCounter(String name, long expected, BaseMetricsSource source) { public void assertCounter(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getCounter(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Counters should be equal", (long) Long.valueOf(expected), found);
assertEquals("Metrics Counters should be equal", Long.valueOf(expected), counters.get(cName));
} }
@Override @Override
public void assertCounterGt(String name, long expected, BaseMetricsSource source) { public void assertCounterGt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getCounter(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(counters.get(cName));
long found = counters.get(cName).longValue();
assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertCounterLt(String name, long expected, BaseMetricsSource source) { 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); getMetrics(source);
String cName = canonicalizeMetricName(name); String cName = canonicalizeMetricName(name);
assertNotNull(counters.get(cName)); assertNotNull(counters.get(cName));
long found = counters.get(cName).longValue(); return counters.get(cName).longValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); }
@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() { private void reset() {

View File

@ -54,6 +54,12 @@ public class MasterMetricsSourceImpl
MasterMetricsWrapper masterWrapper) { MasterMetricsWrapper masterWrapper) {
super(metricsName, metricsDescription, metricsContext, metricsJmxContext); super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
this.masterWrapper = masterWrapper; this.masterWrapper = masterWrapper;
}
@Override
public void init() {
super.init();
clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l); clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l); ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l);
ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_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); 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) { public void incRequests(final int inc) {
this.clusterRequestsCounter.incr(inc); this.clusterRequestsCounter.incr(inc);
} }
@ -107,7 +104,7 @@ public class MasterMetricsSourceImpl
if (masterWrapper != null) { if (masterWrapper != null) {
metricsRecordBuilder metricsRecordBuilder
.addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME, .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, .addGauge(Interns.info(MASTER_START_TIME_NAME,
MASTER_START_TIME_DESC), masterWrapper.getMasterStartTime()) MASTER_START_TIME_DESC), masterWrapper.getMasterStartTime())
.addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC), .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),

View File

@ -65,7 +65,7 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper {
@Override @Override
public MetricsRecordBuilder tag(MetricsInfo metricsInfo, String s) { public MetricsRecordBuilder tag(MetricsInfo metricsInfo, String s) {
tags.put(metricsInfo.name(), s); tags.put(canonicalizeMetricName(metricsInfo.name()), s);
return this; return this;
} }
@ -137,79 +137,83 @@ public class MetricsAssertHelperImpl implements MetricsAssertHelper {
@Override @Override
public void assertGauge(String name, long expected, BaseMetricsSource source) { public void assertGauge(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getGaugeLong(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Should be equal", (long) Long.valueOf(expected), found);
assertEquals("Metrics Should be equal", Long.valueOf(expected), gauges.get(cName));
} }
@Override @Override
public void assertGaugeGt(String name, long expected, BaseMetricsSource source) { public void assertGaugeGt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
long found = gauges.get(cName).longValue();
assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertGaugeLt(String name, long expected, BaseMetricsSource source) { public void assertGaugeLt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
long found = gauges.get(cName).longValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected);
} }
@Override @Override
public void assertGauge(String name, double expected, BaseMetricsSource source) { public void assertGauge(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Should be equal", (double) Double.valueOf(expected), found);
assertEquals("Metrics Should be equal", Double.valueOf(expected), gauges.get(cName));
} }
@Override @Override
public void assertGaugeGt(String name, double expected, BaseMetricsSource source) { public void assertGaugeGt(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
double found = gauges.get(cName).doubleValue();
assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected); assertTrue(name + "(" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertGaugeLt(String name, double expected, BaseMetricsSource source) { public void assertGaugeLt(String name, double expected, BaseMetricsSource source) {
getMetrics(source); double found = getGaugeDouble(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(gauges.get(cName));
double found = gauges.get(cName).doubleValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected); assertTrue(name + "(" + found + ") should be less than " + expected, found < expected);
} }
@Override @Override
public void assertCounter(String name, long expected, BaseMetricsSource source) { public void assertCounter(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getCounter(name, source);
String cName = canonicalizeMetricName(name); assertEquals("Metrics Counters should be equal", (long) Long.valueOf(expected), found);
assertEquals("Metrics Counters should be equal", Long.valueOf(expected), counters.get(cName));
} }
@Override @Override
public void assertCounterGt(String name, long expected, BaseMetricsSource source) { public void assertCounterGt(String name, long expected, BaseMetricsSource source) {
getMetrics(source); long found = getCounter(name, source);
String cName = canonicalizeMetricName(name);
assertNotNull(counters.get(cName));
long found = gauges.get(cName).longValue();
assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected); assertTrue(name + " (" + found + ") should be greater than " + expected, found > expected);
} }
@Override @Override
public void assertCounterLt(String name, long expected, BaseMetricsSource source) { 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); getMetrics(source);
String cName = canonicalizeMetricName(name); String cName = canonicalizeMetricName(name);
assertNotNull(counters.get(cName)); assertNotNull(counters.get(cName));
long found = gauges.get(cName).longValue(); return counters.get(cName).longValue();
assertTrue(name + "(" + found + ") should be less than " + expected, found < expected);
} }
@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() { private void reset() {
tags.clear(); tags.clear();
gauges.clear(); gauges.clear();

View File

@ -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);
}
}

View File

@ -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);
}
}