心跳和选举主要依靠容器启动时的两个定时任务,分别为 HeartBeat 和 MasterElection 两个。这两个定时任务,每 0.5s 执行一次。
当一段时间内(15~20s)没有接受到心跳,就会执行 MasterElection 里面的选举请求逻辑。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| GlobalExecutor.registerMasterElection(new MasterElection());
GlobalExecutor.registerHeartbeat(new HeartBeat());
public class MasterElection implements Runnable { @Override public void run() { try {
if (!peers.isReady()) { return; }
RaftPeer local = peers.local(); local.leaderDueMs -= GlobalExecutor.TICK_PERIOD_MS;
if (local.leaderDueMs > 0) { return; }
local.resetLeaderDue(); local.resetHeartbeatDue();
sendVote(); } catch (Exception e) { Loggers.RAFT.warn("[RAFT] error while master election {}", e); }
} }
public class HeartBeat implements Runnable { @Override public void run() { try {
if (!peers.isReady()) { return; }
RaftPeer local = peers.local(); local.heartbeatDueMs -= GlobalExecutor.TICK_PERIOD_MS; if (local.heartbeatDueMs > 0) { return; } local.resetHeartbeatDue(); sendBeat(); } catch (Exception e) { Loggers.RAFT.warn("[RAFT] error while sending beat {}", e); }
} }
|
参考了更加细节的文章:
文章一:心跳的源码细节:https://www.jianshu.com/p/b0cdaa64688e
文章二:选举的源码细节:https://www.jianshu.com/p/5a2d965174ae
代码地址:https://github.com/LiWenGu/nacos.git
v1.5.2