Search in sources :

Example 31 with RaftClientReply

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

the class LeaderElectionTests method testTransferLeaderTimeout.

@Test
public void testTransferLeaderTimeout() throws Exception {
    try (final MiniRaftCluster cluster = newCluster(3)) {
        cluster.start();
        final RaftServer.Division leader = waitForLeader(cluster);
        try (RaftClient client = cluster.createClient(leader.getId())) {
            List<RaftServer.Division> followers = cluster.getFollowers();
            Assert.assertEquals(followers.size(), 2);
            RaftServer.Division newLeader = followers.get(0);
            // isolate new leader, so that transfer leadership will timeout
            isolate(cluster, newLeader.getId());
            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());
            CompletableFuture<Boolean> transferTimeoutFuture = CompletableFuture.supplyAsync(() -> {
                try {
                    long timeoutMs = 5000;
                    long start = System.currentTimeMillis();
                    try {
                        client.admin().transferLeadership(newLeader.getId(), timeoutMs);
                    } catch (TransferLeadershipException e) {
                        long cost = System.currentTimeMillis() - start;
                        Assert.assertTrue(cost > timeoutMs);
                        Assert.assertTrue(e.getMessage().contains("Failed to transfer leadership to"));
                        Assert.assertTrue(e.getMessage().contains("timed out"));
                    }
                    return true;
                } catch (IOException e) {
                    return false;
                }
            });
            // before transfer timeout, leader should in steppingDown
            JavaUtils.attemptRepeatedly(() -> {
                try {
                    client.io().send(new RaftTestUtil.SimpleMessage("message"));
                } catch (LeaderSteppingDownException e) {
                    Assert.assertTrue(e.getMessage().contains("is stepping down"));
                }
                return null;
            }, 5, TimeDuration.ONE_SECOND, "check leader steppingDown", RaftServer.LOG);
            Assert.assertTrue(transferTimeoutFuture.get());
            // after transfer timeout, leader should accept request
            reply = client.io().send(new RaftTestUtil.SimpleMessage("message"));
            Assert.assertTrue(reply.getReplierId().equals(leader.getId().toString()));
            Assert.assertTrue(reply.isSuccess());
            deIsolate(cluster, newLeader.getId());
        }
        cluster.shutdown();
    }
}
Also used : TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) RaftTestUtil(org.apache.ratis.RaftTestUtil) RaftServer(org.apache.ratis.server.RaftServer) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 32 with RaftClientReply

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

the class InstallSnapshotNotificationTests method testInstallSnapshotDuringBootstrap.

private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exception {
    leaderSnapshotInfoRef.set(null);
    numSnapshotRequests.set(0);
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final RaftServer.Division leader = cluster.getLeader();
        final long nextIndex = leader.getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        RaftSnapshotBaseTest.assertLeaderContent(cluster);
        final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
        final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
        Assert.assertTrue(set);
        // add two more peers
        final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        // leader snapshot.
        for (RaftServer.Division follower : cluster.getFollowers()) {
            Assert.assertEquals(leaderSnapshotInfo.getIndex(), RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
        }
        // Make sure each new peer got one snapshot notification.
        Assert.assertEquals(2, numSnapshotRequests.get());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 33 with RaftClientReply

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

the class RaftAsyncExceptionTests method runTestTimeoutException.

private void runTestTimeoutException(CLUSTER cluster) throws Exception {
    // send a message to make sure the cluster is working
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.io().send(new SimpleMessage("m0"));
        Assert.assertTrue(reply.isSuccess());
        RaftClientConfigKeys.Rpc.setRequestTimeout(properties.get(), ONE_SECOND);
        // Block StartTransaction
        StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockStartTransaction);
        final CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("m1"));
        FIVE_SECONDS.sleep();
        // Unblock StartTransaction
        StreamSupport.stream(cluster.getServers().spliterator(), false).map(cluster::getDivision).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockStartTransaction);
        // The request should succeed after start transaction is unblocked
        Assert.assertTrue(replyFuture.get(FIVE_SECONDS.getDuration(), FIVE_SECONDS.getUnit()).isSuccess());
    }
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient)

Example 34 with RaftClientReply

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

the class RaftAsyncExceptionTests method runTestGroupMismatchException.

private void runTestGroupMismatchException(CLUSTER cluster) throws Exception {
    // send a message to make sure the cluster is working
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.async().send(new SimpleMessage("first")).get();
        Assert.assertTrue(reply.isSuccess());
    }
    // create another group
    final RaftGroup clusterGroup = cluster.getGroup();
    final RaftGroup anotherGroup = RaftGroup.valueOf(RaftGroupId.randomId(), clusterGroup.getPeers());
    Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId());
    // create another client using another group
    final SimpleMessage[] messages = SimpleMessage.create(5);
    try (RaftClient client = cluster.createClient(anotherGroup)) {
        // send a few messages
        final List<CompletableFuture<RaftClientReply>> futures = new ArrayList<>();
        for (SimpleMessage m : messages) {
            futures.add(client.async().send(m));
        }
        Assert.assertEquals(messages.length, futures.size());
        // check replies
        final Iterator<CompletableFuture<RaftClientReply>> i = futures.iterator();
        testFailureCase("First reply is GroupMismatchException", () -> i.next().get(), ExecutionException.class, GroupMismatchException.class);
        for (; i.hasNext(); ) {
            testFailureCase("Following replies are AlreadyClosedException caused by GroupMismatchException", () -> i.next().get(), ExecutionException.class, AlreadyClosedException.class, GroupMismatchException.class);
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ArrayList(java.util.ArrayList) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftClient(org.apache.ratis.client.RaftClient)

Example 35 with RaftClientReply

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

the class RaftBasicTests method testStateMachineMetrics.

public static void testStateMachineMetrics(boolean async, MiniRaftCluster cluster, Logger LOG) throws Exception {
    RaftServer.Division leader = waitForLeader(cluster);
    try (final RaftClient client = cluster.createClient()) {
        Gauge appliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLIED_INDEX_GAUGE);
        Gauge smAppliedIndexGauge = getStatemachineGaugeWithName(leader, STATEMACHINE_APPLY_COMPLETED_GAUGE);
        long appliedIndexBefore = (Long) appliedIndexGauge.getValue();
        long smAppliedIndexBefore = (Long) smAppliedIndexGauge.getValue();
        checkFollowerCommitLagsLeader(cluster);
        if (async) {
            CompletableFuture<RaftClientReply> replyFuture = client.async().send(new SimpleMessage("abc"));
            replyFuture.get();
        } else {
            client.io().send(new SimpleMessage("abc"));
        }
        long appliedIndexAfter = (Long) appliedIndexGauge.getValue();
        long smAppliedIndexAfter = (Long) smAppliedIndexGauge.getValue();
        checkFollowerCommitLagsLeader(cluster);
        Assert.assertTrue("StateMachine Applied Index not incremented", appliedIndexAfter > appliedIndexBefore);
        Assert.assertTrue("StateMachine Apply completed Index not incremented", smAppliedIndexAfter > smAppliedIndexBefore);
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Gauge(com.codahale.metrics.Gauge)

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