use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class MembershipChangeTest method when_newRaftNodeJoins_then_itAppendsMissingEntries.
@Test
public void when_newRaftNodeJoins_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
group = newGroup(3);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val")).get();
RaftNodeImpl newRaftNode = group.createNewRaftNode();
leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
long commitIndex = getCommitIndex(leader);
assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
assertTrueEventually(() -> {
RaftNodeImpl[] nodes = group.getNodes();
for (RaftNodeImpl raftNode : nodes) {
assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
}
});
RaftDataService service = group.getService(newRaftNode);
assertEquals(1, service.size());
assertTrue(service.values().contains("val"));
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class MembershipChangeTest method when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate.
@Test
public void when_replicatedMembershipChangeIsReverted_then_itCanBeCommittedOnSecondReplicate() throws ExecutionException, InterruptedException {
group = new LocalRaftGroupBuilder(3).setAppendNopEntryOnLeaderElection(true).build();
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
leader.replicate(new ApplyRaftRunnable("val1")).get();
long oldLeaderCommitIndexBeforeMemberhipChange = getCommitIndex(leader);
assertTrueEventually(() -> {
for (RaftNodeImpl follower : followers) {
assertEquals(oldLeaderCommitIndexBeforeMemberhipChange, getCommitIndex(follower));
}
});
group.dropMessagesToMember(leader.getLocalMember(), followers[0].getLocalMember(), AppendRequest.class);
group.dropMessagesToMember(leader.getLocalMember(), followers[1].getLocalMember(), AppendRequest.class);
RaftNodeImpl newRaftNode = group.createNewRaftNode();
leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD);
assertTrueEventually(() -> {
long leaderLastLogIndex = getLastLogOrSnapshotEntry(leader).index();
assertTrue(leaderLastLogIndex > oldLeaderCommitIndexBeforeMemberhipChange);
assertEquals(leaderLastLogIndex, getLastLogOrSnapshotEntry(newRaftNode).index());
});
group.dropMessagesToAll(newRaftNode.getLocalMember(), PreVoteRequest.class);
group.dropMessagesToAll(newRaftNode.getLocalMember(), VoteRequest.class);
group.terminateNode(leader.getLocalMember());
assertTrueEventually(() -> {
assertNotNull(followers[0].getLeader());
assertNotNull(followers[1].getLeader());
assertNotEquals(leader.getLocalMember(), followers[0].getLeader());
assertNotEquals(leader.getLocalMember(), followers[1].getLeader());
assertEquals(followers[0].getLeader(), followers[1].getLeader());
});
RaftNodeImpl newLeader = group.getNode(followers[0].getLeader());
newLeader.replicate(new ApplyRaftRunnable("val1")).get();
newLeader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
assertTrueEventually(() -> {
assertEquals(getCommitIndex(newLeader), getCommitIndex(newRaftNode));
assertEquals(getCommittedGroupMembers(newLeader).index(), getCommittedGroupMembers(newRaftNode).index());
});
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class MembershipChangeTest method when_newRaftNodeJoinsAfterAnotherNodeLeaves_then_itAppendsMissingEntries.
@Test
public void when_newRaftNodeJoinsAfterAnotherNodeLeaves_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
group = newGroup(3);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val")).get();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl leavingFollower = followers[0];
RaftNodeImpl stayingFollower = followers[1];
leader.replicateMembershipChange(leavingFollower.getLocalMember(), REMOVE).get();
RaftNodeImpl newRaftNode = group.createNewRaftNode();
leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
long commitIndex = getCommitIndex(leader);
assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : asList(leader, stayingFollower, newRaftNode)) {
assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
assertFalse(getLastGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
assertFalse(getCommittedGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
}
});
RaftDataService service = group.getService(newRaftNode);
assertEquals(1, service.size());
assertTrue(service.values().contains("val"));
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class MembershipChangeTest method when_newRaftNodeJoinsAfterAnotherNodeLeavesAndSnapshotIsTaken_then_itAppendsMissingEntries.
@Test
public void when_newRaftNodeJoinsAfterAnotherNodeLeavesAndSnapshotIsTaken_then_itAppendsMissingEntries() throws ExecutionException, InterruptedException {
int commitIndexAdvanceCountToSnapshot = 10;
RaftAlgorithmConfig config = new RaftAlgorithmConfig().setCommitIndexAdvanceCountToSnapshot(commitIndexAdvanceCountToSnapshot);
group = newGroup(3, config);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val")).get();
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
RaftNodeImpl leavingFollower = followers[0];
RaftNodeImpl stayingFollower = followers[1];
leader.replicateMembershipChange(leavingFollower.getLocalMember(), REMOVE).get();
for (int i = 0; i < commitIndexAdvanceCountToSnapshot; i++) {
leader.replicate(new ApplyRaftRunnable("val" + i)).get();
}
assertTrueEventually(() -> assertTrue(getSnapshotEntry(leader).index() > 0));
RaftNodeImpl newRaftNode = group.createNewRaftNode();
leader.replicateMembershipChange(newRaftNode.getLocalMember(), MembershipChangeMode.ADD).get();
long commitIndex = getCommitIndex(leader);
assertTrueEventually(() -> assertEquals(commitIndex, getCommitIndex(newRaftNode)));
RaftGroupMembers lastGroupMembers = RaftUtil.getLastGroupMembers(leader);
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : asList(leader, stayingFollower, newRaftNode)) {
assertEquals(RaftNodeStatus.ACTIVE, getStatus(raftNode));
assertEquals(lastGroupMembers.members(), getLastGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getLastGroupMembers(raftNode).index());
assertEquals(lastGroupMembers.members(), getCommittedGroupMembers(raftNode).members());
assertEquals(lastGroupMembers.index(), getCommittedGroupMembers(raftNode).index());
assertFalse(getLastGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
assertFalse(getCommittedGroupMembers(raftNode).isKnownMember(leavingFollower.getLocalMember()));
}
});
RaftDataService service = group.getService(newRaftNode);
assertEquals(commitIndexAdvanceCountToSnapshot + 1, service.size());
assertTrue(service.values().contains("val"));
for (int i = 0; i < commitIndexAdvanceCountToSnapshot; i++) {
assertTrue(service.values().contains("val" + i));
}
}
use of com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable in project hazelcast by hazelcast.
the class LocalRaftTest method testReplicateEntriesSequentially.
private void testReplicateEntriesSequentially(int nodeCount) throws ExecutionException, InterruptedException {
final int entryCount = 100;
group = newGroup(nodeCount, newRaftConfigWithNoSnapshotting(entryCount));
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
for (int i = 0; i < entryCount; i++) {
Object val = "val" + i;
leader.replicate(new ApplyRaftRunnable(val)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(entryCount, getCommitIndex(raftNode));
RaftDataService service = group.getService(raftNode);
assertEquals(100, service.size());
for (int i = 0; i < entryCount; i++) {
int commitIndex = i + 1;
Object val = "val" + i;
assertEquals(val, service.get(commitIndex));
}
}
});
}
Aggregations