use of org.apache.ratis.client.AsyncRpcApi in project incubator-ratis by apache.
the class DataStreamManagement method startTransaction.
private CompletableFuture<RaftClientReply> startTransaction(StreamInfo info, DataStreamRequestByteBuf request, long bytesWritten, ChannelHandlerContext ctx) {
final RequestMetrics metrics = getMetrics().newRequestMetrics(RequestType.START_TRANSACTION);
final RequestContext context = metrics.start();
try {
AsyncRpcApi asyncRpcApi = (AsyncRpcApi) (server.getDivision(info.getRequest().getRaftGroupId()).getRaftClient().async());
return asyncRpcApi.sendForward(info.request).whenCompleteAsync((reply, e) -> {
metrics.stop(context, e == null);
if (e != null) {
replyDataStreamException(server, e, info.getRequest(), request, ctx);
} else {
ctx.writeAndFlush(newDataStreamReplyByteBuffer(request, reply, bytesWritten, info.getCommitInfos()));
}
}, requestExecutor);
} catch (IOException e) {
throw new CompletionException(e);
}
}
use of org.apache.ratis.client.AsyncRpcApi in project incubator-ratis by apache.
the class TestNettyDataStreamWithMock method testMockCluster.
private void testMockCluster(int numServers, RaftException leaderException, IllegalStateException submitException, IOException getStateMachineException) throws Exception {
List<RaftServer> raftServers = new ArrayList<>();
ClientId clientId = ClientId.randomId();
RaftGroupId groupId = RaftGroupId.randomId();
for (int i = 0; i < numServers; i++) {
RaftServer raftServer = mock(RaftServer.class);
RaftPeerId peerId = RaftPeerId.valueOf("s" + i);
RaftProperties properties = new RaftProperties();
NettyConfigKeys.DataStream.setPort(properties, NetUtils.createLocalServerAddress().getPort());
RaftConfigKeys.DataStream.setType(properties, SupportedDataStreamType.NETTY);
when(raftServer.getProperties()).thenReturn(properties);
when(raftServer.getId()).thenReturn(peerId);
when(raftServer.getPeer()).thenReturn(RaftPeer.newBuilder().setId(peerId).build());
if (getStateMachineException == null) {
RaftClient client = Mockito.mock(RaftClient.class);
when(client.getId()).thenReturn(clientId);
AsyncRpcApi asyncRpcApi = Mockito.mock(AsyncRpcApi.class);
when(client.async()).thenReturn(asyncRpcApi);
final RaftServer.Division myDivision = mockDivision(raftServer, client);
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenReturn(myDivision);
if (submitException != null) {
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenThrow(submitException);
} else if (i == 0) {
// primary
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenAnswer((Answer<CompletableFuture<RaftClientReply>>) invocation -> {
final RaftClientRequest r = (RaftClientRequest) invocation.getArguments()[0];
final RaftClientReply.Builder b = RaftClientReply.newBuilder().setRequest(r);
final RaftClientReply reply = leaderException != null ? b.setException(leaderException).build() : b.setSuccess().setMessage(() -> DataStreamTestUtils.MOCK).build();
return CompletableFuture.completedFuture(reply);
});
}
} else {
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenThrow(getStateMachineException);
}
raftServers.add(raftServer);
}
runTestMockCluster(groupId, raftServers, clientId, 1_000_000, 10, submitException != null ? submitException : leaderException, getStateMachineException);
}
Aggregations