forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: fix handling of non-voter leader
Openraft does not require Leader/Candidate to be a voter. Before this commit, Openraft's handling of non-voter leader is inconsistent: For a node that has a vote proposed by this node and is committed, and is not a voter: - [`calc_server_state()`](https://github.com/datafuselabs/openraft/blob/97254a31c673e52429ee7187de96fd2ea2a5ce98/openraft/src/raft_state/mod.rs#L323) returns `Learner`. - But [`is_leader()`](https://github.com/datafuselabs/openraft/blob/97254a31c673e52429ee7187de96fd2ea2a5ce98/openraft/src/raft_state/mod.rs#L353) returns true. Since this commit, `calc_server_state()` and `is_leader()` returns consistent result according to the following table: For node-2: - Node-2 with vote `(term=1, node_id=2, committed=true)`: - is a leader if it is **present** in config, either a voter or non-voter. - is a learner if it is **absent** in config. - Node-2 with vote `(term=1, node_id=2, committed=false)`: - is a candidate if it is **present** in config, either a voter or non-voter. - is a learner if it is **absent** in config. - Node-3 with vote `(term=1, node_id=99, committed=false|true)`: - is a follower if it is a **voter** in config, - is a learner if it is a **non-voter** or **absent** in config. | vote \ membership | Voter | Non-voter | Absent | |---------------------------------------|-----------|-----------|---------| | (term=1, node_id=2, committed=true) | leader | leader | learner | | (term=1, node_id=2, committed=false) | candidate | candidate | learner | | (term=1, node_id=99, committed=true) | follower | learner | learner | | (term=1, node_id=99, committed=false) | follower | learner | learner | - Fix: databendlabs#920
- Loading branch information
1 parent
cf4ffe7
commit f4564fe
Showing
13 changed files
with
199 additions
and
33 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
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
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
41 changes: 41 additions & 0 deletions
41
tests/tests/life_cycle/t90_issue_920_non_voter_leader_restart.rs
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,41 @@ | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use openraft::storage::RaftLogStorage; | ||
use openraft::Config; | ||
use openraft::ServerState; | ||
use openraft::Vote; | ||
|
||
use crate::fixtures::init_default_ut_tracing; | ||
use crate::fixtures::RaftRouter; | ||
|
||
/// Special case: A leader that is not a member(neither a voter or non-voter) should be started too, | ||
/// as a learner. | ||
#[async_entry::test(worker_threads = 8, init = "init_default_ut_tracing()", tracing_span = "debug")] | ||
async fn issue_920_non_member_leader_restart() -> anyhow::Result<()> { | ||
let config = Arc::new( | ||
Config { | ||
enable_heartbeat: false, | ||
..Default::default() | ||
} | ||
.validate()?, | ||
); | ||
|
||
let mut router = RaftRouter::new(config.clone()); | ||
|
||
let (mut log_store, sm) = router.new_store(); | ||
// Set committed vote that believes node 0 is the leader. | ||
log_store.save_vote(&Vote::new_committed(1, 0)).await?; | ||
router.new_raft_node_with_sto(0, log_store, sm).await; | ||
|
||
router | ||
.wait(&0, timeout()) | ||
.state(ServerState::Learner, "node 0 becomes learner when startup") | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn timeout() -> Option<Duration> { | ||
Some(Duration::from_millis(1000)) | ||
} |
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