use of com.hazelcast.cp.internal.raft.impl.state.RaftState in project hazelcast by hazelcast.
the class PreVoteResponseHandlerTask method handleResponse.
@Override
protected void handleResponse() {
RaftState state = raftNode.state();
if (state.role() != FOLLOWER) {
logger.info("Ignored " + resp + ". We are not FOLLOWER anymore.");
return;
}
if (resp.term() < state.term()) {
logger.warning("Stale " + resp + " is received, current term: " + state.term());
return;
}
CandidateState preCandidateState = state.preCandidateState();
if (preCandidateState == null) {
if (logger.isFineEnabled()) {
logger.fine("Ignoring " + resp + ". We are not interested in pre-votes anymore.");
}
return;
}
if (resp.granted() && preCandidateState.grantVote(resp.voter())) {
logger.info("Pre-vote granted from " + resp.voter() + " for term: " + resp.term() + ", number of votes: " + preCandidateState.voteCount() + ", majority: " + preCandidateState.majority());
}
if (preCandidateState.isMajorityGranted()) {
logger.info("We have the majority during pre-vote phase. Let's start real election!");
new LeaderElectionTask(raftNode, false).run();
}
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftState in project hazelcast by hazelcast.
the class PreVoteTask method innerRun.
@Override
protected void innerRun() {
RaftState state = raftNode.state();
if (state.leader() != null) {
logger.fine("No new pre-vote phase, we already have a LEADER: " + state.leader());
return;
} else if (state.term() != term) {
logger.fine("No new pre-vote phase for term= " + term + " because of new term: " + state.term());
return;
}
Collection<RaftEndpoint> remoteMembers = state.remoteMembers();
if (remoteMembers.isEmpty()) {
logger.fine("Remote members is empty. No need for pre-voting.");
return;
}
state.initPreCandidateState();
int nextTerm = state.term() + 1;
RaftLog log = state.log();
PreVoteRequest request = new PreVoteRequest(localMember(), nextTerm, log.lastLogOrSnapshotTerm(), log.lastLogOrSnapshotIndex());
logger.info("Pre-vote started for next term: " + request.nextTerm() + ", last log index: " + request.lastLogIndex() + ", last log term: " + request.lastLogTerm());
raftNode.printMemberState();
for (RaftEndpoint endpoint : remoteMembers) {
raftNode.send(request, endpoint);
}
schedulePreVoteTimeout();
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftState in project hazelcast by hazelcast.
the class QueryTask method handleLinearizableRead.
private void handleLinearizableRead() {
if (!raftNode.isLinearizableReadOptimizationEnabled()) {
new ReplicateTask(raftNode, operation, resultFuture).run();
return;
}
RaftState state = raftNode.state();
if (state.role() != LEADER) {
resultFuture.completeExceptionally(new NotLeaderException(raftNode.getGroupId(), raftNode.getLocalMember(), state.leader()));
return;
}
if (!raftNode.canQueryLinearizable()) {
resultFuture.completeExceptionally(new CannotReplicateException(state.leader()));
return;
}
long commitIndex = state.commitIndex();
QueryState queryState = state.leaderState().queryState();
if (logger.isFineEnabled()) {
logger.fine("Adding query at commit index: " + commitIndex + ", query round: " + queryState.queryRound());
}
if (queryState.addQuery(commitIndex, operation, resultFuture) == 1) {
raftNode.broadcastAppendRequest();
}
}
use of com.hazelcast.cp.internal.raft.impl.state.RaftState in project hazelcast by hazelcast.
the class MembershipChangeTask 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 (!isValidGroupMemberCommitIndex()) {
return;
}
Collection<RaftEndpoint> members = new LinkedHashSet<RaftEndpoint>(state.members());
boolean memberExists = members.contains(member);
switch(membershipChangeMode) {
case ADD:
if (memberExists) {
resultFuture.completeExceptionally(new MemberAlreadyExistsException(member));
return;
}
members.add(member);
break;
case REMOVE:
if (!memberExists) {
resultFuture.completeExceptionally(new MemberDoesNotExistException(member));
return;
}
members.remove(member);
break;
default:
resultFuture.completeExceptionally(new IllegalArgumentException("Unknown type: " + membershipChangeMode));
return;
}
logger.info("New members after " + membershipChangeMode + " " + member + " -> " + members);
new ReplicateTask(raftNode, new UpdateRaftGroupMembersCmd(members, member, membershipChangeMode), resultFuture).run();
} catch (Throwable t) {
logger.severe(this + " failed", 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 PreVoteTimeoutTask method innerRun.
@Override
protected void innerRun() {
RaftState state = raftNode.state();
// Remove previously set preCandidateState.
// Since it's now obsolete,
// either a new pre-vote round will begin
// or pre-vote phase will cease.
state.removePreCandidateState();
if (state.role() != FOLLOWER) {
return;
}
logger.fine("Pre-vote for term: " + state.term() + " has timed out!");
new PreVoteTask(raftNode, term).run();
}
Aggregations