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();
}
}
Aggregations