diff --git a/crates/voelin-store/src/store.rs b/crates/voelin-store/src/store.rs index 8553c92..a79b3c6 100644 --- a/crates/voelin-store/src/store.rs +++ b/crates/voelin-store/src/store.rs @@ -652,23 +652,43 @@ mod tests { reader.query_row("SELECT count(*) FROM identities", [], |r| r.get::<_, i64>(0)).unwrap(); store.add_identity("second", &Identity::create(), IdentityOrigin::User).unwrap(); - // A value that is not the default, so that restoring a constant would show. + // A value that is not the default, so that restoring a constant would + // show, and one so long that a checkpoint that waited as long as the + // connection allows would not come back by itself. + const HOUR_MS: i64 = 3_600_000; let timeout = |store: &Store| -> i64 { store.db.pragma_query_value(None, "busy_timeout", |r| r.get(0)).unwrap() }; - store.db.busy_timeout(Duration::from_millis(2345)).unwrap(); - let started = std::time::Instant::now(); - store.truncate_wal(); - let elapsed = started.elapsed(); - assert!(elapsed < Duration::from_secs(1), "the checkpoint waited {elapsed:?}"); + store.db.busy_timeout(Duration::from_secs(3600)).unwrap(); + + // "Briefly" is not a time: a checkpoint ends in two fsyncs, which take + // seconds under disk pressure (it took 6 s here beside `dd conv=fsync` + // loops), and only a checkpoint that comes back while the reader still + // holds its snapshot has not waited for it. So the reader lets go from + // another thread after 30 s, which ends a checkpoint that does not give + // up with a failure here and not with an hour's wait. + let (back, hear) = std::sync::mpsc::channel::<()>(); + let watchdog = std::thread::spawn(move || { + let in_time = hear.recv_timeout(Duration::from_secs(30)).is_ok(); + if !in_time { + reader.execute_batch("COMMIT").unwrap(); + } + (reader, in_time) + }); + // One attempt, which the reader blocks (`1`). + let blocked = store.checkpoint_truncate(); + back.send(()).ok(); + let (reader, in_time) = watchdog.join().unwrap(); + assert!(in_time, "the checkpoint waited for the reader instead of giving up"); + assert_eq!(blocked.unwrap(), 1, "the reader should block the reset"); assert!(std::fs::metadata(&wal).unwrap().len() > 0, "the reader should block the reset"); - assert_eq!(timeout(&store), 2345); + assert_eq!(timeout(&store), HOUR_MS); // Without the reader the same call resets the log. reader.execute_batch("COMMIT").unwrap(); store.truncate_wal(); assert_eq!(std::fs::metadata(&wal).unwrap().len(), 0); - assert_eq!(timeout(&store), 2345); + assert_eq!(timeout(&store), HOUR_MS); drop(reader); drop(store); std::fs::remove_dir_all(dir).unwrap(); diff --git a/crates/voelin-store/tests/keystore.rs b/crates/voelin-store/tests/keystore.rs index 8d93646..6182183 100644 --- a/crates/voelin-store/tests/keystore.rs +++ b/crates/voelin-store/tests/keystore.rs @@ -1714,13 +1714,23 @@ fn removing_through_the_key_store_leaves_no_key_behind_either() { } #[test] -fn a_reader_that_blocks_the_checkpoint_costs_milliseconds_and_starves_no_writer() { +fn a_reader_that_blocks_the_checkpoint_costs_one_checkpoint_and_starves_no_writer() { // A connection that keeps a read snapshot (a long query of the UI, a backup) // keeps the checkpoint that truncates the log from finishing. The sign-out - // still deletes and warns that the bytes may stay in the log, and it waits - // for the checkpoint a moment, not for the busy timeout of five seconds once - // per removed row. While the checkpoint waits it holds the writer lock of the - // log, so a second connection that writes meanwhile must get through. + // still deletes and warns that the bytes may stay in the log, and it makes + // one checkpoint for all its removals, not one per removed row. While the + // checkpoint waits it holds the writer lock of the log, so a second + // connection that writes meanwhile must get through. + // + // No clock decides anything here. A checkpoint ends in two fsyncs, and under + // disk pressure (a build or a copy writing beside the tests) they take + // seconds: the review saw sign-outs of 1.2 to 1.9 s at 10 to 30 % I/O + // pressure, and `dd conv=fsync` loops beside 32 busy loops made them 5 s. A + // bound on the sign-out or a short busy timeout on the writer failed this + // test with "database is locked" for a checkpoint that was only slow. That a + // checkpoint gives up on a reader after a moment and does not wait out the + // connection's busy timeout is pinned where that timeout can be set to an + // hour: `store::tests::a_blocked_checkpoint_waits_briefly_and_leaves_the_busy_timeout_as_it_was`. let scratch = Scratch::new("blocked-checkpoint"); let store = SqliteKeyStore::open(&scratch.db()).unwrap(); store.put_voelin_key(&key()).unwrap(); @@ -1738,9 +1748,12 @@ fn a_reader_that_blocks_the_checkpoint_costs_milliseconds_and_starves_no_writer( let stop = AtomicBool::new(false); let (done, elapsed, writes, errors, lines) = std::thread::scope(|scope| { let writer = scope.spawn(|| { - // Its own connection, which gives up on a lock after one second. + // Its own connection, set up as the app's are (`Store::open`: the log + // and `synchronous = NORMAL`, so that a commit does not wait for the + // disk), with a busy timeout that a slow checkpoint does not reach. let db = Connection::open(scratch.db()).unwrap(); - db.busy_timeout(Duration::from_secs(1)).unwrap(); + db.pragma_update(None, "synchronous", "NORMAL").unwrap(); + db.busy_timeout(Duration::from_secs(60)).unwrap(); let sql = "INSERT OR REPLACE INTO settings (key, value) VALUES ('probe', ?1)"; let (mut writes, mut errors) = (0, Vec::new()); while !stop.load(Ordering::Relaxed) { @@ -1767,9 +1780,9 @@ fn a_reader_that_blocks_the_checkpoint_costs_milliseconds_and_starves_no_writer( assert!(store.list().unwrap().is_empty()); assert!(writes > 0, "the writer never ran"); assert!(errors.is_empty(), "a concurrent writer failed: {errors:?}"); - assert!(elapsed < Duration::from_secs(1), "the sign-out took {elapsed:?}"); // One checkpoint for the four removals: each one that is blocked says so. - assert_eq!(support::blocked_checkpoints(&lines), 1, "{lines:?}"); + let blocked = support::blocked_checkpoints(&lines); + assert_eq!(blocked, 1, "{lines:?} (the sign-out took {elapsed:?})"); // Nothing is lost by the wait: once the reader is gone, the next removal // truncates the log.