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