每个Raft使用一个固定Timer,根据不同的状态执行相应操作。 【简化】不同状态下不管维护管理不同的Timer了。
()
| 519 | * 【简化】不同状态下不管维护管理不同的Timer了。 |
| 520 | */ |
| 521 | private void onTimer() throws Exception { |
| 522 | lock(); |
| 523 | try { |
| 524 | if (isShutdown) |
| 525 | return; |
| 526 | long now = System.currentTimeMillis(); |
| 527 | switch (getState()) { |
| 528 | case Follower: |
| 529 | if (now - logSequence.getLeaderActiveTime() > raftConfig.getElectionTimeout()) { |
| 530 | logger.warn("LeaderLostTimeout: {} > {}", now - logSequence.getLeaderActiveTime(), raftConfig.getElectionTimeout()); |
| 531 | convertStateTo(RaftState.Candidate); |
| 532 | } |
| 533 | break; |
| 534 | case Candidate: |
| 535 | if (now > nextVoteTime) |
| 536 | convertStateTo(RaftState.Candidate); // vote timeout. restart |
| 537 | break; |
| 538 | case Leader: |
| 539 | server.getConfig().forEachConnector(c -> { |
| 540 | var cex = (Server.ConnectorEx)c; |
| 541 | if (now - cex.getHeartbeatTime() > raftConfig.getLeaderHeartbeatTimer()) |
| 542 | logSequence.sendHeartbeatTo(cex); |
| 543 | }); |
| 544 | break; |
| 545 | } |
| 546 | if (++lowPrecisionTimer > 1000) { |
| 547 | lowPrecisionTimer = 0; |
| 548 | onLowPrecisionTimer(); |
| 549 | } |
| 550 | } finally { |
| 551 | unlock(); |
| 552 | //timerTask = Task.scheduleUnsafe(10, this::onTimer); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | private void onLowPrecisionTimer() throws ParseException, RocksDBException { |
| 557 | server.getConfig().forEachConnector(Connector::start); // Connector Reconnect Bug? |
nothing calls this directly
no test coverage detected