-
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.
Fix: restore replication progress when a leader starts up (#884)
As a leader, the replication progress to itself should be restored upon startup. And if this leader is the only node in a cluster, it should re-apply all of the logs to state machine at once. - Fix: #883
- Loading branch information
1 parent
4601faf
commit 63e69b9
Showing
9 changed files
with
120 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env mprocs --config | ||
|
||
|
||
# run local check in parallel with mprocs | ||
# | ||
# Usage: | ||
# mprocs --config ./scripts/check.yaml | ||
# | ||
# Install: | ||
# cargo install mprocs | ||
# | ||
# See: https://github.com/pvolok/mprocs | ||
|
||
|
||
procs: | ||
test-lib: | ||
cmd: ["cargo", "test", "--lib"] | ||
it: | ||
cmd: ["cargo", "test", "--test", "*"] | ||
clippy: | ||
cmd: ["cargo", "clippy", "--no-deps", "--all-targets", "--", "-D", "warnings"] | ||
|
||
# # keeps examples: | ||
# xx: | ||
# shell: "nodemon server.js" | ||
# webpack: "webpack serve" | ||
# tests: | ||
# shell: "jest -w" | ||
# env: | ||
# NODE_ENV: test |
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
60 changes: 60 additions & 0 deletions
60
tests/tests/life_cycle/t50_single_leader_restart_re_apply_logs.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,60 @@ | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use maplit::btreeset; | ||
use openraft::Config; | ||
use openraft::ServerState; | ||
|
||
use crate::fixtures::init_default_ut_tracing; | ||
use crate::fixtures::MemLogStore; | ||
use crate::fixtures::MemRaft; | ||
use crate::fixtures::MemStateMachine; | ||
use crate::fixtures::RaftRouter; | ||
|
||
/// A single leader should re-apply all logs upon startup, | ||
/// because itself is a quorum. | ||
#[async_entry::test(worker_threads = 8, init = "init_default_ut_tracing()", tracing_span = "debug")] | ||
async fn single_leader_restart_re_apply_logs() -> anyhow::Result<()> { | ||
let config = Arc::new( | ||
Config { | ||
enable_heartbeat: false, | ||
..Default::default() | ||
} | ||
.validate()?, | ||
); | ||
|
||
let mut router = RaftRouter::new(config.clone()); | ||
|
||
tracing::info!("--- bring up cluster of 1 node"); | ||
let mut log_index = router.new_cluster(btreeset! {0}, btreeset! {}).await?; | ||
|
||
tracing::info!(log_index, "--- write to 1 log"); | ||
{ | ||
log_index += router.client_request_many(0, "foo", 1).await?; | ||
} | ||
|
||
tracing::info!(log_index, "--- stop and restart node-0"); | ||
{ | ||
let (node, ls, sm): (MemRaft, MemLogStore, MemStateMachine) = router.remove_node(0).unwrap(); | ||
node.shutdown().await?; | ||
|
||
// Clear state machine, logs should be re-applied upon restart, because it is a leader. | ||
ls.storage().await.clear_state_machine().await; | ||
|
||
tracing::info!(log_index, "--- restart node-0"); | ||
|
||
router.new_raft_node_with_sto(0, ls, sm).await; | ||
router.wait(&0, timeout()).state(ServerState::Leader, "become leader upon restart").await?; | ||
} | ||
|
||
tracing::info!(log_index, "--- a single leader should re-apply all logs"); | ||
{ | ||
router.wait(&0, timeout()).log(Some(log_index), "node-0 works").await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn timeout() -> Option<Duration> { | ||
Some(Duration::from_millis(1_000)) | ||
} |