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

promql: ensure series in matrix values returned for instant queries are sorted #14083

Merged
merged 5 commits into from
May 31, 2024

Conversation

charleskorn
Copy link
Contributor

The PromQL engine always sorts matrix values returned for range queries (source), but does not do the same thing for matrix values returned for instant queries.

This PR makes the behaviour for instant queries consistent with range queries: matrix values are now always sorted by series.

I've added the TestInstantQueryWithRangeVectorSelector/matches series with points in range test case to demonstrate the issue and that it has been fixed. (Note that without the fix, the series order is unpredictable, so you may need to run the test multiple times to see the failure.)

I've also added some other test cases for instant queries that return matrices given there weren't any test cases for this scenario.

Signed-off-by: Charles Korn <charles.korn@grafana.com>
…s sorted

Signed-off-by: Charles Korn <charles.korn@grafana.com>
@krajorama
Copy link
Member

/prombench main

@prombot
Copy link
Contributor

prombot commented May 13, 2024

⏱️ Welcome to Prometheus Benchmarking Tool. ⏱️

Compared versions: PR-14083 and main

After successful deployment, the benchmarking results can be viewed at:

Other Commands:
To stop benchmark: /prombench cancel
To restart benchmark: /prombench restart main

Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

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

Looks good, one suggestion and I started prombench.

promql/engine.go Outdated Show resolved Hide resolved
Signed-off-by: Charles Korn <charles.korn@grafana.com>
@krajorama
Copy link
Member

/prombench cancel

@prombot
Copy link
Contributor

prombot commented May 14, 2024

Benchmark cancel is in progress.

Copy link
Member

@krajorama krajorama left a comment

Choose a reason for hiding this comment

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

LGTM

@machine424
Copy link
Collaborator

Thanks.

(Note that without the fix, the series order is unpredictable, so you may need to run the test multiple times to see the failure.)
If the operation is cheap, we could run it multiple times in a loop to increase the likelihood of a change in order.

Taking a look at the benchmark dashboards, I can see some overhead in the CPU/query latency and Avg query engine timings/s dashboards as expected. However, I am unsure if this overhead is negligible.

@darshanime
Copy link
Contributor

I can see some overhead in the CPU/query latency and Avg query engine timings/s dashboards as expected. However, I am unsure if this overhead is negligible.

Running the benchmarks would help quantify the overhead?

Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

Do you think this behaviour is documented anywhere?
I think it should be. Anyway we can go ahead with the change.

@bboreham
Copy link
Member

Running the benchmarks

We did run prombench, although it doesn't have this kind of query.

I don't think there are any benchmarks which currently test the case of an instant query returning a matrix. Would you like to write some?

@charleskorn
Copy link
Contributor Author

Do you think this behaviour is documented anywhere? I think it should be.

I couldn't find it documented anywhere. Perhaps here makes sense?

Should we also require instant vectors to be sorted by series as well? Seems like this would be good for the sake of consistency.

@bboreham
Copy link
Member

I couldn't find it documented anywhere. Perhaps here makes sense?

👍

Should we also require instant vectors to be sorted by series as well?

The code takes care not to do that, since there are PromQL functions like sort which give a specific order.
Arguably it could do it where the expression has not supplied an order, but that seems complicated.

@bboreham
Copy link
Member

Related: the code silently ignores any sort in the PromQL when returning a matrix.
In my mind I imagine some heartless Prometheus maintainer gloating "they'll figure it out eventually", but we could add an Annotation to be nice.

[when multiple points are returned, which set do you sort on?]

@bboreham
Copy link
Member

Also related: sort_by_label could work for matrixes.

Signed-off-by: Charles Korn <charles.korn@grafana.com>
@charleskorn
Copy link
Contributor Author

Related: the code silently ignores any sort in the PromQL when returning a matrix. In my mind I imagine some heartless Prometheus maintainer gloating "they'll figure it out eventually", but we could add an Annotation to be nice.

[when multiple points are returned, which set do you sort on?]

I've created #14115 so we don't lose this.

# Conflicts:
#	promql/engine_test.go
@charleskorn
Copy link
Contributor Author

I've resolved the merge conflicts. The CI failure seems to be an unrelated flake.

Copy link
Member

@bboreham bboreham left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@bboreham bboreham merged commit bfdca40 into prometheus:main May 31, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants