Search in sources :

Example 1 with RaftSnapshotBaseTest

use of org.apache.ratis.statemachine.RaftSnapshotBaseTest in project incubator-ratis by apache.

the class InstallSnapshotNotificationTests method testRestartFollower.

private void testRestartFollower(CLUSTER cluster) throws Exception {
    leaderSnapshotInfoRef.set(null);
    int i = 0;
    final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = leader.getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
            final RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
            Assert.assertTrue(reply.isSuccess());
        }
    }
    // wait for the snapshot to be done
    final long oldLeaderNextIndex = leader.getRaftLog().getNextIndex();
    {
        LOG.info("{}: oldLeaderNextIndex = {}", leaderId, oldLeaderNextIndex);
        final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, oldLeaderNextIndex - SNAPSHOT_TRIGGER_THRESHOLD, oldLeaderNextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
    }
    final RaftPeerId followerId = cluster.getFollowers().get(0).getId();
    cluster.killServer(followerId);
    // generate some more traffic
    try (final RaftClient client = cluster.createClient(leader.getId())) {
        Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
    }
    FIVE_SECONDS.sleep();
    cluster.restartServer(followerId, false);
    final RaftServer.Division follower = cluster.getDivision(followerId);
    JavaUtils.attempt(() -> {
        final long newLeaderNextIndex = leader.getRaftLog().getNextIndex();
        LOG.info("{}: newLeaderNextIndex = {}", leaderId, newLeaderNextIndex);
        Assert.assertTrue(newLeaderNextIndex > oldLeaderNextIndex);
        Assert.assertEquals(newLeaderNextIndex, follower.getRaftLog().getNextIndex());
    }, 10, ONE_SECOND, "followerNextIndex", LOG);
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) List(java.util.List) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftSnapshotBaseTest(org.apache.ratis.statemachine.RaftSnapshotBaseTest)

Aggregations

List (java.util.List)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 RaftServer (org.apache.ratis.server.RaftServer)1 RaftSnapshotBaseTest (org.apache.ratis.statemachine.RaftSnapshotBaseTest)1