-
Notifications
You must be signed in to change notification settings - Fork 51
fix(grpc-proxy): purge unclaimed stateful work on shutdown #1031
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
Open
balajinvda
wants to merge
4
commits into
main
Choose a base branch
from
fix/grpc-proxy-purge-pending-work-on-shutdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3b5a38d
fix(grpc-proxy): purge unclaimed stateful work on shutdown
balaji-g 069be46
fix(grpc-proxy): close the shutdown window that orphaned queued work
balaji-g 152d181
test(grpc-proxy): make the invocation drain assertion unambiguous
balaji-g 2c0f94f
test(grpc-proxy): remove a timing flake in the drain assertion
balaji-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/invocation-plane-services/grpc-proxy/proxy/invocation/pending_work.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package invocation | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/nats-io/nats.go/jetstream" | ||
| ) | ||
|
|
||
| // The work queue a stateful invocation lands in. These are owned by the | ||
| // invocation service, which publishes to requestWorkSubject and removes a | ||
| // cancelled request with the same subject-filtered purge used here. The | ||
| // formats are duplicated rather than shared because the owning service is | ||
| // written in another language; they must not drift. | ||
| func requestWorkStream(region, functionVersionId string) string { | ||
| return fmt.Sprintf("rq_%s_%s", region, functionVersionId) | ||
| } | ||
|
|
||
| func requestWorkSubject(region, functionVersionId string, requestId uuid.UUID) string { | ||
| return fmt.Sprintf("rq.%s.%s.%s", region, functionVersionId, requestId) | ||
| } | ||
|
|
||
| // PurgePendingWork drops a stateful work request that is still queued. | ||
| // | ||
| // It is called for sessions this pod issued a worker token for that never came | ||
| // back to CONNECT, at the point the pod is shutting down. Those tokens only | ||
| // exist in this pod's memory, so every one of those queued requests is already | ||
| // guaranteed to fail authentication whenever a worker eventually pulls it. Left | ||
| // in place they are pulled anyway, occupy a worker concurrency slot, and fail, | ||
| // which is what keeps a saturated function at zero goodput long after the | ||
| // restart that caused it. | ||
| // | ||
| // Purging by subject only removes messages still held by the stream. A session | ||
| // that already has a worker attached had its message delivered, so an | ||
| // established session is unaffected and remains free to reattach to another | ||
| // pod. | ||
| func (f *FunctionInvoker) PurgePendingWork(ctx context.Context, requestId uuid.UUID, functionVersionId string) error { | ||
| streamName := requestWorkStream(f.region, functionVersionId) | ||
| stream, err := f.js.Stream(ctx, streamName) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to look up work stream %s: %w", streamName, err) | ||
| } | ||
| subject := requestWorkSubject(f.region, functionVersionId, requestId) | ||
| if err := stream.Purge(ctx, jetstream.WithPurgeSubject(subject)); err != nil { | ||
| return fmt.Errorf("failed to purge work subject %s: %w", subject, err) | ||
| } | ||
| return nil | ||
| } |
50 changes: 50 additions & 0 deletions
50
src/invocation-plane-services/grpc-proxy/proxy/invocation/pending_work_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package invocation | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // The work queue is owned by the invocation service, which is written in | ||
| // another language, so these formats are duplicated rather than shared. If they | ||
| // drift the purge silently targets a subject nothing was ever published to and | ||
| // removes nothing, with no error to show for it. These cases pin the exact | ||
| // strings against the owning service's request_stream_name and request_subject. | ||
| func TestRequestWorkStreamAndSubjectMatchTheInvocationService(t *testing.T) { | ||
| requestId := uuid.MustParse("11111111-2222-3333-4444-555555555555") | ||
| region := "us-west-2" | ||
| versionId := "66666666-7777-8888-9999-000000000000" | ||
|
|
||
| assert.Equal(t, "rq_us-west-2_66666666-7777-8888-9999-000000000000", | ||
| requestWorkStream(region, versionId)) | ||
| assert.Equal(t, "rq.us-west-2.66666666-7777-8888-9999-000000000000.11111111-2222-3333-4444-555555555555", | ||
| requestWorkSubject(region, versionId, requestId)) | ||
| } | ||
|
|
||
| // The subject has to fall inside the stream's own subject space, otherwise a | ||
| // filtered purge matches nothing. | ||
| func TestRequestWorkSubjectIsCoveredByTheStreamSubjectSpace(t *testing.T) { | ||
| region := "eu-west-1" | ||
| versionId := uuid.New().String() | ||
|
|
||
| subject := requestWorkSubject(region, versionId, uuid.New()) | ||
| assert.Regexp(t, `^rq\.`+region+`\.`+versionId+`\.`, subject) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.