Search in sources :

Example 91 with RaftClient

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

the class TestRaftWithGrpc method runTestUpdateViaHeartbeat.

void runTestUpdateViaHeartbeat(MiniRaftClusterWithGrpc cluster) throws Exception {
    waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        // block append requests
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockWriteStateMachineData);
        CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
        TimeDuration.valueOf(5, TimeUnit.SECONDS).sleep();
        // replyFuture should not be completed until append request is unblocked.
        Assert.assertFalse(replyFuture.isDone());
        // unblock append request.
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData);
        final RaftLog leaderLog = cluster.getLeader().getRaftLog();
        // The entries have been appended in the followers
        // although the append entry timed out at the leader
        cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            final LogEntryHeader[] leaderEntries = leaderLog.getEntries(0, Long.MAX_VALUE);
            final RaftLog followerLog = raftServer.getRaftLog();
            Assert.assertEquals(leaderNextIndex, followerLog.getNextIndex());
            final LogEntryHeader[] serverEntries = followerLog.getEntries(0, Long.MAX_VALUE);
            Assert.assertArrayEquals(serverEntries, leaderEntries);
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + raftServer.getId(), LOG)));
        // Wait for heartbeats from leader to be received by followers
        Thread.sleep(500);
        RaftServerTestUtil.getLogAppenders(cluster.getLeader()).forEach(logAppender -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
            final long leaderNextIndex = leaderLog.getNextIndex();
            // FollowerInfo in the leader state should have updated next and match index.
            final long followerMatchIndex = logAppender.getFollower().getMatchIndex();
            Assert.assertTrue(followerMatchIndex >= leaderNextIndex - 1);
            Assert.assertEquals(followerMatchIndex + 1, logAppender.getFollower().getNextIndex());
        }, 10, HUNDRED_MILLIS, "assertRaftLog-" + logAppender.getFollower(), LOG)));
    }
}
Also used : RaftBasicTests(org.apache.ratis.RaftBasicTests) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftLog(org.apache.ratis.server.raftlog.RaftLog) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) RaftTestUtil(org.apache.ratis.RaftTestUtil) TimeUnit(java.util.concurrent.TimeUnit) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftClient(org.apache.ratis.client.RaftClient) JavaUtils(org.apache.ratis.util.JavaUtils) Assert(org.junit.Assert) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 92 with RaftClient

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

the class AbstractRatisCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    List<InetSocketAddress> addresses = new ArrayList<>();
    String peersStr = cl.getOptionValue(PEER_OPTION_NAME);
    String[] peersArray = peersStr.split(",");
    for (String peer : peersArray) {
        addresses.add(parseInetSocketAddress(peer));
    }
    final RaftGroupId raftGroupIdFromConfig = cl.hasOption(GROUPID_OPTION_NAME) ? RaftGroupId.valueOf(UUID.fromString(cl.getOptionValue(GROUPID_OPTION_NAME))) : DEFAULT_RAFT_GROUP_ID;
    List<RaftPeer> peers = addresses.stream().map(addr -> RaftPeer.newBuilder().setId(RaftUtils.getPeerId(addr)).setAddress(addr).build()).collect(Collectors.toList());
    raftGroup = RaftGroup.valueOf(raftGroupIdFromConfig, peers);
    try (final RaftClient client = RaftUtils.createClient(raftGroup)) {
        final RaftGroupId remoteGroupId;
        if (raftGroupIdFromConfig != DEFAULT_RAFT_GROUP_ID) {
            remoteGroupId = raftGroupIdFromConfig;
        } else {
            final List<RaftGroupId> groupIds = run(peers, p -> client.getGroupManagementApi((p.getId())).list().getGroupIds());
            if (groupIds == null) {
                println("Failed to get group ID from " + peers);
                return -1;
            } else if (groupIds.size() == 1) {
                remoteGroupId = groupIds.get(0);
            } else {
                println("There are more than one groups, you should specific one. " + groupIds);
                return -2;
            }
        }
        groupInfoReply = run(peers, p -> client.getGroupManagementApi((p.getId())).info(remoteGroupId));
        processReply(groupInfoReply, () -> "Failed to get group info for group id " + remoteGroupId.getUuid() + " from " + peers);
        raftGroup = groupInfoReply.getGroup();
    }
    return 0;
}
Also used : PrintStream(java.io.PrintStream) RaftException(org.apache.ratis.protocol.exceptions.RaftException) java.util(java.util) RoleInfoProto(org.apache.ratis.proto.RaftProtos.RoleInfoProto) Command(org.apache.ratis.shell.cli.Command) Options(org.apache.commons.cli.Options) IOException(java.io.IOException) org.apache.ratis.protocol(org.apache.ratis.protocol) InetSocketAddress(java.net.InetSocketAddress) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) FollowerInfoProto(org.apache.ratis.proto.RaftProtos.FollowerInfoProto) RaftPeerRole(org.apache.ratis.proto.RaftProtos.RaftPeerRole) CheckedFunction(org.apache.ratis.util.function.CheckedFunction) BiConsumer(java.util.function.BiConsumer) RaftUtils(org.apache.ratis.shell.cli.RaftUtils) CommandLine(org.apache.commons.cli.CommandLine) RaftClient(org.apache.ratis.client.RaftClient) Option(org.apache.commons.cli.Option) RaftPeerProto(org.apache.ratis.proto.RaftProtos.RaftPeerProto) InetSocketAddress(java.net.InetSocketAddress) RaftClient(org.apache.ratis.client.RaftClient)

