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