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())
092                    .build());
093              } catch (YouAreDeadException e) {
094                throw new UncheckedIOException(e);
095              }
096            }
097          });
098          return resp;
099        }
100      };
101    }
102  }
103
104  public static class MyRegionServer
105    extends SingleProcessHBaseCluster.MiniHBaseClusterRegionServer {
106
107    public MyRegionServer(Configuration conf) throws IOException, InterruptedException {
108      super(conf);
109    }
110
111    @Override
112    protected void tryRegionServerReport(long reportStartTime, long reportEndTime) {
113      // do nothing
114    }
115  }
116
117  @BeforeClass
118  public static void startCluster() throws Exception {
119    LOG.info("Starting cluster");
120    // Set master class and use default values for other options.
121    StartTestingClusterOption option = StartTestingClusterOption.builder()
122      .masterClass(MyMaster.class).rsClass(MyRegionServer.class).build();
123    TEST_UTIL.startMiniCluster(option);
124    cluster = TEST_UTIL.getHBaseCluster();
125    LOG.info("Waiting for active/ready master");
126    cluster.waitForActiveAndReadyMaster();
127    master = cluster.getMaster();
128  }
129
130  @AfterClass
131  public static void after() throws Exception {
132    master.stopMaster();
133    TEST_UTIL.shutdownMiniCluster();
134  }
135
136  @Test
137  public void testClusterRequests() throws Exception {
138    // sending fake request to master to see how metric value has changed
139    RegionServerStatusProtos.RegionServerReportRequest.Builder request =
140      RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
141    ServerName serverName = cluster.getMaster(0).getServerName();
142    request.setServer(ProtobufUtil.toServerName(serverName));
143    long expectedRequestNumber = 10000;
144
145    MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
146    ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
147       .setTotalNumberOfRequests(expectedRequestNumber)
148       .setReadRequestsCount(expectedRequestNumber)
149       .setWriteRequestsCount(expectedRequestNumber).build();
150    request.setLoad(sl);
151
152    master.getMasterRpcServices().regionServerReport(null, request.build());
153    metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
154    metricsHelper.assertCounter("cluster_read_requests", expectedRequestNumber, masterSource);
155    metricsHelper.assertCounter("cluster_write_requests", expectedRequestNumber, masterSource);
156
157    expectedRequestNumber = 15000;
158
159    sl = ClusterStatusProtos.ServerLoad.newBuilder()
160            .setTotalNumberOfRequests(expectedRequestNumber)
161            .setReadRequestsCount(expectedRequestNumber)
162            .setWriteRequestsCount(expectedRequestNumber).build();
163    request.setLoad(sl);
164
165    master.getMasterRpcServices().regionServerReport(null, request.build());
166    metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
167    metricsHelper.assertCounter("cluster_read_requests", expectedRequestNumber, masterSource);
168    metricsHelper.assertCounter("cluster_write_requests", expectedRequestNumber, masterSource);
169  }
170
171  @Test
172  public void testDefaultMasterMetrics() throws Exception {
173    MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
174    metricsHelper.assertGauge("numRegionServers", 1, masterSource);
175    metricsHelper.assertGauge("averageLoad", 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}