forked from databendlabs/openraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Prevent panic when calling
Raft::change_membership()
on uninit…
…ialized node Previously, `change_membership()` assumed the current membership config was always non-empty and used the last config entry. However, uninitialized nodes lack a membership config, leading to panics. This commit adds checks to prevent `change_membership()` from panicking
- Loading branch information
1 parent
0063194
commit bcc9ac2
Showing
4 changed files
with
45 additions
and
2 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
37 changes: 37 additions & 0 deletions
37
tests/tests/membership/t52_change_membership_on_uninitialized_node.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,37 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use maplit::btreemap; | ||
use openraft::ChangeMembers; | ||
use openraft::Config; | ||
|
||
use crate::fixtures::ut_harness; | ||
use crate::fixtures::RaftRouter; | ||
|
||
/// Call `Raft::change_membership()` on an uninitialized node should not panic due to empty | ||
/// membership. | ||
#[tracing::instrument] | ||
#[test_harness::test(harness = ut_harness)] | ||
async fn change_membership_on_uninitialized_node() -> Result<()> { | ||
let config = Arc::new( | ||
Config { | ||
enable_heartbeat: false, | ||
..Default::default() | ||
} | ||
.validate()?, | ||
); | ||
|
||
let mut router = RaftRouter::new(config.clone()); | ||
router.new_raft_node(0).await; | ||
|
||
let n0 = router.get_raft_handle(&0)?; | ||
let res = n0.change_membership(ChangeMembers::AddVoters(btreemap! {0=>()}), false).await; | ||
tracing::info!("{:?}", res); | ||
|
||
let err = res.unwrap_err(); | ||
tracing::info!("{}", err); | ||
|
||
assert!(err.to_string().contains("forward request to")); | ||
|
||
Ok(()) | ||
} |