Repository navigation
test(storage-postgres): accept an empty read only when the numbering finished after the read gave up - #1534
Conversation
…ound A GetRecords read on PostgreSQL numbers its shard first. When another transaction numbers the shard, the read waits for it, for 1 s at most, and then reads the records numbered so far. a_read_during_a_numbering_succeeds_under_repeatable_read opens the gate 200 ms after the first numbering reaches it and expected the read to return that numbering's record. When the server took longer than the rest of the second to commit the numbering, the read returned no record, and the test failed with left: [] right: ["held"]. With the PostgreSQL container paused for 1.2 s every 0.7 s, the test failed in 5 of 30 runs on main, each time at that assertion. The test now accepts an empty read only when the read waited at least 980 ms, the 1 s bound less the longest pause between looks, and then checks that the next read returns the record. A read that gives up early still fails the test. Refs #1508
… outlasted its wait The previous commit let a_read_during_a_numbering_succeeds_under_repeatable_read accept a read that returned no record once it had waited 980 ms. That also accepted a read that kept waiting and returned no record although the numbering had committed long before its wait ran out. The test now records when the numbering finishes. An empty read passes only when the read waited at least 984 ms and the numbering finished at least 984 ms after the read started, so after the read's wait ran out. 984 ms is NUMBER_WAIT (1 s) less NUMBER_PAUSE (16 ms), the least time number_before_read waits before it gives up, and the test names both. The follow-up read's result is no longer shadowed. The CI failure in #1508 is consistent with the numbering committing more than about 1 s after the read started. That cause is inferred: the test failed the same way only when the PostgreSQL container was paused, and the CI log does not show how long the read waited. Refs #1508
Finished means the client saw the commit acknowledged, and a commit in the engine's last pause also counts as late. Both make the test more lenient.
LeeroyHannigan
left a comment
There was a problem hiding this comment.
Thanks. Both bounds hold for a correct engine without a margin. A read that gives up has waited more than 984 ms of the engine's clock, and that clock starts after the test's started. The test takes numbered_at when PostgreSQL acknowledges the numbering's commit. By the code, the three mutations in the description fail the new test, as they failed the old one:
- a read that does not wait on a busy shard
- a
NUMBER_WAITbelow the 200 ms hold - a snapshot taken before the numbering
I ran cargo test -p extenddb-storage-postgres --test stream_commit_order five times at 3edd261 against pgvector/pgvector:pg16, and 12 passed each time. A run at the merge base also passed 12. CI passed, and the branch merges with main. No other PR changes this test or claims #1508.
One note on the description, not a condition. After the change the file has 1361 lines and grows by 44, where the Why section says 1356 and 39.
What
a_read_during_a_numbering_succeeds_under_repeatable_readincrates/storage-postgres/tests/stream_commit_order.rsnow accepts a read that returned no record in one case: the read waited at least 984 ms, the numbering it waited for finished at least 984 ms after the read started, and the next read returns the record. A read that returns the record is checked as before: it returns["held"]and waited at least 150 ms. No engine code changes.Refs #1508
Why
A GetRecords read on PostgreSQL numbers its shard before it reads it (
number_before_readincrates/storage-postgres/src/stream_engine.rs). When another transaction numbers the shard, the read looks again after pauses of 1 ms doubling toNUMBER_PAUSE(16 ms), and gives up once its next pause would passNUMBER_WAIT(1 s) from the start of its wait. It then reads the records numbered so far. A read that gives up has waited more than 984 ms,NUMBER_WAITlessNUMBER_PAUSE. A comment beside the test's 984 ms bound names both constants (the test cannot import them, since they are private to the crate); the test keeps no further margin, since its clock starts before the engine's. A change to either constant needs the same change in the test.The test holds the first numbering at its commit and opens the gate 200 ms after the numbering reaches it. It expected the read to return that numbering's record, which holds only when the numbering finishes within the read's wait. The CI failure,
left: [] right: ["held"], is consistent with the numbering finishing after the read gave up. That cause is inferred, not confirmed: the CI log does not show how long the read waited, and the test failed the same way here only when the PostgreSQL container was paused. The PostgreSQL log of the failing job has no ERROR line between 13:42:42 and 13:42:46, which suggests that the read's numbering did not fail on the server; a failure on the client side would not appear there. Separately, the same log shows a checkpoint from 13:42:43.160 to 13:42:45.831 while the test ran. That was observed, not shown to be related.An empty read is accepted only when the numbering finished after the read's wait ran out, because the read reads the shard after that wait. A read that ignores a numbering that committed during its wait still fails the test. Two narrow gaps remain, both noted in the test's doc comment: "finished" is when the client saw the commit acknowledged, so a stall between the server's commit and its acknowledgment can hide such a read; and a commit in the last pause before the engine gives up (about 16 ms) also counts as late.
The first commit's message (af4d378) states the cause as fact and describes an earlier rule (any empty read after 980 ms). Please build the squash message from this description instead.
crates/storage-postgres/tests/stream_commit_order.rswas already 1317 lines and grows by 39, to 1356. #368 also changescrates/storage-postgres/src/stream_engine.rs; it does not changeNUMBER_WAITorNUMBER_PAUSEtoday, but if it does before merge the bound here needs the same change.Testing done
Against
pgvector/pgvector:pg16, release build, as CI runs it:assert_eq!(values(&records), ["held"])withleft: []. On this branch it passed in 30 of 30 runs, and in 80 of 80 runs with a print in the empty branch. In 46 of those 80 the read returned no record; each had waited 1.331 s to 1.428 s, and the numbering had finished 1.331 s to 1.428 s after the read started.stream_commit_order.rspassed in 20 of 20 runs of the file.NUMBER_WAIT, and reads its page in that snapshot: the test failed in 5 of 5 runs with "the read returned no record, though the numbering finished 202.4 ms to 203.2 ms after the read started, before the read's wait of 984ms ran out". The same mutation with the first version of this change, which accepted any empty read after 980 ms, passed in 5 of 5.NUMBER_WAITset to 100 ms: the test failed in 5 of 5 runs, the read returning no record after 88.2 ms to 102.8 ms. With the wait bound in the test turned off it still failed in 5 of 5 on the numbering bound; with the numbering bound turned off it failed in 5 of 5 on the wait bound; with both turned off it passed in 5 of 5.cargo +1.97.0 fmt --all --checkandcargo +1.97.0 clippy -p extenddb-storage-postgres --all-targets -- -D warnings.Open question
With
number_shard_batchopening its transaction at the database default instead of READ COMMITTED, this test and the other 11 tests in the file still pass (10 of 10 runs of this test, 3 of 3 runs of the file on main). No test in the file pins that isolation level. A follow-up issue will be filed for it.