001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import java.io.IOException; 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hbase.CompatibilityFactory; 023import org.apache.hadoop.hbase.CoordinatedStateManager; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.MiniHBaseCluster; 027import org.apache.hadoop.hbase.ServerName; 028import org.apache.hadoop.hbase.test.MetricsAssertHelper; 029import org.apache.hadoop.hbase.testclassification.MasterTests; 030import org.apache.hadoop.hbase.testclassification.MediumTests; 031import org.apache.zookeeper.KeeperException; 032import org.junit.AfterClass; 033import org.junit.BeforeClass; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037import org.slf4j.Logger; 038import org.slf4j.LoggerFactory; 039 040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 041import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos; 042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos; 043 044@Category({MasterTests.class, MediumTests.class}) 045public class TestMasterMetrics { 046 047 @ClassRule 048 public static final HBaseClassTestRule CLASS_RULE = 049 HBaseClassTestRule.forClass(TestMasterMetrics.class); 050 051 private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetrics.class); 052 private static final MetricsAssertHelper metricsHelper = CompatibilityFactory 053 .getInstance(MetricsAssertHelper.class); 054 055 private static MiniHBaseCluster cluster; 056 private static HMaster master; 057 private static HBaseTestingUtility TEST_UTIL; 058 059 public static class MyMaster extends HMaster { 060 public MyMaster(Configuration conf) throws IOException, KeeperException, InterruptedException { 061 super(conf); 062 } 063 @Override 064 protected void tryRegionServerReport( 065 long reportStartTime, long reportEndTime) { 066 // do nothing 067 } 068 069 } 070 071 @BeforeClass 072 public static void startCluster() throws Exception { 073 LOG.info("Starting cluster"); 074 TEST_UTIL = new HBaseTestingUtility(); 075 TEST_UTIL.startMiniCluster(1, 1, 1, null, MyMaster.class, null); 076 cluster = TEST_UTIL.getHBaseCluster(); 077 LOG.info("Waiting for active/ready master"); 078 cluster.waitForActiveAndReadyMaster(); 079 master = cluster.getMaster(); 080 } 081 082 @AfterClass 083 public static void after() throws Exception { 084 if (TEST_UTIL != null) { 085 TEST_UTIL.shutdownMiniCluster(); 086 } 087 } 088 089 @Test 090 public void testClusterRequests() throws Exception { 091 092 // sending fake request to master to see how metric value has changed 093 094 RegionServerStatusProtos.RegionServerReportRequest.Builder request = 095 RegionServerStatusProtos.RegionServerReportRequest.newBuilder(); 096 ServerName serverName = cluster.getMaster(0).getServerName(); 097 request.setServer(ProtobufUtil.toServerName(serverName)); 098 long expectedRequestNumber = 10000; 099 100 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource(); 101 ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder() 102 .setTotalNumberOfRequests(expectedRequestNumber) 103 .build(); 104 request.setLoad(sl); 105 106 master.getMasterRpcServices().regionServerReport(null, request.build()); 107 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 108 if (tablesOnMaster) { 109 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource); 110 } else { 111 metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource); 112 113 } 114 115 expectedRequestNumber = 15000; 116 117 sl = ClusterStatusProtos.ServerLoad.newBuilder() 118 .setTotalNumberOfRequests(expectedRequestNumber) 119 .build(); 120 request.setLoad(sl); 121 122 master.getMasterRpcServices().regionServerReport(null, request.build()); 123 if (tablesOnMaster) { 124 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource); 125 } else { 126 metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource); 127 } 128 129 master.stopMaster(); 130 } 131 132 @Test 133 public void testDefaultMasterMetrics() throws Exception { 134 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource(); 135 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 136 metricsHelper.assertGauge( "numRegionServers",1 + (tablesOnMaster? 1: 0), masterSource); 137 metricsHelper.assertGauge( "averageLoad", 1 + (tablesOnMaster? 0: 1), masterSource); 138 metricsHelper.assertGauge( "numDeadRegionServers", 0, masterSource); 139 140 metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource); 141 metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource); 142 143 metricsHelper.assertTag("isActiveMaster", "true", masterSource); 144 metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource); 145 metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource); 146 metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource); 147 } 148 149 @Test 150 public void testDefaultMasterProcMetrics() throws Exception { 151 MetricsMasterProcSource masterSource = master.getMasterMetrics().getMetricsProcSource(); 152 metricsHelper.assertGauge("numMasterWALs", master.getNumWALFiles(), masterSource); 153 } 154}