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.metrics.Snapshot;
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      super(name, description);
39    }
40  
41    /**
42     * Returns the type of range histogram size or time 
43     */
44    public abstract String getRangeType();
45    
46    /**
47     * Returns the ranges to be counted 
48     */
49    public abstract long[] getRanges();
50  
51    
52    @Override
53    public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
54      // Get a reference to the old histogram.
55      Snapshot snapshot = histogram.snapshot();
56      if (snapshot != null) {
57        updateSnapshotMetrics(name, desc, histogram, snapshot, metricsRecordBuilder);
58        updateSnapshotRangeMetrics(metricsRecordBuilder, snapshot);
59      }
60    }
61  
62    public void updateSnapshotRangeMetrics(MetricsRecordBuilder metricsRecordBuilder,
63                                           Snapshot snapshot) {
64      long priorRange = 0;
65      long cumNum = 0;
66  
67      final long[] ranges = getRanges();
68      final String rangeType = getRangeType();
69      for (int i = 0; i < ranges.length; i++) {
70        long val = snapshot.getCountAtOrBelow(ranges[i]);
71        if (val - cumNum > 0) {
72          metricsRecordBuilder.addCounter(
73              Interns.info(name + "_" + rangeType + "_" + priorRange + "-" + ranges[i], desc),
74              val - cumNum);
75        }
76        priorRange = ranges[i];
77        cumNum = val;
78      }
79      long val = snapshot.getCount();
80      if (val - cumNum > 0) {
81        metricsRecordBuilder.addCounter(
82            Interns.info(name + "_" + rangeType + "_" + priorRange + "-inf", desc),
83            val - cumNum);
84      }
85    }  
86  }