Repository navigation
Archive a large set of derived outputs in bounded transactions - #3236
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2a062b2aab
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let scheduled = self.select(name, comparison, actor).await?; | ||
| // The version is selected. Other keyed work need not wait for the | ||
| // archive of the previous outputs, which holds this switch's keys. | ||
| self.finish_switches(Some(name)).await?; |
There was a problem hiding this comment.
Prevent per-event passes from racing an unfinished switch
When a switch runs through the API while the daemon's background runner is active, select() releases the derivation lock before finish_switches() archives the outputs and resets progress. The background runner's per_event_pass() takes that same lock but never checks unfinished switches or held keys, so it can start processing new inputs from the old saved position during the archive. If its output batch commits after Switched resets the position, refusals::position() advances the position past the historical inputs again, leaving their archived outputs permanently uncomputed. Keep the switched derivation blocked until the switch finishes, including any pass already queued for its lock.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a3de2ed. A saved position only moves forward, so a batch computed from the old position and committed after the reset would have moved it past the history. The transaction that ends the switch is now submitted while finish_switches holds the derivations lock. A per-event pass holds that lock from reading its position to its last commit, so none is in flight when the position is reset, and the next pass reads the reset position. Passes that run while the outputs are being archived are left alone: the position is not reset yet, they read only new inputs, and their outputs are committed after the selection, outside what the switch archives. The lock is also where the switch is checked again, so two runners that both find it unfinished do not both end it.
a8cdf88 to
8f7b09e
Compare
Replacing a key that no longer produced most of its outputs, and a version switch that recomputes history, archived every previous output in one writer transaction. On the deployed store three keys with about 390,000 outputs held the writer in transactions of 600 to 775 s while capture spooled. Most of that time was one statement: each archived event was read by its own lookup, which on a compressed chunk decompressed the whole chunk's batches of that kind per event. The payload read now takes the archived chain's event types from the live tables, reads only the types a derivation reads, once each, in ID order in groups bounded by their IDs and types. A batch of keys states how many previous outputs it may archive. Admission refuses a batch that would archive more and names them; the runner archives them in transactions of their own, sized by how long the last one held the writer, and submits the batch again. A version switch selects in one transaction and archives the previous outputs the same way, holding its keys and a per-event position until a final transaction ends it; a runner finishes an unfinished switch when it starts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The outputs a batch of keys archives were selected by a row in the keyed live table only, and an archive batch accepted only such rows. An output identified by its record location has its row in the other live table and would have stayed live when its key stopped producing it. Both now use the liveness the chain read uses. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
A batch of keys with no bound serialized the field as null, which changed the scope other producers of such batches write. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The transaction that ends a switch resets a per-event derivation's position. A pass that read the old position before that and committed a batch after it would move the position past the history again, since a saved position only moves forward. A pass holds the derivations lock from reading its position to its last commit, so the switch now ends under that lock, after checking that another runner has not ended it already. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
The test landed on master while this branch was open and builds a batch scope without the new field. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU
8f7b09e to
32fb1b7
Compare
Archiving a large set of derived outputs no longer holds the writer for the whole set. A key that stops producing most of its outputs, and a version switch that recomputes history, now archive in transactions sized by how long the last one held the writer, with live batches admitted between them. The statement that made the old transactions take minutes is replaced by a read that costs about the rows it returns.
What was wrong
On 2026-11-26 three keys with about 390,000 live outputs computed to nothing and their outputs were archived in consecutive writer transactions of 600 to 775 s; the commit position stood still and capture spooled. Two things caused it:
EXPLAIN (ANALYZE, BUFFERS)for 256 livestate.intervaloutputs in a compressed chunk: 114.9 s, about 380,000 rows decompressed per ID.What changed
mark_selected_dirty). The chain to archive comes from the live tables with each member's event type (archive::live_chain, the query the lifecycle already used, now shared with admission). Only the types some derivation reads have their payload read, once each, in ID order, 4,096 per statement, bounded by the group's inclusive IDs and its types. The same 256 rows as above: 0.47 ms, 2,000 rows decompressed. 4,096 IDs: 3.5 ms, 6,000 rows decompressed, 1.46 rows read per row returned.DerivedScope::Keyscarriesreplaces_at_most(the runner passes 4,096). A batch that would archive more is refused whole withError::ArchiveFirst, which names the outputs. The runner archives them inDerivedScope::Archivebatches, each its own writer job: 256 outputs in the first, then a size that doubles while a transaction holds the writer for under 125 ms and halves above 500 ms (writer::next_bulk_size, the rule the lifecycle driver had, now used by both). It then submits the batch again. The archive batch keeps the key dirty with the mark its computation saw, so the completing batch clears it and a stop in between computes the key again.producer_switchoperation with its position and no result, and schedules the keys held by the operation. The runner then lists the live outputs committed up to that position and archives them through the same archive batches (reasonversion_change, the operation's ID).DerivedScope::Switchedends the operation, releases the keys and resets a per-event derivation's position. A runner finishes unfinished switches when it starts, before its first pass. Akeep_historyswitch is complete in the selecting transaction, as before.Decisions
archiveoperation without theversion_changereason or the switch's operation ID, and a lifecycle operation's end has no place for the position reset.Not changed: a per-event batch and a replay sequence still archive what they replace in their own transaction, and a key's new outputs still commit in one transaction.
Verified
a_large_replacement_archives_in_transactions_with_live_admission_between(sinexd): six outputs replaced with a bound of two are archived in three transactions, a live batch commits between the first and the second, and the key is dirty in between. With the previous code the archive is one position, so the assertion that the live commit lies between the first and last archive position fails.a_switch_stopped_before_its_archive_ends_at_the_next_start(sinexd): after the selecting transaction alone the previous outputs are live, the operation has no result and the held keys are not computed; the next runner start archives them withversion_change, ends the operation and the new version computes.checkandtestjobs on this head.Not verified here: the acceptance row that asks for a bench run reporting writer hold time while a 100,000-output key is archived. The bench has no case that replaces a large key; the figures above are plans read on the deployed store.
Task: sxnext-pib.20
🤖 Generated with Claude Code
https://claude.ai/code/session_01UuFixsXwismFLxbfWcuXHU