feat(db): pin a request to the primary after its first write #108
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!108
Loading…
Reference in a new issue
No description provided.
Delete branch "t3code/sticky-primary-reads"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
HOST_REPLICAsetup: after the first database write in a request, its remaining reads go to the primary.db.GetEnginehands out a primary-bound session for a flagged context.db.WithMasterfrom #107 together with its only caller, the review conversation fetch, which the pinned request now covers. The empty-result 404 stays. CI's deadcode check flagged the helper once it lost its caller.Motivation
An engine group routes every autocommit SELECT to a replica.
Any read that follows a write in the same request can therefore observe a lagging replica and miss the row it just wrote.
#107 fixed one such site.
The same shape exists elsewhere, for example the reaction reload in
ChangeCommentReactionand the pending-review lookups inCreateCodeComment, where a miss is swallowed and the comment silently pins to the moving PR head.Fixing these one call site at a time does not scale, so this pins the whole request instead.
Design
models/db/engine_codefloe.go:WithPrimaryPinningattaches a mutable pin to a context;PinPrimaryandIsPinnedToPrimaryset and read it;PrimaryPinHookpins on any statement that is not a SELECT, mirroring xorm's own replica routing check.models/db/engine.go: one line registering the hook before the tracing hook. xorm keeps only the last hook's returned context, soTracingHookmust stay last; the unit test registers both hooks in production order to guard this. The engine group forwards hooks to the primary and every replica.models/db/context.go: three lines inGetEnginereturning a session on the primary engine for a pinned context. Transactions and explicitly bound contexts are unaffected becausegetEngineresolves them first.routers/common/middleware_codefloe.go: middleware attaching pinning to the request context, appended inProtocolMiddlewaresso web, API, private and install routes all get it. TheBasecontext falls back to the request context for values, so the pin is visible through every derived context.Known limits
BEGINandPRAGMA. Over-eager on purpose and harmless.Testing
TestPrimaryPinHook: SELECTs do not pin, writes do, unpinnable contexts stay unpinned, derived contexts share the pin.TestPrimaryPinningReadsYourWrites: in-memory primary and replica with different rows and both hooks in production order; a pinned context reads the replica until it writes, then reads its own write from the primary, while a plain context keeps reading the replica. Fails without theGetEnginechange.TestPrimaryPinningMiddleware: the middleware attaches pinning; without itPinPrimaryis a no-op.go vetandgo test -raceformodels/db,routers/commonandTestRenderConversation*inrouters/web/repopass.golangci-linton the touched packages reports nothing in changed files. The remaining hits are pre-existing and only show up with a newer linter than the pinnedv2.11.4.make lint-go's deadcode step:.deadcode-outdiff is empty.make test-sqlite-migration, the target that failed on the first CI run, passes locally.git diff --check2a9c53c55fce49d6a6e9ce49d6a6e9ebc50f2d92ebc50f2d9261b259d55b