-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add sum to exponential histograms #133381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
140438c
8c6c002
3f8d3c3
611a7da
cde5e65
5a9e788
91067c7
3d1294f
f92ec9e
90e2338
7899f0c
5f94454
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V. | ||
| * under one or more license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| * This file is based on a modification of https://github.com/open-telemetry/opentelemetry-java which is licensed under the Apache 2.0 License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.exponentialhistogram; | ||
|
|
||
| public class ExponentialHistogramUtils { | ||
|
|
||
| /** | ||
| * Estimates the sum of all values of a histogram just based on the populated buckets. | ||
| * Will never return NaN, but might return +/-Infinity if the histogram is too big. | ||
| * | ||
| * @param negativeBuckets the negative buckets of the histogram | ||
| * @param positiveBuckets the positive buckets of the histogram | ||
| * @return the estimated sum of all values in the histogram, guaranteed to be zero if there are no buckets. | ||
| */ | ||
| public static double estimateSum(BucketIterator negativeBuckets, BucketIterator positiveBuckets) { | ||
| assert negativeBuckets.scale() == positiveBuckets.scale(); | ||
|
|
||
| // for each bucket index, sum up the counts, but account for the positive/negative sign | ||
| BucketIterator it = new MergingBucketIterator(negativeBuckets, -1, positiveBuckets, 1, positiveBuckets.scale()); | ||
| double sum = 0.0; | ||
| while (it.hasNext()) { | ||
| long countWithSign = it.peekCount(); | ||
| double bucketMidPoint = ExponentialScaleUtils.getPointOfLeastRelativeError(it.peekIndex(), it.scale()); | ||
| if (countWithSign != 0) { // avoid 0 * INFINITY = NaN | ||
| double toAdd = bucketMidPoint * countWithSign; | ||
| if (Double.isFinite(toAdd)) { | ||
| sum += toAdd; | ||
felixbarny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| // Avoid NaN in case we end up with e.g. -Infinity+Infinity | ||
| // we consider the bucket with the bigger index the winner for the sign | ||
| sum = toAdd; | ||
| } | ||
| } | ||
| it.advance(); | ||
| } | ||
| return sum; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,8 @@ final class FixedCapacityExponentialHistogram implements ReleasableExponentialHi | |
|
|
||
| private final Buckets positiveBuckets = new Buckets(true); | ||
|
|
||
| private double sum; | ||
|
|
||
| private final ExponentialHistogramCircuitBreaker circuitBreaker; | ||
| private boolean closed = false; | ||
|
|
||
|
|
@@ -78,6 +80,7 @@ private FixedCapacityExponentialHistogram(int bucketCapacity, ExponentialHistogr | |
| * Resets this histogram to the same state as a newly constructed one with the same capacity. | ||
| */ | ||
| void reset() { | ||
| sum = 0; | ||
| setZeroBucket(ZeroBucket.minimalEmpty()); | ||
| resetBuckets(MAX_SCALE); | ||
| } | ||
|
|
@@ -110,6 +113,15 @@ void setZeroBucket(ZeroBucket zeroBucket) { | |
| this.zeroBucket = zeroBucket; | ||
| } | ||
|
|
||
| @Override | ||
| public double sum() { | ||
| return sum; | ||
| } | ||
|
|
||
| void setSum(double sum) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only exposed for testing? If so, let's add a comment to call it out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, we try to avoid recalculating in merging. Sounds good - I don't know how I feel about not validating the passed value but it can be expensive and tricky to do once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be sufficient for us to do the validations required on ingestion and trust the values to be sane internally. |
||
| this.sum = sum; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to add a bucket to the positive or negative range of this histogram. | ||
| * <br> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V., and/or licensed to Elasticsearch B.V. | ||
| * under one or more license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| * This file is based on a modification of https://github.com/open-telemetry/opentelemetry-java which is licensed under the Apache 2.0 License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.exponentialhistogram; | ||
|
|
||
| import static org.hamcrest.Matchers.closeTo; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class ExponentialHistogramUtilsTests extends ExponentialHistogramTestCase { | ||
|
|
||
| public void testRandomDataSumEstimation() { | ||
| for (int i = 0; i < 100; i++) { | ||
| int valueCount = randomIntBetween(100, 10_000); | ||
| int bucketCount = randomIntBetween(2, 500); | ||
|
|
||
| double correctSum = 0; | ||
| double sign = randomBoolean() ? 1 : -1; | ||
| double[] values = new double[valueCount]; | ||
| for (int j = 0; j < valueCount; j++) { | ||
| values[j] = sign * Math.pow(10, randomIntBetween(1, 9)) * randomDouble(); | ||
| correctSum += values[j]; | ||
| } | ||
|
|
||
| ExponentialHistogram histo = createAutoReleasedHistogram(bucketCount, values); | ||
|
|
||
| double estimatedSum = ExponentialHistogramUtils.estimateSum( | ||
| histo.negativeBuckets().iterator(), | ||
| histo.positiveBuckets().iterator() | ||
| ); | ||
|
|
||
| double correctAverage = correctSum / valueCount; | ||
| double estimatedAverage = estimatedSum / valueCount; | ||
|
|
||
| // If the histogram does not contain mixed sign values, we have a guaranteed relative error bound of 2^(2^-scale) - 1 | ||
| double histogramBase = Math.pow(2, Math.pow(2, -histo.scale())); | ||
| double allowedError = Math.abs(correctAverage * (histogramBase - 1)); | ||
|
|
||
| assertThat(estimatedAverage, closeTo(correctAverage, allowedError)); | ||
| } | ||
| } | ||
|
|
||
| public void testInfinityHandling() { | ||
| FixedCapacityExponentialHistogram morePositiveValues = createAutoReleasedHistogram(100); | ||
| morePositiveValues.resetBuckets(0); | ||
| morePositiveValues.tryAddBucket(1999, 1, false); | ||
| morePositiveValues.tryAddBucket(2000, 2, false); | ||
| morePositiveValues.tryAddBucket(1999, 2, true); | ||
| morePositiveValues.tryAddBucket(2000, 2, true); | ||
|
|
||
| double sum = ExponentialHistogramUtils.estimateSum( | ||
| morePositiveValues.negativeBuckets().iterator(), | ||
| morePositiveValues.positiveBuckets().iterator() | ||
| ); | ||
| assertThat(sum, equalTo(Double.POSITIVE_INFINITY)); | ||
| FixedCapacityExponentialHistogram moreNegativeValues = createAutoReleasedHistogram(100); | ||
| moreNegativeValues.resetBuckets(0); | ||
| moreNegativeValues.tryAddBucket(1999, 2, false); | ||
| moreNegativeValues.tryAddBucket(2000, 2, false); | ||
| moreNegativeValues.tryAddBucket(1999, 1, true); | ||
| moreNegativeValues.tryAddBucket(2000, 2, true); | ||
|
|
||
| sum = ExponentialHistogramUtils.estimateSum( | ||
| moreNegativeValues.negativeBuckets().iterator(), | ||
| moreNegativeValues.positiveBuckets().iterator() | ||
| ); | ||
| assertThat(sum, equalTo(Double.NEGATIVE_INFINITY)); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not very excited about the negative counts - what are the semantics? I'd rather we have a utility function that's called on each iterator that internally multiplies the sum with
-1for negative buckets.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In 5f94454 I've replaced the "factors" with the ability to provide a custom operator to do the count merging.
Is that what you were thinking of?