-
Notifications
You must be signed in to change notification settings - Fork 51
fix(grpc-proxy): detect stateful sessions whose worker is gone #1029
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
3
commits into
main
Choose a base branch
from
fix/grpc-proxy-stateful-session-rejoin
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
3 commits
Select commit
Hold shift + click to select a range
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
177 changes: 177 additions & 0 deletions
177
src/invocation-plane-services/grpc-proxy/proxy/invocation/join_existing_session_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,177 @@ | ||
| /* | ||
| 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" | ||
| "errors" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| natsserver "github.com/nats-io/nats-server/v2/server" | ||
| "github.com/nats-io/nats.go" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/protobuf/proto" | ||
|
|
||
| "nvcf-grpc-proxy/nvcf/pb" | ||
| ) | ||
|
|
||
| // startEmbeddedNats starts an in-process NATS server on an OS-assigned | ||
| // ephemeral port so these tests can run alongside the rest of the suite | ||
| // without a fixed-port collision. Torn down via t.Cleanup. | ||
| func startEmbeddedNats(t *testing.T) string { | ||
| t.Helper() | ||
|
|
||
| s, err := natsserver.NewServer(&natsserver.Options{ | ||
| Host: "127.0.0.1", | ||
| Port: -1, | ||
| NoSigs: true, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| s.Start() | ||
| t.Cleanup(func() { | ||
| s.Shutdown() | ||
| s.WaitForShutdown() | ||
| }) | ||
|
|
||
| require.True(t, s.ReadyForConnections(10*time.Second), "embedded nats server did not become ready") | ||
| return s.ClientURL() | ||
| } | ||
|
|
||
| func newTestInvoker(t *testing.T) (*FunctionInvoker, *nats.Conn) { | ||
| t.Helper() | ||
|
|
||
| nc, err := nats.Connect(startEmbeddedNats(t)) | ||
| require.NoError(t, err) | ||
| t.Cleanup(nc.Close) | ||
|
|
||
| // no_responders is what lets a rejoin tell a dead session from a live one. | ||
| // If the server or client ever stopped supporting it the rest of these | ||
| // assertions would still pass for the wrong reason, so check it directly. | ||
| require.True(t, nc.HeadersSupported(), "no-responders detection needs header support") | ||
|
|
||
| return &FunctionInvoker{ | ||
| nc: nc, | ||
| region: "region-1", | ||
| connectPaths: ConnectPaths{HTTP1: "http://10.0.0.1:10086/v1/proxy"}, | ||
| }, nc | ||
| } | ||
|
|
||
| // A session whose worker is gone has nothing subscribed to its reconnect | ||
| // subject. That has to surface as ErrSessionNotFound, because that is the only | ||
| // error the director turns into a cookie-clearing response, which is what lets | ||
| // the client open a fresh session without the function being restarted. | ||
| func TestJoinExistingSessionNoWorkerSubscribed(t *testing.T) { | ||
| invoker, _ := newTestInvoker(t) | ||
| requestId := uuid.New() | ||
|
|
||
| start := time.Now() | ||
| err := invoker.joinExistingSession(context.Background(), requestId, &pb.ProxyAuthResponse{FunctionId: "fn-1"}, "worker-token") | ||
|
|
||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, ErrSessionNotFound) | ||
| assert.Contains(t, err.Error(), requestId.String()) | ||
| // No responders is answered from interest state, so detection must not | ||
| // cost the full probe deadline even with the confirmation probe. | ||
| assert.Less(t, time.Since(start), 2*reconnectAckTimeout+reconnectNoRespondersRetryDelay, | ||
| "dead session should be detected without waiting out both probe deadlines") | ||
| } | ||
|
|
||
| // A worker that acknowledges the reconnect is live, so the rejoin succeeds and | ||
| // the worker receives the connection config it needs to CONNECT back. | ||
| func TestJoinExistingSessionWorkerAcks(t *testing.T) { | ||
| invoker, nc := newTestInvoker(t) | ||
| requestId := uuid.New() | ||
|
|
||
| received := make(chan *pb.WorkerInvokeFunctionRequest, 1) | ||
| sub, err := nc.Subscribe(reconnectSubject(requestId), func(msg *nats.Msg) { | ||
| var work pb.WorkerInvokeFunctionRequest | ||
| if err := proto.Unmarshal(msg.Data, &work); err != nil { | ||
| return | ||
| } | ||
| // mirrors the worker's reconnect listener | ||
| _ = msg.Respond(nil) | ||
| received <- &work | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NoError(t, nc.Flush()) | ||
| t.Cleanup(func() { _ = sub.Unsubscribe() }) | ||
|
|
||
| start := time.Now() | ||
| err = invoker.joinExistingSession(context.Background(), requestId, &pb.ProxyAuthResponse{FunctionId: "fn-1"}, "worker-token") | ||
| require.NoError(t, err) | ||
| assert.Less(t, time.Since(start), reconnectAckTimeout, "an acknowledged rejoin should not wait on the deadline") | ||
|
|
||
| select { | ||
| case work := <-received: | ||
| assert.Equal(t, requestId.String(), work.RequestId) | ||
| require.Len(t, work.StatefulConfig.ConnectionConfigs, 1) | ||
| assert.Equal(t, "worker-token", | ||
| work.StatefulConfig.ConnectionConfigs[0].GetHttp1Config().ProxyAuthorizationToken) | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("worker never received the reconnect message") | ||
| } | ||
| } | ||
|
|
||
| // A worker built before the acknowledgement is still subscribed, and interest | ||
| // alone proves the session is live. The rejoin must succeed rather than be | ||
| // mistaken for a dead session, otherwise deploying the proxy ahead of the | ||
| // worker would sever every live session. | ||
| func TestJoinExistingSessionSubscribedWorkerWithoutAck(t *testing.T) { | ||
| invoker, nc := newTestInvoker(t) | ||
| requestId := uuid.New() | ||
|
|
||
| received := make(chan struct{}, 1) | ||
| sub, err := nc.Subscribe(reconnectSubject(requestId), func(msg *nats.Msg) { | ||
| received <- struct{}{} // deliberately no Respond | ||
| }) | ||
| require.NoError(t, err) | ||
| require.NoError(t, nc.Flush()) | ||
| t.Cleanup(func() { _ = sub.Unsubscribe() }) | ||
|
|
||
| err = invoker.joinExistingSession(context.Background(), requestId, &pb.ProxyAuthResponse{FunctionId: "fn-1"}, "worker-token") | ||
| require.NoError(t, err, "a subscribed worker that does not ack must be treated as live") | ||
|
|
||
| select { | ||
| case <-received: | ||
| case <-time.After(5 * time.Second): | ||
| t.Fatal("worker never received the reconnect message") | ||
| } | ||
| } | ||
|
|
||
| // A caller that goes away mid-rejoin must not be reported as a dead session: | ||
| // that would clear a client cookie on the strength of our own cancellation. | ||
| func TestJoinExistingSessionCancelledCallerIsNotASessionLoss(t *testing.T) { | ||
| invoker, nc := newTestInvoker(t) | ||
| requestId := uuid.New() | ||
|
|
||
| sub, err := nc.Subscribe(reconnectSubject(requestId), func(msg *nats.Msg) {}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, nc.Flush()) | ||
| t.Cleanup(func() { _ = sub.Unsubscribe() }) | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
|
|
||
| err = invoker.joinExistingSession(ctx, requestId, &pb.ProxyAuthResponse{FunctionId: "fn-1"}, "worker-token") | ||
| require.Error(t, err) | ||
| assert.NotErrorIs(t, err, ErrSessionNotFound) | ||
| assert.True(t, errors.Is(err, context.Canceled), "expected the caller's cancellation, got %v", err) | ||
| } |
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.