codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

feat(db): pin a request to the primary after its first write #108

Merged
pat-s merged 1 commit from t3code/sticky-primary-reads into v16.0/forgejo-codefloe 2026-09-05 12:12:24 +00:00
Owner

Summary

  • Give every HTTP request read-your-writes consistency on a HOST_REPLICA setup: after the first database write in a request, its remaining reads go to the primary.
  • Implement it once, centrally: an xorm hook flags the request context on any non-SELECT statement, and db.GetEngine hands out a primary-bound session for a flagged context.
  • Remove db.WithMaster from #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.
  • Add a "Database consistency" row to the README changelog table covering #107 and this PR.

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 ChangeCommentReaction and the pending-review lookups in CreateCodeComment, 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: WithPrimaryPinning attaches a mutable pin to a context; PinPrimary and IsPinnedToPrimary set and read it; PrimaryPinHook pins 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, so TracingHook must 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 in GetEngine returning a session on the primary engine for a pinned context. Transactions and explicitly bound contexts are unaffected because getEngine resolves them first.
  • routers/common/middleware_codefloe.go: middleware attaching pinning to the request context, appended in ProtocolMiddlewares so web, API, private and install routes all get it. The Base context falls back to the request context for values, so the pin is visible through every derived context.
  • Cron, queue and migration contexts carry no pin and keep today's routing.

Known limits

  • A session held in a variable across a write and a later read still routes that read to the replica, because routing is decided per statement inside the group session. Most held sessions run a single statement.
  • Once pinned, all remaining reads of that request go to the primary. That is the usual sticky-primary trade-off and only costs replica offload on requests that write.
  • Anything that is not a SELECT pins, including BEGIN and PRAGMA. 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 the GetEngine change.
  • TestPrimaryPinningMiddleware: the middleware attaches pinning; without it PinPrimary is a no-op.
  • go vet and go test -race for models/db, routers/common and TestRenderConversation* in routers/web/repo pass.
  • golangci-lint on the touched packages reports nothing in changed files. The remaining hits are pre-existing and only show up with a newer linter than the pinned v2.11.4.
  • make lint-go's deadcode step: .deadcode-out diff is empty.
  • make test-sqlite-migration, the target that failed on the first CI run, passes locally.
  • git diff --check
