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