use of tezc.core.cluster.state.command.Command in project tezcRaft by tezc.
the class Cluster method handleRequestVoteRespMsg.
/**
* Handle requestVoteMsg callback
* @param node message sender node
* @param requestVoteResp requestVoteMessage
*/
public void handleRequestVoteRespMsg(PeerNode node, ReqVoteResp requestVoteResp) {
if (role != Role.CANDIDATE) {
return;
}
if (requestVoteResp.getTerm() > currentTerm) {
currentTerm = requestVoteResp.getTerm();
writeMeta();
setRole(Role.FOLLOWER);
return;
}
if (requestVoteResp.isVoteGranted()) {
grantedVotes.add(node);
// Check if we got enough votes to become a leader
if (grantedVotes.size() >= activeNodes.size() / 2 + 1) {
setRole(Role.LEADER);
for (PeerNode follower : nodes) {
follower.setNextIndex(lastIndex() + 1);
follower.setMatchIndex(lastIndex());
}
// If First log in the cluster log, append config, otherwise a NoOP
Command cmd = lastIndex() == 0 ? new ConfigCommand(clusterRecord) : new NoOPCommand();
cmd.encode();
if (lastIndex() == 0) {
ClientReq req = new ClientReq(false, RaftState.STATE_ID, sequence++, true, cmd.getRaw());
createEntry(new CommandRequest(own, req));
}
flush();
}
}
}
Aggregations