Search in sources :

Example 71 with RaftClientReply

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

the class TestGrpcMessageMetrics method sendMessages.

static void sendMessages(MiniRaftCluster cluster) throws Exception {
    waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
    }
    // Wait for commits to happen on leader
    JavaUtils.attempt(() -> assertMessageCount(cluster.getLeader()), 100, HUNDRED_MILLIS, cluster.getLeader().getId() + "-assertMessageCount", null);
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftClient(org.apache.ratis.client.RaftClient)

Example 72 with RaftClientReply

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

the class LeaderElectionTests method testTransferLeader.

@Test
public void testTransferLeader() throws Exception {
    try (final MiniRaftCluster cluster = newCluster(3)) {
        cluster.start();
        final RaftServer.Division leader = waitForLeader(cluster);
        try (RaftClient client = cluster.createClient(leader.getId())) {
            client.io().send(new RaftTestUtil.SimpleMessage("message"));
            List<RaftServer.Division> followers = cluster.getFollowers();
            Assert.assertEquals(followers.size(), 2);
            RaftServer.Division newLeader = followers.get(0);
            List<RaftPeer> peers = cluster.getPeers();
            List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
            RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
            Assert.assertTrue(reply.isSuccess());
            reply = client.admin().transferLeadership(newLeader.getId(), 20000);
            assertTrue(reply.isSuccess());
            final RaftServer.Division currLeader = waitForLeader(cluster);
            assertTrue(newLeader.getId() == currLeader.getId());
            reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertTrue(reply.getReplierId().equals(newLeader.getId().toString()));
            Assert.assertTrue(reply.isSuccess());
        }
        cluster.shutdown();
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 73 with RaftClientReply

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

the class LeaderElectionTests method runTestPauseResumeLeaderElection.

void runTestPauseResumeLeaderElection(CLUSTER cluster) throws IOException, InterruptedException {
    final RaftClientReply pauseLeaderReply;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    Assert.assertTrue(followers.size() >= 1);
    final RaftServerImpl f1 = (RaftServerImpl) followers.get(0);
    try (final RaftClient client = cluster.createClient()) {
        pauseLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).pause();
        Assert.assertTrue(pauseLeaderReply.isSuccess());
        client.io().send(new RaftTestUtil.SimpleMessage("message"));
        RaftServer.Division newLeader = followers.get(0);
        List<RaftPeer> peers = cluster.getPeers();
        List<RaftPeer> peersWithNewPriority = getPeersWithPriority(peers, newLeader.getPeer());
        RaftClientReply reply = client.admin().setConfiguration(peersWithNewPriority.toArray(new RaftPeer[0]));
        Assert.assertTrue(reply.isSuccess());
        JavaUtils.attempt(() -> Assert.assertEquals(leaderId, leader.getId()), 20, HUNDRED_MILLIS, "check leader id", LOG);
        final RaftClientReply resumeLeaderReply = client.getLeaderElectionManagementApi(f1.getId()).resume();
        Assert.assertTrue(resumeLeaderReply.isSuccess());
        JavaUtils.attempt(() -> Assert.assertEquals(f1.getId(), cluster.getLeader().getId()), 20, HUNDRED_MILLIS, "check new leader", LOG);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Example 74 with RaftClientReply

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

the class LeaderElectionTests method testPreVote.

@Test
public void testPreVote() {
    try (final MiniRaftCluster cluster = newCluster(3)) {
        cluster.start();
        RaftServer.Division leader = waitForLeader(cluster);
        try (RaftClient client = cluster.createClient(leader.getId())) {
            client.io().send(new RaftTestUtil.SimpleMessage("message"));
            final List<RaftServer.Division> followers = cluster.getFollowers();
            assertEquals(followers.size(), 2);
            RaftServer.Division follower = followers.get(0);
            isolate(cluster, follower.getId());
            // send message so that the isolated follower's log lag the others
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertTrue(reply.isSuccess());
            final long savedTerm = leader.getInfo().getCurrentTerm();
            LOG.info("Wait follower {} timeout and trigger pre-vote", follower.getId());
            Thread.sleep(2000);
            deIsolate(cluster, follower.getId());
            Thread.sleep(2000);
            // with pre-vote leader will not step down
            RaftServer.Division newleader = waitForLeader(cluster);
            assertNotNull(newleader);
            assertEquals(newleader.getId(), leader.getId());
            // with pre-vote, term will not change
            assertEquals(savedTerm, leader.getInfo().getCurrentTerm());
            reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertTrue(reply.isSuccess());
        }
        cluster.shutdown();
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) IOException(java.io.IOException) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 75 with RaftClientReply

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

the class LeaderElectionTests method testDisconnectLeader.

protected void testDisconnectLeader() throws Exception {
    try (final MiniRaftCluster cluster = newCluster(3)) {
        cluster.start();
        final RaftServer.Division leader = waitForLeader(cluster);
        try (RaftClient client = cluster.createClient(leader.getId())) {
            client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Thread.sleep(1000);
            isolate(cluster, leader.getId());
            RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertNotEquals(reply.getReplierId(), leader.getId().toString());
            Assert.assertTrue(reply.isSuccess());
        } finally {
            deIsolate(cluster, leader.getId());
        }
        cluster.shutdown();
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) 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