Skip to content

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

Conversation

pracucci
Copy link
Contributor

@pracucci pracucci commented Aug 7, 2020

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

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

Signed-off-by: Marco Pracucci <[email protected]>

func compareSampleValue(first, second model.SampleValue, tolerance float64) bool {
if tolerance <= 0 {
return first == second

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

Copy link
Contributor Author

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).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if tolerance <= 0 {
return first == second
} else {
return math.Abs(float64(first)-float64(second)) <= tolerance

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.

Copy link

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]>
Copy link
Contributor

@bwplotka bwplotka left a 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 💪

@pracucci pracucci requested a review from pstibrany August 7, 2020 13:34
Copy link
Contributor

@pstibrany pstibrany left a 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.

@pracucci pracucci merged commit 8d8975c into cortexproject:master Aug 10, 2020
@pracucci pracucci deleted the add-tolerance-to-query-tee-values-comparison branch August 10, 2020 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants