-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Server-side timeout mechanism #10360
Open
sorra
wants to merge
23
commits into
grpc:master
Choose a base branch
from
sorra:server-side-timeout
base: master
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.
+1,014
−160
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3798f55
Server-side timeout mechanism
sorra f26f928
Move the unary call if-condition in TimeoutServerInterceptor
sorra 92c0ad3
replace TimeoutTask invalidation with Future cancelation
sorra e8c9894
Rename interceptor class
sorra 4622550
add unit tests
sorra 390592d
fix code style
sorra 7d10f25
move unit tests
sorra 5347855
test streaming method is not intercepted
sorra fad093b
update copyright year
sorra de4fce7
improve unit test
sorra 8ef5cef
improve unit tests
sorra 9ff53b3
Change to CancellableContext and CancellationListener approach
sorra f791d10
Make interruption optional
sorra 05168f2
Builder for ServerTimeoutManager
sorra a7f5cc1
Clear interruption in a finally block
sorra 1200f35
Intercept all stages and close server call using serializing execution
sorra f39e031
Merge branch 'master' into server-side-timeout
sorra c20a372
maintain after merge
sorra 5b78a53
Improve javadoc comments
sorra 06d771c
Close the context
sorra df83e54
Skip listener callback execution if context has been cancelled (serve…
sorra 1a9a9bc
improve code coverage
sorra e6bb04b
add copyright
sorra 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
175 changes: 175 additions & 0 deletions
175
util/src/main/java/io/grpc/util/SerializingServerCall.java
This file contains 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,175 @@ | ||
/* | ||
* Copyright 2017 The gRPC Authors | ||
* | ||
* 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 io.grpc.util; | ||
|
||
import com.google.common.util.concurrent.MoreExecutors; | ||
import com.google.common.util.concurrent.SettableFuture; | ||
import io.grpc.Attributes; | ||
import io.grpc.ExperimentalApi; | ||
import io.grpc.ForwardingServerCall; | ||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.Status; | ||
import io.grpc.internal.SerializingExecutor; | ||
import java.util.concurrent.ExecutionException; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A {@link ServerCall} that wraps around a non thread safe delegate and provides thread safe | ||
* access by serializing everything on an executor. | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2189") | ||
class SerializingServerCall<ReqT, RespT> extends | ||
ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> { | ||
private static final String ERROR_MSG = "Encountered error during serialized access"; | ||
private final SerializingExecutor serializingExecutor = | ||
new SerializingExecutor(MoreExecutors.directExecutor()); | ||
private boolean closeCalled = false; | ||
|
||
SerializingServerCall(ServerCall<ReqT, RespT> delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public void sendMessage(final RespT message) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.sendMessage(message); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void request(final int numMessages) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.request(numMessages); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void sendHeaders(final Metadata headers) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.sendHeaders(headers); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void close(final Status status, final Metadata trailers) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
if (!closeCalled) { | ||
closeCalled = true; | ||
|
||
SerializingServerCall.super.close(status, trailers); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
final SettableFuture<Boolean> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.isReady()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
final SettableFuture<Boolean> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.isCancelled()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void setMessageCompression(final boolean enabled) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.setMessageCompression(enabled); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void setCompression(final String compressor) { | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
SerializingServerCall.super.setCompression(compressor); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Attributes getAttributes() { | ||
final SettableFuture<Attributes> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.getAttributes()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getAuthority() { | ||
final SettableFuture<String> retVal = SettableFuture.create(); | ||
serializingExecutor.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
retVal.set(SerializingServerCall.super.getAuthority()); | ||
} | ||
}); | ||
try { | ||
return retVal.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
throw new RuntimeException(ERROR_MSG, e); | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
util/src/main/java/io/grpc/util/ServerCallTimeoutInterceptor.java
This file contains 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,120 @@ | ||
/* | ||
* Copyright 2023 The gRPC Authors | ||
* | ||
* 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 io.grpc.util; | ||
|
||
import io.grpc.Context; | ||
import io.grpc.ExperimentalApi; | ||
import io.grpc.ForwardingServerCallListener; | ||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.grpc.ServerInterceptor; | ||
|
||
/** | ||
* An optional ServerInterceptor to stop server calls at best effort when the timeout is reached. | ||
* In this way, it prevents problematic code from excessively using up all threads in the pool. | ||
* | ||
* <p>How to use: install it to your server using ServerBuilder#intercept(ServerInterceptor). | ||
* | ||
* <p>Limitation: it only applies the timeout to unary calls | ||
* (long-running streaming calls are allowed, so they can run without this timeout limit). | ||
*/ | ||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/10361") | ||
public class ServerCallTimeoutInterceptor implements ServerInterceptor { | ||
|
||
private final ServerTimeoutManager serverTimeoutManager; | ||
|
||
public ServerCallTimeoutInterceptor(ServerTimeoutManager serverTimeoutManager) { | ||
this.serverTimeoutManager = serverTimeoutManager; | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( | ||
ServerCall<ReqT, RespT> serverCall, | ||
Metadata metadata, | ||
ServerCallHandler<ReqT, RespT> serverCallHandler) { | ||
// Only intercepts unary calls because the timeout is inapplicable to streaming calls. | ||
if (serverCall.getMethodDescriptor().getType().clientSendsOneMessage()) { | ||
ServerCall<ReqT, RespT> serializingServerCall = new SerializingServerCall<>(serverCall); | ||
Context.CancellableContext timeoutContext = | ||
serverTimeoutManager.startTimeoutContext(serializingServerCall); | ||
if (timeoutContext != null) { | ||
return new TimeoutServerCallListener<>( | ||
serverCallHandler.startCall(serializingServerCall, metadata), | ||
timeoutContext, | ||
serverTimeoutManager); | ||
} | ||
} | ||
return serverCallHandler.startCall(serverCall, metadata); | ||
} | ||
|
||
/** A listener that intercepts RPC callbacks for timeout control. */ | ||
static class TimeoutServerCallListener<ReqT> | ||
extends ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> { | ||
|
||
private final Context.CancellableContext context; | ||
private final ServerTimeoutManager serverTimeoutManager; | ||
|
||
private TimeoutServerCallListener( | ||
ServerCall.Listener<ReqT> delegate, | ||
Context.CancellableContext context, | ||
ServerTimeoutManager serverTimeoutManager) { | ||
super(delegate); | ||
this.context = context; | ||
this.serverTimeoutManager = serverTimeoutManager; | ||
} | ||
|
||
@Override | ||
public void onMessage(ReqT message) { | ||
serverTimeoutManager.runWithContext(context, () -> super.onMessage(message)); | ||
} | ||
|
||
/** | ||
* Adds interruption here because the application RPC method is called in halfClose(). See | ||
* io.grpc.stub.ServerCalls.UnaryServerCallHandler.UnaryServerCallListener | ||
*/ | ||
@Override | ||
public void onHalfClose() { | ||
serverTimeoutManager.runWithContextInterruptibly(context, super::onHalfClose); | ||
} | ||
|
||
@Override | ||
public void onCancel() { | ||
try { | ||
serverTimeoutManager.runWithContext(context, super::onCancel); | ||
} finally { | ||
// Cancel the timeout when the call is finished. | ||
context.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
try { | ||
serverTimeoutManager.runWithContext(context, super::onComplete); | ||
} finally { | ||
// Cancel the timeout when the call is finished. | ||
context.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onReady() { | ||
serverTimeoutManager.runWithContext(context, super::onReady); | ||
} | ||
} | ||
} |
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 class is extracted from
TransmitStatusRuntimeExceptionInterceptor
.