-
Notifications
You must be signed in to change notification settings - Fork 10
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
chore: gracefully shutdown when error encountered #152
Merged
Merged
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dc557dc
test - yhl's change
KeranYang 1427e2d
.
KeranYang 4c13792
not forcing System.exit(0)
KeranYang 8888dec
fixed the jvm not terminating issue
KeranYang 757293b
.
KeranYang 9f9eeb8
.
KeranYang 0c49303
.
KeranYang c83462b
side-input
KeranYang 8f53154
batchmap
KeranYang 561dc5d
map
KeranYang b63faee
map streamer
KeranYang af4fa61
source
KeranYang c09d304
transformer
KeranYang 7b77b6d
reducers
KeranYang a675025
fix akka for transformer
KeranYang fd5a3ee
akka change for mapper
KeranYang 00a0429
.
KeranYang 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
110 changes: 110 additions & 0 deletions
110
src/main/java/io/numaproj/numaflow/shared/GrpcServerHelper.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,110 @@ | ||
package io.numaproj.numaflow.shared; | ||
|
||
import io.grpc.BindableService; | ||
import io.grpc.Context; | ||
import io.grpc.Contexts; | ||
import io.grpc.ForwardingServerCallListener; | ||
import io.grpc.Server; | ||
import io.grpc.ServerBuilder; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.grpc.ServerInterceptor; | ||
import io.grpc.Status; | ||
import io.grpc.netty.NettyServerBuilder; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.unix.DomainSocketAddress; | ||
|
||
import static io.numaproj.numaflow.shared.GrpcServerUtils.DATUM_METADATA_WIN_END; | ||
import static io.numaproj.numaflow.shared.GrpcServerUtils.DATUM_METADATA_WIN_START; | ||
import static io.numaproj.numaflow.shared.GrpcServerUtils.WINDOW_END_TIME; | ||
import static io.numaproj.numaflow.shared.GrpcServerUtils.WINDOW_START_TIME; | ||
|
||
public class GrpcServerHelper { | ||
private EventLoopGroup bossEventLoopGroup; | ||
private EventLoopGroup workerEventLoopGroup; | ||
|
||
public void gracefullyShutdownEventLoopGroups() { | ||
if (this.bossEventLoopGroup != null) { | ||
this.bossEventLoopGroup.shutdownGracefully(); | ||
} | ||
if (this.workerEventLoopGroup != null) { | ||
this.workerEventLoopGroup.shutdownGracefully(); | ||
} | ||
} | ||
|
||
public Server createServer( | ||
String socketPath, | ||
int maxMessageSize, | ||
boolean isLocal, | ||
int port, | ||
BindableService service) { | ||
ServerInterceptor interceptor = new ServerInterceptor() { | ||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( | ||
ServerCall<ReqT, RespT> call, | ||
io.grpc.Metadata headers, | ||
ServerCallHandler<ReqT, RespT> next) { | ||
|
||
final var context = | ||
Context.current().withValues( | ||
WINDOW_START_TIME, | ||
headers.get(DATUM_METADATA_WIN_START), | ||
WINDOW_END_TIME, | ||
headers.get(DATUM_METADATA_WIN_END)); | ||
ServerCall.Listener<ReqT> listener = Contexts.interceptCall( | ||
context, | ||
call, | ||
headers, | ||
next); | ||
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<>( | ||
listener) { | ||
@Override | ||
public void onHalfClose() { | ||
try { | ||
super.onHalfClose(); | ||
} catch (RuntimeException ex) { | ||
handleException(ex, call, headers); | ||
throw ex; | ||
} | ||
} | ||
|
||
private void handleException( | ||
RuntimeException e, | ||
ServerCall<ReqT, RespT> serverCall, | ||
io.grpc.Metadata headers) { | ||
// Currently, we only have application level exceptions. | ||
// Translate it to UNKNOWN status. | ||
var status = Status.UNKNOWN.withDescription(e.getMessage()).withCause(e); | ||
var newStatus = Status.fromThrowable(status.asException()); | ||
serverCall.close(newStatus, headers); | ||
e.printStackTrace(); | ||
System.exit(1); | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
if (isLocal) { | ||
return ServerBuilder.forPort(port) | ||
.maxInboundMessageSize(maxMessageSize) | ||
.intercept(interceptor) | ||
.addService(service) | ||
.build(); | ||
} | ||
|
||
this.bossEventLoopGroup = GrpcServerUtils.createEventLoopGroup(1, "netty-boss"); | ||
this.workerEventLoopGroup = GrpcServerUtils.createEventLoopGroup( | ||
ThreadUtils.INSTANCE.availableProcessors(), | ||
"netty-worker"); | ||
|
||
return NettyServerBuilder | ||
.forAddress(new DomainSocketAddress(socketPath)) | ||
.channelType(GrpcServerUtils.getChannelTypeClass()) | ||
.maxInboundMessageSize(maxMessageSize) | ||
.bossEventLoopGroup(this.bossEventLoopGroup) | ||
.workerEventLoopGroup(this.workerEventLoopGroup) | ||
.intercept(interceptor) | ||
.addService(service) | ||
.build(); | ||
} | ||
} |
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
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
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 mostly copied from GrpcServerUtils.java. We need to manage the state of event loop groups hence I created this class.