|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.common.metrics; |
| 11 | + |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.concurrent.atomic.LongAdder; |
| 14 | + |
| 15 | +/** |
| 16 | + * A histogram with a fixed number of buckets of exponentially increasing width. |
| 17 | + * <p> |
| 18 | + * The bucket boundaries are defined by increasing powers of two, e.g. |
| 19 | + * <code> |
| 20 | + * (-∞, 1), [1, 2), [2, 4), [4, 8), ..., [2^({@link #bucketCount}-2), ∞) |
| 21 | + * </code> |
| 22 | + * There are {@link #bucketCount} buckets. |
| 23 | + */ |
| 24 | +public class ExponentialBucketHistogram { |
| 25 | + |
| 26 | + private final int bucketCount; |
| 27 | + private final long lastBucketLowerBound; |
| 28 | + |
| 29 | + public static int[] getBucketUpperBounds(int bucketCount) { |
| 30 | + int[] bounds = new int[bucketCount - 1]; |
| 31 | + for (int i = 0; i < bounds.length; i++) { |
| 32 | + bounds[i] = 1 << i; |
| 33 | + } |
| 34 | + return bounds; |
| 35 | + } |
| 36 | + |
| 37 | + private int getBucket(long observedValue) { |
| 38 | + if (observedValue <= 0) { |
| 39 | + return 0; |
| 40 | + } else if (lastBucketLowerBound <= observedValue) { |
| 41 | + return bucketCount - 1; |
| 42 | + } else { |
| 43 | + return Long.SIZE - Long.numberOfLeadingZeros(observedValue); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private final LongAdder[] buckets; |
| 48 | + |
| 49 | + public ExponentialBucketHistogram(int bucketCount) { |
| 50 | + if (bucketCount < 2 || bucketCount > Integer.SIZE) { |
| 51 | + throw new IllegalArgumentException("Bucket count must be in [2, " + Integer.SIZE + "], got " + bucketCount); |
| 52 | + } |
| 53 | + this.bucketCount = bucketCount; |
| 54 | + this.lastBucketLowerBound = getBucketUpperBounds(bucketCount)[bucketCount - 2]; |
| 55 | + buckets = new LongAdder[bucketCount]; |
| 56 | + for (int i = 0; i < bucketCount; i++) { |
| 57 | + buckets[i] = new LongAdder(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public int[] calculateBucketUpperBounds() { |
| 62 | + return getBucketUpperBounds(bucketCount); |
| 63 | + } |
| 64 | + |
| 65 | + public void addObservation(long observedValue) { |
| 66 | + buckets[getBucket(observedValue)].increment(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return An array of frequencies of handling times in buckets with upper bounds as returned by {@link #calculateBucketUpperBounds()}, |
| 71 | + * plus an extra bucket for handling times longer than the longest upper bound. |
| 72 | + */ |
| 73 | + public long[] getSnapshot() { |
| 74 | + final long[] histogram = new long[bucketCount]; |
| 75 | + for (int i = 0; i < bucketCount; i++) { |
| 76 | + histogram[i] = buckets[i].longValue(); |
| 77 | + } |
| 78 | + return histogram; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Calculate the Nth percentile value |
| 83 | + * |
| 84 | + * @param percentile The percentile as a fraction (in [0, 1.0]) |
| 85 | + * @return A value greater than the specified fraction of values in the histogram |
| 86 | + * @throws IllegalArgumentException if the requested percentile is invalid |
| 87 | + */ |
| 88 | + public long getPercentile(float percentile) { |
| 89 | + return getPercentile(percentile, getSnapshot(), calculateBucketUpperBounds()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Calculate the Nth percentile value |
| 94 | + * |
| 95 | + * @param percentile The percentile as a fraction (in [0, 1.0]) |
| 96 | + * @param snapshot An array of frequencies of handling times in buckets with upper bounds as per {@link #calculateBucketUpperBounds()} |
| 97 | + * @param bucketUpperBounds The upper bounds of the buckets in the histogram, as per {@link #calculateBucketUpperBounds()} |
| 98 | + * @return A value greater than the specified fraction of values in the histogram |
| 99 | + * @throws IllegalArgumentException if the requested percentile is invalid |
| 100 | + */ |
| 101 | + public long getPercentile(float percentile, long[] snapshot, int[] bucketUpperBounds) { |
| 102 | + assert snapshot.length == bucketCount && bucketUpperBounds.length == bucketCount - 1; |
| 103 | + if (percentile < 0 || percentile > 1) { |
| 104 | + throw new IllegalArgumentException("Requested percentile must be in [0, 1.0], percentile=" + percentile); |
| 105 | + } |
| 106 | + final long totalCount = Arrays.stream(snapshot).sum(); |
| 107 | + long percentileIndex = (long) Math.ceil(totalCount * percentile); |
| 108 | + // Find which bucket has the Nth percentile value and return the upper bound value. |
| 109 | + for (int i = 0; i < bucketCount; i++) { |
| 110 | + percentileIndex -= snapshot[i]; |
| 111 | + if (percentileIndex <= 0) { |
| 112 | + if (i == snapshot.length - 1) { |
| 113 | + return Long.MAX_VALUE; |
| 114 | + } else { |
| 115 | + return bucketUpperBounds[i]; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + assert false : "We shouldn't ever get here"; |
| 120 | + return Long.MAX_VALUE; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Clear all values in the histogram (non-atomic) |
| 125 | + */ |
| 126 | + public void clear() { |
| 127 | + for (int i = 0; i < bucketCount; i++) { |
| 128 | + buckets[i].reset(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments