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