Search in sources :

Example 1 with RaftStorageDirectory

use of org.apache.ratis.server.storage.RaftStorageDirectory in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method testBasicInstallSnapshot.

/**
 * Basic test for install snapshot: start a one node cluster and let it
 * generate a snapshot. Then delete the log and restart the node, and add more
 * nodes as followers.
 */
@Test
public void testBasicInstallSnapshot() throws Exception {
    List<LogPathAndIndex> logs;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        int i = 0;
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.send(new SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        RaftStorageDirectory storageDirectory = cluster.getLeader().getState().getStorage().getStorageDir();
        final File snapshotFile = getSnapshotFile(cluster, i);
        logs = storageDirectory.getLogSegmentFiles();
        int retries = 0;
        do {
            Thread.sleep(1000);
        } while (!snapshotFile.exists() && retries++ < 10);
        Assert.assertTrue(snapshotFile + " does not exist", snapshotFile.exists());
    } finally {
        cluster.shutdown();
    }
    // delete the log segments from the leader
    for (LogPathAndIndex path : logs) {
        FileUtils.deleteFile(path.path.toFile());
    }
    // restart the peer
    LOG.info("Restarting the cluster");
    cluster.restart(false);
    try {
        assertLeaderContent(cluster);
        // generate some more traffic
        try (final RaftClient client = cluster.createClient(cluster.getLeader().getId())) {
            Assert.assertTrue(client.send(new SimpleMessage("test")).isSuccess());
        }
        // add two more peers
        MiniRaftCluster.PeerChanges change = cluster.addNewPeers(new String[] { "s3", "s4" }, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
    } finally {
        cluster.shutdown();
    }
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftStorageDirectory(org.apache.ratis.server.storage.RaftStorageDirectory) LogPathAndIndex(org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

File (java.io.File)1 BaseTest (org.apache.ratis.BaseTest)1 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)1 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1 RaftStorageDirectory (org.apache.ratis.server.storage.RaftStorageDirectory)1 LogPathAndIndex (org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex)1 Test (org.junit.Test)1