Search in sources :

Example 56 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply 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)

Example 57 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class RemoveCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), (a, b) -> {
    });
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        final List<RaftPeer> remaining = getRaftGroup().getPeers().stream().filter(raftPeer -> !ids.contains(raftPeer.getId())).collect(Collectors.toList());
        System.out.println("New peer list: " + remaining);
        RaftClientReply reply = client.admin().setConfiguration(remaining);
        processReply(reply, () -> "Failed to change raft peer");
    }
    return 0;
}
Also used : RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Options(org.apache.commons.cli.Options) Context(org.apache.ratis.shell.cli.sh.command.Context) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AbstractRatisCommand(org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand) CommandLine(org.apache.commons.cli.CommandLine) RaftUtils(org.apache.ratis.shell.cli.RaftUtils) RaftClient(org.apache.ratis.client.RaftClient) Option(org.apache.commons.cli.Option) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 58 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class TakeSnapshotCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    long timeout;
    final RaftPeerId peerId;
    if (cl.hasOption(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME)) {
        timeout = Long.parseLong(cl.getOptionValue(TAKE_SNAPSHOT_TIMEOUT_OPTION_NAME));
    } else {
        timeout = 3000;
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        if (cl.hasOption(PEER_ID_OPTION_NAME)) {
            peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
        } else {
            peerId = null;
        }
        RaftClientReply reply = raftClient.getSnapshotManagementApi(peerId).create(timeout);
        processReply(reply, () -> String.format("Failed to take snapshot of peerId %s", peerId));
        printf(String.format("Successful take snapshot on peerId %s, the latest snapshot index is %d", peerId, reply.getLogIndex()));
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 59 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class DataStreamBaseTest method runTestMockCluster.

void runTestMockCluster(ClientId clientId, int bufferSize, int bufferNum, Exception expectedException, Exception headerException) throws IOException {
    try (final RaftClient client = newRaftClientForDataStream(clientId)) {
        final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, DataStreamTestUtils.getRoutingTableChainTopology(peers, getPrimaryServer().getPeer()));
        if (headerException != null) {
            final DataStreamReply headerReply = out.getHeaderFuture().join();
            Assert.assertFalse(headerReply.isSuccess());
            final RaftClientReply clientReply = ClientProtoUtils.toRaftClientReply(((DataStreamReplyByteBuffer) headerReply).slice());
            Assert.assertTrue(clientReply.getException().getMessage().contains(headerException.getMessage()));
            return;
        }
        final RaftClientReply clientReply = DataStreamTestUtils.writeAndCloseAndAssertReplies(CollectionUtils.as(servers, Server::getRaftServer), null, out, bufferSize, bufferNum, getPrimaryClientId(), client.getId(), false).join();
        if (expectedException != null) {
            Assert.assertFalse(clientReply.isSuccess());
            Assert.assertTrue(clientReply.getException().getMessage().contains(expectedException.getMessage()));
        } else {
            Assert.assertTrue(clientReply.isSuccess());
        }
    }
}
Also used : DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamServer(org.apache.ratis.server.DataStreamServer) RaftServer(org.apache.ratis.server.RaftServer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 60 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.

the class AddCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final Map<RaftPeerId, InetSocketAddress> peersInfo = new HashMap<>();
    final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), peersInfo::put);
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        final Stream<RaftPeer> remaining = getRaftGroup().getPeers().stream();
        final Stream<RaftPeer> adding = ids.stream().map(raftPeerId -> RaftPeer.newBuilder().setId(raftPeerId).setAddress(peersInfo.get(raftPeerId)).setPriority(0).build());
        final List<RaftPeer> peers = Stream.concat(remaining, adding).collect(Collectors.toList());
        System.out.println("New peer list: " + peers);
        RaftClientReply reply = client.admin().setConfiguration(peers);
        processReply(reply, () -> "Failed to change raft peer");
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11