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