Repository navigation
test(datastore): Close keeps every write acknowledged to racing writers #840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimeToBuildBob
wants to merge
2
commits into
ActivityWatch:master
Choose a base branch
from
TimeToBuildBob:test/datastore-close-durability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+93
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,93 @@ | ||
| //! `Datastore::close` racing active writers on the shared worker. | ||
| //! | ||
| //! aw-tauri shuts down by closing the server's datastore while tray and API | ||
| //! clones may still be writing. Every write the worker acknowledged before | ||
| //! Close must be durable afterwards, and Close must not be starved by them. | ||
| //! Close does not drain in-flight HTTP handlers; this covers worker requests. | ||
|
|
||
| use aw_datastore::Datastore; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::sync::{mpsc, Arc, Barrier}; | ||
| use std::thread; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| const WRITERS: usize = 4; | ||
| const ROUNDS: usize = 3; | ||
|
|
||
| #[test] | ||
| fn close_keeps_every_acknowledged_write_from_racing_writers() { | ||
| for round in 0..ROUNDS { | ||
| let dir = tempfile::tempdir().unwrap(); | ||
| let path = dir.path().join(format!("close-{round}.db")); | ||
| let path = path.to_str().unwrap().to_owned(); | ||
| let ds = Datastore::new(path.clone(), false); | ||
| ds.set_key_value_and_commit("initial", "ready").unwrap(); | ||
|
|
||
| let barrier = Arc::new(Barrier::new(WRITERS + 1)); | ||
| let closing = Arc::new(AtomicBool::new(false)); | ||
| let (writer_done_tx, writer_done_rx) = mpsc::channel(); | ||
| let writers: Vec<_> = (0..WRITERS) | ||
| .map(|id| { | ||
| let worker = ds.clone(); | ||
| let barrier = barrier.clone(); | ||
| let closing = closing.clone(); | ||
| let writer_done_tx = writer_done_tx.clone(); | ||
| thread::spawn(move || { | ||
| let key = format!("writer-{id}"); | ||
| // Each writer holds an acknowledged value before Close races it. | ||
| worker.set_key_value(&key, "0").unwrap(); | ||
| let mut last_ok = 0; | ||
| barrier.wait(); | ||
| for n in 1..10_000 { | ||
| match worker.set_key_value(&key, &n.to_string()) { | ||
| Ok(()) => last_ok = n, | ||
| Err(_) => { | ||
| assert!( | ||
| closing.load(Ordering::SeqCst), | ||
| "write failed before Close" | ||
| ); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| writer_done_tx.send(()).unwrap(); | ||
| (key, last_ok) | ||
| }) | ||
| }) | ||
| .collect(); | ||
|
|
||
| barrier.wait(); | ||
| closing.store(true, Ordering::SeqCst); | ||
| let closer = ds.clone(); | ||
| let (done_tx, done_rx) = mpsc::channel(); | ||
| thread::spawn(move || { | ||
| closer.close(); | ||
| done_tx.send(()).unwrap(); | ||
| }); | ||
| done_rx | ||
| .recv_timeout(Duration::from_secs(5)) | ||
| .expect("active writers starved Close"); | ||
|
|
||
| drop(writer_done_tx); | ||
| let deadline = Instant::now() + Duration::from_secs(5); | ||
| for _ in 0..WRITERS { | ||
| writer_done_rx | ||
| .recv_timeout(deadline.saturating_duration_since(Instant::now())) | ||
| .expect("writer did not finish after Close"); | ||
| } | ||
| let acknowledged: Vec<_> = writers.into_iter().map(|w| w.join().unwrap()).collect(); | ||
| let reader = Datastore::open_reader(path).unwrap(); | ||
| for (key, value) in acknowledged { | ||
| assert_eq!( | ||
| reader.get_key_value(&key).unwrap(), | ||
| value.to_string(), | ||
| "round {round}: acknowledged write to {key} lost by Close" | ||
| ); | ||
| } | ||
| reader.close(); | ||
| assert!( | ||
| ds.get_buckets().is_err(), | ||
| "closed worker still served a request" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.