RATIS-2421. Gracefully cancel stream after complete in GrpcLogAppender#1363
RATIS-2421. Gracefully cancel stream after complete in GrpcLogAppender#1363symious wants to merge 5 commits intoapache:masterfrom
Conversation
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java
Outdated
Show resolved
Hide resolved
ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GrpcLogAppender.java
Outdated
Show resolved
Hide resolved
| .ifPresent(s -> completeStreamGracefully(s, "heartbeat")); | ||
| } | ||
| final long delayMs = Math.max(1L, completeGracePeriod.toLong(TimeUnit.MILLISECONDS)); | ||
| closer.schedule(this::cancelIfStillNeeded, delayMs, TimeUnit.MILLISECONDS); |
There was a problem hiding this comment.
This schedules cancelIfStillNeeded() outside the completed.compareAndSet(...) block. Repeated onCompleted() calls can queue redundant cancel tasks.
Can this be avoided?
Also I think if it’s invoked again after shutdownCloser() it may throw exception.
There was a problem hiding this comment.
The cancel and shutdown parts can be called multiple times.
There was a problem hiding this comment.
But if it is completed already aren't subsequent cancel calls queuing tasks which are no longer required?
There was a problem hiding this comment.
If onComplete() is invoked, the scheudler should be called and shutdown later, later requests should be rejected then.
| } | ||
|
|
||
| private void cancelStream( | ||
| ClientCallStreamObserver<AppendEntriesRequestProto> stream, |
There was a problem hiding this comment.
We are doing explicit cast to ClientCallStreamObserver<>, but I am thinking can there be a case where appendEntries() can return only CallStreamObserver?
That is the method signature in GrpcServerProtocolClient.
In such a case this can throw error.
Better to accept CallStreamObserver<> and then do an instanceof check?
try {
if (stream instanceof ClientCallStreamObserver) {
((ClientCallStreamObserver<AppendEntriesRequestProto>) stream).cancel(reason, cause);
} else {
...handle cancel when it is not clientcallstreamobserver, something like stream.onError(...)?
}
} catch (Exception e) {
LOG.warn("Failed to cancel {}", name, e);
}
There was a problem hiding this comment.
As a client, this one should always be a ClientCallStreamObserver.
spacemonkd
left a comment
There was a problem hiding this comment.
Thanks for the patch @symious.
Just a few comments, I am not too sure in some cases so definitely another set of eyes would be good to have.
But apart from these it looks good to me
|
@devabhishekpal Thank you for the review. Updated and PTAL. |
spacemonkd
left a comment
There was a problem hiding this comment.
Thanks for addressing the comments @symious. Looks good to me, +1
What changes were proposed in this pull request?
When onCompleted() is called, the client waits for the server response to finish the RPC and release the underlying resources.
However, in some cases the server response may never arrive, which leaves the RPC stream open and prevents resources from being released.
This ticket introduces a client-side cancel() after a short grace period following onCompleted(), to ensure the RPC stream is forcefully closed and resources are cleaned up.
What is the link to the Apache JIRA
https://issues.apache.org/jira/browse/RATIS-2421
How was this patch tested?
Test locally.