@Override public void log(LogMessage request, StreamObserver<EmptyMessage> responseObserver) { try { // FIXME put these in Cassandra and render in central UI LogEvent logEvent = request.getLogEvent(); Proto.Throwable t = logEvent.getThrowable(); if (t == null) { logger.warn( "{} -- {} -- {} -- {}", request.getServerId(), logEvent.getLevel(), logEvent.getLoggerName(), logEvent.getFormattedMessage()); } else { logger.warn( "{} -- {} -- {} -- {}\n{}", request.getServerId(), logEvent.getLevel(), logEvent.getLoggerName(), logEvent.getFormattedMessage(), t); } } catch (Throwable t) { responseObserver.onError(t); return; } responseObserver.onNext(EmptyMessage.getDefaultInstance()); responseObserver.onCompleted(); }
public void cancelAfterFirstResponse() throws Exception { final StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.responseParameters = new Messages.ResponseParameters[1]; request.responseParameters[0] = new ResponseParameters(); request.responseParameters[0].size = 31415; request.payload = new Payload(); request.payload.body = new byte[27182]; final StreamingOutputCallResponse goldenResponse = new StreamingOutputCallResponse(); goldenResponse.payload = new Payload(); goldenResponse.payload.type = Messages.COMPRESSABLE; goldenResponse.payload.body = new byte[31415]; ResponseObserver responseObserver = new ResponseObserver(); StreamObserver<StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver); requestObserver.onNext(request); Object response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); if (!(response instanceof Messages.StreamingOutputCallResponse)) { fail("Unexpected: " + response); } assertMessageEquals(goldenResponse, (Messages.StreamingOutputCallResponse) response); requestObserver.onError(new RuntimeException()); response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); if (!(response instanceof Throwable)) { fail("Unexpected: " + response); } assertCodeEquals(io.grpc.Status.CANCELLED, io.grpc.Status.fromThrowable((Throwable) response)); }
public void halfDuplexCallShouldSucceed() throws Exception { // Build the request. Integer[] responseSizes = {50, 100, 150, 200}; final StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.responseParameters = new ResponseParameters[responseSizes.length]; request.responseType = Messages.COMPRESSABLE; for (int i = 0; i < responseSizes.length; ++i) { request.responseParameters[i] = new ResponseParameters(); request.responseParameters[i].size = responseSizes[i]; request.responseParameters[i].intervalUs = 0; } StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<StreamingOutputCallRequest> requestStream = asyncStub.halfDuplexCall(recorder); final int numRequests = 10; for (int ix = numRequests; ix > 0; --ix) { requestStream.onNext(request); } requestStream.onCompleted(); assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertSuccess(recorder); assertEquals(responseSizes.length * numRequests, recorder.getValues().size()); for (int ix = 0; ix < recorder.getValues().size(); ++ix) { StreamingOutputCallResponse response = recorder.getValues().get(ix); assertEquals(Messages.COMPRESSABLE, response.payload.type); int length = response.payload.body.length; int expectedSize = responseSizes[ix % responseSizes.length]; assertEquals("comparison failed at index " + ix, expectedSize, length); } }
public void gracefulShutdown() throws Exception { StreamingOutputCallRequest[] requests = new StreamingOutputCallRequest[3]; requests[0] = new StreamingOutputCallRequest(); requests[0].responseParameters = new ResponseParameters[1]; requests[0].responseParameters[0] = new ResponseParameters(); requests[0].responseParameters[0].size = 3; requests[0].payload = new Payload(); requests[0].payload.body = new byte[2]; requests[1] = new StreamingOutputCallRequest(); requests[1].responseParameters = new ResponseParameters[1]; requests[1].responseParameters[0] = new ResponseParameters(); requests[1].responseParameters[0].size = 1; requests[1].payload = new Payload(); requests[1].payload.body = new byte[7]; requests[2] = new StreamingOutputCallRequest(); requests[2].responseParameters = new ResponseParameters[1]; requests[2].responseParameters[0] = new ResponseParameters(); requests[2].responseParameters[0].size = 4; requests[2].payload = new Payload(); requests[2].payload.body = new byte[1]; StreamingOutputCallResponse[] goldenResponses = new StreamingOutputCallResponse[3]; goldenResponses[0] = new StreamingOutputCallResponse(); goldenResponses[0].payload = new Payload(); goldenResponses[0].payload.type = Messages.COMPRESSABLE; goldenResponses[0].payload.body = new byte[3]; goldenResponses[1] = new StreamingOutputCallResponse(); goldenResponses[1].payload = new Payload(); goldenResponses[1].payload.type = Messages.COMPRESSABLE; goldenResponses[1].payload.body = new byte[1]; goldenResponses[2] = new StreamingOutputCallResponse(); goldenResponses[2].payload = new Payload(); goldenResponses[2].payload.type = Messages.COMPRESSABLE; goldenResponses[2].payload.body = new byte[4]; ResponseObserver responseObserver = new ResponseObserver(); StreamObserver<StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver); requestObserver.onNext(requests[0]); Object response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); assertTrue(response instanceof Messages.StreamingOutputCallResponse); assertMessageEquals(goldenResponses[0], (Messages.StreamingOutputCallResponse) response); // Initiate graceful shutdown. channel.shutdown(); // The previous ping-pong could have raced with the shutdown, but this one certainly shouldn't. requestObserver.onNext(requests[1]); response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); assertTrue(response instanceof Messages.StreamingOutputCallResponse); assertMessageEquals(goldenResponses[1], (Messages.StreamingOutputCallResponse) response); requestObserver.onNext(requests[2]); response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); assertTrue(response instanceof Messages.StreamingOutputCallResponse); assertMessageEquals(goldenResponses[2], (Messages.StreamingOutputCallResponse) response); requestObserver.onCompleted(); assertEquals( responseObserver.magicTailResponse, responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); }
public void emptyStream() throws Exception { @SuppressWarnings("unchecked") ResponseObserver responseObserver = new ResponseObserver(); StreamObserver<StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver); requestObserver.onCompleted(); assertEquals( responseObserver.magicTailResponse, responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); }
public void cancelAfterBegin() throws Exception { StreamRecorder<StreamingInputCallResponse> responseObserver = StreamRecorder.create(); StreamObserver<StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver); requestObserver.onError(new RuntimeException()); assertTrue(responseObserver.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertEquals(Arrays.<StreamingInputCallResponse>asList(), responseObserver.getValues()); assertCodeEquals( io.grpc.Status.CANCELLED, io.grpc.Status.fromThrowable(responseObserver.getError())); }
@Override public void collectTrace(TraceMessage request, StreamObserver<EmptyMessage> responseObserver) { try { traceRepository.collect(request.getServerId(), request.getTrace()); } catch (Throwable t) { logger.error(t.getMessage(), t); responseObserver.onError(t); return; } responseObserver.onNext(EmptyMessage.getDefaultInstance()); responseObserver.onCompleted(); }
@Override public void collectInit(InitMessage request, StreamObserver<EmptyMessage> responseObserver) { // FIXME store request.getConfig() try { serverRepository.storeProcessInfo(request.getServerId(), request.getProcessInfo()); } catch (Throwable t) { logger.error(t.getMessage(), t); responseObserver.onError(t); return; } responseObserver.onNext(EmptyMessage.getDefaultInstance()); responseObserver.onCompleted(); }
@Override public void asyncRecognize( AsyncRecognizeRequest request, StreamObserver<Operation> responseObserver) { Object response = responses.remove(); if (response instanceof Operation) { requests.add(request); responseObserver.onNext((Operation) response); responseObserver.onCompleted(); } else if (response instanceof Exception) { responseObserver.onError((Exception) response); } else { responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); } }
@Override public void collectAggregates( AggregateMessage request, StreamObserver<EmptyMessage> responseObserver) { try { aggregateRepository.store( request.getServerId(), request.getCaptureTime(), request.getAggregatesByTypeList()); } catch (Throwable t) { logger.error(t.getMessage(), t); responseObserver.onError(t); return; } responseObserver.onNext(EmptyMessage.getDefaultInstance()); responseObserver.onCompleted(); }
public void pingPong() throws Exception { final Messages.StreamingOutputCallRequest[] requests = new Messages.StreamingOutputCallRequest[4]; for (int i = 0; i < 4; i++) { requests[i] = new Messages.StreamingOutputCallRequest(); requests[i].responseParameters = new Messages.ResponseParameters[1]; requests[i].responseParameters[0] = new Messages.ResponseParameters(); requests[i].payload = new Payload(); } requests[0].responseParameters[0].size = 31415; requests[0].payload.body = new byte[27182]; requests[1].responseParameters[0].size = 9; requests[1].payload.body = new byte[8]; requests[2].responseParameters[0].size = 2653; requests[2].payload.body = new byte[1828]; requests[3].responseParameters[0].size = 58979; requests[3].payload.body = new byte[45904]; final Messages.StreamingOutputCallResponse[] goldenResponses = new Messages.StreamingOutputCallResponse[4]; for (int i = 0; i < 4; i++) { goldenResponses[i] = new Messages.StreamingOutputCallResponse(); goldenResponses[i].payload = new Payload(); goldenResponses[i].payload.type = Messages.COMPRESSABLE; } goldenResponses[0].payload.body = new byte[31415]; goldenResponses[1].payload.body = new byte[9]; goldenResponses[2].payload.body = new byte[2653]; goldenResponses[3].payload.body = new byte[58979]; @SuppressWarnings("unchecked") ResponseObserver responseObserver = new ResponseObserver(); StreamObserver<Messages.StreamingOutputCallRequest> requestObserver = asyncStub.fullDuplexCall(responseObserver); for (int i = 0; i < requests.length; i++) { requestObserver.onNext(requests[i]); Object response = responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); if (!(response instanceof Messages.StreamingOutputCallResponse)) { fail("Unexpected: " + response); } assertMessageEquals(goldenResponses[i], (Messages.StreamingOutputCallResponse) response); assertTrue( "More than 1 responses received for ping pong test.", responseObserver.responses.isEmpty()); } requestObserver.onCompleted(); assertEquals( responseObserver.magicTailResponse, responseObserver.responses.poll(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); }
@Override public void executeSynchronously( RemoteWorkRequest request, StreamObserver<RemoteWorkResponse> responseObserver) { Path tempRoot = workPath.getRelative("build-" + UUID.randomUUID().toString()); try { FileSystemUtils.createDirectoryAndParents(tempRoot); final ConcurrentMapActionCache actionCache = new ConcurrentMapActionCache(tempRoot, cache); final MemcacheWorkExecutor workExecutor = MemcacheWorkExecutor.createLocalWorkExecutor(actionCache, tempRoot); if (LOG_FINER) { LOG.fine( "Work received has " + request.getInputFilesCount() + " input files and " + request.getOutputFilesCount() + " output files."); } RemoteWorkResponse response = workExecutor.executeLocally(request); responseObserver.onNext(response); if (options.debug) { if (!response.getSuccess()) { LOG.warning("Work failed. Request: " + request.toString() + "."); } else if (LOG_FINER) { LOG.fine("Work completed."); } } if (!options.debug || response.getSuccess()) { FileSystemUtils.deleteTree(tempRoot); } else { LOG.warning("Preserving work directory " + tempRoot.toString() + "."); } } catch (IOException | InterruptedException e) { RemoteWorkResponse.Builder response = RemoteWorkResponse.newBuilder(); response.setSuccess(false).setOut("").setErr("").setException(e.toString()); responseObserver.onNext(response.build()); if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } } finally { responseObserver.onCompleted(); } }
/** Start a fullDuplexCall which the server will not respond, and verify the deadline expires. */ public void timeoutOnSleepingServer() throws Exception { TestServiceGrpc.TestService stub = TestServiceGrpc.newStub(channel).withDeadlineAfter(1, TimeUnit.MILLISECONDS); StreamRecorder<StreamingOutputCallResponse> recorder = StreamRecorder.create(); StreamObserver<StreamingOutputCallRequest> requestObserver = stub.fullDuplexCall(recorder); try { StreamingOutputCallRequest request = new StreamingOutputCallRequest(); request.payload = new Messages.Payload(); request.payload.body = new byte[27182]; requestObserver.onNext(request); } catch (IllegalStateException expected) { // This can happen if the stream has already been terminated due to deadline exceeded. } assertTrue(recorder.awaitCompletion(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)); assertCodeEquals( io.grpc.Status.DEADLINE_EXCEEDED, io.grpc.Status.fromThrowable(recorder.getError())); }
public void clientStreaming() throws Exception { final Messages.StreamingInputCallRequest[] requests = new Messages.StreamingInputCallRequest[4]; for (int i = 0; i < 4; i++) { requests[i] = new Messages.StreamingInputCallRequest(); requests[i].payload = new Payload(); } requests[0].payload.body = new byte[27182]; requests[1].payload.body = new byte[8]; requests[2].payload.body = new byte[1828]; requests[3].payload.body = new byte[45904]; final Messages.StreamingInputCallResponse goldenResponse = new Messages.StreamingInputCallResponse(); goldenResponse.aggregatedPayloadSize = 74922; StreamRecorder<Messages.StreamingInputCallResponse> responseObserver = StreamRecorder.create(); StreamObserver<Messages.StreamingInputCallRequest> requestObserver = asyncStub.streamingInputCall(responseObserver); for (Messages.StreamingInputCallRequest request : requests) { requestObserver.onNext(request); } requestObserver.onCompleted(); assertMessageEquals(goldenResponse, responseObserver.firstValue().get()); }
private ClientResponse sendRequest(ServerRequest request) throws Exception { ResponseHolder responseHolder = new ResponseHolder(); responseHolders.put(request.getRequestId(), responseHolder); requestObserver.onNext(request); // timeout is in case agent never responds // passing ClientResponse.getDefaultInstance() is just dummy (non-null) value ClientResponse response = responseHolder.response.exchange(ClientResponse.getDefaultInstance(), 1, HOURS); if (response.getMessageCase() == MessageCase.UNKNOWN_REQUEST_RESPONSE) { throw new OutdatedAgentException(); } if (response.getMessageCase() == MessageCase.EXCEPTION_RESPONSE) { throw new AgentException(); } return response; }
@Override public void onFailure(final Throwable throwable) { responseObserver.onValue( AddDataPointResponse.newBuilder().setMessage(throwable.getMessage()).build()); responseObserver.onCompleted(); }
@Override public void onSuccess(@Nullable final Void result) { responseObserver.onValue(AddDataPointResponse.newBuilder().setMessage("OK").build()); responseObserver.onCompleted(); }
@Override public void sayHello(HelloRequest req, StreamObserver<HelloResponse> responseObserver) { HelloResponse reply = HelloResponse.newBuilder().setMessage("Hello " + req.getName()).build(); responseObserver.onValue(reply); responseObserver.onCompleted(); }