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