public float getRps() { long now = clock.now(); int v = value; float result = (v - lastValue) * 1000.0f / (now - lastTs); lastTs = now; lastValue = v; return result; } }
public float getRps() { long now = clock.now(); int v = value; float result = (v - lastValue) * 1000.0f / (now - lastTs); lastTs = now; lastValue = v; return result; } }
void sendRequestVote() throws Exception { if (clock.now() >= requestVoteTs) { // Clear the prevotes before sending prevote peers.forEach(p -> p.prevoteGranted = false); sendRequestVoteRequest(true); } }
public void reset() { lock.lock(); try { backfilling = false; backfillAhead = 0; backfillToIndex = 0; matchIndex = 0; nextIndex = -1; votedTerm = -1; votedFor = -1; outQueue.clear(); lastReceivedTs = clock.now(); prevoteGranted = false; backfillCond.signal(); } finally { lock.unlock(); } }
void sendHeartbeat(boolean force) { // Send heartbeat only if there were no sent messages within the period if (force || clock.now() >= lastSentTs + heartbeatPeriod) { sendHeartbeatRequest(sentRid); } }
/** * Sends a prevote if it's time to send one. */ void sendPrevote() throws GondolaException { if (clock.now() >= prevoteTs) { // Clear the prevotes before sending prevote peers.forEach((id, p) -> p.prevoteGranted = false); slaves.forEach(p -> p.prevoteGranted = false); sendRequestVoteRequest(true); } }
void sendHeartbeat(boolean force) { // Send heartbeat only if there were no sent messages within the period if (force || clock.now() >= lastSentTs + heartbeatPeriod) { sendHeartbeatRequest(sentRid); } }
long computeWaitTime() { long now = clock.now(); long t = 0; if (isLeader()) { t = heartbeatPeriod - (now - lastSentTs); } else if (isCandidate()) { t = Math.max(0, requestVoteTs - now); } else { t = Math.min(electionTimeoutTs - now, heartbeatPeriod); } return t; }
public void becomeCandidate() throws Exception { logger.info("[{}-{}] Becomes CANDIDATE {}", gondola.getHostId(), memberId, isPrimary ? "(primary)" : ""); become(Role.CANDIDATE, -1); // Set timeout requestVoteTs = clock.now() + (long) ((Math.random() * requestVotePeriod)); }
long computeWaitTime() { long now = clock.now(); long t = 0; if (isLeader()) { t = heartbeatPeriod - (now - lastSentTs); } else if (isCandidate()) { t = Math.max(0, prevoteTs - now); } else { t = Math.min(electionTimeoutTs - now, heartbeatPeriod); } return t; }
void checkHeartbeat() throws Exception { long late = clock.now() - electionTimeoutTs; if (late >= 0) { logger.info("[{}-{}] No heartbeat from {} in {}ms (timeout={}ms)", gondola.getHostId(), memberId, leaderId, electionTimeout + late, electionTimeout); becomeCandidate(); } }
void checkHeartbeat() throws GondolaException { long late = clock.now() - electionTimeoutTs; if (late >= 0) { logger.info("[{}-{}] No heartbeat from {} in {}ms (timeout={}ms)", gondola.getHostId(), memberId, leaderId, electionTimeout + late, electionTimeout); becomeCandidate(); } }
public void becomeCandidate() throws GondolaException { logger.info("[{}-{}] Becomes CANDIDATE{} {}", gondola.getHostId(), memberId, masterId >= 0 ? "-SLAVE" : "", isPrimary ? "(primary)" : ""); removeSlaves(); become(Role.CANDIDATE, -1); // Set time to send prevote prevoteTs = clock.now() + (long) ((Math.random() * prevotePeriod)); }
public void becomeFollower(int leaderId) throws Exception { logger.info("[{}-{}] Becomes FOLLOWER of {} {}", gondola.getHostId(), memberId, leaderId, isPrimary ? "(primary)" : ""); become(Role.FOLLOWER, leaderId); // To avoid the case where this member becomes a candidate and an RV is received for the current term if (votedFor == -1 && leaderId != -1) { storage.saveVote(memberId, currentTerm, leaderId); } // Set timeout electionTimeoutTs = clock.now() + electionTimeout; }
/** * @param message A non-null append entry request message. * @param prevLogIndex This value is identical to the prevLogIndex in message. */ public void sendAppendEntryRequest(Message message, int prevLogIndex) throws Exception { // Send command first assert message.getType() == Message.TYPE_APPEND_ENTRY_REQ; peers.forEach(p -> p.send(message, prevLogIndex)); // Then store command saveQueue.add(message); lastSentTs = clock.now(); }
public void becomeLeader() throws Exception { logger.info("[{}-{}] Becomes LEADER for term {} {}", gondola.getHostId(), memberId, currentTerm, isPrimary ? "(primary)" : ""); become(Role.LEADER, memberId); // Initialize raft variables for (Peer peer : peers) { int nextIndex = sentRid.index + 1; peer.setNextIndex(nextIndex, nextIndex); peer.lastReceivedTs = clock.now(); } // If command queue is empty, add a no-op command to commit entries from the previous term if (commandQueue.size() == 0 && sentRid.term > 0 && sentRid.term < currentTerm) { commandQueue.add(new CoreCmd(gondola, cluster, this)); } }
public void becomeLeader() throws GondolaException { logger.info("[{}-{}] Becomes LEADER for term {} {}", gondola.getHostId(), memberId, currentTerm, isPrimary ? "(primary)" : ""); become(Role.LEADER, memberId); // Initialize raft variables for (Peer peer : peers.values()) { int nextIndex = sentRid.index + 1; peer.setNextIndex(nextIndex, nextIndex); peer.lastReceivedTs = clock.now(); } for (Peer slave : slaves) { int nextIndex = sentRid.index + 1; slave.setNextIndex(nextIndex, nextIndex); slave.lastReceivedTs = clock.now(); } // If command queue is empty, add a no-op command to commit entries from the previous term if (commandQueue.size() == 0 && sentRid.term < currentTerm) { commandQueue.add(new CoreCmd(gondola, shard, this)); } }
/** * @param rid non-null object containing the leader's latest term and index. */ public void sendHeartbeatRequest(Rid rid) { Message message = pool.checkout(); try { message.heartbeat(memberId, currentTerm, rid, commitIndex); peers.forEach(p -> p.send(message, rid.index - 1)); } finally { message.release(); } lastSentTs = clock.now(); }
public void becomeFollower(int leaderId) throws GondolaException { logger.info("[{}-{}] Becomes FOLLOWER{} of {} {}", gondola.getHostId(), memberId, masterId >= 0 ? "-SLAVE" : "", leaderId, isPrimary ? "(primary)" : ""); removeSlaves(); become(Role.FOLLOWER, leaderId); // To avoid the case where this member becomes a candidate and an RV is received for the current term if (votedFor == -1 && leaderId != -1) { storage.saveVote(memberId, currentTerm, leaderId); } // Set timeout electionTimeoutTs = clock.now() + electionTimeout; }
/** * Sent after follower has advanced the savedIndex. */ public void sendAppendEntryReply() throws Exception { Peer leader = peerMap.get(leaderId); if (leader != null) { Message message = pool.checkout(); try { saveQueue.getLatest(savedRid, false); message.appendEntryReply(memberId, currentTerm, savedRid.index, true, false); leader.send(message); latency.tail(savedRid.index); } finally { message.release(); } lastSentTs = clock.now(); } }