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    private MetricMutableGaugeLong ritGauge;
44    private MetricMutableGaugeLong ritCountOverThresholdGauge;
45    private MetricMutableGaugeLong ritOldestAgeGauge;
46    private MetricMutableHistogram splitTimeHisto;
47    private MetricMutableHistogram splitSizeHisto;
48    private MetricMutableStat snapshotTimeHisto;
49    private MetricMutableStat snapshotCloneTimeHisto;
50    private MetricMutableStat snapshotRestoreTimeHisto;
51    private MetricMutableHistogram metaSplitTimeHisto;
52    private MetricMutableHistogram metaSplitSizeHisto;
53  
54    public MetricsMasterSourceImpl(MetricsMasterWrapper masterWrapper) {
55      this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT, masterWrapper);
56    }
57  
58    public MetricsMasterSourceImpl(String metricsName,
59                                   String metricsDescription,
60                                   String metricsContext,
61                                   String metricsJmxContext,
62                                   MetricsMasterWrapper masterWrapper) {
63      super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
64      this.masterWrapper = masterWrapper;
65    }
66  
67    @Override
68    public void init() {
69      super.init();
70      clusterRequestsCounter = metricsRegistry.newCounter(CLUSTER_REQUESTS_NAME, "", 0l);
71      ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, "", 0l);
72      ritCountOverThresholdGauge = metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_NAME, "", 0l);
73      ritOldestAgeGauge = metricsRegistry.newGauge(RIT_OLDEST_AGE_NAME, "", 0l);
74      splitSizeHisto = metricsRegistry.newHistogram(SPLIT_SIZE_NAME, SPLIT_SIZE_DESC);
75      splitTimeHisto = metricsRegistry.newHistogram(SPLIT_TIME_NAME, SPLIT_TIME_DESC);
76      snapshotTimeHisto = metricsRegistry.newStat(
77          SNAPSHOT_TIME_NAME, SNAPSHOT_TIME_DESC, "Ops", "Time", true);
78      snapshotCloneTimeHisto = metricsRegistry.newStat(
79          SNAPSHOT_CLONE_TIME_NAME, SNAPSHOT_CLONE_TIME_DESC, "Ops", "Time", true);
80      snapshotRestoreTimeHisto = metricsRegistry.newStat(
81          SNAPSHOT_RESTORE_TIME_NAME, SNAPSHOT_RESTORE_TIME_DESC, "Ops", "Time", true);
82      metaSplitTimeHisto = metricsRegistry.newHistogram(META_SPLIT_TIME_NAME, META_SPLIT_TIME_DESC);
83      metaSplitSizeHisto = metricsRegistry.newHistogram(META_SPLIT_SIZE_NAME, META_SPLIT_SIZE_DESC);
84    }
85  
86    public void incRequests(final int inc) {
87      this.clusterRequestsCounter.incr(inc);
88    }
89  
90    public void setRIT(int ritCount) {
91      ritGauge.set(ritCount);
92    }
93  
94    public void setRITCountOverThreshold(int ritCount) {
95      ritCountOverThresholdGauge.set(ritCount);
96    }
97  
98    public void setRITOldestAge(long ritCount) {
99      ritOldestAgeGauge.set(ritCount);
100   }
101 
102   @Override
103   public void updateSplitTime(long time) {
104     splitTimeHisto.add(time);
105   }
106 
107   @Override
108   public void updateSplitSize(long size) {
109     splitSizeHisto.add(size);
110   }
111 
112   @Override
113   public void updateSnapshotTime(long time) {
114     snapshotTimeHisto.add(time);
115   }
116 
117   @Override
118   public void updateSnapshotCloneTime(long time) {
119     snapshotCloneTimeHisto.add(time);
120   }
121 
122   @Override
123   public void updateSnapshotRestoreTime(long time) {
124     snapshotRestoreTimeHisto.add(time);
125   }
126 
127   @Override
128   public void updateMetaWALSplitTime(long time) {
129     metaSplitTimeHisto.add(time);
130   }
131 
132   @Override
133   public void updateMetaWALSplitSize(long size) {
134     metaSplitSizeHisto.add(size);
135   }
136 
137   /**
138    * Method to export all the metrics.
139    *
140    * @param metricsBuilder Builder to accept metrics
141    * @param all            push all or only changed?
142    */
143   @Override
144   public void getMetrics(MetricsBuilder metricsBuilder, boolean all) {
145 
146     MetricsRecordBuilder metricsRecordBuilder = metricsBuilder.addRecord(metricsName)
147         .setContext(metricsContext);
148 
149     // masterWrapper can be null because this function is called inside of init.
150     if (masterWrapper != null) {
151       metricsRecordBuilder
152           .addGauge(MASTER_ACTIVE_TIME_NAME,
153               MASTER_ACTIVE_TIME_DESC, masterWrapper.getActiveTime())
154           .addGauge(MASTER_START_TIME_NAME,
155               MASTER_START_TIME_DESC, masterWrapper.getStartTime())
156           .addGauge(AVERAGE_LOAD_NAME, AVERAGE_LOAD_DESC, masterWrapper.getAverageLoad())
157           .addGauge(NUM_REGION_SERVERS_NAME,
158               NUMBER_OF_REGION_SERVERS_DESC, masterWrapper.getRegionServers())
159           .addGauge(NUM_DEAD_REGION_SERVERS_NAME,
160               NUMBER_OF_DEAD_REGION_SERVERS_DESC,
161               masterWrapper.getDeadRegionServers())
162           .tag(ZOOKEEPER_QUORUM_NAME, ZOOKEEPER_QUORUM_DESC, masterWrapper.getZookeeperQuorum())
163           .tag(SERVER_NAME_NAME, SERVER_NAME_DESC, masterWrapper.getServerName())
164           .tag(CLUSTER_ID_NAME, CLUSTER_ID_DESC, masterWrapper.getClusterId())
165           .tag(IS_ACTIVE_MASTER_NAME,
166               IS_ACTIVE_MASTER_DESC,
167               String.valueOf(masterWrapper.getIsActiveMaster()));
168     }
169 
170     metricsRegistry.snapshot(metricsRecordBuilder, all);
171   }
172 
173 }