use of com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers in project hazelcast by hazelcast.
the class MembershipChangeTask method isValidGroupMemberCommitIndex.
private boolean isValidGroupMemberCommitIndex() {
if (groupMembersCommitIndex != null) {
RaftState state = raftNode.state();
RaftGroupMembers groupMembers = state.committedGroupMembers();
if (groupMembers.index() != groupMembersCommitIndex) {
logger.severe("Cannot " + membershipChangeMode + " " + member + " because expected members commit index: " + groupMembersCommitIndex + " is different than group members commit index: " + groupMembers.index());
Exception e = new MismatchingGroupMembersCommitIndexException(groupMembers.index(), groupMembers.members());
resultFuture.completeExceptionally(e);
return false;
}
}
return true;
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftGroupMembers 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.state.RaftGroupMembers 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.state.RaftGroupMembers 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.state.RaftGroupMembers in project hazelcast by hazelcast.
the class RaftNodeImpl method takeSnapshotIfCommitIndexAdvanced.
/**
* Takes a snapshot if the advance in {@code commitIndex} is equal to
* {@link RaftAlgorithmConfig#getCommitIndexAdvanceCountToSnapshot()}.
* <p>
* Snapshot is not created if the Raft group is being destroyed.
*/
@SuppressWarnings("checkstyle:npathcomplexity")
private void takeSnapshotIfCommitIndexAdvanced() {
long commitIndex = state.commitIndex();
if ((commitIndex - state.log().snapshotIndex()) < commitIndexAdvanceCountToSnapshot) {
return;
}
if (isTerminatedOrSteppedDown()) {
// If the status is TERMINATED or STEPPED_DOWN, then there will not be any new appends.
return;
}
RaftLog log = state.log();
Object snapshot = raftIntegration.takeSnapshot(commitIndex);
if (snapshot instanceof Throwable) {
Throwable t = (Throwable) snapshot;
logger.severe("Could not take snapshot at commit index: " + commitIndex, t);
return;
}
int snapshotTerm = log.getLogEntry(commitIndex).term();
RaftGroupMembers members = state.committedGroupMembers();
SnapshotEntry snapshotEntry = new SnapshotEntry(snapshotTerm, commitIndex, snapshot, members.index(), members.members());
long highestLogIndexToTruncate = commitIndex - maxNumberOfLogsToKeepAfterSnapshot;
LeaderState leaderState = state.leaderState();
if (leaderState != null) {
long[] matchIndices = leaderState.matchIndices();
// Last slot is reserved for leader index and always zero.
// If there is at least one follower with unknown match index,
// its log can be close to the leader's log so we are keeping the old log entries.
boolean allMatchIndicesKnown = Arrays.stream(matchIndices, 0, matchIndices.length - 1).noneMatch(i -> i == 0);
if (allMatchIndicesKnown) {
// Otherwise, we will keep the log entries until the minimum match index
// that is bigger than (commitIndex - maxNumberOfLogsToKeepAfterSnapshot).
// If there is no such follower (all of the minority followers are far behind),
// then there is no need to keep the old log entries.
highestLogIndexToTruncate = Arrays.stream(matchIndices).filter(i -> i < commitIndex).filter(i -> i > commitIndex - maxNumberOfLogsToKeepAfterSnapshot).map(i -> i - 1).sorted().findFirst().orElse(commitIndex);
}
}
int truncatedEntryCount = log.setSnapshot(snapshotEntry, highestLogIndexToTruncate);
if (logger.isFineEnabled()) {
logger.fine(snapshotEntry + " is taken, " + truncatedEntryCount + " entries are truncated.");
}
}
Aggregations