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      TEST_UTIL.startMiniCluster(1, 1, 1, null, MyMaster.class, null);
70      cluster = TEST_UTIL.getHBaseCluster();
71      LOG.info("Waiting for active/ready master");
72      cluster.waitForActiveAndReadyMaster();
73      master = cluster.getMaster();
74    }
75  
76    @AfterClass
77    public static void after() throws Exception {
78      if (TEST_UTIL != null) {
79        TEST_UTIL.shutdownMiniCluster();
80      }
81    }
82  
83    @Test(timeout = 300000)
84    public void testClusterRequests() throws Exception {
85  
86      // sending fake request to master to see how metric value has changed
87      RegionServerStatusProtos.RegionServerReportRequest.Builder request =
88          RegionServerStatusProtos.RegionServerReportRequest.newBuilder();
89      ServerName serverName = cluster.getMaster(0).getServerName();
90      request.setServer(ProtobufUtil.toServerName(serverName));
91  
92      MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
93      ClusterStatusProtos.ServerLoad sl = ClusterStatusProtos.ServerLoad.newBuilder()
94                                             .setTotalNumberOfRequests(10000)
95                                             .build();
96      masterSource.init();
97      request.setLoad(sl);
98      master.getMasterRpcServices().regionServerReport(null, request.build());
99  
100     metricsHelper.assertCounter("cluster_requests", 10000, masterSource);
101 
102     sl = ClusterStatusProtos.ServerLoad.newBuilder()
103         .setTotalNumberOfRequests(15000)
104         .build();
105     request.setLoad(sl);
106     master.getMasterRpcServices().regionServerReport(null, request.build());
107 
108     metricsHelper.assertCounter("cluster_requests", 15000, masterSource);
109 
110     master.getMasterRpcServices().regionServerReport(null, request.build());
111 
112     metricsHelper.assertCounter("cluster_requests", 15000, masterSource);
113     master.stopMaster();
114   }
115 
116   @Test
117   public void testDefaultMasterMetrics() throws Exception {
118     MetricsMasterSource masterSource = master.getMasterMetrics().getMetricsSource();
119     metricsHelper.assertGauge( "numRegionServers", 1, masterSource);
120     metricsHelper.assertGauge( "averageLoad", 2, masterSource);
121     metricsHelper.assertGauge( "numDeadRegionServers", 0, masterSource);
122 
123     metricsHelper.assertGauge("masterStartTime", master.getMasterStartTime(), masterSource);
124     metricsHelper.assertGauge("masterActiveTime", master.getMasterActiveTime(), masterSource);
125 
126     metricsHelper.assertTag("isActiveMaster", "true", masterSource);
127     metricsHelper.assertTag("serverName", master.getServerName().toString(), masterSource);
128     metricsHelper.assertTag("clusterId", master.getClusterId(), masterSource);
129     metricsHelper.assertTag("zookeeperQuorum", master.getZooKeeper().getQuorum(), masterSource);
130   }
131 }