Search in sources :

Example 1 with ServerCallStreamObserver

use of io.grpc.stub.ServerCallStreamObserver in project ballerina by ballerina-lang.

the class IsCancelled method execute.

@Override
public void execute(Context context) {
    BStruct endpointClient = (BStruct) context.getRefArgument(CLIENT_RESPONDER_REF_INDEX);
    StreamObserver responseObserver = MessageUtils.getResponseObserver(endpointClient);
    if (responseObserver instanceof ServerCallStreamObserver) {
        ServerCallStreamObserver serverCallStreamObserver = (ServerCallStreamObserver) responseObserver;
        context.setReturnValues(new BBoolean(serverCallStreamObserver.isCancelled()));
    } else {
        context.setReturnValues(new BBoolean(Boolean.TRUE));
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) BStruct(org.ballerinalang.model.values.BStruct) BBoolean(org.ballerinalang.model.values.BBoolean)

Example 2 with ServerCallStreamObserver

use of io.grpc.stub.ServerCallStreamObserver in project pinpoint by naver.

the class HelloWorldStreamServer method start.

@PostConstruct
public void start() throws IOException {
    StreamingGreeterGrpc.StreamingGreeterImplBase svc = new StreamingGreeterGrpc.StreamingGreeterImplBase() {

        @Override
        public StreamObserver<HelloRequest> sayHelloStreaming(final StreamObserver<HelloReply> responseObserver) {
            // Set up manual flow control for the request stream. It feels backwards to configure the request
            // stream's flow control using the response stream's observer, but this is the way it is.
            final ServerCallStreamObserver<HelloReply> serverCallStreamObserver = (ServerCallStreamObserver<HelloReply>) responseObserver;
            serverCallStreamObserver.disableAutoInboundFlowControl();
            // Guard against spurious onReady() calls caused by a race between onNext() and onReady(). If the transport
            // toggles isReady() from false to true while onNext() is executing, but before onNext() checks isReady(),
            // request(1) would be called twice - once by onNext() and once by the onReady() scheduled during onNext()'s
            // execution.
            final AtomicBoolean wasReady = new AtomicBoolean(false);
            // Set up a back-pressure-aware consumer for the request stream. The onReadyHandler will be invoked
            // when the consuming side has enough buffer space to receive more messages.
            // 
            // Note: the onReadyHandler's invocation is serialized on the same thread pool as the incoming StreamObserver's
            // onNext(), onError(), and onComplete() handlers. Blocking the onReadyHandler will prevent additional messages
            // from being processed by the incoming StreamObserver. The onReadyHandler must return in a timely manor or else
            // message processing throughput will suffer.
            serverCallStreamObserver.setOnReadyHandler(new Runnable() {

                public void run() {
                    if (serverCallStreamObserver.isReady() && wasReady.compareAndSet(false, true)) {
                        logger.info("READY");
                        // Signal the request sender to send one message. This happens when isReady() turns true, signaling that
                        // the receive buffer has enough free space to receive more messages. Calling request() serves to prime
                        // the message pump.
                        serverCallStreamObserver.request(1);
                    }
                }
            });
            // Give gRPC a StreamObserver that can observe and process incoming requests.
            return new StreamObserver<HelloRequest>() {

                @Override
                public void onNext(HelloRequest request) {
                    requestCount++;
                    // Process the request and send a response or an error.
                    try {
                        // Accept and enqueue the request.
                        String name = request.getName();
                        logger.info("--> " + name);
                        // Simulate server "work"
                        Thread.sleep(100);
                        // Send a response.
                        String message = "Hello " + name;
                        logger.info("<-- " + message);
                        HelloReply reply = HelloReply.newBuilder().setMessage(message).build();
                        responseObserver.onNext(reply);
                        // Check the provided ServerCallStreamObserver to see if it is still ready to accept more messages.
                        if (serverCallStreamObserver.isReady()) {
                            // Signal the sender to send another request. As long as isReady() stays true, the server will keep
                            // cycling through the loop of onNext() -> request()...onNext() -> request()... until either the client
                            // runs out of messages and ends the loop or the server runs out of receive buffer space.
                            // 
                            // If the server runs out of buffer space, isReady() will turn false. When the receive buffer has
                            // sufficiently drained, isReady() will turn true, and the serverCallStreamObserver's onReadyHandler
                            // will be called to restart the message pump.
                            serverCallStreamObserver.request(1);
                        } else {
                            // If not, note that back-pressure has begun.
                            wasReady.set(false);
                        }
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                        responseObserver.onError(Status.UNKNOWN.withDescription("Error handling request").withCause(throwable).asException());
                    }
                }

                @Override
                public void onError(Throwable t) {
                    // End the response stream if the client presents an error.
                    t.printStackTrace();
                    responseObserver.onCompleted();
                }

                @Override
                public void onCompleted() {
                    // Signal the end of work when the client ends the request stream.
                    logger.info("COMPLETED");
                    responseObserver.onCompleted();
                }
            };
        }
    };
    bindPort = SocketUtils.findAvailableTcpPort(27675);
    NettyServerBuilder serverBuilder = NettyServerBuilder.forPort(bindPort);
    serverBuilder.bossEventLoopGroup(eventExecutors);
    serverBuilder.workerEventLoopGroup(eventExecutors);
    serverBuilder.channelType(NioServerSocketChannel.class);
    serverBuilder.addService(svc);
    this.server = serverBuilder.build().start();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) NettyServerBuilder(io.grpc.netty.NettyServerBuilder) HelloRequest(io.grpc.examples.manualflowcontrol.HelloRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamingGreeterGrpc(io.grpc.examples.manualflowcontrol.StreamingGreeterGrpc) HelloReply(io.grpc.examples.manualflowcontrol.HelloReply) PostConstruct(javax.annotation.PostConstruct)

Example 3 with ServerCallStreamObserver

use of io.grpc.stub.ServerCallStreamObserver in project grpc-java by grpc.

the class CascadingTest method startChainingServer.

/**
 * Create a chain of client to server calls which can be cancelled top down.
 *
 * @return a Future that completes when call chain is created
 */
private Future<?> startChainingServer(final int depthThreshold) throws IOException {
    final AtomicInteger serversReady = new AtomicInteger();
    final SettableFuture<Void> chainReady = SettableFuture.create();
    class ChainingService extends TestServiceGrpc.TestServiceImplBase {

        @Override
        public void unaryCall(final SimpleRequest request, final StreamObserver<SimpleResponse> responseObserver) {
            ((ServerCallStreamObserver) responseObserver).setOnCancelHandler(new Runnable() {

                @Override
                public void run() {
                    receivedCancellations.countDown();
                }
            });
            if (serversReady.incrementAndGet() == depthThreshold) {
                // Stop recursion
                chainReady.set(null);
                return;
            }
            Context.currentContextExecutor(otherWork).execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        blockingStub.unaryCall(request);
                    } catch (StatusRuntimeException e) {
                        Status status = e.getStatus();
                        if (status.getCode() == Status.Code.CANCELLED) {
                            observedCancellations.countDown();
                        } else {
                            responseObserver.onError(e);
                        }
                    }
                }
            });
        }
    }
    server = InProcessServerBuilder.forName("channel").executor(otherWork).addService(new ChainingService()).build().start();
    return chainReady;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Status(io.grpc.Status) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleRequest(io.grpc.testing.integration.Messages.SimpleRequest)

