-
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: last_applied should be updated only when logs actually applied.
- Loading branch information
1 parent
8cb99fe
commit 89bb48f
Showing
3 changed files
with
82 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
|
||
use anyhow::Result; | ||
use maplit::hashset; | ||
|
||
use async_raft::{Config, State}; | ||
use fixtures::RaftRouter; | ||
|
||
mod fixtures; | ||
|
||
/// Cluster metrics_state_machine_consistency test. | ||
/// | ||
/// What does this test do? | ||
/// | ||
/// - brings 2 nodes online: one leader and one non-voter. | ||
/// - write one log to the leader. | ||
/// - asserts that when metrics.last_applied is upto date, the state machine should be upto date too. | ||
/// | ||
/// RUST_LOG=async_raft,memstore,metrics_state_machine_consistency=trace cargo test -p async-raft --test metrics_state_machine_consistency | ||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)] | ||
async fn metrics_state_machine_consistency() -> Result<()> { | ||
fixtures::init_tracing(); | ||
|
||
// Setup test dependencies. | ||
let config = Arc::new(Config::build("test".into()).validate().expect("failed to build Raft config")); | ||
let router = Arc::new(RaftRouter::new(config.clone())); | ||
|
||
router.new_raft_node(0).await; | ||
router.new_raft_node(1).await; | ||
|
||
tracing::info!("--- initializing single node cluster"); | ||
|
||
// Wait for node 0 to become leader. | ||
router.initialize_with(0, hashset![0]).await?; | ||
router | ||
.wait_for_metrics( | ||
&0u64, | ||
|x| x.state == State::Leader, | ||
Duration::from_micros(100), | ||
"n0.state -> Leader", | ||
) | ||
.await; | ||
|
||
tracing::info!("--- add one non-voter"); | ||
router.add_non_voter(0, 1).await?; | ||
|
||
tracing::info!("--- write one log"); | ||
router.client_request(0, "foo", 1).await; | ||
|
||
// Wait for metrics to be up to date. | ||
// Once last_applied updated, the key should be visible in state machine. | ||
tracing::info!("--- wait for log to sync"); | ||
let want = 2u64; | ||
for node_id in 0..2 { | ||
router | ||
.wait_for_metrics( | ||
&node_id, | ||
|x| x.last_applied == want, | ||
Duration::from_micros(100), | ||
&format!("n{}.last_applied -> {}", node_id, want), | ||
) | ||
.await; | ||
|
||
let sto = router.get_sto(&node_id).await; | ||
assert!(sto.get_state_machine().await.client_status.get("foo").is_some()); | ||
|
||
} | ||
|
||
Ok(()) | ||
} |