Search in sources :

Example 1 with ClientCallStreamObserver

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

the class HelloWorldStreamClient method greet.

/**
 * Say hello to server.
 */
public void greet(final int callCount) throws InterruptedException {
    final CountDownLatch done = new CountDownLatch(1);
    ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply> clientResponseObserver = new ClientResponseObserver<io.grpc.examples.manualflowcontrol.HelloRequest, io.grpc.examples.manualflowcontrol.HelloReply>() {

        ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream;

        @Override
        public void beforeStart(final ClientCallStreamObserver<io.grpc.examples.manualflowcontrol.HelloRequest> requestStream) {
            this.requestStream = requestStream;
            // Set up manual flow control for the response stream. It feels backwards to configure the response
            // stream's flow control using the request stream's observer, but this is the way it is.
            requestStream.disableAutoInboundFlowControl();
            // Set up a back-pressure-aware producer for the request stream. The onReadyHandler will be invoked
            // when the consuming side has enough buffer space to receive more messages.
            // 
            // Messages are serialized into a transport-specific transmit buffer. Depending on the size of this buffer,
            // MANY messages may be buffered, however, they haven't yet been sent to the server. The server must call
            // request() to pull a buffered message from the client.
            // 
            // Note: the onReadyHandler's invocation is serialized on the same thread pool as the incoming
            // StreamObserver'sonNext(), 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.
            requestStream.setOnReadyHandler(new Runnable() {

                // An iterator is used so we can pause and resume iteration of the request data.
                Iterator<String> iterator = names().iterator();

                @Override
                public void run() {
                    int count = 0;
                    // Start generating values from where we left off on a non-gRPC thread.
                    while (requestStream.isReady()) {
                        if (iterator.hasNext() && callCount > count) {
                            // Send more messages if there are more messages to send.
                            String name = iterator.next();
                            logger.info("--> " + name);
                            io.grpc.examples.manualflowcontrol.HelloRequest request = io.grpc.examples.manualflowcontrol.HelloRequest.newBuilder().setName(name).build();
                            requestStream.onNext(request);
                            count++;
                        } else {
                            // Signal completion if there is nothing left to send.
                            requestStream.onCompleted();
                        }
                    }
                }
            });
        }

        @Override
        public void onNext(io.grpc.examples.manualflowcontrol.HelloReply value) {
            logger.info("<-- " + value.getMessage());
            // Signal the sender to send one message.
            requestStream.request(1);
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
            done.countDown();
        }

        @Override
        public void onCompleted() {
            logger.info("All Done");
            done.countDown();
        }
    };
    // Note: clientResponseObserver is handling both request and response stream processing.
    stub.sayHelloStreaming(clientResponseObserver);
    done.await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ClientResponseObserver(io.grpc.stub.ClientResponseObserver) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver)

Example 2 with ClientCallStreamObserver

use of io.grpc.stub.ClientCallStreamObserver in project alluxio by Alluxio.

the class BlockInStreamTest method before.

@Before
public void before() throws Exception {
    BlockWorkerClient workerClient = Mockito.mock(BlockWorkerClient.class);
    ClientCallStreamObserver requestObserver = Mockito.mock(ClientCallStreamObserver.class);
    when(requestObserver.isReady()).thenReturn(true);
    when(workerClient.openLocalBlock(any(StreamObserver.class))).thenAnswer(new Answer() {

        public Object answer(InvocationOnMock invocation) {
            mResponseObserver = invocation.getArgument(0, StreamObserver.class);
            return requestObserver;
        }
    });
    doAnswer(invocation -> {
        mResponseObserver.onNext(OpenLocalBlockResponse.newBuilder().setPath("/tmp").build());
        mResponseObserver.onCompleted();
        return null;
    }).when(requestObserver).onNext(any(OpenLocalBlockRequest.class));
    mMockContext = Mockito.mock(FileSystemContext.class);
    when(mMockContext.acquireBlockWorkerClient(ArgumentMatchers.any(WorkerNetAddress.class))).thenReturn(new NoopClosableResource<>(workerClient));
    when(mMockContext.getClientContext()).thenReturn(ClientContext.create(mConf));
    when(mMockContext.getClusterConf()).thenReturn(mConf);
    mInfo = new BlockInfo().setBlockId(1);
    mOptions = new InStreamOptions(new URIStatus(new FileInfo().setBlockIds(Collections.singletonList(1L))), mConf);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) URIStatus(alluxio.client.file.URIStatus) InStreamOptions(alluxio.client.file.options.InStreamOptions) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) FileInfo(alluxio.wire.FileInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo) FileSystemContext(alluxio.client.file.FileSystemContext) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) OpenLocalBlockRequest(alluxio.grpc.OpenLocalBlockRequest) Before(org.junit.Before)

Example 3 with ClientCallStreamObserver

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

the class AbstractInteropTest method clientCompressedStreaming.

/**
 * Tests client per-message compression for streaming calls. The Java API does not support
 * inspecting a message's compression level, so this is primarily intended to run against a gRPC
 * C++ server.
 */
