Search in sources :

Example 1 with NoOPCommand

use of tezc.core.cluster.state.command.NoOPCommand 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)

Example 2 with NoOPCommand

use of tezc.core.cluster.state.command.NoOPCommand in project tezcRaft by tezc.

the class Cluster method startLog.

/**
 * Cluster is started, so initialize our entry list and mark our file
 * storage as started
 */
private void startLog() {
    if (!started) {
        stores.add(createStore());
        addEntry(new Entry(0, 0, 0, 0, new NoOPCommand().getRaw()));
        writeMeta();
    }
}
Also used : NoOPCommand(tezc.core.cluster.state.command.NoOPCommand)

Aggregations

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