View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.CompatibilityFactory;
26  import org.apache.hadoop.hbase.CoordinatedStateManager;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.testclassification.MediumTests;
29  import org.apache.hadoop.hbase.MiniHBaseCluster;
30  import org.apache.hadoop.hbase.ServerName;
31  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
33  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos;
34  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
35  import org.apache.zookeeper.KeeperException;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category(MediumTests.class)
42  public class TestMasterMetrics {
43  
44    private static final Log LOG = LogFactory.getLog(TestMasterMetrics.class);
45    private static final MetricsAssertHelper metricsHelper = CompatibilityFactory
46        .getInstance(MetricsAssertHelper.class);
47  
48    private static MiniHBaseCluster cluster;
49    private static HMaster master;
50    private static HBaseTestingUtility TEST_UTIL;
51  
52    public static class MyMaster extends HMaster {
53      public MyMaster(Configuration conf, CoordinatedStateManager cp) throws IOException,
54          KeeperException, InterruptedException {
55        super(conf, cp);
56      }
57  
58      @Override
59      protected void tryRegionServerReport(
60          long reportStartTime, long reportEndTime) {
61        // do nothing
62      }
63    }
64  
65    @BeforeClass
66    public static void startCluster() throws Exception {
67      LOG.info("Starting cluster");
68      TEST_UTIL = new HBaseTestingUtility();
69      // The metrics depends on namespace region is online, therefore, we have to
70      // wait for namespace manager starting.
71      TEST_UTIL.getConfiguration().setBoolean("hbase.master.start.wait.for.namespacemanager", true);
72      TEST_UTIL.startMiniCluster(1, 1, 1, null, MyMaster.class, null);
73      cluster = TEST_UTIL.getHBaseCluster();
74      LOG.info("Waiting for active/ready master");
75      cluster.waitForActiveAndReadyMaster();
76      master = cluster.getMaster();
77    }
78  
79    @AfterClass
80    public static void after() throws Exception {
81      if (TEST_UTIL != null) {
82        TEST_UTIL.shutdownMiniCluster();
83      }
84    }
85  
86    @Test(timeout = 300000)
87    public void testClusterRequests() throws Exception {
88  
89      // sending fake request to master to see how metric value has changed
90      RegionServerStatusProtos.RegionServerReportRequest.Builder request =
91          RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
92      ServerName serverName = cluster.getMaster(0).getServerName();
93      request.setServer(ProtobufUtil.toServerName(serverName));
94  
95      MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
96      ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
97                                             .setTotalNumberOfRequests(10000)
98                                             .build();
99      masterSource.init();
100     request.setLoad(sl);
101     master.getMasterRpcServices().regionServerReport(null, request.build());
102 
103     metricsHelper.assertCounter("cluster_requests", 10000, masterSource);
104 
105     sl = ClusterStatusProtos.ServerLoad.newBuilder()
106         .setTotalNumberOfRequests(15000)
107         .build();
108     request.setLoad(sl);
109     master.getMasterRpcServices().regionServerReport(null, request.build());
110 
111     metricsHelper.assertCounter("cluster_requests", 15000, masterSource);
112 
113     master.getMasterRpcServices().regionServerReport(null, request.build());
114 
115     metricsHelper.assertCounter("cluster_requests", 15000, masterSource);
116     master.stopMaster();
117   }
118 
119   @Test
120   public void testDefaultMasterMetrics() throws Exception {
121     MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
122     metricsHelper.assertGauge( "numRegionServers", 1, masterSource);
123     metricsHelper.assertGauge( "averageLoad", 2, masterSource);
124     metricsHelper.assertGauge( "numDeadRegionServers", 0, masterSource);
125 
126     metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource);
127     metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource);
128 
129     metricsHelper.assertTag("isActiveMaster", "true", masterSource);
130     metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource);
131     metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource);
132     metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource);
133   }
134 
135   @Test
136   public void testDefaultMasterProcMetrics() throws Exception {
137     MetricsMasterProcSource masterSource = master.getMasterMetrics().getMetricsProcSource();
138     metricsHelper.assertGauge("numMasterWALs", master.getNumWALFiles(), masterSource);
139   }
140 }