-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: if the application does not persist snapshot, build a snapshot w…
…hen starting up
- Loading branch information
1 parent
bbfe4f4
commit 04e4060
Showing
5 changed files
with
76 additions
and
5 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,55 @@ | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use maplit::btreeset; | ||
use openraft::storage::RaftLogStorage; | ||
use openraft::Config; | ||
use openraft::SnapshotPolicy; | ||
|
||
use crate::fixtures::init_default_ut_tracing; | ||
use crate::fixtures::log_id; | ||
use crate::fixtures::RaftRouter; | ||
|
||
/// When startup, if there is no snapshot and there are logs purged, it should build a snapshot at | ||
/// once. | ||
#[async_entry::test(worker_threads = 8, init = "init_default_ut_tracing()", tracing_span = "debug")] | ||
async fn startup_build_snapshot() -> anyhow::Result<()> { | ||
let snapshot_threshold = 10; | ||
|
||
let config = Arc::new( | ||
Config { | ||
enable_heartbeat: false, | ||
snapshot_policy: SnapshotPolicy::LogsSinceLast(snapshot_threshold), | ||
max_in_snapshot_log_to_keep: 0, | ||
..Default::default() | ||
} | ||
.validate()?, | ||
); | ||
|
||
let mut router = RaftRouter::new(config.clone()); | ||
|
||
tracing::info!("--- initializing cluster"); | ||
let mut log_index = router.new_cluster(btreeset! {0}, btreeset! {}).await?; | ||
|
||
tracing::info!("--- send client requests"); | ||
{ | ||
router.client_request_many(0, "0", (snapshot_threshold - 1 - log_index) as usize).await?; | ||
log_index = snapshot_threshold - 1; | ||
} | ||
|
||
tracing::info!("--- shut down and purge to log index: {}", 5); | ||
let (_, mut log_store, sm) = router.remove_node(0).unwrap(); | ||
log_store.purge(log_id(1, 0, 5)).await?; | ||
|
||
tracing::info!("--- restart, expect snapshot at index: {} for node-1", log_index); | ||
{ | ||
router.new_raft_node_with_sto(0, log_store, sm).await; | ||
router.wait(&0, timeout()).snapshot(log_id(1, 0, log_index), "node-1 snapshot").await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn timeout() -> Option<Duration> { | ||
Some(Duration::from_millis(1_000)) | ||
} |