Search in sources :

Example 1 with RaftClientReplyProto

use of org.apache.ratis.proto.RaftProtos.RaftClientReplyProto in project incubator-ratis by apache.

the class GrpcClientRpc method sendRequest.

private CompletableFuture<RaftClientReply> sendRequest(RaftClientRequest request, GrpcClientProtocolClient proxy) throws IOException {
    final RaftClientRequestProto requestProto = toRaftClientRequestProto(request);
    final CompletableFuture<RaftClientReplyProto> replyFuture = new CompletableFuture<>();
    // create a new grpc stream for each non-async call.
    final StreamObserver<RaftClientRequestProto> requestObserver = proxy.unorderedWithTimeout(new StreamObserver<RaftClientReplyProto>() {

        @Override
        public void onNext(RaftClientReplyProto value) {
            replyFuture.complete(value);
        }

        @Override
        public void onError(Throwable t) {
            replyFuture.completeExceptionally(GrpcUtil.unwrapIOException(t));
        }

        @Override
        public void onCompleted() {
            if (!replyFuture.isDone()) {
                replyFuture.completeExceptionally(new AlreadyClosedException(clientId + ": Stream completed but no reply for request " + request));
            }
        }
    });
    requestObserver.onNext(requestProto);
    requestObserver.onCompleted();
    return replyFuture.thenApply(ClientProtoUtils::toRaftClientReply);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) ClientProtoUtils(org.apache.ratis.client.impl.ClientProtoUtils) RaftClientReplyProto(org.apache.ratis.proto.RaftProtos.RaftClientReplyProto) AlreadyClosedException(org.apache.ratis.protocol.exceptions.AlreadyClosedException)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 ClientProtoUtils (org.apache.ratis.client.impl.ClientProtoUtils)1 RaftClientReplyProto (org.apache.ratis.proto.RaftProtos.RaftClientReplyProto)1 RaftClientRequestProto (org.apache.ratis.proto.RaftProtos.RaftClientRequestProto)1 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)1