-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: use blank log for heartbeat (#483)
* Feature: use blank log for heartbeat Heartbeat in standard raft is the way for a leader to assert it is still alive. - A leader send heartbeat at a regular interval. - A follower that receives a heartbeat believes there is an active leader thus it rejects election request(`send_vote`) from another node unreachable to the leader, for a short period. Openraft heartbeat is a blank log Such a heartbeat mechanism depends on clock time. But raft as a distributed consensus already has its own **pseudo time** defined very well. The **pseudo time** in openraft is a tuple `(vote, last_log_id)`, compared in dictionary order. Why it works To refuse the election by a node that does not receive recent messages from the current leader, just let the active leader send a **blank log** to increase the **pseudo time** on a quorum. Because the leader must have the greatest **pseudo time**, thus by comparing the **pseudo time**, a follower automatically refuse election request from a node unreachable to the leader. And comparing the **pseudo time** is already done by `handle_vote_request()`, there is no need to add another timer for the active leader. Other changes: - Feature: add API to switch timeout based events: - `Raft::enable_tick()`: switch on/off election and heartbeat. - `Raft::enable_heartbeat()`: switch on/off heartbeat. - `Raft::enable_elect()`: switch on/off election. These methods make some testing codes easier to write. The corresponding `Config` entries are also added: `Config::enable_tick` `Config::enable_heartbeat` `Config::enable_elect` - Refactor: remove Engine `Command::RejectElection`. Rejecting election now is part of `handle_vote_req()` as blank-log heartbeat is introduced. - Refactor: heartbeat is removed from `ReplicationCore`. Instead, heartbeat is emitted by `RaftCore`. - Fix: when failed to sending append-entries, do not clear `need_to_replicate` flag. - CI: add test with higher network delay. - Doc: explain why using blank log as heartbeat. - Fix: #151
- Loading branch information
1 parent
a37a450
commit 956177d
Showing
63 changed files
with
603 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Heartbeat in openraft | ||
|
||
## Heartbeat in standard raft | ||
|
||
Heartbeat in standard raft is the way for a leader to assert it is still alive: | ||
- A leader send heartbeat at a regular interval. | ||
- A follower that receives a heartbeat believes there is an active leader thus it rejects election request(`send_vote`) from another node unreachable to the leader, for a short period. | ||
|
||
## Openraft heartbeat is a blank log | ||
|
||
Such a heartbeat mechanism depends on clock time. | ||
But raft as a distributed consensus already has its own **pseudo time** defined very well. | ||
The **pseudo time** in openraft is a tuple `(vote, last_log_id)`, compared in dictionary order. | ||
|
||
### Why it works | ||
|
||
To refuse the election by a node that does not receive recent messages from the current leader, | ||
just let the active leader send a **blank log** to increase the **pseudo time** on a quorum. | ||
|
||
Because the leader must have the greatest **pseudo time**, | ||
thus by comparing the **pseudo time**, a follower automatically refuse election request from a node unreachable to the leader. | ||
|
||
And comparing the **pseudo time** is already done by `handle_vote_request()`, | ||
there is no need to add another timer for the active leader. | ||
|
||
Thus making heartbeat request a blank log is the simplest way. | ||
|
||
## Why blank log heartbeat? | ||
|
||
- Simple, get rid of a timer. | ||
- Easy to prove, and reduce code complexity. | ||
|
||
## Other Concerns | ||
|
||
- More raft logs are generated. | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.