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);
}
}
}
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));
}
}
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);
}
}
}
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();
}
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);
}
}
}
Aggregations