Example 4 with ServerCallStreamObserver

use of io.grpc.stub.ServerCallStreamObserver in project grpc-java by grpc.

the class ManualFlowControlServer method main.

public static void main(String[] args) throws InterruptedException, IOException {
    // Service class implementation
    StreamingGreeterGrpc.StreamingGreeterImplBase svc = new StreamingGreeterGrpc.StreamingGreeterImplBase() {

        @Override
        public StreamObserver<HelloRequest> sayHelloStreaming(final StreamObserver<HelloReply> responseObserver) {
            // Set up manual flow control for the request stream. It feels backwards to configure the request
            // stream's flow control using the response stream's observer, but this is the way it is.
            final ServerCallStreamObserver<HelloReply> serverCallStreamObserver = (ServerCallStreamObserver<HelloReply>) responseObserver;
            serverCallStreamObserver.disableAutoRequest();
            // else message processing throughput will suffer.
            class OnReadyHandler implements Runnable {

                // Guard against spurious onReady() calls caused by a race between onNext() and onReady(). If the transport
                // toggles isReady() from false to true while onNext() is executing, but before onNext() checks isReady(),
                // request(1) would be called twice - once by onNext() and once by the onReady() scheduled during onNext()'s
                // execution.
                private boolean wasReady = false;

                @Override
                public void run() {
                    if (serverCallStreamObserver.isReady() && !wasReady) {
                        wasReady = true;
                        logger.info("READY");
                        // Signal the request sender to send one message. This happens when isReady() turns true, signaling that
                        // the receive buffer has enough free space to receive more messages. Calling request() serves to prime
                        // the message pump.
                        serverCallStreamObserver.request(1);
                    }
                }
            }
            final OnReadyHandler onReadyHandler = new OnReadyHandler();
            serverCallStreamObserver.setOnReadyHandler(onReadyHandler);
            // Give gRPC a StreamObserver that can observe and process incoming requests.
            return new StreamObserver<HelloRequest>() {

                @Override
                public void onNext(HelloRequest request) {
                    // Process the request and send a response or an error.
                    try {
                        // Accept and enqueue the request.
                        String name = request.getName();
                        logger.info("--> " + name);
                        // Simulate server "work"
                        Thread.sleep(100);
                        // Send a response.
                        String message = "Hello " + name;
                        logger.info("<-- " + message);
                        HelloReply reply = HelloReply.newBuilder().setMessage(message).build();
                        responseObserver.onNext(reply);
                        // Check the provided ServerCallStreamObserver to see if it is still ready to accept more messages.
                        if (serverCallStreamObserver.isReady()) {
                            // Signal the sender to send another request. As long as isReady() stays true, the server will keep
                            // cycling through the loop of onNext() -> request(1)...onNext() -> request(1)... until the client runs
                            // out of messages and ends the loop (via onCompleted()).
                            // 
                            // If request() was called here with the argument of more than 1, the server might runs out of receive
                            // buffer space, and isReady() will turn false. When the receive buffer has sufficiently drained,
                            // isReady() will turn true, and the serverCallStreamObserver's onReadyHandler will be called to restart
                            // the message pump.
                            serverCallStreamObserver.request(1);
                        } else {
                            // If not, note that back-pressure has begun.
                            onReadyHandler.wasReady = false;
                        }
                    } catch (Throwable throwable) {
                        throwable.printStackTrace();
                        responseObserver.onError(Status.UNKNOWN.withDescription("Error handling request").withCause(throwable).asException());
                    }
                }

                @Override
                public void onError(Throwable t) {
                    // End the response stream if the client presents an error.
                    t.printStackTrace();
                    responseObserver.onCompleted();
                }

                @Override
                public void onCompleted() {
                    // Signal the end of work when the client ends the request stream.
                    logger.info("COMPLETED");
                    responseObserver.onCompleted();
                }
            };
        }
    };
    final Server server = ServerBuilder.forPort(50051).addService(svc).build().start();
    logger.info("Listening on " + server.getPort());
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // Use stderr here since the logger may have been reset by its JVM shutdown hook.
            System.err.println("Shutting down");
            try {
                server.shutdown().awaitTermination(30, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace(System.err);
            }
        }
    });
    server.awaitTermination();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Server(io.grpc.Server)

