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 java.util.concurrent.atomic.AtomicLongArray;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
25  
26  /**
27   * Extended histogram implementation with metric range counters.
28   */
29  @InterfaceAudience.Private
30  public abstract class MetricMutableRangeHistogram extends MetricMutableHistogram {
31    
32    public MetricMutableRangeHistogram(String name, String description) {
33      super(name, description);    
34    }
35    
36    /**
37     * Returns the type of range histogram size or time 
38     */
39    public abstract String getRangeType();
40    
41    /**
42     * Returns the ranges to be counted 
43     */
44    public abstract long[] getRange();
45    
46    /**
47     * Returns the range counts 
48     */
49    public abstract AtomicLongArray getRangeVals();
50  
51    @Override
52    public void add(final long val) {
53      super.add(val);
54      updateBand(val);
55    }
56  
57    private void updateBand(final long val) {
58      int i;
59      for (i=0; i<getRange().length && val > getRange()[i]; i++);
60      getRangeVals().incrementAndGet(i);
61    }
62    
63    @Override
64    public void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
65      if (all || changed()) {
66        clearChanged();
67        updateSnapshotMetrics(metricsRecordBuilder);
68        updateSnapshotRangeMetrics(metricsRecordBuilder);
69      }
70    }
71    
72    public void updateSnapshotRangeMetrics(MetricsRecordBuilder metricsRecordBuilder) {
73      long prior = 0;
74      for (int i = 0; i < getRange().length; i++) {
75        long val = getRangeVals().get(i);
76        if (val > 0) {
77          metricsRecordBuilder
78              .addCounter(name + "_" + getRangeType() + "_" + prior + "-" + getRange()[i], "", val);
79        }
80        prior = getRange()[i];
81      }
82      long val = getRangeVals().get(getRange().length);
83      if (val > 0) {
84        metricsRecordBuilder.addCounter(
85          name + "_" + getRangeType() + "_" + getRange()[getRange().length - 1] + "-inf", "",
86          getRangeVals().get(getRange().length));
87      }
88    }  
89  }