HBASE-25032 Do not assign regions to region server which has not called regionServerR...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestMasterMetrics.java
blob890399c54ca501841a82fb3b6867eb7028f8bed1
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import java.io.IOException;
21 import java.io.UncheckedIOException;
22 import java.util.HashMap;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.CompatibilityFactory;
25 import org.apache.hadoop.hbase.HBaseClassTestRule;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.MiniHBaseCluster;
28 import org.apache.hadoop.hbase.ServerMetricsBuilder;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.apache.hadoop.hbase.StartMiniClusterOption;
31 import org.apache.hadoop.hbase.YouAreDeadException;
32 import org.apache.hadoop.hbase.regionserver.RSRpcServices;
33 import org.apache.hadoop.hbase.test.MetricsAssertHelper;
34 import org.apache.hadoop.hbase.testclassification.MasterTests;
35 import org.apache.hadoop.hbase.testclassification.MediumTests;
36 import org.apache.zookeeper.KeeperException;
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.ClassRule;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
46 import org.apache.hbase.thirdparty.com.google.protobuf.ServiceException;
48 import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
49 import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
50 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos;
51 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupRequest;
52 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupResponse;
54 @Category({ MasterTests.class, MediumTests.class })
55 public class TestMasterMetrics {
57 @ClassRule
58 public static final HBaseClassTestRule CLASS_RULE =
59 HBaseClassTestRule.forClass(TestMasterMetrics.class);
61 private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetrics.class);
62 private static final MetricsAssertHelper metricsHelper =
63 CompatibilityFactory.getInstance(MetricsAssertHelper.class);
65 private static MiniHBaseCluster cluster;
66 private static HMaster master;
67 private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
69 public static class MyMaster extends HMaster {
71 public MyMaster(Configuration conf) throws IOException, KeeperException, InterruptedException {
72 super(conf);
75 @Override
76 protected void tryRegionServerReport(long reportStartTime, long reportEndTime) {
77 // do nothing
80 @Override
81 protected RSRpcServices createRpcServices() throws IOException {
82 return new MasterRpcServices(this) {
84 @Override
85 public RegionServerStartupResponse regionServerStartup(RpcController controller,
86 RegionServerStartupRequest request) throws ServiceException {
87 RegionServerStartupResponse resp = super.regionServerStartup(controller, request);
88 ServerManager serverManager = getServerManager();
89 // to let the region server actual online otherwise we can not assign meta region
90 new HashMap<>(serverManager.getOnlineServers()).forEach((sn, sm) -> {
91 if (sm.getLastReportTimestamp() <= 0) {
92 try {
93 serverManager.regionServerReport(sn,
94 ServerMetricsBuilder.newBuilder(sn).setVersionNumber(sm.getVersionNumber())
95 .setVersion(sm.getVersion()).setLastReportTimestamp(System.currentTimeMillis())
96 .build());
97 } catch (YouAreDeadException e) {
98 throw new UncheckedIOException(e);
102 return resp;
108 public static class MyRegionServer extends MiniHBaseCluster.MiniHBaseClusterRegionServer {
110 public MyRegionServer(Configuration conf) throws IOException, InterruptedException {
111 super(conf);
114 @Override
115 protected void tryRegionServerReport(long reportStartTime, long reportEndTime) {
116 // do nothing
120 @BeforeClass
121 public static void startCluster() throws Exception {
122 LOG.info("Starting cluster");
123 // Set master class and use default values for other options.
124 StartMiniClusterOption option = StartMiniClusterOption.builder().masterClass(MyMaster.class)
125 .rsClass(MyRegionServer.class).build();
126 TEST_UTIL.startMiniCluster(option);
127 cluster = TEST_UTIL.getHBaseCluster();
128 LOG.info("Waiting for active/ready master");
129 cluster.waitForActiveAndReadyMaster();
130 master = cluster.getMaster();
133 @AfterClass
134 public static void after() throws Exception {
135 master.stopMaster();
136 TEST_UTIL.shutdownMiniCluster();
139 @Test
140 public void testClusterRequests() throws Exception {
141 // sending fake request to master to see how metric value has changed
142 RegionServerStatusProtos.RegionServerReportRequest.Builder request =
143 RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
144 ServerName serverName = cluster.getMaster(0).getServerName();
145 request.setServer(ProtobufUtil.toServerName(serverName));
146 long expectedRequestNumber = 10000;
148 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
149 ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
150 .setTotalNumberOfRequests(expectedRequestNumber).build();
151 request.setLoad(sl);
153 master.getMasterRpcServices().regionServerReport(null, request.build());
154 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
156 expectedRequestNumber = 15000;
158 sl = ClusterStatusProtos.ServerLoad.newBuilder().setTotalNumberOfRequests(expectedRequestNumber)
159 .build();
160 request.setLoad(sl);
162 master.getMasterRpcServices().regionServerReport(null, request.build());
163 metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
166 @Test
167 public void testDefaultMasterMetrics() throws Exception {
168 MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
169 metricsHelper.assertGauge("numRegionServers", 1, masterSource);
170 metricsHelper.assertGauge("averageLoad", 1, masterSource);
171 metricsHelper.assertGauge("numDeadRegionServers", 0, masterSource);
172 metricsHelper.assertGauge("numDrainingRegionServers", 0, masterSource);
174 metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource);
175 metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource);
177 metricsHelper.assertTag("isActiveMaster", "true", masterSource);
178 metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource);
179 metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource);
180 metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource);
182 metricsHelper.assertCounter(MetricsMasterSource.SERVER_CRASH_METRIC_PREFIX+"SubmittedCount",
183 0, masterSource);
186 @Test
187 public void testDefaultMasterProcMetrics() throws Exception {
188 MetricsMasterProcSource masterSource = master.getMasterMetrics().getMetricsProcSource();
189 metricsHelper.assertGauge("numMasterWALs", master.getNumWALFiles(), masterSource);