use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class SnapshotTest method when_followerMissesTheLastEntryThatGoesIntoTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot.
@Test
public void when_followerMissesTheLastEntryThatGoesIntoTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot() throws ExecutionException, InterruptedException {
final int entryCount = 50;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
group = newGroup(3, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl slowFollower = followers[1];
for (int i = 0; i < entryCount - 1; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl follower : group.getNodesExcept(leader.getLocalMember())) {
assertEquals(entryCount - 1, getMatchIndex(leader, follower.getLocalMember()));
}
});
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
leader.replicate(new ApplyRaftRunnable("val" + (entryCount - 1))).get();
assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
leader.replicate(new ApplyRaftRunnable("valFinal")).get();
group.allowMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount + 1, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(entryCount + 1, service.size());
for (int i = 0; i < entryCount; i++) {
assertEquals(("val" + i), service.get(i + 1));
}
assertEquals("valFinal", service.get(51));
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class LocalRaftTest method testSingleCommitEntry.
private void testSingleCommitEntry(final int nodeCount) throws Exception {
group = newGroup(nodeCount);
group.start();
group.waitUntilLeaderElected();
Object val = "val";
Object result = group.getLeaderNode().replicate(new ApplyRaftRunnable(val)).get();
assertEquals(result, val);
int commitIndex = 1;
assertTrueEventually(() -> {
for (int i = 0; i < nodeCount; i++) {
RaftNodeImpl node = group.getNode(i);
long index = getCommitIndex(node);
assertEquals(commitIndex, index);
RaftDataService service = group.getIntegration(i).getService();
Object actual = service.get(commitIndex);
assertEquals(val, actual);
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class LocalRaftTest method testReplicateEntriesConcurrently.
private void testReplicateEntriesConcurrently(int nodeCount) throws ExecutionException, InterruptedException {
final int entryCount = 100;
group = newGroup(nodeCount, newRaftConfigWithNoSnapshotting(entryCount));
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
List<Future> futures = new ArrayList<>(entryCount);
for (int i = 0; i < entryCount; i++) {
Object val = "val" + i;
futures.add(leader.replicate(new ApplyRaftRunnable(val)));
}
for (Future f : futures) {
f.get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(100, service.size());
Set<Object> values = service.values();
for (int i = 0; i < entryCount; i++) {
Object val = "val" + i;
assertTrue(values.contains(val));
}
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class LocalRaftTest method when_leaderCrashes_then_theFollowerWithLongestLogMayNotBecomeLeaderIfItsLogIsNotMajority.
@Test
public void when_leaderCrashes_then_theFollowerWithLongestLogMayNotBecomeLeaderIfItsLogIsNotMajority() throws Exception {
group = newGroup(5);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val1")).get();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl followerWithLongestLog = followers[0];
long commitIndex = getCommitIndex(leader);
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(commitIndex, getCommitIndex(raftNode));
}
});
for (int i = 1; i < followers.length; i++) {
group.dropMessagesToMember(leader.getLocalMember(), followers[i].getLocalMember(), AppendRequest.class);
}
leader.replicate(new ApplyRaftRunnable("val2"));
assertTrueEventually(() -> assertTrue(getLastLogOrSnapshotEntry(followerWithLongestLog).index() > commitIndex));
assertTrueAllTheTime(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(commitIndex, getCommitIndex(raftNode));
}
}, 10);
group.dropMessagesToMember(followerWithLongestLog.getLocalMember(), followers[1].getLocalMember(), VoteRequest.class);
group.dropMessagesToMember(followerWithLongestLog.getLocalMember(), followers[2].getLocalMember(), VoteRequest.class);
group.terminateNode(leader.getLocalMember());
RaftNodeImpl newLeader = group.waitUntilLeaderElected();
// followerWithLongestLog has 2 entries, other 3 followers have 1 entry
// and those 3 followers will elect a leader among themselves
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
RaftEndpoint newLeader1 = getLeaderMember(raftNode);
assertNotEquals(leader.getLocalMember(), newLeader1);
assertNotEquals(followerWithLongestLog.getLocalMember(), newLeader1);
}
});
for (int i = 1; i < followers.length; i++) {
assertEquals(commitIndex, getCommitIndex(followers[i]));
assertEquals(commitIndex, getLastLogOrSnapshotEntry(followers[i]).index());
}
// followerWithLongestLog does not truncate its extra log entry until the new leader appends a new entry
assertTrue(getLastLogOrSnapshotEntry(followerWithLongestLog).index() > commitIndex);
newLeader.replicate(new ApplyRaftRunnable("val3")).get();
assertTrueEventually(() -> {
for (RaftNodeImpl follower : followers) {
assertEquals(2, getCommitIndex(follower));
RaftDataService service = group.getService(follower);
assertEquals(2, service.size());
assertEquals("val1", service.get(1));
assertEquals("val3", service.get(2));
}
});
assertEquals(2, getLastLogOrSnapshotEntry(followerWithLongestLog).index());
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService in project hazelcast by hazelcast.
the class SnapshotTest method when_followersMatchIndexIsUnknown_then_itInstallsSnapshot.
@Test
public void when_followersMatchIndexIsUnknown_then_itInstallsSnapshot() throws ExecutionException, InterruptedException {
final int entryCount = 50;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
group = newGroup(3, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl slowFollower = followers[1];
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
for (int i = 0; i < entryCount; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> assertEquals(entryCount, getSnapshotEntry(leader).index()));
leader.replicate(new ApplyRaftRunnable("valFinal")).get();
group.allowMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
assertTrueEventually(() -> assertEquals(entryCount, getCommitIndex(slowFollower)));
group.resetAllRulesFrom(leader.getLocalMember());
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount + 1, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(entryCount + 1, service.size());
for (int i = 0; i < entryCount; i++) {
assertEquals(("val" + i), service.get(i + 1));
}
assertEquals("valFinal", service.get(51));
}
});
}
Aggregations