Skip to content
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

gRPC: more logging on failures #17795

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.function.Consumer;
import java.util.function.Function;

import org.jboss.logging.Logger;

import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
Expand All @@ -13,6 +15,8 @@
import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor;

public class ServerCalls {
private static final Logger log = Logger.getLogger(ServerCalls.class);

private static StreamCollector streamCollector = StreamCollector.NO_OP;

private ServerCalls() {
Expand All @@ -23,6 +27,13 @@ public static <I, O> void oneToOne(I request, StreamObserver<O> response, String
trySetCompression(response, compression);
try {
Uni<O> uni = implementation.apply(request);
if (uni == null) {
log.error("gRPC service method returned null instead of Uni. " +
"Please change the implementation to return a Uni object, either carrying a value or a failure," +
" or throw StatusRuntimeException");
response.onError(Status.fromCode(Status.Code.INTERNAL).asException());
return;
}
uni.subscribe().with(
new Consumer<O>() {
@Override
Expand All @@ -46,26 +57,32 @@ public static <I, O> void oneToMany(I request, StreamObserver<O> response, Strin
Function<I, Multi<O>> implementation) {
try {
streamCollector.add(response);
implementation.apply(request)
.subscribe().with(
new Consumer<O>() {
@Override
public void accept(O v) {
response.onNext(v);
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
onError(response, throwable);
}
},
new Runnable() {
@Override
public void run() {
onCompleted(response);
}
});
Multi<O> returnValue = implementation.apply(request);
if (returnValue == null) {
log.error("gRPC service method returned null instead of Multi. " +
"Please change the implementation to return a Multi object or throw StatusRuntimeException");
response.onError(Status.fromCode(Status.Code.INTERNAL).asException());
return;
}
returnValue.subscribe().with(
new Consumer<O>() {
@Override
public void accept(O v) {
response.onNext(v);
}
},
new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
onError(response, throwable);
}
},
new Runnable() {
@Override
public void run() {
onCompleted(response);
}
});
} catch (Throwable throwable) {
onError(response, toStatusFailure(throwable));
}
Expand All @@ -79,6 +96,13 @@ public static <I, O> StreamObserver<I> manyToOne(StreamObserver<O> response,
streamCollector.add(response);

Uni<O> uni = implementation.apply(input);
if (uni == null) {
log.error("gRPC service method returned null instead of Uni. " +
"Please change the implementation to return a Uni object, either carrying a value or a failure," +
" or throw StatusRuntimeException");
response.onError(Status.fromCode(Status.Code.INTERNAL).asException());
return null;
}
uni.subscribe().with(
new Consumer<O>() {
@Override
Expand Down Expand Up @@ -106,8 +130,14 @@ public static <I, O> StreamObserver<I> manyToMany(StreamObserver<O> response,
streamCollector.add(response);
UnicastProcessor<I> input = UnicastProcessor.create();
StreamObserver<I> pump = getStreamObserverFeedingProcessor(input);
Multi<O> uni = implementation.apply(input);
uni.subscribe().with(
Multi<O> multi = implementation.apply(input);
if (multi == null) {
log.error("gRPC service method returned null instead of Multi. " +
"Please change the implementation to return a Multi object or throw StatusRuntimeException");
response.onError(Status.fromCode(Status.Code.INTERNAL).asException());
return null;
}
multi.subscribe().with(
new Consumer<O>() {
@Override
public void accept(O v) {
Expand Down Expand Up @@ -179,6 +209,7 @@ private static Throwable toStatusFailure(Throwable throwable) {
if (throwable instanceof IllegalArgumentException) {
return Status.INVALID_ARGUMENT.withDescription(desc).asException();
}
log.warn("gRPC service threw an exception other than StatusRuntimeException", throwable);
return Status.fromThrowable(throwable)
.withDescription(desc)
.asException();
Expand Down