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