use of com.hazelcast.cp.internal.raft.impl.state.RaftState 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.RaftState 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.RaftState in project hazelcast by hazelcast.
the class ReplicateTask method run.
@Override
public void run() {
try {
if (!verifyRaftNodeStatus()) {
return;
}
RaftState state = raftNode.state();
if (state.role() != LEADER) {
resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
return;
}
if (!raftNode.canReplicateNewEntry(operation)) {
resultFuture.completeExceptionally(new CannotReplicateException(raftNode.getLocalMember()));
return;
}
if (logger.isFineEnabled()) {
logger.fine("Replicating: " + operation + " in term: " + state.term());
}
RaftLog log = state.log();
if (!log.checkAvailableCapacity(1)) {
resultFuture.completeExceptionally(new IllegalStateException("Not enough capacity in RaftLog!"));
return;
}
long newEntryLogIndex = log.lastLogOrSnapshotIndex() + 1;
raftNode.registerFuture(newEntryLogIndex, resultFuture);
log.appendEntries(new LogEntry(state.term(), newEntryLogIndex, operation));
preApplyRaftGroupCmd(newEntryLogIndex, operation);
raftNode.broadcastAppendRequest();
} catch (Throwable t) {
logger.severe(operation + " could not be replicated to leader: " + raftNode.getLocalMember(), t);
RaftEndpoint leader = raftNode.getLeader();
UUID leaderUuid = leader != null ? leader.getUuid() : null;
resultFuture.completeExceptionally(new CPSubsystemException("Internal failure", t, leaderUuid));
}
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftState in project hazelcast by hazelcast.
the class LeaderElectionTask method innerRun.
@Override
protected void innerRun() {
RaftState state = raftNode.state();
if (state.leader() != null) {
logger.warning("No new election round, we already have a LEADER: " + state.leader());
return;
}
VoteRequest request = state.toCandidate(disruptive);
logger.info("Leader election started for term: " + request.term() + ", last log index: " + request.lastLogIndex() + ", last log term: " + request.lastLogTerm());
raftNode.printMemberState();
for (RaftEndpoint endpoint : state.remoteMembers()) {
raftNode.send(request, endpoint);
}
scheduleLeaderElectionTimeout();
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftState 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();
}
Aggregations