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