View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.metrics.impl;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.metrics.Histogram;
25  import org.apache.hadoop.hbase.metrics.Snapshot;
26  
27  /**
28   * Custom histogram implementation based on FastLongHistogram. Dropwizard-based histograms are
29   * slow compared to this implementation, so we are using our implementation here.
30   * See HBASE-15222.
31   */
32  @InterfaceAudience.Private
33  public class HistogramImpl implements Histogram {
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    private final CounterImpl counter;
39  
40    public HistogramImpl() {
41      this((long) Integer.MAX_VALUE << 2);
42    }
43  
44    public HistogramImpl(long maxExpected) {
45      this(FastLongHistogram.DEFAULT_NBINS, 1, maxExpected);
46    }
47  
48    public HistogramImpl(int numBins, long min, long maxExpected) {
49      this.counter = new CounterImpl();
50      this.histogram = new FastLongHistogram(numBins, min, maxExpected);
51    }
52  
53    protected HistogramImpl(CounterImpl counter, FastLongHistogram histogram) {
54      this.counter = counter;
55      this.histogram = histogram;
56    }
57  
58    @Override
59    public void update(int value) {
60      counter.increment();
61      histogram.add(value, 1);
62    }
63  
64    @Override
65    public void update(long value) {
66      counter.increment();
67      histogram.add(value, 1);
68    }
69  
70    @Override
71    public long getCount() {
72      return counter.getCount();
73    }
74  
75    public long getMax() {
76      return this.histogram.getMax();
77    }
78  
79    @Override
80    public Snapshot snapshot() {
81      return histogram.snapshotAndReset();
82    }
83  }