Search in sources :

Example 1 with ConfigCommand

use of tezc.core.cluster.state.command.ConfigCommand 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();
        }
    }
}
Also used : ConfigCommand(tezc.core.cluster.state.command.ConfigCommand) Command(tezc.core.cluster.state.command.Command) RegisterCommand(tezc.core.cluster.state.command.RegisterCommand) NoOPCommand(tezc.core.cluster.state.command.NoOPCommand) NoOPCommand(tezc.core.cluster.state.command.NoOPCommand) ConfigCommand(tezc.core.cluster.state.command.ConfigCommand) PeerNode(tezc.core.node.PeerNode)

Aggregations

Command (tezc.core.cluster.state.command.Command)1 ConfigCommand (tezc.core.cluster.state.command.ConfigCommand)1 NoOPCommand (tezc.core.cluster.state.command.NoOPCommand)1 RegisterCommand (tezc.core.cluster.state.command.RegisterCommand)1 PeerNode (tezc.core.node.PeerNode)1