Search in sources :

Example 1 with RaftClientRequestProto

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

the class NettyClientRpc method sendRequest.

@Override
public RaftClientReply sendRequest(RaftClientRequest request) throws IOException {
    final RaftPeerId serverId = request.getServerId();
    final NettyRpcProxy proxy = getProxies().getProxy(serverId);
    final RaftNettyServerRequestProto.Builder b = RaftNettyServerRequestProto.newBuilder();
    final RaftRpcRequestProto rpcRequest;
    if (request instanceof GroupManagementRequest) {
        final GroupManagementRequestProto proto = ClientProtoUtils.toGroupManagementRequestProto((GroupManagementRequest) request);
        b.setGroupManagementRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof SetConfigurationRequest) {
        final SetConfigurationRequestProto proto = ClientProtoUtils.toSetConfigurationRequestProto((SetConfigurationRequest) request);
        b.setSetConfigurationRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof GroupListRequest) {
        final RaftProtos.GroupListRequestProto proto = ClientProtoUtils.toGroupListRequestProto((GroupListRequest) request);
        b.setGroupListRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof GroupInfoRequest) {
        final RaftProtos.GroupInfoRequestProto proto = ClientProtoUtils.toGroupInfoRequestProto((GroupInfoRequest) request);
        b.setGroupInfoRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof TransferLeadershipRequest) {
        final RaftProtos.TransferLeadershipRequestProto proto = ClientProtoUtils.toTransferLeadershipRequestProto((TransferLeadershipRequest) request);
        b.setTransferLeadershipRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof SnapshotManagementRequest) {
        final RaftProtos.SnapshotManagementRequestProto proto = ClientProtoUtils.toSnapshotManagementRequestProto((SnapshotManagementRequest) request);
        b.setSnapshotManagementRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else if (request instanceof LeaderElectionManagementRequest) {
        final RaftProtos.LeaderElectionManagementRequestProto proto = ClientProtoUtils.toLeaderElectionManagementRequestProto((LeaderElectionManagementRequest) request);
        b.setLeaderElectionManagementRequest(proto);
        rpcRequest = proto.getRpcRequest();
    } else {
        final RaftClientRequestProto proto = ClientProtoUtils.toRaftClientRequestProto(request);
        b.setRaftClientRequest(proto);
        rpcRequest = proto.getRpcRequest();
    }
    if (request instanceof GroupListRequest) {
        return ClientProtoUtils.toGroupListReply(proxy.send(rpcRequest, b.build()).getGroupListReply());
    } else if (request instanceof GroupInfoRequest) {
        return ClientProtoUtils.toGroupInfoReply(proxy.send(rpcRequest, b.build()).getGroupInfoReply());
    } else {
        return ClientProtoUtils.toRaftClientReply(proxy.send(rpcRequest, b.build()).getRaftClientReply());
    }
}
Also used : NettyRpcProxy(org.apache.ratis.netty.NettyRpcProxy) RaftRpcRequestProto(org.apache.ratis.proto.RaftProtos.RaftRpcRequestProto) GroupManagementRequestProto(org.apache.ratis.proto.RaftProtos.GroupManagementRequestProto) SetConfigurationRequestProto(org.apache.ratis.proto.RaftProtos.SetConfigurationRequestProto) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) RaftProtos(org.apache.ratis.proto.RaftProtos) RaftNettyServerRequestProto(org.apache.ratis.proto.netty.NettyProtos.RaftNettyServerRequestProto)

Example 2 with RaftClientRequestProto

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

the class TestClientProtoUtils method runTestToRaftClientRequestProto.

void runTestToRaftClientRequestProto(int n, SizeInBytes messageSize) throws Exception {
    final ClientId clientId = ClientId.randomId();
    final RaftPeerId leaderId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    TimeDuration toProto = TimeDuration.ZERO;
    TimeDuration toRequest = TimeDuration.ZERO;
    for (int i = 0; i < n; i++) {
        final ByteString bytes = newByteString(messageSize.getSizeInt(), i);
        final RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(clientId).setServerId(leaderId).setGroupId(groupId).setCallId(1).setMessage(() -> bytes).setType(RaftClientRequest.writeRequestType()).build();
        final Timestamp startTime = Timestamp.currentTime();
        final RaftClientRequestProto proto = ClientProtoUtils.toRaftClientRequestProto(request);
        final TimeDuration p = startTime.elapsedTime();
        final RaftClientRequest computed = ClientProtoUtils.toRaftClientRequest(proto);
        final TimeDuration r = startTime.elapsedTime().subtract(p);
        Assert.assertEquals(request.getMessage().getContent(), computed.getMessage().getContent());
        toProto = toProto.add(p);
        toRequest = toRequest.add(r);
    }
    System.out.printf("%nmessageSize=%s, n=%d%n", messageSize, n);
    print("toProto  ", toProto, n);
    print("toRequest", toRequest, n);
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) ClientId(org.apache.ratis.protocol.ClientId) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Timestamp(org.apache.ratis.util.Timestamp)

Example 3 with RaftClientRequestProto

use of org.apache.ratis.proto.RaftProtos.RaftClientRequestProto 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

RaftClientRequestProto (org.apache.ratis.proto.RaftProtos.RaftClientRequestProto)3 CompletableFuture (java.util.concurrent.CompletableFuture)1 ClientProtoUtils (org.apache.ratis.client.impl.ClientProtoUtils)1 NettyRpcProxy (org.apache.ratis.netty.NettyRpcProxy)1 RaftProtos (org.apache.ratis.proto.RaftProtos)1 GroupManagementRequestProto (org.apache.ratis.proto.RaftProtos.GroupManagementRequestProto)1 RaftClientReplyProto (org.apache.ratis.proto.RaftProtos.RaftClientReplyProto)1 RaftRpcRequestProto (org.apache.ratis.proto.RaftProtos.RaftRpcRequestProto)1 SetConfigurationRequestProto (org.apache.ratis.proto.RaftProtos.SetConfigurationRequestProto)1 RaftNettyServerRequestProto (org.apache.ratis.proto.netty.NettyProtos.RaftNettyServerRequestProto)1 ClientId (org.apache.ratis.protocol.ClientId)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 AlreadyClosedException (org.apache.ratis.protocol.exceptions.AlreadyClosedException)1 ByteString (org.apache.ratis.thirdparty.com.google.protobuf.ByteString)1 TimeDuration (org.apache.ratis.util.TimeDuration)1 Timestamp (org.apache.ratis.util.Timestamp)1