Repository navigation
fix(content-parse,collab): converter small-write priority and full event-loop yields (wiki-v2 issue 731) - #82
Merged
Merged
Conversation
…ion (docmost#731) Aging granted an aged large waiter ahead of a small write at every release. Whenever large conversions ran past the 3 s aging bound (always, at the A1 load), a small write waited the running conversion AND an aged one, past its 8 s wait: 503 engine_busy for 2.7% of A1's small writes. An urgent waiter that has waited through a normal holder's turn is now served before any aged one (OpSemaphore: a normal holder's release marks the queued urgent waiters). So a small write waits out at most one large conversion (≤ deadline + lag, reload credited) plus the small conversions queued ahead. A large waiter still never starves: once it is the oldest and aged, no other large runs first, so it waits at most the small writes that waited out the last large conversion (one per small principal), whatever the small rate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…uded (docmost#731) yieldToEventLoop was one setImmediate. A content write's heavy steps run in poll-phase callbacks (a pg or Redis reply, an HTTP request) or their promise continuations, and an immediate queued there runs in the SAME iteration's check phase, before libuv polls again: the steps on either side of the yield were one block to every other request. It is now two hops, so the continuation runs after the next poll. The helper moves to its own dependency-free module (the persistence seam uses it next) and replace-update re-exports it, so the handler's import is unchanged. Every existing caller gets the fix: after the build, after a fresh load (incl. the comment-mark paths through withYdocConnection). Pinned with real loopback sockets: an I/O event that arrives during a poll-phase step runs before the continuation, and a control shows the one-hop form fails it (and the mutation reds the spec). Co-Authored-By: Claude Opus 5.5 <[email protected]>
… between its page-sized steps (docmost#731) F2. onLoadDocument read `content` (a page-sized jsonb parse plus Kysely's CamelCasePlugin key walk) on every load but uses it only for a legacy row with no ydoc. findPageRowForLoad reads the ydoc only, and reads again with the content just for such a row; the branches use that second read, so they see what one read at that moment would have returned. F3. Inside onStoreDocument's transaction (row lock held) the store now yields a loop turn after the locked read, after the snapshot and after jsonToText. The snapshot (generation read, fromYdoc, encodeStateAsUpdate) is ONE synchronous call, snapshotForStore, after the reconcile: content and ydoc are always the same snapshot taken under the lock (docmost#390), and the generation recorded is the snapshot's, so an edit landing in a later yield makes the next store run (the docmost#731 skip). docmost#345, R4 and the failure signal unchanged. Specs (real Hocuspocus 3.4.4 stack): no content read when the ydoc exists, the legacy branch unchanged; the exact store step order with a macrotask marker at each yield and none inside the snapshot; an edit in each yield is persisted and never skipped over; an edit queued during fromYdoc would make content and ydoc disagree if a yield were added inside the snapshot. Each yield removed, a yield added inside the snapshot, and the old load read all red the specs. content-write-passes counts unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-check (docmost#731) When a conditional write names a digest and the page is resident, an edit since the settle left the memo cold, so the pre-check's page walk ran in the same block as opening the connection and the transaction. The handler now calls warmLiveContentDigest first and, only when that had to walk the page, yields a loop turn; then the unchanged resident lookup, pre-check and transaction. The memo is keyed on the afterTransaction generation, so an edit in that yield makes the pre-check recompute (correct, never stale). Nothing yields between the pre-check and the transaction (docmost#282). A warm memo (the settle's digest) walks nothing and yields nothing extra. Specs: warm → turn → pre-check (memo hit) → open → transaction; an edit in the turn re-walks at the pre-check with no macrotask before the mutation; the warm-up's hit/miss semantics against real Yjs. Dropping the warm yield, or adding one after the pre-check, reds them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…re-safe JSON) The store's snapshot moved into persistence-steps (docmost#731 F3), so docmost#773's storeSafeJson wrap moves with it, and the store-safe-json fitness allowlist names persistence-steps.ts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
The second half of the fork work for cccvu/wiki-v2 issue 731 (the first half was PR 79):
contentread it does not use.It is paired with the super PR, which carries the gitlink,
UPSTREAM_MODIFICATIONS.md, ADR 0031/0019 and the load README.What changed
authz/page-write/op-semaphore.ts. Before this change, under a saturating abuser, the 3 s aging rule gave the slot to an aged large waiter ahead of a queued small write. The small write then waited out the running conversion (up to the 7 s deadline and a reload) plus the aged one, overran its 8 s wait, and got503 engine_busy(8 of 293 small writes in A1).content-parse.service.tsnow state the correct worst case.authz/page-write/yield-to-event-loop.ts. A singlesetImmediatequeued from a poll-phase callback (pg, Redis, a worker message) runs in the same check phase, before libuv polls again. So waiting requests saw the load and the transaction, or the build and the transaction, as one block. The yield is nowsetImmediate(() => setImmediate(resolve)).replace-update.tsre-exports it.persistence-steps.tsplus seam §122.onLoadDocumentreadcontent(jsonb, which Kysely's CamelCasePlugin deep-walks) even when the row'sydocis present. It now readscontentonly when the ydoc is missing.executeTx(row lock held) there are three yields:snapshotForStore: the generation read,fromYdocand encode stay one synchronous call, because a yield between them would break Revert "fix: Add alignment styles for task list items" docmost/docmost#390);jsonToText.An edit that lands in a yield is either part of this store or makes the next store run.
doc-version.tswarmLiveContentDigestplus seam §2. On a resident CAS write whose memo is cold, the handler warms the digest and yields only if the warm-up walked the page. The resident lookup, the pre-check and the transaction then follow with no yield between the pre-check and the transaction (add a public switch, which can be viewed without logging in docmost/docmost#282).Why
These close the two engine-side blockers that the Graviton-calibrated A1 re-run found (cccvu/wiki-v2 issue 731, comment 6725645012): the converter-priority
engine_busyfailures, and lag-probe blocks caused by yields that never reached the poll phase.Tests
Semaphore (fake clock), five new specs:
Pool specs: a regression of the i2-M-r1 trace.
F1: a real loopback TCP spec. An I/O frame that arrives during a step is handled before the continuation; the one-hop form fails it.
F2: unit cases and a real-stack load that never selects
contentwhen a ydoc exists, with byte-identical output. The legacy path is unchanged.F3: pinned order
lockedRead ~ reconcile fromYdoc encode ~ jsonToText ~ updatePage. An edit at each yield is persisted, and the next store's skip is decided correctly.contentmatchesydoc.F4: order
get, walk, tick, get(hit), open, transaction, and no macrotask between the pre-check and the transaction.Mutation proofs: 3 for D3 and 8 for F1–F4, all red.
Runs:
check-upstream-boundary.shpasses;Probe latency on the Mac (longest wait of an I/O event during one 990 KB write):
What remains is the one replace transaction, about 175–195 ms.
Risk & rollback
🤖 Generated with Claude Code
Implementation plan and progress (canonical, covers both PRs): https://github.com/cccvu/wiki-v2/issues/731#issuecomment-6726465600 . Review comments for this PR set live on cccvu/wiki-v2 PR 785.