001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.metrics.impl;
020
021import org.apache.hadoop.hbase.metrics.Histogram;
022import org.apache.hadoop.hbase.metrics.Snapshot;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Custom histogram implementation based on FastLongHistogram. Dropwizard-based histograms are
027 * slow compared to this implementation, so we are using our implementation here.
028 * See HBASE-15222.
029 */
030@InterfaceAudience.Private
031public class HistogramImpl implements Histogram {
032  // Double buffer the two FastLongHistograms.
033  // As they are reset they learn how the buckets should be spaced
034  // So keep two around and use them
035  protected final FastLongHistogram histogram;
036  private final CounterImpl counter;
037
038  public HistogramImpl() {
039    this(Integer.MAX_VALUE << 2);
040  }
041
042  public HistogramImpl(long maxExpected) {
043    this(FastLongHistogram.DEFAULT_NBINS, 1, maxExpected);
044  }
045
046  public HistogramImpl(int numBins, long min, long maxExpected) {
047    this.counter = new CounterImpl();
048    this.histogram = new FastLongHistogram(numBins, min, maxExpected);
049  }
050
051  protected HistogramImpl(CounterImpl counter, FastLongHistogram histogram) {
052    this.counter = counter;
053    this.histogram = histogram;
054  }
055
056  @Override
057  public void update(int value) {
058    counter.increment();
059    histogram.add(value, 1);
060  }
061
062  @Override
063  public void update(long value) {
064    counter.increment();
065    histogram.add(value, 1);
066  }
067
068  @Override
069  public long getCount() {
070    return counter.getCount();
071  }
072
073  public long getMax() {
074    return this.histogram.getMax();
075  }
076
077  @Override
078  public Snapshot snapshot() {
079    return histogram.snapshotAndReset();
080  }
081}