Java Code Examples for io.grpc.ClientCall#request()
The following examples show how to use
io.grpc.ClientCall#request() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadClient.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Override public void run() { while (true) { maxOutstanding.acquireUninterruptibly(); if (shutdown) { maxOutstanding.release(); return; } final ClientCall<ByteBuf, ByteBuf> call = channel.newCall(LoadServer.GENERIC_STREAMING_PING_PONG_METHOD, CallOptions.DEFAULT); call.start(new ClientCall.Listener<ByteBuf>() { long now = System.nanoTime(); @Override public void onMessage(ByteBuf message) { delay(System.nanoTime() - now); if (shutdown) { call.cancel("Shutting down", null); return; } call.request(1); call.sendMessage(genericRequest.slice()); now = System.nanoTime(); } @Override public void onClose(Status status, Metadata trailers) { maxOutstanding.release(); Level level = shutdown ? Level.FINE : Level.INFO; if (!status.isOk() && status.getCode() != Status.Code.CANCELLED) { log.log(level, "Error in Generic Async Ping-Pong call", status.getCause()); } } }, new Metadata()); call.request(1); call.sendMessage(genericRequest.slice()); } }
Example 2
Source File: ClientCalls.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
private static <ReqT, RespT> void startCall( ClientCall<ReqT, RespT> call, ClientCall.Listener<RespT> responseListener, boolean streamingResponse) { //----begin----客户端的流量控制---- String fullMethodName = call.getFullMethod(); try { if (ConsumerRequestsControllerUtils.isNeedRequestsControl(fullMethodName)) { ConsumerRequestsControllerUtils.addRequestNum(fullMethodName); } } catch (Throwable t) { throw cancelThrow(call, t); } //----end----客户端的流量控制---- call.start(responseListener, new Metadata()); if (streamingResponse) { call.request(1); } else { // Initially ask for two responses from flow-control so that if a misbehaving server sends // more than one responses, we can catch it and fail it in the listener. call.request(2); } }
Example 3
Source File: GrpcStreamClientCall.java From saluki with Apache License 2.0 | 5 votes |
static void startCall(ClientCall<Message, Message> call, ClientCall.Listener<Message> responseListener, boolean streamingResponse) { call.start(responseListener, new Metadata()); if (streamingResponse) { call.request(1); } else { call.request(2); } }
Example 4
Source File: LoadClient.java From grpc-java with Apache License 2.0 | 5 votes |
@Override public void run() { while (true) { maxOutstanding.acquireUninterruptibly(); if (shutdown) { maxOutstanding.release(); return; } final ClientCall<ByteBuf, ByteBuf> call = channel.newCall(LoadServer.GENERIC_STREAMING_PING_PONG_METHOD, CallOptions.DEFAULT); call.start(new ClientCall.Listener<ByteBuf>() { long now = System.nanoTime(); @Override public void onMessage(ByteBuf message) { delay(System.nanoTime() - now); if (shutdown) { call.cancel("Shutting down", null); return; } call.request(1); call.sendMessage(genericRequest.slice()); now = System.nanoTime(); } @Override public void onClose(Status status, Metadata trailers) { maxOutstanding.release(); Level level = shutdown ? Level.FINE : Level.INFO; if (!status.isOk() && status.getCode() != Status.Code.CANCELLED) { log.log(level, "Error in Generic Async Ping-Pong call", status.getCause()); } } }, new Metadata()); call.request(1); call.sendMessage(genericRequest.slice()); } }
Example 5
Source File: AbstractInteropTest.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Test public void serverStreamingShouldBeFlowControlled() throws Exception { final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder() .addResponseParameters(ResponseParameters.newBuilder().setSize(100000)) .addResponseParameters(ResponseParameters.newBuilder().setSize(100001)) .build(); final List<StreamingOutputCallResponse> goldenResponses = Arrays.asList( StreamingOutputCallResponse.newBuilder() .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[100000]))).build(), StreamingOutputCallResponse.newBuilder() .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[100001]))).build()); long start = System.nanoTime(); final ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(10); ClientCall<StreamingOutputCallRequest, StreamingOutputCallResponse> call = channel.newCall(TestServiceGrpc.getStreamingOutputCallMethod(), CallOptions.DEFAULT); call.start(new ClientCall.Listener<StreamingOutputCallResponse>() { @Override public void onHeaders(Metadata headers) {} @Override public void onMessage(final StreamingOutputCallResponse message) { queue.add(message); } @Override public void onClose(Status status, Metadata trailers) { queue.add(status); } }, new Metadata()); call.sendMessage(request); call.halfClose(); // Time how long it takes to get the first response. call.request(1); Object response = queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS); assertTrue(response instanceof StreamingOutputCallResponse); assertResponse(goldenResponses.get(0), (StreamingOutputCallResponse) response); long firstCallDuration = System.nanoTime() - start; // Without giving additional flow control, make sure that we don't get another response. We wait // until we are comfortable the next message isn't coming. We may have very low nanoTime // resolution (like on Windows) or be using a testing, in-process transport where message // handling is instantaneous. In both cases, firstCallDuration may be 0, so round up sleep time // to at least 1ms. assertNull(queue.poll(Math.max(firstCallDuration * 4, 1 * 1000 * 1000), TimeUnit.NANOSECONDS)); // Make sure that everything still completes. call.request(1); response = queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS); assertTrue(response instanceof StreamingOutputCallResponse); assertResponse(goldenResponses.get(1), (StreamingOutputCallResponse) response); assertEquals(Status.OK, queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS)); }
Example 6
Source File: AbstractBenchmark.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
/** * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when * {@code done.get()} is true. Each completed call will increment the counter by the specified * delta which benchmarks can use to measure messages per second or bandwidth. */ protected CountDownLatch startFlowControlledStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) { final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length); for (final ManagedChannel channel : channels) { for (int i = 0; i < callsPerChannel; i++) { final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(flowControlledStreaming, CALL_OPTIONS); final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<StreamObserver<ByteBuf>>(); final AtomicBoolean ignoreMessages = new AtomicBoolean(); StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall( streamingCall, new StreamObserver<ByteBuf>() { @Override public void onNext(ByteBuf value) { StreamObserver<ByteBuf> obs = requestObserverRef.get(); if (done.get()) { if (!ignoreMessages.getAndSet(true)) { obs.onCompleted(); } return; } if (record.get()) { counter.addAndGet(counterDelta); } // request is called automatically because the observer implicitly has auto // inbound flow control } @Override public void onError(Throwable t) { logger.log(Level.WARNING, "call error", t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }); requestObserverRef.set(requestObserver); // Add some outstanding requests to ensure the server is filling the connection streamingCall.request(5); requestObserver.onNext(request.slice()); } } return latch; }
Example 7
Source File: ServerCallsTest.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Test public void inprocessTransportManualFlow() throws Exception { final Semaphore semaphore = new Semaphore(1); ServerServiceDefinition service = ServerServiceDefinition.builder( new ServiceDescriptor("some", STREAMING_METHOD)) .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall( new ServerCalls.BidiStreamingMethod<Integer, Integer>() { int iteration; @Override public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) { final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver; serverCallObserver.setOnReadyHandler(new Runnable() { @Override public void run() { while (serverCallObserver.isReady()) { serverCallObserver.onNext(iteration); } iteration++; semaphore.release(); } }); return new ServerCalls.NoopStreamObserver<Integer>() { @Override public void onCompleted() { serverCallObserver.onCompleted(); } }; } })) .build(); long tag = System.nanoTime(); InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start(); ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build(); final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT); final CountDownLatch latch = new CountDownLatch(1); final int[] receivedMessages = new int[6]; clientCall.start(new ClientCall.Listener<Integer>() { int index; @Override public void onMessage(Integer message) { receivedMessages[index++] = message; } @Override public void onClose(Status status, Metadata trailers) { latch.countDown(); } }, new Metadata()); semaphore.acquire(); clientCall.request(1); semaphore.acquire(); clientCall.request(2); semaphore.acquire(); clientCall.request(3); clientCall.halfClose(); latch.await(5, TimeUnit.SECONDS); // Very that number of messages produced in each onReady handler call matches the number // requested by the client. assertArrayEquals(new int[]{0, 1, 1, 2, 2, 2}, receivedMessages); }
Example 8
Source File: ByteStreamServiceTest.java From bazel-buildfarm with Apache License 2.0 | 4 votes |
@Test public void writePutsIntoBlobStore() throws IOException, InterruptedException { ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!"); Digest digest = DIGEST_UTIL.compute(helloWorld); String uuid = UUID.randomUUID().toString(); String resourceName = createBlobUploadResourceName(uuid, digest); Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(); ClientCall<WriteRequest, WriteResponse> call = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT); ClientCall.Listener<WriteResponse> callListener = new ClientCall.Listener<WriteResponse>() { boolean complete = false; boolean callHalfClosed = false; @Override public void onReady() { while (call.isReady()) { if (complete) { if (!callHalfClosed) { call.halfClose(); callHalfClosed = true; } return; } call.sendMessage( WriteRequest.newBuilder() .setResourceName(resourceName) .setData(helloWorld) .setFinishWrite(true) .build()); complete = true; } } }; call.start(callListener, new Metadata()); call.request(1); verify(simpleBlobStore, times(1)) .put(eq(digest.getHash()), eq(digest.getSizeBytes()), any(InputStream.class)); }
Example 9
Source File: ByteStreamServiceTest.java From bazel-buildfarm with Apache License 2.0 | 4 votes |
@Test public void writeCanBeResumed() throws IOException, InterruptedException { ByteString helloWorld = ByteString.copyFromUtf8("Hello, World!"); Digest digest = DIGEST_UTIL.compute(helloWorld); String uuid = UUID.randomUUID().toString(); String resourceName = createBlobUploadResourceName(uuid, digest); Channel channel = InProcessChannelBuilder.forName(fakeServerName).directExecutor().build(); ClientCall<WriteRequest, WriteResponse> initialCall = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT); ByteString initialData = helloWorld.substring(0, 6); ClientCall.Listener<WriteResponse> initialCallListener = new ClientCall.Listener<WriteResponse>() { boolean complete = false; boolean callHalfClosed = false; @Override public void onReady() { while (initialCall.isReady()) { if (complete) { if (!callHalfClosed) { initialCall.halfClose(); callHalfClosed = true; } return; } initialCall.sendMessage( WriteRequest.newBuilder() .setResourceName(resourceName) .setData(initialData) .build()); complete = true; } } }; initialCall.start(initialCallListener, new Metadata()); initialCall.request(1); ByteStreamBlockingStub service = ByteStreamGrpc.newBlockingStub(channel); QueryWriteStatusResponse response = service.queryWriteStatus( QueryWriteStatusRequest.newBuilder().setResourceName(resourceName).build()); assertThat(response.getCommittedSize()).isEqualTo(initialData.size()); assertThat(response.getComplete()).isFalse(); ClientCall<WriteRequest, WriteResponse> finishCall = channel.newCall(ByteStreamGrpc.getWriteMethod(), CallOptions.DEFAULT); ClientCall.Listener<WriteResponse> finishCallListener = new ClientCall.Listener<WriteResponse>() { boolean complete = false; boolean callHalfClosed = false; @Override public void onReady() { while (finishCall.isReady()) { if (complete) { if (!callHalfClosed) { finishCall.halfClose(); callHalfClosed = true; } return; } finishCall.sendMessage( WriteRequest.newBuilder() .setResourceName(resourceName) .setWriteOffset(initialData.size()) .setData(helloWorld.substring(initialData.size())) .setFinishWrite(true) .build()); complete = true; } } }; finishCall.start(finishCallListener, new Metadata()); finishCall.request(1); ArgumentCaptor<InputStream> inputStreamCaptor = ArgumentCaptor.forClass(InputStream.class); verify(simpleBlobStore, times(1)) .put(eq(digest.getHash()), eq(digest.getSizeBytes()), inputStreamCaptor.capture()); InputStream inputStream = inputStreamCaptor.getValue(); assertThat(inputStream.available()).isEqualTo(helloWorld.size()); byte[] data = new byte[helloWorld.size()]; assertThat(inputStream.read(data)).isEqualTo(helloWorld.size()); assertThat(data).isEqualTo(helloWorld.toByteArray()); }
Example 10
Source File: AbstractInteropTest.java From grpc-java with Apache License 2.0 | 4 votes |
@Test public void serverStreamingShouldBeFlowControlled() throws Exception { final StreamingOutputCallRequest request = StreamingOutputCallRequest.newBuilder() .addResponseParameters(ResponseParameters.newBuilder().setSize(100000)) .addResponseParameters(ResponseParameters.newBuilder().setSize(100001)) .build(); final List<StreamingOutputCallResponse> goldenResponses = Arrays.asList( StreamingOutputCallResponse.newBuilder() .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[100000]))).build(), StreamingOutputCallResponse.newBuilder() .setPayload(Payload.newBuilder() .setBody(ByteString.copyFrom(new byte[100001]))).build()); long start = System.nanoTime(); final ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(10); ClientCall<StreamingOutputCallRequest, StreamingOutputCallResponse> call = channel.newCall(TestServiceGrpc.getStreamingOutputCallMethod(), CallOptions.DEFAULT); call.start(new ClientCall.Listener<StreamingOutputCallResponse>() { @Override public void onHeaders(Metadata headers) {} @Override public void onMessage(final StreamingOutputCallResponse message) { queue.add(message); } @Override public void onClose(Status status, Metadata trailers) { queue.add(status); } }, new Metadata()); call.sendMessage(request); call.halfClose(); // Time how long it takes to get the first response. call.request(1); Object response = queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS); assertTrue(response instanceof StreamingOutputCallResponse); assertResponse(goldenResponses.get(0), (StreamingOutputCallResponse) response); long firstCallDuration = System.nanoTime() - start; // Without giving additional flow control, make sure that we don't get another response. We wait // until we are comfortable the next message isn't coming. We may have very low nanoTime // resolution (like on Windows) or be using a testing, in-process transport where message // handling is instantaneous. In both cases, firstCallDuration may be 0, so round up sleep time // to at least 1ms. assertNull(queue.poll(Math.max(firstCallDuration * 4, 1 * 1000 * 1000), TimeUnit.NANOSECONDS)); // Make sure that everything still completes. call.request(1); response = queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS); assertTrue(response instanceof StreamingOutputCallResponse); assertResponse(goldenResponses.get(1), (StreamingOutputCallResponse) response); assertEquals(Status.OK, queue.poll(operationTimeoutMillis(), TimeUnit.MILLISECONDS)); }
Example 11
Source File: XdsTestClient.java From grpc-java with Apache License 2.0 | 4 votes |
private void runQps() throws InterruptedException, ExecutionException { final SettableFuture<Void> failure = SettableFuture.create(); final class PeriodicRpc implements Runnable { @Override public void run() { final long requestId; final Set<XdsStatsWatcher> savedWatchers = new HashSet<>(); synchronized (lock) { currentRequestId += 1; requestId = currentRequestId; savedWatchers.addAll(watchers); } SimpleRequest request = SimpleRequest.newBuilder().setFillServerId(true).build(); ManagedChannel channel = channels.get((int) (requestId % channels.size())); final ClientCall<SimpleRequest, SimpleResponse> call = channel.newCall( TestServiceGrpc.getUnaryCallMethod(), CallOptions.DEFAULT.withDeadlineAfter(rpcTimeoutSec, TimeUnit.SECONDS)); call.start( new ClientCall.Listener<SimpleResponse>() { private String hostname; @Override public void onMessage(SimpleResponse response) { hostname = response.getHostname(); // TODO(ericgribkoff) Currently some test environments cannot access the stats RPC // service and rely on parsing stdout. if (printResponse) { System.out.println( "Greeting: Hello world, this is " + hostname + ", from " + call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); } } @Override public void onClose(Status status, Metadata trailers) { if (printResponse && !status.isOk()) { logger.log(Level.WARNING, "Greeting RPC failed with status {0}", status); } for (XdsStatsWatcher watcher : savedWatchers) { watcher.rpcCompleted(requestId, hostname); } } }, new Metadata()); call.sendMessage(request); call.request(1); call.halfClose(); } } long nanosPerQuery = TimeUnit.SECONDS.toNanos(1) / qps; ListenableScheduledFuture<?> future = exec.scheduleAtFixedRate(new PeriodicRpc(), 0, nanosPerQuery, TimeUnit.NANOSECONDS); Futures.addCallback( future, new FutureCallback<Object>() { @Override public void onFailure(Throwable t) { failure.setException(t); } @Override public void onSuccess(Object o) {} }, MoreExecutors.directExecutor()); failure.get(); }
Example 12
Source File: AbstractBenchmark.java From grpc-java with Apache License 2.0 | 4 votes |
/** * Start a continuously executing set of duplex streaming ping-pong calls that will terminate when * {@code done.get()} is true. Each completed call will increment the counter by the specified * delta which benchmarks can use to measure messages per second or bandwidth. */ protected CountDownLatch startFlowControlledStreamingCalls(int callsPerChannel, final AtomicLong counter, final AtomicBoolean record, final AtomicBoolean done, final long counterDelta) { final CountDownLatch latch = new CountDownLatch(callsPerChannel * channels.length); for (final ManagedChannel channel : channels) { for (int i = 0; i < callsPerChannel; i++) { final ClientCall<ByteBuf, ByteBuf> streamingCall = channel.newCall(flowControlledStreaming, CALL_OPTIONS); final AtomicReference<StreamObserver<ByteBuf>> requestObserverRef = new AtomicReference<>(); final AtomicBoolean ignoreMessages = new AtomicBoolean(); StreamObserver<ByteBuf> requestObserver = ClientCalls.asyncBidiStreamingCall( streamingCall, new StreamObserver<ByteBuf>() { @Override public void onNext(ByteBuf value) { StreamObserver<ByteBuf> obs = requestObserverRef.get(); if (done.get()) { if (!ignoreMessages.getAndSet(true)) { obs.onCompleted(); } return; } if (record.get()) { counter.addAndGet(counterDelta); } // request is called automatically because the observer implicitly has auto // inbound flow control } @Override public void onError(Throwable t) { logger.log(Level.WARNING, "call error", t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }); requestObserverRef.set(requestObserver); // Add some outstanding requests to ensure the server is filling the connection streamingCall.request(5); requestObserver.onNext(request.slice()); } } return latch; }
Example 13
Source File: ServerCallsTest.java From grpc-java with Apache License 2.0 | 4 votes |
@Test public void inprocessTransportManualFlow() throws Exception { final Semaphore semaphore = new Semaphore(1); ServerServiceDefinition service = ServerServiceDefinition.builder( new ServiceDescriptor("some", STREAMING_METHOD)) .addMethod(STREAMING_METHOD, ServerCalls.asyncBidiStreamingCall( new ServerCalls.BidiStreamingMethod<Integer, Integer>() { int iteration; @Override public StreamObserver<Integer> invoke(StreamObserver<Integer> responseObserver) { final ServerCallStreamObserver<Integer> serverCallObserver = (ServerCallStreamObserver<Integer>) responseObserver; serverCallObserver.setOnReadyHandler(new Runnable() { @Override public void run() { while (serverCallObserver.isReady()) { serverCallObserver.onNext(iteration); } iteration++; semaphore.release(); } }); return new ServerCalls.NoopStreamObserver<Integer>() { @Override public void onCompleted() { serverCallObserver.onCompleted(); } }; } })) .build(); long tag = System.nanoTime(); InProcessServerBuilder.forName("go-with-the-flow" + tag).addService(service).build().start(); ManagedChannel channel = InProcessChannelBuilder.forName("go-with-the-flow" + tag).build(); final ClientCall<Integer, Integer> clientCall = channel.newCall(STREAMING_METHOD, CallOptions.DEFAULT); final CountDownLatch latch = new CountDownLatch(1); final int[] receivedMessages = new int[6]; clientCall.start(new ClientCall.Listener<Integer>() { int index; @Override public void onMessage(Integer message) { receivedMessages[index++] = message; } @Override public void onClose(Status status, Metadata trailers) { latch.countDown(); } }, new Metadata()); semaphore.acquire(); clientCall.request(1); semaphore.acquire(); clientCall.request(2); semaphore.acquire(); clientCall.request(3); clientCall.halfClose(); assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); // Very that number of messages produced in each onReady handler call matches the number // requested by the client. assertArrayEquals(new int[]{0, 1, 1, 2, 2, 2}, receivedMessages); }