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.hbase.metrics.Interns;
24  import org.apache.hadoop.metrics2.MetricsCollector;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  import org.apache.hadoop.metrics2.lib.MutableFastCounter;
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 MutableFastCounter 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    @Override
65    public void incRequests(final long inc) {
66      this.clusterRequestsCounter.incr(inc);
67    }
68  
69    @Override
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(MERGE_PLAN_COUNT_NAME, MERGE_PLAN_COUNT_DESC),
78                masterWrapper.getMergePlanCount())
79            .addGauge(Interns.info(SPLIT_PLAN_COUNT_NAME, SPLIT_PLAN_COUNT_DESC),
80                masterWrapper.getSplitPlanCount())
81            .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME,
82                MASTER_ACTIVE_TIME_DESC), masterWrapper.getActiveTime())
83            .addGauge(Interns.info(MASTER_START_TIME_NAME,
84                MASTER_START_TIME_DESC), masterWrapper.getStartTime())
85            .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),
86                masterWrapper.getAverageLoad())
87            .tag(Interns.info(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC),
88                  masterWrapper.getRegionServers())
89            .addGauge(Interns.info(NUM_REGION_SERVERS_NAME,
90                NUMBER_OF_REGION_SERVERS_DESC), masterWrapper.getNumRegionServers())
91            .tag(Interns.info(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC),
92                  masterWrapper.getDeadRegionServers())
93            .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME,
94                NUMBER_OF_DEAD_REGION_SERVERS_DESC),
95                masterWrapper.getNumDeadRegionServers())
96            .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC),
97                masterWrapper.getZookeeperQuorum())
98            .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName())
99            .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId())
100           .tag(Interns.info(IS_ACTIVE_MASTER_NAME,
101               IS_ACTIVE_MASTER_DESC),
102               String.valueOf(masterWrapper.getIsActiveMaster()));
103     }
104 
105     metricsRegistry.snapshot(metricsRecordBuilder, all);
106   }
107 
108 }