001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import org.apache.hadoop.hbase.metrics.BaseSourceImpl;
021import org.apache.hadoop.hbase.metrics.OperationMetrics;
022import org.apache.hadoop.metrics2.MetricHistogram;
023import org.apache.hadoop.metrics2.MetricsCollector;
024import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025import org.apache.hadoop.metrics2.lib.MutableFastCounter;
026import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
027import org.apache.yetus.audience.InterfaceAudience;
028
029@InterfaceAudience.Private
030public class MetricsAssignmentManagerSourceImpl extends BaseSourceImpl
031  implements MetricsAssignmentManagerSource {
032
033  private MutableGaugeLong ritGauge;
034  private MutableGaugeLong ritCountOverThresholdGauge;
035  private MutableGaugeLong ritOldestAgeGauge;
036  private MetricHistogram ritDurationHisto;
037  private MutableGaugeLong deadServerOpenRegions;
038  private MutableGaugeLong unknownServerOpenRegions;
039
040  private MutableGaugeLong orphanRegionsOnRsGauge;
041  private MutableGaugeLong orphanRegionsOnFsGauge;
042  private MutableGaugeLong inconsistentRegionsGauge;
043
044  private MutableGaugeLong holesGauge;
045  private MutableGaugeLong overlapsGauge;
046  private MutableGaugeLong unknownServerRegionsGauge;
047  private MutableGaugeLong emptyRegionInfoRegionsGauge;
048
049  private MutableFastCounter operationCounter;
050
051  private OperationMetrics assignMetrics;
052  private OperationMetrics unassignMetrics;
053  private OperationMetrics moveMetrics;
054  private OperationMetrics reopenMetrics;
055  private OperationMetrics openMetrics;
056  private OperationMetrics closeMetrics;
057  private OperationMetrics splitMetrics;
058  private OperationMetrics mergeMetrics;
059
060  public MetricsAssignmentManagerSourceImpl() {
061    this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, METRICS_JMX_CONTEXT);
062  }
063
064  public MetricsAssignmentManagerSourceImpl(String metricsName, String metricsDescription,
065    String metricsContext, String metricsJmxContext) {
066    super(metricsName, metricsDescription, metricsContext, metricsJmxContext);
067  }
068
069  public void init() {
070    ritGauge = metricsRegistry.newGauge(RIT_COUNT_NAME, RIT_COUNT_DESC, 0L);
071    ritCountOverThresholdGauge =
072      metricsRegistry.newGauge(RIT_COUNT_OVER_THRESHOLD_NAME, RIT_COUNT_OVER_THRESHOLD_DESC, 0L);
073    ritOldestAgeGauge = metricsRegistry.newGauge(RIT_OLDEST_AGE_NAME, RIT_OLDEST_AGE_DESC, 0L);
074    ritDurationHisto = metricsRegistry.newTimeHistogram(RIT_DURATION_NAME, RIT_DURATION_DESC);
075    operationCounter = metricsRegistry.getCounter(OPERATION_COUNT_NAME, 0L);
076    deadServerOpenRegions = metricsRegistry.newGauge(DEAD_SERVER_OPEN_REGIONS, "", 0);
077    unknownServerOpenRegions = metricsRegistry.newGauge(UNKNOWN_SERVER_OPEN_REGIONS, "", 0);
078
079    orphanRegionsOnRsGauge =
080      metricsRegistry.newGauge(ORPHAN_REGIONS_ON_RS, ORPHAN_REGIONS_ON_RS_DESC, 0L);
081    orphanRegionsOnFsGauge =
082      metricsRegistry.newGauge(ORPHAN_REGIONS_ON_FS, ORPHAN_REGIONS_ON_FS_DESC, 0L);
083    inconsistentRegionsGauge =
084      metricsRegistry.newGauge(INCONSISTENT_REGIONS, INCONSISTENT_REGIONS_DESC, 0L);
085
086    holesGauge = metricsRegistry.newGauge(HOLES, HOLES_DESC, 0L);
087    overlapsGauge = metricsRegistry.newGauge(OVERLAPS, OVERLAPS_DESC, 0L);
088    unknownServerRegionsGauge =
089      metricsRegistry.newGauge(UNKNOWN_SERVER_REGIONS, UNKNOWN_SERVER_REGIONS_DESC, 0L);
090    emptyRegionInfoRegionsGauge =
091      metricsRegistry.newGauge(EMPTY_REGION_INFO_REGIONS, EMPTY_REGION_INFO_REGIONS_DESC, 0L);
092
093    /**
094     * NOTE: Please refer to HBASE-9774 and HBASE-14282. Based on these two issues, HBase is moving
095     * away from using Hadoop's metric2 to having independent HBase specific Metrics. Use
096     * {@link BaseSourceImpl#registry} to register the new metrics.
097     */
098    assignMetrics = new OperationMetrics(registry, ASSIGN_METRIC_PREFIX);
099    unassignMetrics = new OperationMetrics(registry, UNASSIGN_METRIC_PREFIX);
100    moveMetrics = new OperationMetrics(registry, MOVE_METRIC_PREFIX);
101    reopenMetrics = new OperationMetrics(registry, REOPEN_METRIC_PREFIX);
102    openMetrics = new OperationMetrics(registry, OPEN_METRIC_PREFIX);
103    closeMetrics = new OperationMetrics(registry, CLOSE_METRIC_PREFIX);
104    splitMetrics = new OperationMetrics(registry, SPLIT_METRIC_PREFIX);
105    mergeMetrics = new OperationMetrics(registry, MERGE_METRIC_PREFIX);
106  }
107
108  @Override
109  public void setRIT(final int ritCount) {
110    ritGauge.set(ritCount);
111  }
112
113  @Override
114  public void setRITCountOverThreshold(final int ritCount) {
115    ritCountOverThresholdGauge.set(ritCount);
116  }
117
118  @Override
119  public void setRITOldestAge(final long ritOldestAge) {
120    ritOldestAgeGauge.set(ritOldestAge);
121  }
122
123  @Override
124  public void incrementOperationCounter() {
125    operationCounter.incr();
126  }
127
128  @Override
129  public void updateRitDuration(long duration) {
130    ritDurationHisto.add(duration);
131  }
132
133  @Override
134  public void updateDeadServerOpenRegions(int deadRegions) {
135    deadServerOpenRegions.set(deadRegions);
136  }
137
138  @Override
139  public void updateUnknownServerOpenRegions(int unknownRegions) {
140    unknownServerOpenRegions.set(unknownRegions);
141  }
142
143  @Override
144  public void setOrphanRegionsOnRs(int orphanRegionsOnRs) {
145    orphanRegionsOnRsGauge.set(orphanRegionsOnRs);
146  }
147
148  @Override
149  public void setOrphanRegionsOnFs(int orphanRegionsOnFs) {
150    orphanRegionsOnFsGauge.set(orphanRegionsOnFs);
151  }
152
153  @Override
154  public void setInconsistentRegions(int inconsistentRegions) {
155    inconsistentRegionsGauge.set(inconsistentRegions);
156  }
157
158  @Override
159  public void setHoles(int holes) {
160    holesGauge.set(holes);
161  }
162
163  @Override
164  public void setOverlaps(int overlaps) {
165    overlapsGauge.set(overlaps);
166  }
167
168  @Override
169  public void setUnknownServerRegions(int unknownServerRegions) {
170    unknownServerRegionsGauge.set(unknownServerRegions);
171  }
172
173  @Override
174  public void setEmptyRegionInfoRegions(int emptyRegionInfoRegions) {
175    emptyRegionInfoRegionsGauge.set(emptyRegionInfoRegions);
176  }
177
178  @Override
179  public OperationMetrics getAssignMetrics() {
180    return assignMetrics;
181  }
182
183  @Override
184  public OperationMetrics getUnassignMetrics() {
185    return unassignMetrics;
186  }
187
188  @Override
189  public OperationMetrics getSplitMetrics() {
190    return splitMetrics;
191  }
192
193  @Override
194  public OperationMetrics getMergeMetrics() {
195    return mergeMetrics;
196  }
197
198  @Override
199  public OperationMetrics getMoveMetrics() {
200    return moveMetrics;
201  }
202
203  @Override
204  public OperationMetrics getReopenMetrics() {
205    return reopenMetrics;
206  }
207
208  @Override
209  public OperationMetrics getOpenMetrics() {
210    return openMetrics;
211  }
212
213  @Override
214  public OperationMetrics getCloseMetrics() {
215    return closeMetrics;
216  }
217
218  @Override
219  public void getMetrics(MetricsCollector metricsCollector, boolean all) {
220    MetricsRecordBuilder metricsRecordBuilder = metricsCollector.addRecord(metricsName);
221    metricsRegistry.snapshot(metricsRecordBuilder, all);
222    if (metricsAdapter != null) {
223      metricsAdapter.snapshotAllMetrics(registry, metricsRecordBuilder);
224    }
225  }
226}