## Summary - Give every HTTP request read-your-writes consistency on a `HOST_REPLICA` setup: after the first database write in a request, its remaining reads go to the primary. - Implement it once, centrally: an xorm hook flags the request context on any non-SELECT statement, and `db.GetEngine` hands out a primary-bound session for a flagged context. - Remove `db.WithMaster` from #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. - Add a "Database consistency" row to the README changelog table covering #107 and this PR. ## 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 `ChangeCommentReaction` and the pending-review lookups in `CreateCodeComment`, 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`: `WithPrimaryPinning` attaches a mutable pin to a context; `PinPrimary` and `IsPinnedToPrimary` set and read it; `PrimaryPinHook` pins 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, so `TracingHook` must 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 in `GetEngine` returning a session on the primary engine for a pinned context. Transactions and explicitly bound contexts are unaffected because `getEngine` resolves them first. - `routers/common/middleware_codefloe.go`: middleware attaching pinning to the request context, appended in `ProtocolMiddlewares` so web, API, private and install routes all get it. The `Base` context falls back to the request context for values, so the pin is visible through every derived context. - Cron, queue and migration contexts carry no pin and keep today's routing. ## Known limits - A session held in a variable across a write and a later read still routes that read to the replica, because routing is decided per statement inside the group session. Most held sessions run a single statement. - Once pinned, all remaining reads of that request go to the primary. That is the usual sticky-primary trade-off and only costs replica offload on requests that write. - Anything that is not a SELECT pins, including `BEGIN` and `PRAGMA`. 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 the `GetEngine` change. - `TestPrimaryPinningMiddleware`: the middleware attaches pinning; without it `PinPrimary` is a no-op. - `go vet` and `go test -race` for `models/db`, `routers/common` and `TestRenderConversation*` in `routers/web/repo` pass. - `golangci-lint` on the touched packages reports nothing in changed files. The remaining hits are pre-existing and only show up with a newer linter than the pinned `v2.11.4`. - `make lint-go`'s deadcode step: `.deadcode-out` diff is empty. - `make test-sqlite-migration`, the target that failed on the first CI run, passes locally. - `git diff --check`
pat-s force-pushed t3code/sticky-primary-reads from 2a9c53c55f
Some checks failed
testing / test-unit (pull_request) Has been cancelled
testing / test-e2e (pull_request) Has been cancelled
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / backend-checks (pull_request) Has been cancelled
testing / frontend-checks (pull_request) Has been cancelled
to ce49d6a6e9
Some checks failed
testing / test-sqlite (pull_request) Has been skipped
testing / test-remote-cacher (garnet) (pull_request) Has been skipped
testing / frontend-checks (pull_request) Successful in 1m49s
testing / test-unit (pull_request) Has been skipped
testing / test-e2e (pull_request) Has been skipped
testing / test-pgsql (pull_request) Has been skipped
testing / test-mysql (pull_request) Has been skipped
testing / backend-checks (pull_request) Failing after 4m51s
testing / test-remote-cacher (redis) (pull_request) Has been skipped
testing / test-remote-cacher (valkey) (pull_request) Has been skipped
2026-09-05 10:22:37 +00:00
Compare
pat-s force-pushed t3code/sticky-primary-reads from ce49d6a6e9
Some checks failed
testing / test-sqlite (pull_request) Has been skipped
testing / test-remote-cacher (garnet) (pull_request) Has been skipped
testing / frontend-checks (pull_request) Successful in 1m49s
testing / test-unit (pull_request) Has been skipped
testing / test-e2e (pull_request) Has been skipped
testing / test-pgsql (pull_request) Has been skipped
testing / test-mysql (pull_request) Has been skipped
testing / backend-checks (pull_request) Failing after 4m51s
testing / test-remote-cacher (redis) (pull_request) Has been skipped
testing / test-remote-cacher (valkey) (pull_request) Has been skipped
to ebc50f2d92
Some checks failed
testing / frontend-checks (pull_request) Successful in 1m8s
testing / test-unit (pull_request) Has been cancelled
testing / backend-checks (pull_request) Successful in 6m20s
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Failing after 2m41s
testing / test-mysql (pull_request) Failing after 3m11s
testing / test-e2e (pull_request) Failing after 3m56s
testing / test-pgsql (pull_request) Failing after 4m27s
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
2026-09-05 11:02:23 +00:00
Compare
pat-s force-pushed t3code/sticky-primary-reads from ebc50f2d92
Some checks failed
testing / frontend-checks (pull_request) Successful in 1m8s
testing / test-unit (pull_request) Has been cancelled
testing / backend-checks (pull_request) Successful in 6m20s
testing / test-remote-cacher (redis) (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Failing after 2m41s
testing / test-mysql (pull_request) Failing after 3m11s
testing / test-e2e (pull_request) Failing after 3m56s
testing / test-pgsql (pull_request) Failing after 4m27s
testing / test-remote-cacher (valkey) (pull_request) Has been cancelled
testing / test-remote-cacher (garnet) (pull_request) Has been cancelled
to 61b259d55b
All checks were successful
testing / backend-checks (pull_request) Successful in 4m54s
testing / frontend-checks (pull_request) Successful in 1m4s
testing / test-remote-cacher (garnet) (pull_request) Successful in 1m28s
testing / test-unit (pull_request) Successful in 10m21s
testing / test-remote-cacher (redis) (pull_request) Successful in 1m42s
testing / test-remote-cacher (valkey) (pull_request) Successful in 2m21s
testing / test-e2e (pull_request) Successful in 27m24s
testing / test-mysql (pull_request) Successful in 32m41s
testing / test-sqlite (pull_request) Successful in 40m2s
testing / test-pgsql (pull_request) Successful in 42m5s
issue-labels / backporting (pull_request_target) Has been skipped
milestone / set (pull_request_target) Has been skipped
2026-09-05 11:14:10 +00:00
Compare
pat-s merged commit 0060cec458 into v16.0/forgejo-codefloe 2026-09-05 12:12:24 +00:00
pat-s deleted branch t3code/sticky-primary-reads 2026-09-05 12:12:24 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!108
No description provided.