use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project pinpoint by naver.
the class MetadataClientMock method request.
private void request(GeneratedMessageV3 message, int retryCount) {
if (retryCount <= 0) {
logger.warn("Drop message {}", debugLog(message));
return;
}
if (message instanceof PSqlMetaData) {
PSqlMetaData sqlMetaData = (PSqlMetaData) message;
StreamObserver<PResult> responseObserver = newResponseObserver(message, retryCount);
this.metadataStub.requestSqlMetaData(sqlMetaData, responseObserver);
} else if (message instanceof PApiMetaData) {
final PApiMetaData apiMetaData = (PApiMetaData) message;
StreamObserver<PResult> responseObserver = newResponseObserver(message, retryCount);
this.metadataStub.requestApiMetaData(apiMetaData, responseObserver);
} else if (message instanceof PStringMetaData) {
final PStringMetaData stringMetaData = (PStringMetaData) message;
StreamObserver<PResult> responseObserver = newResponseObserver(message, retryCount);
this.metadataStub.requestStringMetaData(stringMetaData, responseObserver);
} else {
logger.warn("Unsupported message {}", debugLog(message));
}
int requestCount = requestCounter.getAndIncrement();
logger.info("Request {} {}", requestCount, message);
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project pinpoint by naver.
the class MetadataGrpcDataSender method request0.
// Request
private void request0(final GeneratedMessageV3 message, final int remainingRetryCount) {
if (message instanceof PSqlMetaData) {
final PSqlMetaData sqlMetaData = (PSqlMetaData) message;
final StreamObserver<PResult> responseObserver = newResponseStream(message, remainingRetryCount);
this.metadataStub.requestSqlMetaData(sqlMetaData, responseObserver);
} else if (message instanceof PApiMetaData) {
final PApiMetaData apiMetaData = (PApiMetaData) message;
final StreamObserver<PResult> responseObserver = newResponseStream(message, remainingRetryCount);
this.metadataStub.requestApiMetaData(apiMetaData, responseObserver);
} else if (message instanceof PStringMetaData) {
final PStringMetaData stringMetaData = (PStringMetaData) message;
final StreamObserver<PResult> responseObserver = newResponseStream(message, remainingRetryCount);
this.metadataStub.requestStringMetaData(stringMetaData, responseObserver);
} else {
logger.warn("Unsupported message {}", MessageFormatUtils.debugLog(message));
}
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver 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();
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project pinpoint by naver.
the class StatService method sendAgentStat.
@Override
public StreamObserver<PStatMessage> sendAgentStat(StreamObserver<Empty> responseObserver) {
StreamObserver<PStatMessage> observer = new StreamObserver<PStatMessage>() {
@Override
public void onNext(PStatMessage statMessage) {
if (isDebug) {
logger.debug("Send PAgentStat={}", MessageFormatUtils.debugLog(statMessage));
}
if (statMessage.hasAgentStat()) {
final Message<PAgentStat> message = newMessage(statMessage.getAgentStat(), DefaultTBaseLocator.AGENT_STAT);
send(message, responseObserver);
} else if (statMessage.hasAgentStatBatch()) {
final Message<PAgentStatBatch> message = newMessage(statMessage.getAgentStatBatch(), DefaultTBaseLocator.AGENT_STAT_BATCH);
send(message, responseObserver);
} else if (statMessage.hasAgentUriStat()) {
final Message<PAgentUriStat> message = newMessage(statMessage.getAgentUriStat(), DefaultTBaseLocator.AGENT_URI_STAT);
send(message, responseObserver);
} else {
if (isDebug) {
logger.debug("Found empty stat message {}", MessageFormatUtils.debugLog(statMessage));
}
}
}
@Override
public void onError(Throwable throwable) {
final StatusError statusError = StatusErrors.throwable(throwable);
if (statusError.isSimpleError()) {
logger.info("Failed to stat stream, cause={}", statusError.getMessage());
} else {
logger.warn("Failed to stat stream, cause={}", statusError.getMessage(), statusError.getThrowable());
}
}
@Override
public void onCompleted() {
com.navercorp.pinpoint.grpc.Header header = ServerContext.getAgentInfo();
logger.info("onCompleted {}", header);
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
}
};
return observer;
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.stub.StreamObserver in project alluxio by Alluxio.
the class AlluxioBlockStoreTest method getOutStreamLocal.
@Test
public void getOutStreamLocal() throws Exception {
File file = File.createTempFile("test", ".tmp");
CreateLocalBlockResponse response = CreateLocalBlockResponse.newBuilder().setPath(file.getAbsolutePath()).build();
when(mWorkerClient.createLocalBlock(any(StreamObserver.class))).thenAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
StreamObserver<CreateLocalBlockResponse> observer = invocation.getArgument(0, StreamObserver.class);
observer.onNext(response);
return mStreamObserver;
}
});
OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setLocationPolicy(new MockBlockLocationPolicy(Lists.newArrayList(WORKER_NET_ADDRESS_LOCAL))).setWriteType(WriteType.MUST_CACHE);
BlockOutStream stream = mBlockStore.getOutStream(BLOCK_ID, BLOCK_LENGTH, options);
assertEquals(WORKER_NET_ADDRESS_LOCAL, stream.getAddress());
}
Aggregations