use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testInstallSnapshotDuringBootstrap.
/**
* Test for install snapshot during a peer bootstrap: start a one node cluster
* and let it generate a snapshot. Add another node and verify that the new
* node installs a snapshot from the old node.
*/
@Test
public void testInstallSnapshotDuringBootstrap() throws Exception {
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 SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
// wait for the snapshot to be done
final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
LOG.info("nextIndex = {}", nextIndex);
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
verifyTakeSnapshotMetric(cluster.getLeader());
assertLeaderContent(cluster);
// add two more peers
String[] newPeers = new String[] { "s3", "s4" };
MiniRaftCluster.PeerChanges change = cluster.addNewPeers(newPeers, true, false);
// trigger setConfiguration
cluster.setConfiguration(change.allPeersInNewConf);
for (String newPeer : newPeers) {
final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
}
// Verify installSnapshot counter on leader
verifyInstallSnapshotMetric(cluster.getLeader());
RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method testRestartPeer.
/**
* Keep generating writing traffic and make sure snapshots are taken.
* We then restart the whole raft peer and check if it can correctly load
* snapshots + raft log.
*/
@Test
public void testRestartPeer() throws Exception {
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.io().send(new SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
}
final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
LOG.info("nextIndex = {}", nextIndex);
// wait for the snapshot to be done
final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
return null;
}, 10, ONE_SECOND, "snapshotFile.exist", LOG);
// restart the peer and check if it can correctly load snapshot
cluster.restart(false);
try {
// 200 messages + two leader elections --> last committed = 201
assertLeaderContent(cluster);
} finally {
cluster.shutdown();
}
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class SnapshotManagementTest method runTestTakeSnapshotOnSpecificServer.
void runTestTakeSnapshotOnSpecificServer(CLUSTER cluster) throws Exception {
final RaftClientReply snapshotReply;
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftServer.Division follower = cluster.getFollowers().get(0);
final RaftPeerId followerId = follower.getId();
Assert.assertTrue(follower.getInfo().isFollower());
try (final RaftClient client = cluster.createClient(followerId)) {
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
snapshotReply = client.getSnapshotManagementApi(followerId).create(3000);
}
Assert.assertTrue(snapshotReply.isSuccess());
final long snapshotIndex = snapshotReply.getLogIndex();
LOG.info("snapshotIndex = {} on {} server {}", snapshotIndex, follower.getInfo().getCurrentRole(), follower.getId());
final File snapshotFile = SimpleStateMachine4Testing.get(follower).getStateMachineStorage().getSnapshotFile(follower.getInfo().getCurrentTerm(), snapshotIndex);
Assert.assertTrue(snapshotFile.exists());
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class SnapshotManagementTest method runTestTakeSnapshot.
void runTestTakeSnapshot(CLUSTER cluster) throws Exception {
final RaftClientReply snapshotReply;
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()); i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
snapshotReply = client.getSnapshotManagementApi().create(3000);
}
Assert.assertTrue(snapshotReply.isSuccess());
final long snapshotIndex = snapshotReply.getLogIndex();
LOG.info("snapshotIndex = {}", snapshotIndex);
final File snapshotFile = SimpleStateMachine4Testing.get(leader).getStateMachineStorage().getSnapshotFile(leader.getInfo().getCurrentTerm(), snapshotIndex);
Assert.assertTrue(snapshotFile.exists());
}
use of org.apache.ratis.protocol.RaftClientReply in project incubator-ratis by apache.
the class SnapshotManagementTest method runTestTakeSnapshotWithConfigurableGap.
void runTestTakeSnapshotWithConfigurableGap(CLUSTER cluster) throws Exception {
RaftClientReply snapshotReply;
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
try (final RaftClient client = cluster.createClient(leaderId)) {
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()) / 2 - 1; i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
Assert.assertTrue(leader.getStateMachine().getLastAppliedTermIndex().getIndex() < RaftServerConfigKeys.Snapshot.creationGap(getProperties()));
snapshotReply = client.getSnapshotManagementApi(leaderId).create(3000);
Assert.assertTrue(snapshotReply.isSuccess());
Assert.assertEquals(0, snapshotReply.getLogIndex());
for (int i = 0; i < RaftServerConfigKeys.Snapshot.creationGap(getProperties()) / 2 - 1; i++) {
RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
Assert.assertTrue(reply.isSuccess());
}
final SnapshotManagementRequest r1 = SnapshotManagementRequest.newCreate(client.getId(), leaderId, cluster.getGroupId(), CallId.getAndIncrement(), 3000);
snapshotReply = client.getSnapshotManagementApi(leaderId).create(3000);
}
Assert.assertTrue(snapshotReply.isSuccess());
final long snapshotIndex = snapshotReply.getLogIndex();
LOG.info("snapshotIndex = {}", snapshotIndex);
final File snapshotFile = SimpleStateMachine4Testing.get(leader).getStateMachineStorage().getSnapshotFile(leader.getInfo().getCurrentTerm(), snapshotIndex);
Assert.assertTrue(snapshotFile.exists());
}
Aggregations