voelin-store: the writer of the blocked-checkpoint test paces itself by its own commits

The first version of this test (2cbb816) still failed under heavy load, with
the sign-out itself returning DatabaseBusy. The cause is the writer's fixed
pause of 5 ms: when disk pressure makes each of its commits take 100 ms and
more, it holds the writer lock nearly all the time, and the sign-out, whose
busy timeout is the default five seconds, never gets it between two commits.
(The commit message of 2cbb816 blamed the writer's synchronous = FULL; the
setting is right to mirror the app, but it was not the cause.)

Reproduced without load by delaying every pwrite64 of the process by 700 ms
(strace -f -e inject=pwrite64:delay_enter=700ms): the test of 2cbb816 fails
with DatabaseBusy from the sign-out in two runs of two, this version passes in
two of two. The lib test of 2cbb816 passes with the same delay and with fsync
delayed by 3 s as well.

Now the writer pauses ten times as long as its last write took (5 ms at least),
so its share of the lock stays under a tenth whatever the disk does, and the
sign-out starts only after the writer has written once (a thread of a busy
machine can start after the whole sign-out is over, which made "the writer
never ran" a failure of the machine). A drop guard ends the writer if an
assertion of the test fails first.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-25 10:54:10 +03:00
1 parent 8c891a057e
commit f183b22b63
1 file changed
+36 -9
+36 -9
View File
@@ -1745,8 +1745,18 @@ fn a_reader_that_blocks_the_checkpoint_costs_one_checkpoint_and_starves_no_write
let rows: i64 = reader.query_row("SELECT count(*) FROM identities", [], |r| r.get(0)).unwrap();
assert_eq!(rows, 4);
/// Stops the writer when dropped, so that a failed assertion of the test does
/// not leave the scope waiting for it.
struct StopsTheWriter<'a>(&'a AtomicBool);
impl Drop for StopsTheWriter<'_> {
fn drop(&mut self) {
self.0.store(true, Ordering::Relaxed);
}
}
let stop = AtomicBool::new(false);
let (done, elapsed, writes, errors, lines) = std::thread::scope(|scope| {
let written = AtomicUsize::new(0);
let (done, elapsed, errors, lines) = std::thread::scope(|scope| {
let _stops = StopsTheWriter(&stop);
let writer = scope.spawn(|| {
// 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
@@ -1755,30 +1765,47 @@ fn a_reader_that_blocks_the_checkpoint_costs_one_checkpoint_and_starves_no_write
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());
let mut errors = Vec::new();
while !stop.load(Ordering::Relaxed) {
match db.execute(sql, [writes.to_string()]) {
Ok(_) => writes += 1,
let began = Instant::now();
match db.execute(sql, [written.load(Ordering::Relaxed).to_string()]) {
Ok(_) => {
written.fetch_add(1, Ordering::Relaxed);
}
Err(error) => errors.push(error.to_string()),
}
std::thread::sleep(Duration::from_millis(5));
// It pauses ten times as long as the write took, and 5 ms at least.
// Under disk pressure a commit takes 100 ms and more, and a writer
// that paused 5 ms after each would hold the writer lock nearly all
// the time and keep the sign-out, whose busy timeout is the default
// five seconds, from getting it (`DatabaseBusy`).
let wake = Instant::now() + (began.elapsed() * 10).max(Duration::from_millis(5));
while Instant::now() < wake && !stop.load(Ordering::Relaxed) {
std::thread::sleep(Duration::from_millis(1));
}
}
(writes, errors)
errors
});
// The sign-out starts when the writer is up and has written, not when
// the thread was spawned: a thread of a busy machine can start late.
let deadline = Instant::now() + Duration::from_secs(60);
while written.load(Ordering::Relaxed) == 0 {
assert!(Instant::now() < deadline, "the writer never ran");
std::thread::sleep(Duration::from_millis(1));
}
let started = Instant::now();
let (done, lines) = support::at_trace(|| {
store.sign_out(&binding().account, DeviceKeyChoice::Forget(key().uid()))
});
let elapsed = started.elapsed();
stop.store(true, Ordering::Relaxed);
let (writes, errors) = writer.join().unwrap();
(done.unwrap(), elapsed, writes, errors, lines)
let errors = writer.join().unwrap();
(done.unwrap(), elapsed, errors, lines)
});
// Four removals: the two copies, the device key the user forgot, the record.
assert_eq!(done.removed.len(), 4, "{done:?}");
assert!(store.list().unwrap().is_empty());
assert!(writes > 0, "the writer never ran");
assert!(errors.is_empty(), "a concurrent writer failed: {errors:?}");
// One checkpoint for the four removals: each one that is blocked says so.
let blocked = support::blocked_checkpoints(&lines);