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.metrics.BaseSourceImpl;
22  import org.apache.hadoop.metrics2.MetricsCollector;
23  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
24  import org.apache.hadoop.metrics2.lib.Interns;
25  import org.apache.hadoop.metrics2.lib.MutableCounterLong;
26  import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
27  import org.apache.hadoop.metrics2.lib.MutableHistogram;
28  
29  /**
30   * Hadoop2 implementation of MetricsMasterSource.
31   *
32   * Implements BaseSource through BaseSourceImpl, following the pattern
33   */
34  public class MetricsMasterSourceImpl
35      extends BaseSourceImpl implements MetricsMasterSource {
36  
37    private final MetricsMasterWrapper masterWrapper;
38    private MutableCounterLong clusterRequestsCounter;
39    private MutableGaugeLong ritGauge;
40    private MutableGaugeLong ritCountOverThresholdGauge;
41    private MutableGaugeLong ritOldestAgeGauge;
42    private MutableHistogram splitTimeHisto;
43    private MutableHistogram splitSizeHisto;
44  
45    public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
46      this(METRICS_NAME,
47          METRICS_DESCRIPTION,
48          METRICS_CONTEXT,
49          METRICS_JMX_CONTEXT,
50          masterWrapper);
51    }
52  
53    public MetricsMasterSourceImpl(String metricsName,
54                                   String metricsDescription,
55                                   String metricsContext,
56                                   String metricsJmxContext,
57                                   MetricsMasterWrapper masterWrapper) {
58      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
59      this.masterWrapper = masterWrapper;
60  
61    }
62  
63    @Override
64    public void init() {
65      super.init();
66      clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
67      ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l);
68      ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_NAME, "", 0l);
69      ritOldestAgeGauge = metricsRegistry.newGauge(RIT_OLDEST_AGE_NAME, "", 0l);
70      splitTimeHisto = metricsRegistry.newHistogram(SPLIT_SIZE_NAME, SPLIT_SIZE_DESC);
71      splitSizeHisto = metricsRegistry.newHistogram(SPLIT_TIME_NAME, SPLIT_TIME_DESC);
72    }
73  
74    public void incRequests(final int inc) {
75      this.clusterRequestsCounter.incr(inc);
76    }
77  
78    public void setRIT(int ritCount) {
79      ritGauge.set(ritCount);
80    }
81  
82    public void setRITCountOverThreshold(int ritCount) {
83      ritCountOverThresholdGauge.set(ritCount);
84    }
85  
86    public void setRITOldestAge(long ritCount) {
87      ritOldestAgeGauge.set(ritCount);
88    }
89  
90    @Override
91    public void updateSplitTime(long time) {
92      splitTimeHisto.add(time);
93    }
94  
95    @Override
96    public void updateSplitSize(long size) {
97      splitSizeHisto.add(size);
98    }
99  
100   @Override
101   public void getMetrics(MetricsCollector metricsCollector, boolean all) {
102 
103     MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName)
104         .setContext(metricsContext);
105 
106     // masterWrapper can be null because this function is called inside of init.
107     if (masterWrapper != null) {
108       metricsRecordBuilder
109           .addGauge(Interns.info(MASTER_ACTIVE_TIME_NAME,
110               MASTER_ACTIVE_TIME_DESC), masterWrapper.getActiveTime())
111           .addGauge(Interns.info(MASTER_START_TIME_NAME,
112               MASTER_START_TIME_DESC), masterWrapper.getStartTime())
113           .addGauge(Interns.info(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC),
114               masterWrapper.getAverageLoad())
115           .addGauge(Interns.info(NUM_REGION_SERVERS_NAME,
116               NUMBER_OF_REGION_SERVERS_DESC), masterWrapper.getRegionServers())
117           .addGauge(Interns.info(NUM_DEAD_REGION_SERVERS_NAME,
118               NUMBER_OF_DEAD_REGION_SERVERS_DESC),
119               masterWrapper.getDeadRegionServers())
120           .tag(Interns.info(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC),
121               masterWrapper.getZookeeperQuorum())
122           .tag(Interns.info(SERVER_NAME_NAME, SERVER_NAME_DESC), masterWrapper.getServerName())
123           .tag(Interns.info(CLUSTER_ID_NAME, CLUSTER_ID_DESC), masterWrapper.getClusterId())
124           .tag(Interns.info(IS_ACTIVE_MASTER_NAME,
125               IS_ACTIVE_MASTER_DESC),
126               String.valueOf(masterWrapper.getIsActiveMaster()));
127     }
128 
129     metricsRegistry.snapshot(metricsRecordBuilder, all);
130   }
131 
132 }