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