use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.
the class RaftNodeImpl method invalidateFuturesFrom.
/**
* Invalidates futures registered with indexes {@code >= entryIndex}. Note that {@code entryIndex} is inclusive.
* {@link LeaderDemotedException} is set a result to futures.
*/
public void invalidateFuturesFrom(long entryIndex) {
int count = 0;
Iterator<Entry<Long, InternalCompletableFuture>> iterator = futures.entrySet().iterator();
while (iterator.hasNext()) {
Entry<Long, InternalCompletableFuture> entry = iterator.next();
long index = entry.getKey();
if (index >= entryIndex) {
entry.getValue().completeExceptionally(new LeaderDemotedException(state.localEndpoint(), state.leader()));
iterator.remove();
count++;
}
}
if (count > 0) {
logger.warning("Invalidated " + count + " futures from log index: " + entryIndex);
}
}
use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.
the class RaftNodeImpl method toFollower.
/**
* Switches this node to follower role by clearing the known leader
* endpoint and (pre) candidate states, and updating the term. If this Raft
* node was leader before switching to the follower state, it may have some
* queries waiting to be executed. Those queries are also failed with
* {@link LeaderDemotedException}. After the state switch,
* {@link RaftIntegration#onNodeStatusChange(RaftNodeStatus)} is called.
*
* @param term the new term to switch
*/
public void toFollower(int term) {
LeaderState leaderState = state.leaderState();
if (leaderState != null) {
for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
}
}
state.toFollower(term);
printMemberState();
}
use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.
the class RaftNodeImpl method forceSetTerminatedStatus.
@Override
public InternalCompletableFuture forceSetTerminatedStatus() {
InternalCompletableFuture resultFuture = raftIntegration.newCompletableFuture();
if (isTerminatedOrSteppedDown()) {
if (logger.isFineEnabled()) {
logger.fine("Already stepped down or terminated, not setting `TERMINATED` status.");
}
resultFuture.complete(null);
return resultFuture;
}
execute(() -> {
Throwable failure = null;
try {
if (isTerminatedOrSteppedDown()) {
return;
} else if (status == INITIAL) {
setStatus(TERMINATED);
return;
}
invalidateFuturesFrom(state.commitIndex() + 1);
LeaderState leaderState = state.leaderState();
if (leaderState != null) {
for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
}
}
state.completeLeadershipTransfer(new LeaderDemotedException(state.localEndpoint(), null));
setStatus(TERMINATED);
} catch (Throwable t) {
failure = t;
logger.severe("Failure during force-termination", t);
if (status != TERMINATED && status != STEPPED_DOWN) {
setStatus(TERMINATED);
}
} finally {
if (failure == null) {
resultFuture.complete(null);
} else {
resultFuture.completeExceptionally(failure);
}
}
});
return resultFuture;
}
use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.
the class LinearizableQueryTest method when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails.
@Test(timeout = 300_000)
public void when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails() throws Exception {
group = newGroup();
group.start();
RaftNodeImpl oldLeader = group.waitUntilLeaderElected();
final int[] split = group.createMajoritySplitIndexes(false);
group.split(split);
InternalCompletableFuture queryFuture = oldLeader.query(new QueryRaftRunnable(), LINEARIZABLE);
assertTrueEventually(() -> {
for (int ix : split) {
RaftEndpoint newLeader = getLeaderMember(group.getNode(ix));
assertNotNull(newLeader);
assertNotEquals(oldLeader.getLocalMember(), newLeader);
}
});
RaftNodeImpl newLeader = group.getNode(getLeaderMember(group.getNode(split[0])));
newLeader.replicate(new ApplyRaftRunnable("value1")).get();
group.merge();
group.waitUntilLeaderElected();
assertEquals(oldLeader.getLeader(), newLeader.getLocalMember());
try {
queryFuture.joinInternal();
fail();
} catch (LeaderDemotedException ignored) {
}
}
use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.
the class LocalRaftTest method when_leaderStaysInMinorityDuringSplit_then_itCannotCommitNewEntries.
@Test
public void when_leaderStaysInMinorityDuringSplit_then_itCannotCommitNewEntries() throws ExecutionException, InterruptedException {
group = newGroup(3, newRaftConfigWithNoSnapshotting(100));
group.start();
RaftNodeImpl leader = group.waitUntilLeaderElected();
leader.replicate(new ApplyRaftRunnable("val1")).get();
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : group.getNodes()) {
assertEquals(1, getCommitIndex(raftNode));
}
});
RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
group.split(leader.getLocalMember());
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
assertNotNull(leaderEndpoint);
assertNotEquals(leader.getLocalMember(), leaderEndpoint);
}
});
List<InternalCompletableFuture> isolatedFutures = new ArrayList<>();
for (int i = 0; i < 10; i++) {
isolatedFutures.add(leader.replicate(new ApplyRaftRunnable("isolated" + i)));
}
RaftNodeImpl newLeader = group.getNode(getLeaderMember(followers[0]));
for (int i = 0; i < 10; i++) {
newLeader.replicate(new ApplyRaftRunnable("valNew" + i)).get();
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
assertEquals(11, getCommitIndex(raftNode));
}
});
group.merge();
RaftNodeImpl finalLeader = group.waitUntilLeaderElected();
assertNotEquals(leader.getLocalMember(), finalLeader.getLocalMember());
for (InternalCompletableFuture f : isolatedFutures) {
try {
f.joinInternal();
fail();
} catch (LeaderDemotedException ignored) {
}
}
assertTrueEventually(() -> {
for (RaftNodeImpl raftNode : followers) {
RaftDataService service = group.getService(raftNode);
assertEquals(11, service.size());
assertEquals("val1", service.get(1));
for (int i = 0; i < 10; i++) {
assertEquals(("valNew" + i), service.get(i + 2));
}
}
});
}
Aggregations