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