-
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
140438c
Add sum to exponential histograms
JonasKunz 8c6c002
Add missing test case
JonasKunz 3f8d3c3
[CI] Auto commit changes from spotless
611a7da
Merge branch 'main' into histo-sum
JonasKunz cde5e65
Fix and test infinity handling
JonasKunz 5a9e788
Fix benchmark
JonasKunz 91067c7
Avoid NaN in sum computation
JonasKunz 3d1294f
[CI] Auto commit changes from spotless
f92ec9e
Refactor sum computation to reuse MergingBucketIterator
JonasKunz 90e2338
Merge branch 'main' into histo-sum
JonasKunz 7899f0c
wording fix
JonasKunz 5f94454
Replace factors with custom merge operator
JonasKunz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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. | ||
| * | ||
| * @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) { | ||
| double sum = 0.0; | ||
| while (negativeBuckets.hasNext()) { | ||
| double bucketMidPoint = ExponentialScaleUtils.getPointOfLeastRelativeError( | ||
| negativeBuckets.peekIndex(), | ||
| negativeBuckets.scale() | ||
| ); | ||
| sum += -bucketMidPoint * negativeBuckets.peekCount(); | ||
| negativeBuckets.advance(); | ||
| } | ||
| while (positiveBuckets.hasNext()) { | ||
| double bucketMidPoint = ExponentialScaleUtils.getPointOfLeastRelativeError( | ||
| positiveBuckets.peekIndex(), | ||
| positiveBuckets.scale() | ||
| ); | ||
| sum += bucketMidPoint * positiveBuckets.peekCount(); | ||
| positiveBuckets.advance(); | ||
| } | ||
| return sum; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
.../src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramUtilsTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| 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)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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 comment
The 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.
Also we don't just avoid recalculating while merging for performance reasons: The calculation we have is just an estimation. User can instead provide the exact sum on ingestion, which means we'll preserve exactness when merging, giving exact averages. In OTLP, the sum is provided by default.