Skip to content

Commit

Permalink
HBASE-15222 Use less contended classes for metrics
Browse files Browse the repository at this point in the history
Summary:
Use less contended things for metrics.
For histogram which was the largest culprit we use FastLongHistogram
For atomic long where possible we now use counter.

Test Plan: unit tests

Reviewers:

Subscribers:

Differential Revision: https://reviews.facebook.net/D54381
  • Loading branch information
elliottneilclark committed Feb 24, 2016
1 parent 20e14f4 commit 630a658
Show file tree
Hide file tree
Showing 40 changed files with 565 additions and 702 deletions.
Expand Up @@ -19,7 +19,6 @@

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;

import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.classification.InterfaceStability;
Expand All @@ -31,36 +30,50 @@
@InterfaceAudience.Public
@InterfaceStability.Evolving
public class FastLongHistogram {

/**
* Default number of bins.
*/
public static final int DEFAULT_NBINS = 255;

public static final double[] DEFAULT_QUANTILES =
new double[]{0.25, 0.5, 0.75, 0.90, 0.95, 0.98, 0.99, 0.999};

/**
* Bins is a class containing a list of buckets(or bins) for estimation histogram of some data.
*/
private static class Bins {
private final AtomicLongArray counts;
private final Counter[] counts;
// inclusive
private final long binsMin;
// exclusive
private final long binsMax;
private final long bins10XMax;
private final AtomicLong min = new AtomicLong(Long.MAX_VALUE);
private final AtomicLong max = new AtomicLong(0L);

private final Counter count = new Counter(0);
private final Counter total = new Counter(0);

// set to true when any of data has been inserted to the Bins. It is set after the counts are
// updated.
private final AtomicBoolean hasData = new AtomicBoolean(false);

/**
* The constructor for creating a Bins without any prior data.
*/
public Bins() {
this.counts = new AtomicLongArray(4);
this.binsMin = 0L;
this.binsMax = Long.MAX_VALUE;
this.bins10XMax = Long.MAX_VALUE;
public Bins(int numBins) {
counts = createCounters(numBins + 3);
this.binsMin = 1L;

// These two numbers are total guesses
// and should be treated as highly suspect.
this.binsMax = 1000;
this.bins10XMax = binsMax * 10;
}

/**
* The constructor for creating a Bins with last Bins.
* @param last the last Bins instance.
* @param quantiles the quantiles for creating the bins of the histogram.
*/
public Bins(Bins last, int numOfBins, double minQ, double maxQ) {
long[] values = last.getQuantiles(new double[] { minQ, maxQ });
Expand All @@ -72,36 +85,58 @@ public Bins(Bins last, int numOfBins, double minQ, double maxQ) {
this.binsMax = Math.max(binsMax, this.binsMin + numOfBins);
this.bins10XMax = Math.max((long) (values[1] + (binsMax - 1) * 9), this.binsMax + 1);

this.counts = new AtomicLongArray(numOfBins + 3);
this.counts = createCounters(numOfBins + 3);
}

private Counter[] createCounters(int num) {
Counter[] counters = new Counter[num];
for (int i = 0; i < num; i++) {
counters[i] = new Counter();
}
return counters;
}

private int getIndex(long value) {
if (value < this.binsMin) {
return 0;
} else if (value > this.bins10XMax) {
return this.counts.length - 1;
} else if (value >= this.binsMax) {
return this.counts.length - 2;
}
// compute the position
return 1 + (int) ((value - this.binsMin) * (this.counts.length - 3) /
(this.binsMax - this.binsMin));

}

/**
* Adds a value to the histogram.
*/
public void add(long value, long count) {
if (value < 0) {
// The whole computation is completely thrown off if there are negative numbers
//
// Normally we would throw an IllegalArgumentException however this is the metrics
// system and it should be completely safe at all times.
// So silently throw it away.
return;
}
AtomicUtils.updateMin(min, value);
AtomicUtils.updateMax(max, value);

if (value < this.binsMin) {
this.counts.addAndGet(0, count);
} else if (value > this.bins10XMax) {
this.counts.addAndGet(this.counts.length() - 1, count);
} else if (value >= this.binsMax) {
this.counts.addAndGet(this.counts.length() - 2, count);
} else {
// compute the position
int pos =
1 + (int) ((value - this.binsMin) * (this.counts.length() - 3) / (this.binsMax - this.binsMin));
this.counts.addAndGet(pos, count);
}
this.count.add(count);
this.total.add(value * count);

int pos = getIndex(value);
this.counts[pos].add(count);

// hasData needs to be updated as last
this.hasData.set(true);
}

/**
* Computes the quantiles give the ratios.
* @param smooth set to true to have a prior on the distribution. Used for recreating the bins.
*/
public long[] getQuantiles(double[] quantiles) {
if (!this.hasData.get()) {
Expand All @@ -112,10 +147,10 @@ public long[] getQuantiles(double[] quantiles) {
// Make a snapshot of lowerCounter, higherCounter and bins.counts to counts.
// This is not synchronized, but since the counter are accumulating, the result is a good
// estimation of a snapshot.
long[] counts = new long[this.counts.length()];
long[] counts = new long[this.counts.length];
long total = 0L;
for (int i = 0; i < this.counts.length(); i++) {
counts[i] = this.counts.get(i);
for (int i = 0; i < this.counts.length; i++) {
counts[i] = this.counts[i].get();
total += counts[i];
}

Expand All @@ -137,8 +172,8 @@ public long[] getQuantiles(double[] quantiles) {
mn = this.binsMax;
mx = this.bins10XMax;
} else {
mn = this.binsMin + (i - 1) * (this.binsMax - this.binsMin) / (this.counts.length() - 3);
mx = this.binsMin + i * (this.binsMax - this.binsMin) / (this.counts.length() - 3);
mn = this.binsMin + (i - 1) * (this.binsMax - this.binsMin) / (this.counts.length - 3);
mx = this.binsMin + i * (this.binsMax - this.binsMin) / (this.counts.length - 3);
}

if (mx < this.min.get()) {
Expand Down Expand Up @@ -177,20 +212,35 @@ public long[] getQuantiles(double[] quantiles) {

return res;
}


long getNumAtOrBelow(long val) {
final int targetIndex = getIndex(val);
long totalToCurrentIndex = 0;
for (int i = 0; i <= targetIndex; i++) {
totalToCurrentIndex += this.counts[i].get();
}
return totalToCurrentIndex;
}
}

// The bins counting values. It is replaced with a new one in calling of reset().
private volatile Bins bins = new Bins();
// The quantiles for creating a Bins with last Bins.
private final int numOfBins;
private volatile Bins bins;

/**
* Constructor.
*/
public FastLongHistogram() {
this(DEFAULT_NBINS);
}

/**
* Constructor.
* @param numOfBins the number of bins for the histogram. A larger value results in more precise
* results but with lower efficiency, and vice versus.
*/
public FastLongHistogram(int numOfBins) {
this.numOfBins = numOfBins;
this.bins = new Bins(numOfBins);
}

/**
Expand All @@ -202,10 +252,14 @@ public FastLongHistogram(int numOfBins) {
*/
public FastLongHistogram(int numOfBins, long min, long max) {
this(numOfBins);
Bins bins = new Bins();
Bins bins = new Bins(numOfBins);
bins.add(min, 1);
bins.add(max, 1);
this.bins = new Bins(bins, numOfBins, 0.01, 0.99);
this.bins = new Bins(bins, numOfBins, 0.01, 0.999);
}

private FastLongHistogram(Bins bins) {
this.bins = bins;
}

/**
Expand All @@ -222,12 +276,46 @@ public long[] getQuantiles(double[] quantiles) {
return this.bins.getQuantiles(quantiles);
}

public long[] getQuantiles() {
return this.bins.getQuantiles(DEFAULT_QUANTILES);
}

public long getMin() {
return this.bins.min.get();
}

public long getMax() {
return this.bins.max.get();
}

public long getCount() {
return this.bins.count.get();
}

public long getMean() {
Bins bins = this.bins;
long count = bins.count.get();
long total = bins.total.get();
if (count == 0) {
return 0;
}
return total / count;
}

public long getNumAtOrBelow(long value) {
return this.bins.getNumAtOrBelow(value);
}

/**
* Resets the histogram for new counting.
*/
public void reset() {
public FastLongHistogram reset() {
if (this.bins.hasData.get()) {
this.bins = new Bins(this.bins, numOfBins, 0.01, 0.99);
Bins oldBins = this.bins;
this.bins = new Bins(this.bins, this.bins.counts.length - 3, 0.01, 0.99);
return new FastLongHistogram(oldBins);
}

return null;
}
}
Expand Up @@ -26,6 +26,8 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.assertEquals;

/**
* Testcases for FastLongHistogram.
*/
Expand Down Expand Up @@ -88,6 +90,36 @@ public void testAdaptionOfChange() {
}
}


@Test
public void testGetNumAtOrBelow() {
long[] VALUES = { 1, 10, 20, 30, 40, 50 };

FastLongHistogram h = new FastLongHistogram();
for (long v : VALUES) {
for (int i = 0; i < 100; i++) {
h.add(v, 1);
}
}

h.add(Integer.MAX_VALUE, 1);

h.reset();

for (long v : VALUES) {
for (int i = 0; i < 100; i++) {
h.add(v, 1);
}
}
// Add something way out there to make sure it doesn't throw off the counts.
h.add(Integer.MAX_VALUE, 1);

assertEquals(100, h.getNumAtOrBelow(1));
assertEquals(200, h.getNumAtOrBelow(11));
assertEquals(601, h.getNumAtOrBelow(Long.MAX_VALUE));
}


@Test
public void testSameValues() {
FastLongHistogram hist = new FastLongHistogram(100);
Expand Down
Expand Up @@ -79,14 +79,6 @@ public interface BaseSource {
void updateHistogram(String name, long value);


/**
* Add some value to a Quantile (An accurate histogram).
*
* @param name the name of the quantile
* @param value the value to add to the quantile
*/
void updateQuantile(String name, long value);

/**
* Get the metrics context. For hadoop metrics2 system this is usually an all lowercased string.
* eg. regionserver, master, thriftserver
Expand Down
Expand Up @@ -30,10 +30,13 @@ public interface MetricHistogram {
String MAX_METRIC_NAME = "_max";
String MEAN_METRIC_NAME = "_mean";
String MEDIAN_METRIC_NAME = "_median";
String TWENTY_FIFTH_PERCENTILE_METRIC_NAME = "_25th_percentile";
String SEVENTY_FIFTH_PERCENTILE_METRIC_NAME = "_75th_percentile";
String NINETIETH_PERCENTILE_METRIC_NAME = "_90th_percentile";
String NINETY_FIFTH_PERCENTILE_METRIC_NAME = "_95th_percentile";
String NINETY_EIGHTH_PERCENTILE_METRIC_NAME = "_98th_percentile";
String NINETY_NINETH_PERCENTILE_METRIC_NAME = "_99th_percentile";
String NINETY_NINE_POINT_NINETH_PERCENTILE_METRIC_NAME = "_99.9th_percentile";

/**
* Add a single value to a histogram's stream of values.
Expand Down
8 changes: 4 additions & 4 deletions hbase-hadoop2-compat/pom.xml
Expand Up @@ -160,6 +160,10 @@ limitations under the License.
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-hadoop-compat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-hadoop-compat</artifactId>
Expand All @@ -181,10 +185,6 @@ limitations under the License.
<artifactId>hadoop-common</artifactId>
<version>${hadoop-two.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down

0 comments on commit 630a658

Please sign in to comment.