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 
19 package org.apache.hadoop.hbase.master;
20 
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
23 import org.apache.hadoop.metrics2.MetricsCollector;
24 import org.apache.hadoop.metrics2.MetricsRecordBuilder;
25 import org.apache.hadoop.metrics2.lib.Interns;
26 import org.apache.hadoop.metrics2.lib.MutableCounterLong;
27 
28 /**
29  * Hadoop2 implementation of MetricsMasterSource.
30  *
31  * Implements BaseSource through BaseSourceImpl, following the pattern
32  */
33 @InterfaceAudience.Private
34 public class MetricsMasterSourceImpl
35     extends BaseSourceImpl implements MetricsMasterSource {
36 
37   private final MetricsMasterWrapper masterWrapper;
38   private MutableCounterLong clusterRequestsCounter;
39 
MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper)40   public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
41     this(METRICS_NAME,
42         METRICS_DESCRIPTION,
43         METRICS_CONTEXT,
44         METRICS_JMX_CONTEXT,
45         masterWrapper);
46   }
47 
MetricsMasterSourceImpl(String metricsName, String metricsDescription, String metricsContext, String metricsJmxContext, MetricsMasterWrapper masterWrapper)48   public MetricsMasterSourceImpl(String metricsName,
49                                  String metricsDescription,
50                                  String metricsContext,
51                                  String metricsJmxContext,
52                                  MetricsMasterWrapper masterWrapper) {
53     super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
54     this.masterWrapper = masterWrapper;
55 
56   }
57 
58   @Override
init()59   public void init() {
60     super.init();
61     clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
62   }
63 
64   @Override
incRequests(final long inc)65   public void incRequests(final long inc) {
66     this.clusterRequestsCounter.incr(inc);
67   }
68 
69   @Override
getMetrics(MetricsCollector metricsCollector, boolean all)70   public void getMetrics(MetricsCollector metricsCollector, boolean all) {
71 
72     MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
73 
74     // masterWrapper can be null because this function is called inside of init.
75     if (masterWrapper != null) {
76       metricsRecordBuilder
77           .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME,
78               MASTER_ACTIVE_TIME_DESC), masterWrapper.getActiveTime())
79           .addGauge(Interns.info(MASTER_START_TIME_NAME,
80               MASTER_START_TIME_DESC), masterWrapper.getStartTime())
81           .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),
82               masterWrapper.getAverageLoad())
83           .tag(Interns.info(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC),
84                 masterWrapper.getRegionServers())
85           .addGauge(Interns.info(NUM_REGION_SERVERS_NAME,
86               NUMBER_OF_REGION_SERVERS_DESC), masterWrapper.getNumRegionServers())
87           .tag(Interns.info(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC),
88                 masterWrapper.getDeadRegionServers())
89           .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME,
90               NUMBER_OF_DEAD_REGION_SERVERS_DESC),
91               masterWrapper.getNumDeadRegionServers())
92           .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC),
93               masterWrapper.getZookeeperQuorum())
94           .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName())
95           .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId())
96           .tag(Interns.info(IS_ACTIVE_MASTER_NAME,
97               IS_ACTIVE_MASTER_DESC),
98               String.valueOf(masterWrapper.getIsActiveMaster()));
99     }
100 
101     metricsRegistry.snapshot(metricsRecordBuilder, all);
102   }
103 
104 }
105