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.metrics2.lib;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.util.FastLongHistogram;
23  import org.apache.hadoop.metrics2.MetricHistogram;
24  import org.apache.hadoop.metrics2.MetricsInfo;
25  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
26  /**
27   * Extended histogram implementation with metric range counters.
28   */
29  @InterfaceAudience.Private
30  public abstract class MutableRangeHistogram extends MutableHistogram implements MetricHistogram {
31  
32    public MutableRangeHistogram(MetricsInfo info) {
33      this(info.name(), info.description());
34    }
35  
36    public MutableRangeHistogram(String name, String description) {
37      this(name, description, Integer.MAX_VALUE << 2);
38    }
39  
40    public MutableRangeHistogram(String name, String description, long expectedMax) {
41      super(name, description, expectedMax);
42    }
43  
44    /**
45     * Returns the type of range histogram size or time 
46     */
47    public abstract String getRangeType();
48    
49    /**
50     * Returns the ranges to be counted 
51     */
52    public abstract long[] getRanges();
53  
54    
55    @Override
56    public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
57      // Get a reference to the old histogram.
58      FastLongHistogram histo = histogram.reset();
59      if (histo != null) {
60        updateSnapshotMetrics(metricsRecordBuilder, histo);
61        updateSnapshotRangeMetrics(metricsRecordBuilder, histo);
62      }
63    }
64  
65    public void updateSnapshotRangeMetrics(MetricsRecordBuilder metricsRecordBuilder,
66                                           FastLongHistogram histogram) {
67      long priorRange = 0;
68      long cumNum = 0;
69  
70      final long[] ranges = getRanges();
71      final String rangeType = getRangeType();
72      for (int i = 0; i < ranges.length - 1; i++) {
73        long val = histogram.getNumAtOrBelow(ranges[i]);
74        if (val - cumNum > 0) {
75          metricsRecordBuilder.addCounter(
76              Interns.info(name + "_" + rangeType + "_" + priorRange + "-" + ranges[i], desc),
77              val - cumNum);
78        }
79        priorRange = ranges[i];
80        cumNum = val;
81      }
82      long val = histogram.getCount();
83      if (val - cumNum > 0) {
84        metricsRecordBuilder.addCounter(
85            Interns.info(name + "_" + rangeType + "_" + ranges[ranges.length - 1] + "-inf", desc),
86            val - cumNum);
87      }
88    }  
89  }