use of com.hazelcast.cp.exception.StaleAppendRequestException in project hazelcast by hazelcast.
the class LocalRaftTest method when_leaderStaysInMinority_then_itDemotesItselfToFollower.
@Test
public void when_leaderStaysInMinority_then_itDemotesItselfToFollower() {
group = newGroup(3);
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
group.split(leader.getLocalMember());
InternalCompletableFuture f = leader.replicate(new ApplyRaftRunnable("val"));
try {
f.joinInternal();
fail();
} catch (StaleAppendRequestException ignored) {
}
}
use of com.hazelcast.cp.exception.StaleAppendRequestException in project hazelcast by hazelcast.
the class RaftNodeImpl method installSnapshot.
/**
* Restores the snapshot sent by the leader if it's not applied before.
*
* @return true if snapshot is restored, false otherwise.
*/
public boolean installSnapshot(SnapshotEntry snapshot) {
long commitIndex = state.commitIndex();
if (commitIndex > snapshot.index()) {
logger.info("Ignored stale " + snapshot + ", commit index at: " + commitIndex);
return false;
} else if (commitIndex == snapshot.index()) {
logger.info("Ignored " + snapshot + " since commit index is same.");
return true;
}
state.commitIndex(snapshot.index());
RaftLog raftLog = state.log();
int truncated = raftLog.setSnapshot(snapshot);
raftLog.flush();
if (truncated > 0) {
logger.info(truncated + " entries are truncated to install " + snapshot);
}
raftIntegration.restoreSnapshot(snapshot.operation(), snapshot.index());
// If I am installing a snapshot, it means I am still present in the last member list,
// but it is possible that the last entry I appended before the snapshot could be a membership change.
// Because of this, I need to update my status.
// Nevertheless, I may not be present in the restored member list, which is ok.
setStatus(ACTIVE);
state.restoreGroupMembers(snapshot.groupMembersLogIndex(), snapshot.groupMembers());
printMemberState();
state.lastApplied(snapshot.index());
invalidateFuturesUntil(snapshot.index(), new StaleAppendRequestException(state.leader()));
logger.info(snapshot + " is installed.");
return true;
}
use of com.hazelcast.cp.exception.StaleAppendRequestException 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));
}
}
});
}
Aggregations