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  
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  
40    public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
41      this(METRICS_NAME,
42          METRICS_DESCRIPTION,
43          METRICS_CONTEXT,
44          METRICS_JMX_CONTEXT,
45          masterWrapper);
46    }
47  
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
59    public void init() {
60      super.init();
61      clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
62    }
63  
64    public void incRequests(final int inc) {
65      this.clusterRequestsCounter.incr(inc);
66    }
67  
68    @Override
69    public void getMetrics(MetricsCollector metricsCollector, boolean all) {
70  
71      MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
72  
73      // masterWrapper can be null because this function is called inside of init.
74      if (masterWrapper != null) {
75        metricsRecordBuilder
76            .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME,
77                MASTER_ACTIVE_TIME_DESC), masterWrapper.getActiveTime())
78            .addGauge(Interns.info(MASTER_START_TIME_NAME,
79                MASTER_START_TIME_DESC), masterWrapper.getStartTime())
80            .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),
81                masterWrapper.getAverageLoad())
82            .tag(Interns.info(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC),
83                  masterWrapper.getRegionServers())
84            .addGauge(Interns.info(NUM_REGION_SERVERS_NAME,
85                NUMBER_OF_REGION_SERVERS_DESC), masterWrapper.getNumRegionServers())
86            .tag(Interns.info(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC),
87                  masterWrapper.getDeadRegionServers())
88            .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME,
89                NUMBER_OF_DEAD_REGION_SERVERS_DESC),
90                masterWrapper.getNumDeadRegionServers())
91            .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC),
92                masterWrapper.getZookeeperQuorum())
93            .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName())
94            .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId())
95            .tag(Interns.info(IS_ACTIVE_MASTER_NAME,
96                IS_ACTIVE_MASTER_DESC),
97                String.valueOf(masterWrapper.getIsActiveMaster()));
98      }
99  
100     metricsRegistry.snapshot(metricsRecordBuilder, all);
101   }
102 
103 }