Search in sources :

Example 6 with PeerNode

use of tezc.core.node.PeerNode 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 7 with PeerNode

use of tezc.core.node.PeerNode in project tezcRaft by tezc.

the class Cluster method handleIncomingConnEvent.

/**
 * Handle incoming connection callback
 * @param conn incoming connection
 * @param req  connectRequest message
 */
public void handleIncomingConnEvent(Connection conn, ConnectReq req) {
    if (req.isClient()) {
        if (role != Role.LEADER) {
            conn.sendCancelEvent();
            return;
        }
        PeerNode node = clients.get(req.getName());
        if (node != null) {
            node.inherit(conn);
            conn.sendAcceptConnEvent(node);
            node.sendConnectResp(clusterRecord, node.getId(), node.getSequence(), true);
        } else {
            node = new PeerNode(worker, this, conn, null, new NodeRecord(req.getName(), ""), PeerNode.Type.CLIENT);
            conn.sendAcceptConnEvent(node);
            clients.put(req.getName(), node);
            RegisterCommand cmd = new RegisterCommand(req.getName());
            cmd.encode();
            createEntry(new CommandRequest(own, new ClientReq(false, 0, sequence++, false, cmd.getRaw())));
            flush();
        }
    } else {
        for (PeerNode node : nodes) {
            if (req.getName().equals(node.getRemote().name)) {
                node.inherit(conn);
                conn.sendAcceptConnEvent(node);
                node.sendConnectResp(clusterRecord, node.getId(), node.getSequence(), true);
                if (!activeNodes.contains(node)) {
                    activeNodes.add(node);
                }
                return;
            }
        }
        conn.sendCancelEvent();
    }
}
Also used : NodeRecord(tezc.core.record.NodeRecord) PeerNode(tezc.core.node.PeerNode) RegisterCommand(tezc.core.cluster.state.command.RegisterCommand)

Example 8 with PeerNode

use of tezc.core.node.PeerNode in project tezcRaft by tezc.

the class Cluster method checkCommit.

/**
 * Check if index is eligible to be marked committed
 *
 * @param index commit index
 */
private void checkCommit(long index) {
    int count = 0;
    for (PeerNode peer : activeNodes) {
        if (peer.getMatchIndex() >= index) {
            count++;
        }
    }
    if (count == activeNodes.size()) {
        matchIndex = index;
    }
    if (count + 1 >= (nodes.size() + 1) / 2) {
        flushPendings();
        incrementCommit(index);
        Iterator<CommandRequest> it = requests.iterator();
        while (it.hasNext()) {
            CommandRequest request = it.next();
            if (request.getCommit() > index) {
                return;
            }
            handleCommandRequest(request);
            it.remove();
        }
    }
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 9 with PeerNode

use of tezc.core.node.PeerNode in project tezcRaft by tezc.

the class Cluster method handleRegisterCompleted.

/**
 * Register completed callback, initiated by clients to register themselves
 * @param id       client id
 * @param sequence client sequence
 */
public void handleRegisterCompleted(int id, long sequence) {
    PeerNode client = clients.get(id);
    if (client == null) {
        return;
    }
    client.setId(id);
    client.sendConnectResp(clusterRecord, id, sequence, true);
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 10 with PeerNode

use of tezc.core.node.PeerNode in project tezcRaft by tezc.

the class Cluster method start.

/**
 * Start cluster
 *
 * @throws RaftException on a missing configuration
 */
public void start() {
    electionTimer = new ElectionTimer(this, true, new Random().nextInt(150) + 500, worker.timestamp() + 500);
    heartBeatTimer = new HeartBeatTimer(this, true, 2000, worker.timestamp() + 150);
    startLog();
    if (!isStarted() && nodeId == -1) {
        worker.logInfo("Cannot start cluster ", clusterRecord);
        throw new RaftException("Local record is not set :" + clusterRecord);
    }
    local = clusterRecord.getRecord(nodeId);
    for (TransportRecord record : local.transports) {
        worker.listenAt(record);
    }
    for (TransportRecord record : local.secureTransports) {
        worker.listenAt(record);
    }
    for (NodeRecord record : clusterRecord.nodes) {
        if (record.id != local.id) {
            nodes.add(new PeerNode(worker, this, null, local, record, PeerNode.Type.PEER));
        }
    }
    startElectionTimer();
    active = true;
}
Also used : TransportRecord(tezc.core.record.TransportRecord) NodeRecord(tezc.core.record.NodeRecord) RaftException(tezc.base.exception.RaftException) PeerNode(tezc.core.node.PeerNode)

Aggregations

PeerNode (tezc.core.node.PeerNode)11 RegisterCommand (tezc.core.cluster.state.command.RegisterCommand)2 NodeRecord (tezc.core.record.NodeRecord)2 RaftException (tezc.base.exception.RaftException)1 Cluster (tezc.core.cluster.Cluster)1 Command (tezc.core.cluster.state.command.Command)1 ConfigCommand (tezc.core.cluster.state.command.ConfigCommand)1 NoOPCommand (tezc.core.cluster.state.command.NoOPCommand)1 TransportRecord (tezc.core.record.TransportRecord)1