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.MetricHistogram;
24  import org.apache.hadoop.metrics2.MetricsBuilder;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  import org.apache.hadoop.metrics2.lib.MetricMutableCounterLong;
27  
28  /**
29   * Hadoop1 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 MetricMutableCounterLong clusterRequestsCounter;
39  
40    // pause monitor metrics
41    private final MetricMutableCounterLong infoPauseThresholdExceeded;
42    private final MetricMutableCounterLong warnPauseThresholdExceeded;
43    private final MetricHistogram pausesWithGc;
44    private final MetricHistogram pausesWithoutGc;
45  
46    public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
47      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, masterWrapper);
48    }
49  
50    public MetricsMasterSourceImpl(String metricsName,
51                                   String metricsDescription,
52                                   String metricsContext,
53                                   String metricsJmxContext,
54                                   MetricsMasterWrapper masterWrapper) {
55      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
56      this.masterWrapper = masterWrapper;
57  
58      // pause monitor metrics
59      infoPauseThresholdExceeded = getMetricsRegistry().newCounter(INFO_THRESHOLD_COUNT_KEY,
60        INFO_THRESHOLD_COUNT_DESC, 0L);
61      warnPauseThresholdExceeded = getMetricsRegistry().newCounter(WARN_THRESHOLD_COUNT_KEY,
62        WARN_THRESHOLD_COUNT_DESC, 0L);
63      pausesWithGc = getMetricsRegistry().newTimeHistogram(PAUSE_TIME_WITH_GC_KEY);
64      pausesWithoutGc = getMetricsRegistry().newTimeHistogram(PAUSE_TIME_WITHOUT_GC_KEY);
65    }
66  
67    @Override
68    public void init() {
69      super.init();
70      clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
71    }
72  
73    public void incRequests(final int inc) {
74      this.clusterRequestsCounter.incr(inc);
75    }
76  
77    /**
78     * Method to export all the metrics.
79     *
80     * @param metricsBuilder Builder to accept metrics
81     * @param all            push all or only changed?
82     */
83    @Override
84    public void getMetrics(MetricsBuilder metricsBuilder, boolean all) {
85  
86      MetricsRecordBuilder metricsRecordBuilder = metricsBuilder.addRecord(metricsName);
87  
88      // masterWrapper can be null because this function is called inside of init.
89      if (masterWrapper != null) {
90        metricsRecordBuilder
91            .addGauge(MASTER_ACTIVE_TIME_NAME,
92                MASTER_ACTIVE_TIME_DESC, masterWrapper.getActiveTime())
93            .addGauge(MASTER_START_TIME_NAME,
94                MASTER_START_TIME_DESC, masterWrapper.getStartTime())
95            .addGauge(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC, masterWrapper.getAverageLoad())
96            .tag(LIVE_REGION_SERVERS_NAME, LIVE_REGION_SERVERS_DESC,
97                  masterWrapper.getRegionServers())
98            .addGauge(NUM_REGION_SERVERS_NAME,
99                NUMBER_OF_REGION_SERVERS_DESC, masterWrapper.getNumRegionServers())
100           .tag(DEAD_REGION_SERVERS_NAME, DEAD_REGION_SERVERS_DESC,
101                 masterWrapper.getDeadRegionServers())
102           .addGauge(NUM_DEAD_REGION_SERVERS_NAME,
103               NUMBER_OF_DEAD_REGION_SERVERS_DESC,
104               masterWrapper.getNumDeadRegionServers())
105           .tag(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC, masterWrapper.getZookeeperQuorum())
106           .tag(SERVER_NAME_NAME, SERVER_NAME_DESC, masterWrapper.getServerName())
107           .tag(CLUSTER_ID_NAME, CLUSTER_ID_DESC, masterWrapper.getClusterId())
108           .tag(IS_ACTIVE_MASTER_NAME,
109               IS_ACTIVE_MASTER_DESC,
110               String.valueOf(masterWrapper.getIsActiveMaster()));
111     }
112 
113     metricsRegistry.snapshot(metricsRecordBuilder, all);
114   }
115 
116   @Override
117   public void incInfoThresholdExceeded(int count) {
118     infoPauseThresholdExceeded.incr(count);
119   }
120 
121   @Override
122   public void incWarnThresholdExceeded(int count) {
123     warnPauseThresholdExceeded.incr(count);
124   }
125 
126   @Override
127   public void updatePauseTimeWithGc(long t) {
128     pausesWithGc.add(t);
129   }
130 
131   @Override
132   public void updatePauseTimeWithoutGc(long t) {
133     pausesWithoutGc.add(t);
134   }
135 }