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