Repository navigation
fix(api): a key rotation that reads completed is done for every client - #2277
Conversation
The sweep records `completed` in its last checkpoint and its task returns after that, so a status read in between reports completed with `active_here` still true. rotation_runs_and_verifies_over_http asserted `active_here == false` on the first completed read and failed when it landed in that window. It now polls until the task has ended too. Holding the window open (a delay after the last checkpoint) failed the old assertion every time, as CI saw it.
aaj3f
left a comment
There was a problem hiding this comment.
@bplatz yes to the fix: polling until both completed and the task has ended is the honest shape for an HTTP test, and it's a real wait on the condition, not a sleep. Two notes. (1) One level up, the old assertion is the same assumption the CLI's remote --wait makes, and in that window a client's next rotate gets a 409 while pause returns OK; there's a question below about whether start/pause should treat a terminal in-memory state as finished so completed means done. (2) This is at least the sixth "a test races a background task" fix since September:
32acd8986(store load / tracing, onnext);594d82c4d(sketch builds vs background indexing);baa152750(key rotation tests vs background indexing);c1db1a86a(flush count vs the WAL thread);6290dedb7/ #1852 (polling deadlines); this one
Not for this PR, the point fix is right here, but the class fix is probably test builders with background work off by default plus an explicit settle hook for the tasks a test does start. Good to merge once it's ready.
Full review below:
✅ Approve.
Nice diagnosis: the sweep publishes completed to the in-process view before its last record write, and the task only ends after that write, so completed with active_here: true is a real state, and the old test asserted it couldn't happen. CI caught it on #2276 (run 42416047379 attempt 1, encryption_rotation_http.rs:161, left: Bool(true)), and Claude reproduced it on a box by holding the window open: the old test fails every time, this version passes. The new loop waits on the condition itself and then asserts active_here == false with a message, so a sweep that never ends still fails clearly. The one note below is about the server rather than the test.
Adherence to repo commitments:
- Patterns/abstractions: ✔ polls the HTTP status for the full terminal condition, the only synchronization an HTTP client has;
wait_for_key_rotationexists for in-process callers. - Performance (speed first, memory second): ✔ test-only; no performance-degradation risk.
- Deployment targets: ✔ n/a — test-only; the server-side window it documents is the same on every host (one record write wide).
- Testing: ✔ red-green verified with the window forced open; 5/5 at head; it runs in
grp_httpunder CI'stestjob. - Conventions: ✔ self-describing title, commit body explains the race and how it was forced.
Verified at branch HEAD (ad0263e44): Claude ran rotation_runs_and_verifies_over_http five times on a box (pass), forced the window with a 500 ms hold after the final checkpoint (this test passes; the base test fails at :161), and probed pause/rotate in the window (200 and 409); CI test and clippy are green on this SHA.
Approving so you can merge when ready, but maybe worth a look at the server-side question first.
| assert_eq!(body["state"], json!("running")); | ||
| assert!(body["holder"].as_str().is_some_and(|h| !h.is_empty())); | ||
|
|
||
| // The sweep records `completed` just before its task returns, so the |
There was a problem hiding this comment.
Optional (question, server side). This is more of a question than a suggestion, and not about the test, which I think is right. The old assertion was "once status says completed, nothing is running here", and that's the same assumption the CLI's remote wait makes: wait_for_stop (fluree-db-cli/src/commands/encryption.rs:304-322) returns on the first status whose state isn't running.
Claude probed what a client sees if it acts in that window (same 500 ms hold after the final checkpoint, acting on the first completed read): status completed with active_here: true, then POST …/rotate/pause → 200 OK {"ok":true} (nothing left to pause), and POST …/rotate {"retire_key_id": 1} → 409 "a key rotation is already running in this process". So fluree encryption rotate --wait … followed straight away by the next rotation can hit a 409, for as long as one record write takes (milliseconds on a file store, a round trip on S3).
I don't think the 409 should just go, since it's what keeps a new sweep from starting while the old task's last record write is still in flight. But I wonder if start_key_rotation could treat an active job whose in-memory state is terminal as finished: take its handle and await it (it's about to return), the way wait_for_key_rotation already does, and have signal_key_rotation refuse such a job. Then completed would mean done for every client, and this test's old assertion would hold again. That's a fluree-db-api change rather than this test's, so if you'd rather keep this PR test-only I understand; if you agree with the direction, I'd rather it land alongside this than get lost. Commenting here because fluree-db-api/src/key_rotation.rs is not in this diff.
There was a problem hiding this comment.
Addressed in 126e421. Instead of having start await a terminal job's handle, a checkpoint now writes the record before publishing to the in-process view, and a job counts as active only while its state is Running. That also covers active_here in status, so the HTTP test's original assertion holds again, and it's restored.
The sweep published each checkpoint to the in-process view before writing the record, and a job counted as active until its task returned. So for the length of the final record write, status read `completed` with `active_here: true`, `pause` returned OK on a finished job, and the next `rotate` got a 409. The CLI's remote `--wait` returns on the first status that is not `running`, so `rotate --wait` followed by the next rotation could hit that 409. A checkpoint now writes the record before publishing, so the view is never ahead of the record, and a job is active only while its state is `Running`. A terminal state is published only after its record is written, and the task does nothing after that but return, so status, start, pause, cancel and release all treat it as done. The HTTP rotation test goes back to asserting `active_here: false` on the first `completed` read.
rotation_runs_and_verifies_over_httpfails intermittently in CI (fluree-db-server::grp_http, line 161:left: true, right: false). It failed PR #2276, which changes only a workflow file.The sweep published each checkpoint to the in-process view before writing the record, and a job counted as active until its task returned. For the length of the final record write, then, status read
completedwithactive_here: true. The test asserted that couldn't happen, and the same window reaches clients:pausereturns OK on a job that has finished.rotategets a 409.The CLI's remote
--waitreturns on the first status that is notrunning, sofluree encryption rotate --waitfollowed straight away by the next rotation could hit that 409.A checkpoint now writes the record before publishing, so the in-process view is never ahead of the record. A job counts as active only while its state is
Running. A terminal state is published only after its record is written, and the task does nothing after that but return. So status, start, pause, cancel and release all treat a job as done as soon as status readscompleted. The 409 still guards a sweep that is running.it_key_rotation::first_completed_status_means_the_sweep_is_donepolls status without sleeping and acts on the first terminal read. It expectsactive_here: false, a refusedpause, and a successful nextrotate. Against the old code it failed 20 of 20 runs, at theactive_hereassertion. The HTTP test goes back to its original assertion.Follow-up: #2355