public void clientCompressedStreaming(boolean probe) throws Exception {
    final StreamingInputCallRequest expectCompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(true)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[27182]))).build();
    final StreamingInputCallRequest expectUncompressedRequest = StreamingInputCallRequest.newBuilder().setExpectCompressed(BoolValue.newBuilder().setValue(false)).setPayload(Payload.newBuilder().setBody(ByteString.copyFrom(new byte[45904]))).build();
    final StreamingInputCallResponse goldenResponse = StreamingInputCallResponse.newBuilder().setAggregatedPayloadSize(73086).build();
    StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create();
    StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver);
    if (probe) {
        // Send a non-compressed message with expectCompress=true. Servers supporting this test case
        // should return INVALID_ARGUMENT.
        requestObserver.onNext(expectCompressedRequest);
        responseObserver.awaitCompletion(operationTimeoutMillis(), TimeUnit.MILLISECONDS);
        Throwable e = responseObserver.getError();
        assertNotNull("expected INVALID_ARGUMENT", e);
        assertEquals(Status.INVALID_ARGUMENT.getCode(), Status.fromThrowable(e).getCode());
    }
    // Start a new stream
    responseObserver = StreamRecorder.create();
    @SuppressWarnings("unchecked") ClientCallStreamObserver<StreamingInputCallRequest> clientCallStreamObserver = (ClientCallStreamObserver) asyncStub.withCompression("gzip").streamingInputCall(responseObserver);
    clientCallStreamObserver.setMessageCompression(true);
    clientCallStreamObserver.onNext(expectCompressedRequest);
    clientCallStreamObserver.setMessageCompression(false);
    clientCallStreamObserver.onNext(expectUncompressedRequest);
    clientCallStreamObserver.onCompleted();
    responseObserver.awaitCompletion();
    assertSuccess(responseObserver);
    assertEquals(goldenResponse, responseObserver.firstValue().get());
}
Also used : StreamingInputCallRequest(io.grpc.testing.integration.Messages.StreamingInputCallRequest) StreamingInputCallResponse(io.grpc.testing.integration.Messages.StreamingInputCallResponse) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver)

Example 4 with ClientCallStreamObserver

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

the class SpanClientMock method newSpanStream.

private StreamObserver<PSpanMessage> newSpanStream() {
    System.out.println("### ");
    System.out.println("NEW SpanStream");
    System.out.println("###");
    StreamId spanId = StreamId.newStreamId("SpanStream");
    StreamEventListener<PSpanMessage> listener = new StreamEventListener<PSpanMessage>() {

        @Override
        public void start(ClientCallStreamObserver<PSpanMessage> requestStream) {
            spanStreamReconnector.reset();
        }

        @Override
        public void onError(Throwable t) {
            spanStreamReconnector.reconnect();
        }

        @Override
        public void onCompleted() {
            spanStreamReconnector.reconnect();
        }
    };
    ResponseStreamObserver<PSpanMessage, Empty> responseStreamObserver = new ResponseStreamObserver<PSpanMessage, Empty>(listener);
    return spanStub.sendSpan(responseStreamObserver);
}
Also used : StreamId(com.navercorp.pinpoint.profiler.sender.grpc.StreamId) Empty(com.google.protobuf.Empty) PSpanMessage(com.navercorp.pinpoint.grpc.trace.PSpanMessage) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) ResponseStreamObserver(com.navercorp.pinpoint.profiler.sender.grpc.ResponseStreamObserver) StreamEventListener(com.navercorp.pinpoint.profiler.sender.grpc.StreamEventListener)

Example 5 with ClientCallStreamObserver

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

the class DefaultStreamTask method start.

@Override
public void start() {
    this.latch = new CountDownLatch(1);
    StreamJob<ReqT> job = new StreamJob<ReqT>() {

        @Override
        public Future<?> start(final ClientCallStreamObserver<ReqT> requestStream) {
            Runnable runnable = DefaultStreamTask.this.newRunnable(requestStream, latch);
            StreamExecutor<ReqT> streamExecutor = streamExecutorFactory.newStreamExecutor();
            return streamExecutor.execute(runnable);
        }

        @Override
        public String toString() {
            return streamId.toString();
        }
    };
    this.stream = clientStreamingService.newStream(job);
}
Also used : NamedRunnable(com.navercorp.pinpoint.profiler.util.NamedRunnable) CountDownLatch(java.util.concurrent.CountDownLatch) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver)

Aggregations

ClientCallStreamObserver (io.grpc.stub.ClientCallStreamObserver)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClientResponseObserver (io.grpc.stub.ClientResponseObserver)2 FileSystemContext (alluxio.client.file.FileSystemContext)1 URIStatus (alluxio.client.file.URIStatus)1 InStreamOptions (alluxio.client.file.options.InStreamOptions)1 OpenLocalBlockRequest (alluxio.grpc.OpenLocalBlockRequest)1 BlockInfo (alluxio.wire.BlockInfo)1 FileInfo (alluxio.wire.FileInfo)1 WorkerNetAddress (alluxio.wire.WorkerNetAddress)1 Empty (com.google.protobuf.Empty)1 PSpanMessage (com.navercorp.pinpoint.grpc.trace.PSpanMessage)1 ResponseStreamObserver (com.navercorp.pinpoint.profiler.sender.grpc.ResponseStreamObserver)1 StreamEventListener (com.navercorp.pinpoint.profiler.sender.grpc.StreamEventListener)1 StreamId (com.navercorp.pinpoint.profiler.sender.grpc.StreamId)1 NamedRunnable (com.navercorp.pinpoint.profiler.util.NamedRunnable)1 ManagedChannel (io.grpc.ManagedChannel)1 StreamObserver (io.grpc.stub.StreamObserver)1 StreamingInputCallRequest (io.grpc.testing.integration.Messages.StreamingInputCallRequest)1 StreamingInputCallResponse (io.grpc.testing.integration.Messages.StreamingInputCallResponse)1