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 org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.hadoop.hbase.CompatibilityFactory;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.MediumTests;
25  import org.apache.hadoop.hbase.MiniHBaseCluster;
26  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27  import org.apache.hadoop.hbase.protobuf.generated.ClusterStatusProtos;
28  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos;
29  import org.apache.hadoop.hbase.regionserver.HRegionServer;
30  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  @Category(MediumTests.class)
37  public class TestMasterMetrics {
38  
39    private static final Log LOG = LogFactory.getLog(TestMasterMetrics.class);
40    private static final MetricsAssertHelper metricsHelper = CompatibilityFactory
41        .getInstance(MetricsAssertHelper.class);
42  
43    private static MiniHBaseCluster cluster;
44    private static HMaster master;
45    private static HBaseTestingUtility TEST_UTIL;
46  
47  
48    @BeforeClass
49    public static void startCluster() throws Exception {
50      LOG.info("Starting cluster");
51      TEST_UTIL = new HBaseTestingUtility();
52      TEST_UTIL.startMiniCluster(1, 1);
53      cluster = TEST_UTIL.getHBaseCluster();
54      LOG.info("Waiting for active/ready master");
55      cluster.waitForActiveAndReadyMaster();
56      master = cluster.getMaster();
57    }
58  
59    @AfterClass
60    public static void after() throws Exception {
61      if (TEST_UTIL != null) {
62        TEST_UTIL.shutdownMiniCluster();
63      }
64    }
65  
66    @Test(timeout = 300000)
67    public void testClusterRequests() throws Exception {
68  
69      // sending fake request to master to see how metric value has changed
70      RegionServerStatusProtos.RegionServerReportRequest.Builder request =
71          RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
72      HRegionServer rs = cluster.getRegionServer(0);
73      request.setServer(ProtobufUtil.toServerName(rs.getServerName()));
74  
75      ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
76                                             .setTotalNumberOfRequests(10000)
77                                             .build();
78      master.getMetrics().getMetricsSource().init();
79      request.setLoad(sl);
80      master.regionServerReport(null, request.build());
81  
82      metricsHelper.assertCounter("cluster_requests", 10000,
83          master.getMetrics().getMetricsSource());
84      master.stopMaster();
85    }
86  
87    @Test
88    public void testDefaultMasterMetrics() throws Exception {
89      MetricsMasterSource masterSource = master.getMetrics().getMetricsSource();
90      metricsHelper.assertGauge( "numRegionServers", 1, masterSource);
91      metricsHelper.assertGauge( "averageLoad", 2, masterSource);
92      metricsHelper.assertGauge( "numDeadRegionServers", 0, masterSource);
93  
94      metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource);
95      metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource);
96  
97      metricsHelper.assertTag("isActiveMaster", "true", masterSource);
98      metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource);
99      metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource);
100     metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource);
101 
102   }
103 }