Skip to content
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

MQE: Add support for changes function #9724

Merged
merged 25 commits into from
Nov 11, 2024
Merged

Conversation

lamida
Copy link
Contributor

@lamida lamida commented Oct 23, 2024

What this PR does

Which issue(s) this PR fixes or relates to

Fixes #

Checklist

  • Tests updated.
  • n.a Documentation added.
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX].
  • n.a about-versioning.md updated with experimental features.

@lamida lamida changed the title WIP First step mqe changes function WIP MQE: changes function Oct 23, 2024
@lamida lamida changed the title WIP MQE: changes function MQE: Add support for changes function in Mimir Oct 24, 2024
@lamida lamida changed the title MQE: Add support for changes function in Mimir MQE: Add support for changes function Oct 24, 2024
@lamida lamida marked this pull request as ready for review October 24, 2024 18:37
@lamida lamida requested a review from a team as a code owner October 24, 2024 18:37
Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

Nice work, thanks for that!

I think it's also worth putting this function through TestCompareVariousMixedMetricsVectorSelectors to test some edge cases (just add it to line 2310).

pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
haveFloats := len(fHead) > 0 || len(fTail) > 0

if !haveFloats {
// PromQL engine doesn't support histogram for `changes` function yet,
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] We use the term "Prometheus' engine" to refer to it - "PromQL engine" is ambiguous because MQE is a PromQL engine too.

Suggested change
// PromQL engine doesn't support histogram for `changes` function yet,
// Prometheus' engine doesn't support histogram for `changes` function yet,

Might also be good to add a link to the Prometheus' issue tracking adding support for native histograms in changes, if there is one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to find but it seems there are no issue tracking for that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Charles is likely referring to this one: prometheus/prometheus#13934

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That issue didn't mention changes. This is the comment that mentioned the need to add support (code link).

@lamida
Copy link
Contributor Author

lamida commented Oct 31, 2024

@jhesketh @charleskorn I have addressed all the comments. These are changes from the previous reviews:

  • Ring-buffer wrap around is now checked.
  • Moved the function logic from range_vector.go to fpoint_ring_buffer.go so that I can test ring-buffer wrap around easier. Let me know if anyone have concern.

jhesketh
jhesketh previously approved these changes Nov 1, 2024
@@ -237,6 +239,49 @@ func (b *FPointRingBuffer) AnyAtOrBefore(maxT int64) bool {
return b.points[b.firstIndex].T <= maxT
}

// ChangesAtOrBefore returns the number of changes between subsequent points in this ring buffer from the first
// point to the point with timestamp less than or equal to maxT.
func (b *FPointRingBuffer) ChangesAtOrBefore(maxT int64) (float64, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is better fit into functions/range_vectors in terms of consistency. Can we not create a test in the functions package and pass it a constructed types.RangeVectorStepData?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is better fit into functions/range_vectors in terms of consistency

Actually some functions are implemented in fpoint_ring_buffer.go too like CountAtOrBefore, LastAtOrBefore and AnyAtOrBefore.

Can we not create a test in the functions package and pass it a constructed types.RangeVectorStepData?
It is not possible to create the buffer to simulate wrap around because points and other fields are private. I prefer not to make them public just for testing.

Other than that, if we put the logic in range_vectors.go we are exposing the internal representation of the ring buffer in range_vector.go. Like on how iterating head and tail which I believe should be internal fpoint_ring_buffer.go.

At the end I don't have any strong opinion and I am fine if there is a better argument.

Copy link
Contributor Author

@lamida lamida Nov 4, 2024

Choose a reason for hiding this comment

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

Can we not create a test in the functions package and pass it a constructed types.RangeVectorStepData?

It is not possible to create the buffer to simulate wrap around because the fields are private.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would favour this not being on the ring buffer type - ChangesAtOrBefore can efficiently be implemented by using UnsafePoints, whereas CountAtOrBefore, LastAtOrBefore and AnyAtOrBefore don't need the slices produced by UnsafePoints and so benefit from being implemented directly on the ring buffer type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will moved back to range_vectors.go then. I will figure out how the best to test that.

Comment on lines 270 to 281
// The points buffer is wrapped around, therefore we need to check changes starting from the buffer's head
// and then continue to the tail.
if len(fHead) > 0 && len(fTail) > 0 {
// We don't need to compare the first point with itself, hence start from fHead[1:]
accumulate(fHead[1:])
accumulate(fTail)
}

if len(fHead) > 0 && len(fTail) == 0 {
// We don't need to compare the first point with itself, hence start from fHead[1:]
accumulate(fHead[1:])
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is consistent with how we use accumulate elsewhere. I don't think there is much overhead in range when the len is 0 vs using an if.

Suggested change
// The points buffer is wrapped around, therefore we need to check changes starting from the buffer's head
// and then continue to the tail.
if len(fHead) > 0 && len(fTail) > 0 {
// We don't need to compare the first point with itself, hence start from fHead[1:]
accumulate(fHead[1:])
accumulate(fTail)
}
if len(fHead) > 0 && len(fTail) == 0 {
// We don't need to compare the first point with itself, hence start from fHead[1:]
accumulate(fHead[1:])
}
// The points buffer is wrapped around, therefore we need to check changes starting from the buffer's head
// and then continue to the tail.
// We don't need to compare the first point with itself, hence start from fHead[1:]
accumulate(fHead[1:])
accumulate(fTail)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. We can simplify that. Updated in a7d0474

{T: 9, F: 99},
{T: 1, F: 1},
{T: 3, F: 2},
{T: 5, F: 3},
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to have a test where the value doesn't change at the wrap around

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in af5c867

@jhesketh jhesketh self-requested a review November 1, 2024 05:53
@jhesketh jhesketh dismissed their stale review November 1, 2024 05:53

Did not mean to apporve yet

Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

LGTM modulo suggestion below

pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
Copy link
Contributor

@jhesketh jhesketh left a comment

Choose a reason for hiding this comment

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

lgtm too, but agree with Charles. Thanks Jon!

pkg/streamingpromql/operators/functions/range_vectors.go Outdated Show resolved Hide resolved
lamida and others added 15 commits November 11, 2024 14:00
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
lamida and others added 9 commits November 11, 2024 14:02
This was missed from previous commits.

Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
Signed-off-by: Jon Kartago Lamida <[email protected]>
@lamida lamida force-pushed the lamida/mqe-changes-function branch from 7f5623d to fa584f5 Compare November 11, 2024 06:03
Signed-off-by: Jon Kartago Lamida <[email protected]>
@lamida lamida merged commit 384eb7f into main Nov 11, 2024
29 checks passed
@lamida lamida deleted the lamida/mqe-changes-function branch November 11, 2024 06:24
@jhesketh jhesketh mentioned this pull request Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants