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.metrics2.lib;
019
020import org.apache.hadoop.hbase.metrics.Interns;
021import org.apache.hadoop.hbase.metrics.Snapshot;
022import org.apache.hadoop.metrics2.MetricHistogram;
023import org.apache.hadoop.metrics2.MetricsInfo;
024import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * Extended histogram implementation with metric range counters.
029 */
030@InterfaceAudience.Private
031public abstract class MutableRangeHistogram extends MutableHistogram implements MetricHistogram {
032
033  public MutableRangeHistogram(MetricsInfo info) {
034    this(info.name(), info.description());
035  }
036
037  public MutableRangeHistogram(String name, String description) {
038    super(name, description);
039  }
040
041  /**
042   * Returns the type of range histogram size or time
043   */
044  public abstract String getRangeType();
045
046  /**
047   * Returns the ranges to be counted
048   */
049  public abstract long[] getRanges();
050
051  @Override
052  public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
053    // Get a reference to the old histogram.
054    Snapshot snapshot = histogram.snapshot();
055    if (snapshot != null) {
056      updateSnapshotMetrics(name, desc, histogram, snapshot, metricsRecordBuilder);
057      updateSnapshotRangeMetrics(metricsRecordBuilder, snapshot);
058    }
059  }
060
061  public void updateSnapshotRangeMetrics(MetricsRecordBuilder metricsRecordBuilder,
062    Snapshot snapshot) {
063    long priorRange = 0;
064    long cumNum = 0;
065
066    final long[] ranges = getRanges();
067    final String rangeType = getRangeType();
068    for (int i = 0; i < ranges.length; i++) {
069      long val = snapshot.getCountAtOrBelow(ranges[i]);
070      if (val - cumNum > 0) {
071        metricsRecordBuilder.addCounter(
072          Interns.info(name + "_" + rangeType + "_" + priorRange + "-" + ranges[i], desc),
073          val - cumNum);
074      }
075      priorRange = ranges[i];
076      cumNum = val;
077    }
078    long val = snapshot.getCount();
079    if (val - cumNum > 0) {
080      metricsRecordBuilder.addCounter(
081        Interns.info(name + "_" + rangeType + "_" + priorRange + "-inf", desc), val - cumNum);
082    }
083  }
084
085  @Override
086  public long getCount() {
087    return histogram.getCount();
088  }
089}