Search in sources :

Example 96 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class GroupListCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final RaftPeerId peerId;
    final String address;
    if (cl.hasOption(PEER_ID_OPTION_NAME)) {
        peerId = RaftPeerId.getRaftPeerId(cl.getOptionValue(PEER_ID_OPTION_NAME));
        address = getRaftGroup().getPeer(peerId).getAddress();
    } else if (cl.hasOption(SERVER_ADDRESS_OPTION_NAME)) {
        address = cl.getOptionValue(SERVER_ADDRESS_OPTION_NAME);
        final InetSocketAddress serverAddress = parseInetSocketAddress(address);
        peerId = RaftUtils.getPeerId(serverAddress);
    } else {
        throw new IllegalArgumentException("Both " + PEER_ID_OPTION_NAME + " and " + SERVER_ADDRESS_OPTION_NAME + " options are missing.");
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        GroupListReply reply = raftClient.getGroupManagementApi(peerId).list();
        processReply(reply, () -> String.format("Failed to get group information of peerId %s (server %s)", peerId, address));
        printf(String.format("The peerId %s (server %s) is in %d groups, and the groupIds is: %s", peerId, address, reply.getGroupIds().size(), reply.getGroupIds()));
    }
    return 0;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) GroupListReply(org.apache.ratis.protocol.GroupListReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 97 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftStateMachineExceptionTests method runTestRetryOnExceptionDuringReplication.

private void runTestRetryOnExceptionDuringReplication(CLUSTER cluster) throws Exception {
    final RaftServer.Division oldLeader = RaftTestUtil.waitForLeader(cluster);
    cluster.getLeaderAndSendFirstMessage(true);
    // turn on the preAppend failure switch
    failPreAppend = true;
    try (final RaftClient client = cluster.createClient(oldLeader.getId())) {
        final RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        final SimpleMessage message = new SimpleMessage("message");
        RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), oldLeader.getId(), callId, message);
        RaftClientReply reply = rpc.sendRequest(r);
        Objects.requireNonNull(reply.getStateMachineException());
        final RetryCache.Entry oldEntry = RetryCacheTestUtil.get(oldLeader, client.getId(), callId);
        Assert.assertNotNull(oldEntry);
        Assert.assertTrue(RetryCacheTestUtil.isFailed(oldEntry));
        // At this point of time the old leader would have stepped down. wait for leader election to complete
        final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
        // retry
        r = cluster.newRaftClientRequest(client.getId(), leader.getId(), callId, message);
        reply = rpc.sendRequest(r);
        Objects.requireNonNull(reply.getStateMachineException());
        final RetryCache.Entry currentEntry = RetryCacheTestUtil.get(leader, client.getId(), callId);
        Assert.assertNotNull(currentEntry);
        Assert.assertTrue(RetryCacheTestUtil.isFailed(currentEntry));
        Assert.assertNotEquals(oldEntry, currentEntry);
        failPreAppend = false;
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RetryCache(org.apache.ratis.server.RetryCache) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 98 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftStateMachineExceptionTests method runTestRetryOnStateMachineException.

private void runTestRetryOnStateMachineException(CLUSTER cluster) throws Exception {
    RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    cluster.getLeaderAndSendFirstMessage(true);
    final long oldLastApplied = cluster.getLeader().getInfo().getLastAppliedIndex();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        final RaftClientRpc rpc = client.getClientRpc();
        final long callId = 999;
        final SimpleMessage message = new SimpleMessage("message");
        final RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, message);
        RaftClientReply reply = rpc.sendRequest(r);
        Assert.assertFalse(reply.isSuccess());
        Assert.assertNotNull(reply.getStateMachineException());
        // retry with the same callId
        for (int i = 0; i < 5; i++) {
            reply = rpc.sendRequest(r);
            Assert.assertEquals(client.getId(), reply.getClientId());
            Assert.assertEquals(callId, reply.getCallId());
            Assert.assertFalse(reply.isSuccess());
            Assert.assertNotNull(reply.getStateMachineException());
        }
        for (RaftServer.Division server : cluster.iterateDivisions()) {
            LOG.info("check server " + server.getId());
            JavaUtils.attemptRepeatedly(() -> {
                Assert.assertNotNull(RetryCacheTestUtil.get(server, client.getId(), callId));
                return null;
            }, 5, BaseTest.ONE_SECOND, "GetRetryEntry", LOG);
            final RaftLog log = server.getRaftLog();
            RaftTestUtil.logEntriesContains(log, oldLastApplied + 1, log.getNextIndex(), message);
        }
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 99 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftStateMachineExceptionTests method runTestHandleStateMachineException.

private void runTestHandleStateMachineException(CLUSTER cluster) throws Exception {
    RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        client.io().send(new RaftTestUtil.SimpleMessage("m"));
        fail("Exception expected");
    } catch (StateMachineException e) {
        e.printStackTrace();
        Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception"));
    }
    cluster.shutdown();
}
Also used : StateMachineException(org.apache.ratis.protocol.exceptions.StateMachineException) RaftTestUtil(org.apache.ratis.RaftTestUtil) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient)

Example 100 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class ResumeCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    String strAddr = cl.getOptionValue(ADDRESS_OPTION_NAME);
    final RaftPeerId peerId = getRaftGroup().getPeers().stream().filter(p -> p.getAddress().equals(strAddr)).findAny().map(RaftPeer::getId).orElse(null);
    if (peerId == null) {
        printf("Can't find a sever with the address:%s", strAddr);
        return -1;
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        RaftClientReply reply = raftClient.getLeaderElectionManagementApi(peerId).resume();
        processReply(reply, () -> String.format("Failed to resume leader election on peer %s", strAddr));
        printf(String.format("Successful pause leader election on peer %s", strAddr));
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AbstractRatisCommand(org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) RaftUtils(org.apache.ratis.shell.cli.RaftUtils) Context(org.apache.ratis.shell.cli.sh.command.Context) IOException(java.io.IOException) RaftClient(org.apache.ratis.client.RaftClient) Option(org.apache.commons.cli.Option) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)134 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)66 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)54 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)46 RaftServer (org.apache.ratis.server.RaftServer)46 Test (org.junit.Test)44 IOException (java.io.IOException)41 RaftPeer (org.apache.ratis.protocol.RaftPeer)33 BaseTest (org.apache.ratis.BaseTest)27 RaftTestUtil (org.apache.ratis.RaftTestUtil)22 ArrayList (java.util.ArrayList)20 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)20 RaftGroup (org.apache.ratis.protocol.RaftGroup)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 RaftProperties (org.apache.ratis.conf.RaftProperties)14 File (java.io.File)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)11 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)11