Example 5 with ServerCallStreamObserver

use of io.grpc.stub.ServerCallStreamObserver in project grpc-java by grpc.

the class TestServiceImpl method unaryCall.

/**
 * Immediately responds with a payload of the type and size specified in the request.
 */
@Override
public void unaryCall(SimpleRequest req, StreamObserver<SimpleResponse> responseObserver) {
    ServerCallStreamObserver<SimpleResponse> obs = (ServerCallStreamObserver<SimpleResponse>) responseObserver;
    SimpleResponse.Builder responseBuilder = SimpleResponse.newBuilder();
    try {
        if (req.hasResponseCompressed() && req.getResponseCompressed().getValue()) {
            obs.setCompression("gzip");
        } else {
            obs.setCompression("identity");
        }
    } catch (IllegalArgumentException e) {
        obs.onError(Status.UNIMPLEMENTED.withDescription("compression not supported.").withCause(e).asRuntimeException());
        return;
    }
    if (req.getResponseSize() != 0) {
        // For consistency with the c++ TestServiceImpl, use a random offset for unary calls.
        // TODO(wonderfly): whether or not this is a good approach needs further discussion.
        int offset = random.nextInt(compressableBuffer.size());
        ByteString payload = generatePayload(compressableBuffer, offset, req.getResponseSize());
        responseBuilder.setPayload(Payload.newBuilder().setBody(payload));
    }
    if (req.hasResponseStatus()) {
        obs.onError(Status.fromCodeValue(req.getResponseStatus().getCode()).withDescription(req.getResponseStatus().getMessage()).asRuntimeException());
        return;
    }
    responseObserver.onNext(responseBuilder.build());
    responseObserver.onCompleted();
}
Also used : ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ByteString(com.google.protobuf.ByteString) SimpleResponse(io.grpc.testing.integration.Messages.SimpleResponse)

Aggregations

ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)7 StreamObserver (io.grpc.stub.StreamObserver)6 AgentInfo (com.navercorp.pinpoint.collector.cluster.AgentInfo)2 PinpointGrpcServer (com.navercorp.pinpoint.collector.receiver.grpc.PinpointGrpcServer)2 PCmdMessage (com.navercorp.pinpoint.grpc.trace.PCmdMessage)2 PCmdRequest (com.navercorp.pinpoint.grpc.trace.PCmdRequest)2 PCmdResponse (com.navercorp.pinpoint.grpc.trace.PCmdResponse)2 StatusException (io.grpc.StatusException)2 ByteString (com.google.protobuf.ByteString)1 Server (io.grpc.Server)1 Status (io.grpc.Status)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 HelloReply (io.grpc.examples.manualflowcontrol.HelloReply)1 HelloRequest (io.grpc.examples.manualflowcontrol.HelloRequest)1 StreamingGreeterGrpc (io.grpc.examples.manualflowcontrol.StreamingGreeterGrpc)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 SimpleRequest (io.grpc.testing.integration.Messages.SimpleRequest)1 SimpleResponse (io.grpc.testing.integration.Messages.SimpleResponse)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1