use of com.hazelcast.cp.internal.raft.impl.state.LeaderState in project hazelcast by hazelcast.
the class InitLeadershipTransferTask method run.
@Override
public void run() {
if (!raftNode.getCommittedMembers().contains(targetEndpoint)) {
resultFuture.completeExceptionally(new IllegalArgumentException("Cannot transfer leadership to " + targetEndpoint + " because it is not in the committed group member list!"));
return;
}
if (raftNode.getStatus() != RaftNodeStatus.ACTIVE) {
resultFuture.completeExceptionally(new IllegalStateException("Cannot transfer leadership to " + targetEndpoint + " because the status is " + raftNode.getStatus()));
return;
}
RaftState state = raftNode.state();
LeaderState leaderState = state.leaderState();
if (leaderState == null) {
resultFuture.completeExceptionally(new IllegalStateException("Cannot transfer leadership to " + targetEndpoint + " because I am not the leader!"));
return;
}
if (raftNode.getLocalMember().equals(targetEndpoint)) {
raftNode.getLogger(getClass()).warning("I am already the leader... There is no leadership transfer to myself.");
resultFuture.complete(null);
return;
}
if (state.initLeadershipTransfer(targetEndpoint, resultFuture)) {
new LeadershipTransferTask(raftNode, LEADERSHIP_TRANSFER_RETRY_COUNT).run();
}
}
use of com.hazelcast.cp.internal.raft.impl.state.LeaderState in project hazelcast by hazelcast.
the class LeadershipTransferTask method run.
@Override
public void run() {
ILogger logger = raftNode.getLogger(getClass());
RaftState state = raftNode.state();
LeaderState leaderState = state.leaderState();
if (leaderState == null) {
logger.fine("Not retrying leadership transfer since not leader...");
return;
}
LeadershipTransferState leadershipTransferState = state.leadershipTransferState();
checkTrue(leadershipTransferState != null, "No leadership transfer state!");
if (retryCount == maxRetryCount) {
String msg = "Leadership transfer to " + leadershipTransferState.endpoint() + " timed out!";
logger.warning(msg);
state.completeLeadershipTransfer(new IllegalStateException(msg));
return;
}
RaftEndpoint targetEndpoint = leadershipTransferState.endpoint();
if (state.commitIndex() < state.log().lastLogOrSnapshotIndex()) {
logger.warning("Waiting until all appended entries to be committed before transferring leadership to " + targetEndpoint);
reschedule();
return;
}
if (retryCount > 0) {
logger.fine("Retrying leadership transfer to " + leadershipTransferState.endpoint());
} else {
logger.info("Transferring leadership to " + leadershipTransferState.endpoint());
}
leaderState.getFollowerState(targetEndpoint).appendRequestAckReceived();
raftNode.sendAppendRequest(targetEndpoint);
LogEntry entry = state.log().lastLogOrSnapshotEntry();
raftNode.send(new TriggerLeaderElection(raftNode.getLocalMember(), state.term(), entry.term(), entry.index()), targetEndpoint);
reschedule();
}
use of com.hazelcast.cp.internal.raft.impl.state.LeaderState in project hazelcast by hazelcast.
the class AppendFailureResponseHandlerTask method updateNextIndex.
private boolean updateNextIndex(RaftState state) {
LeaderState leaderState = state.leaderState();
FollowerState followerState = leaderState.getFollowerState(resp.follower());
long nextIndex = followerState.nextIndex();
long matchIndex = followerState.matchIndex();
if (resp.expectedNextIndex() == nextIndex) {
// Received a response for the last append request. Resetting the flag...
followerState.appendRequestAckReceived();
// this is the response of the request I have sent for this nextIndex
nextIndex--;
if (nextIndex <= matchIndex) {
logger.severe("Cannot decrement next index: " + nextIndex + " below match index: " + matchIndex + " for follower: " + resp.follower());
return false;
}
if (logger.isFineEnabled()) {
logger.fine("Updating next index: " + nextIndex + " for follower: " + resp.follower());
}
followerState.nextIndex(nextIndex);
return true;
}
return false;
}
use of com.hazelcast.cp.internal.raft.impl.state.LeaderState in project hazelcast by hazelcast.
the class AppendSuccessResponseHandlerTask method updateFollowerIndices.
private boolean updateFollowerIndices(RaftState state) {
// If successful: update nextIndex and matchIndex for follower (ยง5.3)
RaftEndpoint follower = resp.follower();
LeaderState leaderState = state.leaderState();
FollowerState followerState = leaderState.getFollowerState(follower);
QueryState queryState = leaderState.queryState();
if (queryState.tryAck(resp.queryRound(), follower)) {
if (logger.isFineEnabled()) {
logger.fine("Ack from " + follower + " for query round: " + resp.queryRound());
}
}
long matchIndex = followerState.matchIndex();
long followerLastLogIndex = resp.lastLogIndex();
if (followerLastLogIndex > matchIndex) {
// Received a response for the last append request. Resetting the flag...
followerState.appendRequestAckReceived();
long newNextIndex = followerLastLogIndex + 1;
followerState.matchIndex(followerLastLogIndex);
followerState.nextIndex(newNextIndex);
if (logger.isFineEnabled()) {
logger.fine("Updated match index: " + followerLastLogIndex + " and next index: " + newNextIndex + " for follower: " + follower);
}
return true;
} else if (followerLastLogIndex == matchIndex) {
// Received a response for the last append request. Resetting the flag...
followerState.appendRequestAckReceived();
} else if (logger.isFineEnabled()) {
logger.fine("Will not update match index for follower: " + follower + ". follower last log index: " + followerLastLogIndex + ", match index: " + matchIndex);
}
return false;
}
use of com.hazelcast.cp.internal.raft.impl.state.LeaderState 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();
}
Aggregations