Search in sources :

Example 1 with AsyncRpcApi

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);
    }
}
Also used : AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) CompletionException(java.util.concurrent.CompletionException) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) IOException(java.io.IOException)

Example 2 with AsyncRpcApi

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);
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) ArrayList(java.util.ArrayList) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Answer(org.mockito.stubbing.Answer) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ClientId(org.apache.ratis.protocol.ClientId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

AsyncRpcApi (org.apache.ratis.client.AsyncRpcApi)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CompletionException (java.util.concurrent.CompletionException)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftProperties (org.apache.ratis.conf.RaftProperties)1 RequestContext (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext)1 RequestMetrics (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics)1 ClientId (org.apache.ratis.protocol.ClientId)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)1 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 RaftServer (org.apache.ratis.server.RaftServer)1 Answer (org.mockito.stubbing.Answer)1