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 org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.CompatibilityFactory;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.MiniHBaseCluster;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.StartMiniClusterOption;
028import org.apache.hadoop.hbase.test.MetricsAssertHelper;
029import org.apache.hadoop.hbase.testclassification.MasterTests;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.zookeeper.KeeperException;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
041import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos;
043
044@Category({MasterTests.class, MediumTests.class})
045public class TestMasterMetrics {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestMasterMetrics.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestMasterMetrics.class);
052  private static final MetricsAssertHelper metricsHelper = CompatibilityFactory
053      .getInstance(MetricsAssertHelper.class);
054
055  private static MiniHBaseCluster cluster;
056  private static HMaster master;
057  private static HBaseTestingUtility TEST_UTIL;
058
059  public static class MyMaster extends HMaster {
060    public MyMaster(Configuration conf) throws IOException, KeeperException, InterruptedException {
061      super(conf);
062    }
063    @Override
064    protected void tryRegionServerReport(
065        long reportStartTime, long reportEndTime) {
066      // do nothing
067    }
068
069  }
070
071  @BeforeClass
072  public static void startCluster() throws Exception {
073    LOG.info("Starting cluster");
074    TEST_UTIL = new HBaseTestingUtility();
075    // Set master class and use default values for other options.
076    StartMiniClusterOption option = StartMiniClusterOption.builder()
077        .masterClass(MyMaster.class).build();
078    TEST_UTIL.startMiniCluster(option);
079    cluster = TEST_UTIL.getHBaseCluster();
080    LOG.info("Waiting for active/ready master");
081    cluster.waitForActiveAndReadyMaster();
082    master = cluster.getMaster();
083  }
084
085  @AfterClass
086  public static void after() throws Exception {
087    if (TEST_UTIL != null) {
088      TEST_UTIL.shutdownMiniCluster();
089    }
090  }
091
092  @Test
093  public void testClusterRequests() throws Exception {
094
095    // sending fake request to master to see how metric value has changed
096
097    RegionServerStatusProtos.RegionServerReportRequest.Builder request =
098        RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
099    ServerName serverName = cluster.getMaster(0).getServerName();
100    request.setServer(ProtobufUtil.toServerName(serverName));
101    long expectedRequestNumber = 10000;
102
103    MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
104    ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
105                                           .setTotalNumberOfRequests(expectedRequestNumber)
106                                           .build();
107    request.setLoad(sl);
108
109    master.getMasterRpcServices().regionServerReport(null, request.build());
110    boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration());
111    if (tablesOnMaster) {
112      metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
113    } else {
114      metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource);
115
116    }
117
118    expectedRequestNumber = 15000;
119
120    sl = ClusterStatusProtos.ServerLoad.newBuilder()
121        .setTotalNumberOfRequests(expectedRequestNumber)
122        .build();
123    request.setLoad(sl);
124
125    master.getMasterRpcServices().regionServerReport(null, request.build());
126    if (tablesOnMaster) {
127      metricsHelper.assertCounter("cluster_requests", expectedRequestNumber, masterSource);
128    } else {
129      metricsHelper.assertCounterGt("cluster_requests", expectedRequestNumber, masterSource);
130    }
131
132    master.stopMaster();
133  }
134
135  @Test
136  public void testDefaultMasterMetrics() throws Exception {
137    MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
138    boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration());
139    metricsHelper.assertGauge( "numRegionServers",1 + (tablesOnMaster? 1: 0), masterSource);
140    metricsHelper.assertGauge( "averageLoad", 1 + (tablesOnMaster? 0: 1), masterSource);
141    metricsHelper.assertGauge( "numDeadRegionServers", 0, masterSource);
142
143    metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource);
144    metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource);
145
146    metricsHelper.assertTag("isActiveMaster", "true", masterSource);
147    metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource);
148    metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource);
149    metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource);
150
151    metricsHelper.assertCounter(MetricsMasterSource.SERVER_CRASH_METRIC_PREFIX+"SubmittedCount",
152      0, masterSource);
153  }
154
155  @Test
156  public void testDefaultMasterProcMetrics() throws Exception {
157    MetricsMasterProcSource masterSource = master.getMasterMetrics().getMetricsProcSource();
158    metricsHelper.assertGauge("numMasterWALs", master.getNumWALFiles(), masterSource);
159  }
160}