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