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());
}
}
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);
}
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);
}
Aggregations