-
Notifications
You must be signed in to change notification settings - Fork 817
Query-tee: added a small tolerance to floating point sample values comparison #2994
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
Query-tee: added a small tolerance to floating point sample values comparison #2994
Conversation
…mparison Signed-off-by: Marco Pracucci <[email protected]>
Signed-off-by: Marco Pracucci <[email protected]>
|
||
func compareSampleValue(first, second model.SampleValue, tolerance float64) bool { | ||
if tolerance <= 0 { | ||
return first == second |
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.
It's not safe to do equality comparisons on floats due to NaNs, use math.Float64bits
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.
Thanks for spotting it! The NaN comparison is also wrong when the tolerance is enabled (> 0).
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.
👍
We sometimes do this: https://github.com/prometheus/prometheus/blob/e6d7cc5fa43716281bdf5d1cf40415f99a0e9623/storage/series.go#L270 to avoid issues (so use Float compare indeed)
if tolerance <= 0 { | ||
return first == second | ||
} else { | ||
return math.Abs(float64(first)-float64(second)) <= tolerance |
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.
Tolerance is generally a ratio or number of decimal places, not a absolute difference.
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.
Yeah. You might want to look at https://github.com/beorn7/floats/blob/master/equal.go (shameless plug).
Signed-off-by: Marco Pracucci <[email protected]>
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.
Solid tool and code, looking forward to use it on our side as well! LGTM 💪
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.
Good to know about. Thanks! Change lgtm.
What this PR does:
While comparing query results between the Cortex chunks and blocks storage, we learned that floating point rounding could be different for the same exact query executed twice due to the non deterministic series ordering in PromQL engine (for more details please refer to prometheus/prometheus#7757).
This PR adds a small (configureable) tolerance to avoid such false positives.
Which issue(s) this PR fixes:
N/A
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]