Example 93 with RaftClient

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

the class PauseCommand 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("Peer not found: %s", strAddr);
        return -1;
    }
    try (final RaftClient raftClient = RaftUtils.createClient(getRaftGroup())) {
        RaftClientReply reply = raftClient.getLeaderElectionManagementApi(peerId).pause();
        processReply(reply, () -> String.format("Failed to pause 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)

Example 94 with RaftClient

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

the class StepDownCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        final RaftClientReply transferLeadershipReply = client.admin().transferLeadership(null, 60_000);
        processReply(transferLeadershipReply, () -> "Failed to step down leader");
    } catch (Throwable t) {
        printf("caught an error when executing step down leader: %s%n", t.getMessage());
        return -1;
    }
    println("Step down leader successfully");
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 95 with RaftClient

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

the class TransferCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    String strAddr = cl.getOptionValue(ADDRESS_OPTION_NAME);
    RaftPeerId newLeaderId = null;
    // update priorities to enable transfer
    List<RaftPeer> peersWithNewPriorities = new ArrayList<>();
    for (RaftPeer peer : getRaftGroup().getPeers()) {
        peersWithNewPriorities.add(RaftPeer.newBuilder(peer).setPriority(peer.getAddress().equals(strAddr) ? 2 : 1).build());
        if (peer.getAddress().equals(strAddr)) {
            newLeaderId = peer.getId();
        }
    }
    if (newLeaderId == null) {
        return -2;
    }
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        String stringPeers = "[" + peersWithNewPriorities.stream().map(RaftPeer::toString).collect(Collectors.joining(", ")) + "]";
        printf("Applying new peer state before transferring leadership: %n%s%n", stringPeers);
        RaftClientReply setConfigurationReply = client.admin().setConfiguration(peersWithNewPriorities);
        processReply(setConfigurationReply, () -> "failed to set priorities before initiating election");
        // transfer leadership
        printf("Transferring leadership to server with address <%s> %n", strAddr);
        try {
            Thread.sleep(3_000);
            RaftClientReply transferLeadershipReply = client.admin().transferLeadership(newLeaderId, 60_000);
            processReply(transferLeadershipReply, () -> "election failed");
        } catch (Throwable t) {
            printf("caught an error when executing transfer: %s%n", t.getMessage());
            return -1;
        }
        println("Transferring leadership initiated");
    }
    return 0;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ArrayList(java.util.ArrayList) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) 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