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