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

add context cancellation check at get series result iteration #13766

Merged

Conversation

erlan-z
Copy link
Contributor

@erlan-z erlan-z commented Mar 13, 2024

For series api when the response is big, we found out that it still will be iterating over the result SeriesSet even if query already timed out and context was cancelled.

This PR will add context check while iterating on the SeriesSet. There was similar context check changes here

Signed-off-by: Erlan Zholdubai uulu <erlanz@amazon.com>
@@ -882,6 +882,9 @@ func (api *API) series(r *http.Request) (result apiFuncResult) {
warnings := set.Warnings()

for set.Next() {
if err := ctx.Err(); err != nil {
return apiFuncResult{nil, contextErr(err), nil, nil}
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to still return warnings and the closer?

return apiFuncResult{nil, contextErr(err), warnings, closer}

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, added the warning and closer.

…and closer on error

Signed-off-by: Erlan Zholdubai uulu <erlanz@amazon.com>
@@ -896,6 +899,17 @@ func (api *API) series(r *http.Request) (result apiFuncResult) {
return apiFuncResult{metrics, nil, warnings, closer}
}

func contextErr(err error) *apiError {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see we already have a similar util: returnAPIError, maybe we can adjust and re-use it.

A regression test for this would be great.

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, I have changed to use existing returnAPIError. It covers context cancellation error, we can just use it.
For tests, I have added test for the case when context is cancelled while iteration. Other regression test cases looks to be covered by the existing tests.

@erlan-z erlan-z requested a review from machine424 March 15, 2024 19:46
t.Run("should throw error when context cancelled on SeriesSet iteration", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
timer := time.AfterFunc(10*time.Millisecond, func() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn’t know the test would be so dependent on the implementation. I’m afraid that if the series body changes, we’ll have no guarantee that the context check you added is the one responsible for the cancellation.

I think we can go without a test for now. Perhaps we can just add a check for the error mapping in TestReturnAPIError. If you’d like, you can create an issue to discuss how we can improve the utils to better test such changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, this test doesn't look right with timer and at all. Removed test and added the mapping for context.Canceled error case.

Signed-off-by: Erlan Zholdubai uulu <erlanz@amazon.com>
@erlan-z erlan-z force-pushed the check-context-cancellation-on-get-series branch from 184af8e to 129321d Compare March 19, 2024 00:35
Signed-off-by: Erlan Zholdubai uulu <erlanz@amazon.com>
@erlan-z erlan-z force-pushed the check-context-cancellation-on-get-series branch from 05ba797 to 29913e9 Compare March 19, 2024 03:03
@erlan-z erlan-z requested a review from machine424 March 19, 2024 03:21
Copy link
Collaborator

@machine424 machine424 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!

@beorn7 beorn7 merged commit 742196b into prometheus:main Mar 19, 2024
24 checks passed
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.

None yet

4 participants