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.commons.lang.StringUtils;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.util.Counter;
24  import org.apache.hadoop.hbase.util.FastLongHistogram;
25  import org.apache.hadoop.metrics2.MetricHistogram;
26  import org.apache.hadoop.metrics2.MetricsInfo;
27  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
28  
29  /**
30   * A histogram implementation that runs in constant space, and exports to hadoop2's metrics2 system.
31   */
32  @InterfaceAudience.Private
33  public class MutableHistogram extends MutableMetric implements MetricHistogram {
34    // Double buffer the two FastLongHistograms.
35    // As they are reset they learn how the buckets should be spaced
36    // So keep two around and use them
37    protected final FastLongHistogram histogram;
38  
39    protected final String name;
40    protected final String desc;
41  
42    protected final Counter counter = new Counter(0);
43  
44    public MutableHistogram(MetricsInfo info) {
45      this(info.name(), info.description());
46    }
47  
48    public MutableHistogram(String name, String description) {
49      this(name, description, Integer.MAX_VALUE << 2);
50    }
51  
52    protected MutableHistogram(String name, String description, long maxExpected) {
53      this.name = StringUtils.capitalize(name);
54      this.desc = StringUtils.uncapitalize(description);
55  
56      this.histogram = new FastLongHistogram(FastLongHistogram.DEFAULT_NBINS, 1, maxExpected);
57    }
58  
59    public void add(final long val) {
60      counter.increment();
61      histogram.add(val, 1);
62    }
63  
64    @Override
65    public synchronized void snapshot(MetricsRecordBuilder metricsRecordBuilder, boolean all) {
66      // Get a reference to the old histogram.
67      FastLongHistogram histo = histogram.reset();
68      if (histo != null) {
69        updateSnapshotMetrics(metricsRecordBuilder, histo);
70      }
71    }
72  
73    protected void updateSnapshotMetrics(MetricsRecordBuilder metricsRecordBuilder,
74                                         FastLongHistogram histo) {
75      metricsRecordBuilder.addCounter(Interns.info(name + NUM_OPS_METRIC_NAME, desc), counter.get());
76      metricsRecordBuilder.addGauge(Interns.info(name + MIN_METRIC_NAME, desc), histo.getMin());
77      metricsRecordBuilder.addGauge(Interns.info(name + MAX_METRIC_NAME, desc), histo.getMax());
78      metricsRecordBuilder.addGauge(Interns.info(name + MEAN_METRIC_NAME, desc), histo.getMean());
79  
80      long[] percentiles = histo.getQuantiles();
81  
82      metricsRecordBuilder.addGauge(Interns.info(name + TWENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
83          percentiles[0]);
84      metricsRecordBuilder.addGauge(Interns.info(name + MEDIAN_METRIC_NAME, desc),
85          percentiles[1]);
86      metricsRecordBuilder.addGauge(Interns.info(name + SEVENTY_FIFTH_PERCENTILE_METRIC_NAME, desc),
87          percentiles[2]);
88      metricsRecordBuilder.addGauge(Interns.info(name + NINETIETH_PERCENTILE_METRIC_NAME, desc),
89          percentiles[3]);
90      metricsRecordBuilder.addGauge(Interns.info(name + NINETY_FIFTH_PERCENTILE_METRIC_NAME, desc),
91          percentiles[4]);
92      metricsRecordBuilder.addGauge(Interns.info(name + NINETY_EIGHTH_PERCENTILE_METRIC_NAME, desc),
93          percentiles[5]);
94      metricsRecordBuilder.addGauge(Interns.info(name + NINETY_NINETH_PERCENTILE_METRIC_NAME, desc),
95          percentiles[6]);
96      metricsRecordBuilder.addGauge(
97          Interns.info(name + NINETY_NINE_POINT_NINETH_PERCENTILE_METRIC_NAME, desc),
98          percentiles[7]);
99    }
100 }