use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class LocalRaftTest method when_followerSlowsDown_then_itCatchesLeaderEventually.
@Test
public void when_followerSlowsDown_then_itCatchesLeaderEventually() throws ExecutionException, InterruptedException {
final int entryCount = 100;
group = newGroup(3, newRaftConfigWithNoSnapshotting(entryCount));
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
RaftNodeImpl slowFollower = group.getAnyFollowerNode();
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
for (int i = 0; i < entryCount; i++) {
Object val = "val" + i;
leader.replicate(new ApplyRaftRunnable(val)).get();
}
assertEquals(0, getCommitIndex(slowFollower));
group.resetAllRulesFrom(leader.getLocalMember());
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(entryCount, 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.ApplyRaftRunnable in project hazelcast by hazelcast.
the class SnapshotTest method when_followerMissesAFewEntriesBeforeTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot.
@Test
public void when_followerMissesAFewEntriesBeforeTheSnapshot_then_itCatchesUpWithoutInstallingSnapshot() throws ExecutionException, InterruptedException {
final int entryCount = 50;
// entryCount * 0.1 - 2
final int missingEntryCountOnSlowFollower = 4;
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 - missingEntryCountOnSlowFollower; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl follower : group.getNodesExcept(leader.getLocalMember())) {
assertEquals(entryCount - missingEntryCountOnSlowFollower, getMatchIndex(leader, follower.getLocalMember()));
}
});
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), slowFollower.getLocalMember(), InstallSnapshot.class);
for (int i = entryCount - missingEntryCountOnSlowFollower; 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(), 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.ApplyRaftRunnable in project hazelcast by hazelcast.
the class SnapshotTest method when_slowFollowerReceivesAppendRequestThatDoesNotFitIntoItsRaftLog_then_itTruncatesAppendRequestEntries.
@Test
public void when_slowFollowerReceivesAppendRequestThatDoesNotFitIntoItsRaftLog_then_itTruncatesAppendRequestEntries() throws ExecutionException, InterruptedException {
int appendRequestMaxEntryCount = 100;
int commitIndexAdvanceCount = 100;
int uncommittedEntryCount = 10;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setAppendRequestMaxEntryCount(appendRequestMaxEntryCount).setCommitIndexAdvanceCountToSnapshot(commitIndexAdvanceCount).setUncommittedEntryCountToRejectNewAppends(uncommittedEntryCount);
group = newGroup(5, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl slowFollower1 = followers[0];
RaftNodeImpl slowFollower2 = followers[1];
int count = 1;
for (int i = 0; i < commitIndexAdvanceCount; i++) {
leader.replicate(new ApplyRaftRunnable("val" + (count++))).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl node : group.getNodes()) {
assertTrue(getSnapshotEntry(node).index() > 0);
}
});
group.dropMessagesToMember(leader.getLocalMember(), slowFollower1.getLocalMember(), AppendRequest.class);
for (int i = 0; i < commitIndexAdvanceCount - 1; i++) {
leader.replicate(new ApplyRaftRunnable("val" + (count++))).get();
}
assertTrueEventually(() -> assertEquals(getCommitIndex(leader), getCommitIndex(slowFollower2)));
// slowFollower2's log: [ <91 - 100 before snapshot>, <100 snapshot>, <101 - 199 committed> ]
group.dropMessagesToMember(leader.getLocalMember(), slowFollower2.getLocalMember(), AppendRequest.class);
for (int i = 0; i < commitIndexAdvanceCount / 2; i++) {
leader.replicate(new ApplyRaftRunnable("val" + (count++))).get();
}
assertTrueEventually(() -> assertTrue(getSnapshotEntry(leader).index() > commitIndexAdvanceCount));
// leader's log: [ <191 - 199 before snapshot>, <200 snapshot>, <201 - 249 committed> ]
group.allowMessagesToMember(leader.getLocalMember(), slowFollower2.getLocalMember(), AppendRequest.class);
// leader replicates 50 entries to slowFollower2 but slowFollower2 has only available capacity of 11 indices.
// so, slowFollower2 appends 11 of these 50 entries in the first AppendRequest, takes a snapshot,
// and receives another AppendRequest for the remaining entries...
assertTrueEventually(() -> {
assertEquals(getCommitIndex(leader), getCommitIndex(slowFollower2));
assertTrue(getSnapshotEntry(slowFollower2).index() > commitIndexAdvanceCount);
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class SnapshotTest method when_isolatedLeaderAppendsEntries_then_itInvalidatesTheirFeaturesUponInstallSnapshot.
@Test
public void when_isolatedLeaderAppendsEntries_then_itInvalidatesTheirFeaturesUponInstallSnapshot() throws ExecutionException, InterruptedException {
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());
for (int i = 0; i < 40; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(40, getCommitIndex(raftNode));
}
});
group.split(leader.getLocalMember());
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
assertNotNull(leaderEndpoint);
assertNotEquals(leader.getLocalMember(), leaderEndpoint);
}
});
List<InternalCompletableFuture> futures = new ArrayList<>();
for (int i = 40; i < 45; i++) {
InternalCompletableFuture f = leader.replicate(new ApplyRaftRunnable("isolated" + i));
futures.add(f);
}
RaftNodeImpl newLeader = group.getNode(getLeaderMember(followers[0]));
for (int i = 40; i < 51; i++) {
newLeader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
assertTrue(getSnapshotEntry(raftNode).index() > 0);
}
});
group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
group.merge();
for (InternalCompletableFuture f : futures) {
try {
f.joinInternal();
fail();
} catch (StaleAppendRequestException ignored) {
}
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(51, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(51, service.size());
for (int i = 0; i < 51; i++) {
assertEquals(("val" + i), service.get(i + 1));
}
}
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class SnapshotTest method when_commitLogAdvances_then_snapshotIsTaken.
@Test
public void when_commitLogAdvances_then_snapshotIsTaken() throws ExecutionException, InterruptedException {
final int entryCount = 50;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(entryCount);
group = newGroup(3, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
for (int i = 0; i < entryCount; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount, getCommitIndex(raftNode));
assertEquals(entryCount, getSnapshotEntry(raftNode).index());
RaftDataService service = group.getService(raftNode);
assertEquals(entryCount, service.size());
for (int i = 0; i < entryCount; i++) {
assertEquals(("val" + i), service.get(i + 1));
}
}
});
}
Aggregations