-
Notifications
You must be signed in to change notification settings - Fork 698
[Feature] Add timeout for apiserver grpc server #3427
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
Merged
kevin85421
merged 17 commits into
ray-project:master
from
machichima:3344-apiserver-timeout-grpc-server
May 6, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c677b80
feat: add grpc server timeout
machichima 9cbf138
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 33cf5da
test: unit test for timeout interceptor
machichima d34686f
fix: update timeout to 60 seconds
machichima ebb5ba4
feat: enable setting grpc timeout by env variable
machichima cade329
refactor: default timeout to constant
machichima a8fee22
fix: update time out error message
machichima d4a554a
fix: add default timeout value in warning message
machichima f1b5baf
feat: set gRPC timeout in flag instead of env var
machichima f8ad90f
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 4c897aa
test: update mock handler that mimic grpc package handler
machichima aa7fcb5
feat: simplify timeout interceptor
machichima 6425633
fix: variable type to log
machichima 79a6349
docs: add docstring for mock Handle
machichima 54c8d57
fix: remove suffix in timeout constant
machichima 897ad01
Merge branch 'master' of github.com:ray-project/kuberay into 3344-api…
machichima 47f872a
style: add comment for constant args
machichima 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,10 +7,13 @@ import ( | |
| "io" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| klog "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
|
|
@@ -20,9 +23,28 @@ type mockHandler struct { | |
| called bool | ||
| } | ||
|
|
||
| func (h *mockHandler) Handle(_ context.Context, _ interface{}) (interface{}, error) { | ||
| // Handle simulates the behavior of a gRPC handler with an optional delay. | ||
| // If the delay completes before the context expires, it returns "test_response" along with predefined error. | ||
| // If the context is canceled or the deadline is exceeded before the delay completes, | ||
| // it returns a corresponding gRPC status error instead. | ||
| func (h *mockHandler) Handle(ctx context.Context, _ interface{}, delay time.Duration) (interface{}, error) { | ||
| h.called = true | ||
| return "test_response", h.returnErr | ||
|
|
||
| select { | ||
| case <-time.After(delay): | ||
| return "test_response", h.returnErr | ||
dentiny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case <-ctx.Done(): | ||
| var grpcCode codes.Code | ||
| switch ctx.Err() { | ||
| case context.Canceled: | ||
| grpcCode = codes.Canceled | ||
| case context.DeadlineExceeded: | ||
| grpcCode = codes.DeadlineExceeded | ||
| default: | ||
| grpcCode = codes.Unknown | ||
| } | ||
| return nil, status.Error(grpcCode, ctx.Err().Error()) | ||
| } | ||
|
Comment on lines
+33
to
+47
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding this to mimic the grpc IO handler for testing |
||
| } | ||
|
|
||
| func TestAPIServerInterceptor(t *testing.T) { | ||
|
|
@@ -61,7 +83,7 @@ func TestAPIServerInterceptor(t *testing.T) { | |
| req, | ||
| info, | ||
| func(ctx context.Context, req interface{}) (interface{}, error) { | ||
| return tt.handler.Handle(ctx, req) | ||
| return tt.handler.Handle(ctx, req, 0 /*delay*/) | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -96,7 +118,7 @@ func TestAPIServerInterceptorContextPassing(t *testing.T) { | |
| func(receivedCtx context.Context, req interface{}) (interface{}, error) { | ||
| // Verify context value is passed through | ||
| assert.Equal(t, "test_value", receivedCtx.Value(testContextKey("test_key"))) | ||
| return handler.Handle(receivedCtx, req) | ||
| return handler.Handle(receivedCtx, req, 0 /*delay*/) | ||
| }, | ||
| ) | ||
| } | ||
|
|
@@ -153,7 +175,7 @@ func TestAPIServerInterceptorLogging(t *testing.T) { | |
| "test_request", | ||
| info, | ||
| func(receivedCtx context.Context, req interface{}) (interface{}, error) { | ||
| return handler.Handle(receivedCtx, req) | ||
| return handler.Handle(receivedCtx, req, 0 /*delay*/) | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -192,3 +214,62 @@ func TestAPIServerInterceptorLogging(t *testing.T) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTimeoutInterceptor(t *testing.T) { | ||
| tests := []struct { | ||
| expectedError error | ||
| name string | ||
| timeout time.Duration | ||
| handlerDelay time.Duration | ||
| expectedCalled bool | ||
| }{ | ||
| { | ||
| name: "handler completes before timeout", | ||
| timeout: 100 * time.Millisecond, | ||
| handlerDelay: 50 * time.Millisecond, | ||
| expectedError: nil, | ||
| expectedCalled: true, | ||
| }, | ||
| { | ||
| name: "handler exceeds timeout", | ||
| timeout: 50 * time.Millisecond, | ||
| handlerDelay: 100 * time.Millisecond, | ||
| expectedError: status.Error(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), | ||
| expectedCalled: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create test context and request | ||
| ctx := context.Background() | ||
| req := "test_request" | ||
| handler := &mockHandler{} | ||
|
|
||
| // Create the interceptor with the specified timeout | ||
| interceptor := TimeoutInterceptor(tt.timeout) | ||
|
|
||
| // Call the interceptor | ||
| resp, err := interceptor( | ||
| ctx, | ||
| req, | ||
| &grpc.UnaryServerInfo{FullMethod: "TestTimeoutMethod"}, | ||
| func(ctx context.Context, req interface{}) (interface{}, error) { | ||
| return handler.Handle(ctx, req, tt.handlerDelay) | ||
| }, | ||
| ) | ||
|
|
||
| // Verify response and error | ||
| if tt.expectedError == nil { | ||
| // Verify handler was called | ||
| assert.Equal(t, tt.expectedCalled, handler.called, "handler call status should match expected") | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Equal(t, "test_response", resp, "response should match expected") | ||
| } else { | ||
| require.Error(t, err) | ||
| require.Equal(t, tt.expectedError, err, "A matching error is expected") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is automatically updated when running
make test