Search in sources :

Example 1 with PeerNode

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

the class Cluster method peerDisconnected.

/**
 * Peer is disconnected
 * @param peer     disconnected peer
 * @param takeOver is this a reconnect?
 */
public void peerDisconnected(PeerNode peer, boolean takeOver) {
    if (leader == peer) {
        leader = null;
    }
    activeNodes.remove(peer);
    nodes.remove(peer);
    if (!takeOver) {
        nodes.add(new PeerNode(worker, this, null, local, peer.getRemote(), PeerNode.Type.PEER));
    }
    if (role == Role.LEADER) {
        if (activeNodes.size() < (nodes.size() + 1) / 2) {
            setRole(Role.FOLLOWER);
        }
    }
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 2 with PeerNode

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

the class Cluster method handleCommandRequest.

/**
 * Command request callback, initiated by clients
 * @param req client request
 */
private void handleCommandRequest(CommandRequest req) {
    if (req.getCommit() == 1) {
        started = true;
        writeMeta();
    }
    PeerNode node = req.getNode();
    if (node == null) {
        return;
    }
    if (node != own) {
        node.sendClientResp(true, req.getSequence(), ByteBuffer.allocate(0));
    }
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 3 with PeerNode

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

the class Cluster method onHeartBeatTimeOut.

/**
 * Heartbeat timeout callback
 * This timer used by leader to make leader send heartbeat messages if
 * necessary to inform followers that it is up and operating as expected
 */
public void onHeartBeatTimeOut() {
    for (PeerNode peer : activeNodes) {
        if (peer.getOutTimestamp() < worker.timestamp() + 6000) {
            AppendReq req = new AppendReq(currentTerm, lastIndex(), lastLogTerm, commit);
            peer.sendAppendEntries(req);
        }
    }
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 4 with PeerNode

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

the class Cluster method flush.

/**
 * Flush cluster operations (pending logs, logs to replicated in cluster)
 */
public void flush() {
    if (role == Role.LEADER) {
        writePendings();
        for (PeerNode node : activeNodes) {
            long nextIndex = node.getNextIndex();
            if (nextIndex > lastIndex()) {
                continue;
            }
            EntryRecord prev = getEntry(nextIndex - 1);
            AppendReq req = new AppendReq(currentTerm, nextIndex - 1, prev.getTerm(), commit);
            req.setEntriesBuffer(getEntriesBuffer(nextIndex), getEntriesCount(nextIndex));
            node.setNextIndex(lastIndex() + 1);
            node.sendAppendEntries(req);
        }
        handleAppendRespMsg(own, new AppendResp(lastIndex(), currentTerm, true));
    }
    checkCompaction();
}
Also used : PeerNode(tezc.core.node.PeerNode)

Example 5 with PeerNode

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

the class Cluster method onElectionTimeout.

/**
 * Election timeout callback, in this case, we didn't get heartbeat
 * messages from leader so timeout occured, we must start our election to
 * become the leader
 */
public void onElectionTimeout() {
    if (leader != null) {
        if (leader.getInTimestamp() + 2000 > worker.timestamp()) {
            return;
        }
    }
    if (activeNodes.size() >= (nodes.size() + 1) / 2) {
        setRole(Role.CANDIDATE);
        votedFor = local.id;
        currentTerm++;
        writeMeta();
        handleRequestVoteRespMsg(own, new ReqVoteResp(currentTerm, true));
        grantedVotes.clear();
        for (PeerNode node : activeNodes) {
            node.sendRequestVote(currentTerm, local.id, lastIndex(), lastLogTerm);
        }
    }
}
Also used : 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