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 java.io.UncheckedIOException; 022import java.util.HashMap; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.CompatibilityFactory; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseTestingUtility; 027import org.apache.hadoop.hbase.MiniHBaseCluster; 028import org.apache.hadoop.hbase.ServerMetricsBuilder; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.hadoop.hbase.StartMiniClusterOption; 031import org.apache.hadoop.hbase.YouAreDeadException; 032import org.apache.hadoop.hbase.regionserver.RSRpcServices; 033import org.apache.hadoop.hbase.test.MetricsAssertHelper; 034import org.apache.hadoop.hbase.testclassification.MasterTests; 035import org.apache.hadoop.hbase.testclassification.MediumTests; 036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 037import org.apache.zookeeper.KeeperException; 038import org.junit.AfterClass; 039import org.junit.BeforeClass; 040import org.junit.ClassRule; 041import org.junit.Test; 042import org.junit.experimental.categories.Category; 043import org.slf4j.Logger; 044import org.slf4j.LoggerFactory; 045 046import org.apache.hbase.thirdparty.com.google.protobuf.RpcController; 047import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException; 048 049import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 050import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos; 051import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos; 052import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupRequest; 053import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse; 054 055@Category({ MasterTests.class, MediumTests.class }) 056public class TestMasterMetrics { 057 058 @ClassRule 059 public static final HBaseClassTestRule CLASS_RULE = 060 HBaseClassTestRule.forClass(TestMasterMetrics.class); 061 062 private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetrics.class); 063 private static final MetricsAssertHelper metricsHelper = 064 CompatibilityFactory.getInstance(MetricsAssertHelper.class); 065 066 private static MiniHBaseCluster cluster; 067 private static HMaster master; 068 private static HBaseTestingUtility TEST_UTIL; 069 070 public static class MyMaster extends HMaster { 071 public MyMaster(Configuration conf) throws IOException, KeeperException, InterruptedException { 072 super(conf); 073 } 074 075 @Override 076 protected void tryRegionServerReport(long reportStartTime, long reportEndTime) { 077 // do nothing 078 } 079 080 @Override 081 protected RSRpcServices createRpcServices() throws IOException { 082 return new MasterRpcServices(this) { 083 084 @Override 085 public RegionServerStartupResponse regionServerStartup(RpcController controller, 086 RegionServerStartupRequest request) throws ServiceException { 087 RegionServerStartupResponse resp = super.regionServerStartup(controller, request); 088 ServerManager serverManager = getServerManager(); 089 // to let the region server actual online otherwise we can not assign meta region 090 new HashMap<>(serverManager.getOnlineServers()).forEach((sn, sm) -> { 091 if (sm.getLastReportTimestamp() <= 0) { 092 try { 093 serverManager.regionServerReport(sn, 094 ServerMetricsBuilder.newBuilder(sn).setVersionNumber(sm.getVersionNumber()) 095 .setVersion(sm.getVersion()) 096 .setLastReportTimestamp(EnvironmentEdgeManager.currentTime()).build()); 097 } catch (YouAreDeadException e) { 098 throw new UncheckedIOException(e); 099 } 100 } 101 }); 102 return resp; 103 } 104 }; 105 } 106 } 107 108 @BeforeClass 109 public static void startCluster() throws Exception { 110 LOG.info("Starting cluster"); 111 TEST_UTIL = new HBaseTestingUtility(); 112 // Set master class and use default values for other options. 113 StartMiniClusterOption option = 114 StartMiniClusterOption.builder().masterClass(MyMaster.class).build(); 115 TEST_UTIL.startMiniCluster(option); 116 cluster = TEST_UTIL.getHBaseCluster(); 117 LOG.info("Waiting for active/ready master"); 118 cluster.waitForActiveAndReadyMaster(); 119 master = cluster.getMaster(); 120 } 121 122 @AfterClass 123 public static void after() throws Exception { 124 if (TEST_UTIL != null) { 125 TEST_UTIL.shutdownMiniCluster(); 126 } 127 } 128 129 @Test 130 public void testClusterRequests() throws Exception { 131 132 // sending fake request to master to see how metric value has changed 133 134 RegionServerStatusProtos.RegionServerReportRequest.Builder request = 135 RegionServerStatusProtos.RegionServerReportRequest.newBuilder(); 136 ServerName serverName = cluster.getMaster(0).getServerName(); 137 request.setServer(ProtobufUtil.toServerName(serverName)); 138 long expectedRequestNumber = 10000; 139 140 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource(); 141 ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder() 142 .setTotalNumberOfRequests(expectedRequestNumber).build(); 143 request.setLoad(sl); 144 145 master.getMasterRpcServices().regionServerReport(null, request.build()); 146 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 147 if (tablesOnMaster) { 148 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource); 149 } else { 150 metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource); 151 152 } 153 154 expectedRequestNumber = 15000; 155 156 sl = ClusterStatusProtos.ServerLoad.newBuilder().setTotalNumberOfRequests(expectedRequestNumber) 157 .build(); 158 request.setLoad(sl); 159 160 master.getMasterRpcServices().regionServerReport(null, request.build()); 161 if (tablesOnMaster) { 162 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource); 163 } else { 164 metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource); 165 } 166 167 master.stopMaster(); 168 } 169 170 @Test 171 public void testDefaultMasterMetrics() throws Exception { 172 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource(); 173 boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration()); 174 metricsHelper.assertGauge("numRegionServers", 1 + (tablesOnMaster ? 1 : 0), masterSource); 175 metricsHelper.assertGauge("averageLoad", 1 + (tablesOnMaster ? 0 : 1), masterSource); 176 metricsHelper.assertGauge("numDeadRegionServers", 0, masterSource); 177 metricsHelper.assertGauge("numDrainingRegionServers", 0, masterSource); 178 179 metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource); 180 metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource); 181 182 metricsHelper.assertTag("isActiveMaster", "true", masterSource); 183 metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource); 184 metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource); 185 metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource); 186 187 metricsHelper.assertCounter(MetricsMasterSource.SERVER_CRASH_METRIC_PREFIX + "SubmittedCount", 188 0, masterSource); 189 } 190 191 @Test 192 public void testDefaultMasterProcMetrics() throws Exception { 193 MetricsMasterProcSource masterSource = master.getMasterMetrics().getMetricsProcSource(); 194 metricsHelper.assertGauge("numMasterWALs", master.getNumWALFiles(), masterSource); 195 } 196}