fix(db): pin the request to the primary when a transaction starts #111
No reviewers
Labels
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
codefloe/forgejo-codefloe!111
Loading…
Reference in a new issue
No description provided.
Delete branch "t3code/fix-pr-comment-500"
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
db.WithTxgives its request read-your-writes consistency.HOST_REPLICAsetup intermittently answered404 Not found.because the conversation refetch read a lagging replica.models/db/engine_codefloe_test.go, the case the existing tests missed.Motivation
#108 pins a request to the primary after its first write, via an xorm hook that flags the request context on any non-SELECT statement.
The hook receives the context the statement runs on, and
db.WithTx/db.TxContextcreate their session withx.NewSession(), which xorm givesengine.defaultContext(xorm/session.go:102-108) rather than the request context.Every statement in a transaction therefore runs on the process-wide context,
PinPrimaryis a no-op, and the request stays unpinned.Nearly every write in Forgejo runs inside
db.WithTx, so the pin only ever fired for autocommit writes.The existing
TestPrimaryPinningReadsYourWritespasses because it writes throughdb.GetEngine(request), which is the autocommit path, and only entersWithTxonce the request is already pinned.Because #108 removed the explicit
db.WithMasterfrom #107 on the assumption the pin covered it, the review conversation refetch went back to reading the replica:pull_service.CreateCodeCommentwrites the comment inside a transaction, leaving the request unpinned.renderConversationrefetches the conversation through the unpinned request context, so the read goes to the replica.FetchCodeConversationreturns no rows,fetchCodeConversationmaps the empty result toErrCommentNotExistandctx.NotFoundOrServerErroranswers404with a plain-textNot found..Network errortoast, which is why this reads as a server error rather than a 404.Observed on codefloe.com, where
HOST_PRIMARYandHOST_REPLICAare separate Patroni backends: most comments win the race, some do not.Design
models/db/context.go:PinPrimary(parentCtx)inTxContextandtxWithNoCheck, right before the session is created. Both are the only paths that open a new transaction; the reuse paths run under a parent that has already pinned.BEGINrather than at the first write inside the transaction keeps this independent of xorm's session context handling, and matches what #108 already accepted forBEGINandPRAGMA.Known limits
GetCurrentReviewinservices/pull/review.gois one of them: adding comments in quick succession can miss the pending review created by the previous request and open a second one. That is a separate bug and is not addressed here.Testing
TestPrimaryPinningTransactionalWrite: a stale replica and a write performed only insidedb.WithTx; the request must be pinned afterwards and must read its own transactional write, while an unpinned context keeps reading the replica. Fails onv16.0/forgejo-codefloewithexpected: "tx write", actual: "stale replica".newLaggingReplicaSetupextracts the primary/replica harness thatTestPrimaryPinningReadsYourWriteshad inline, so both tests share it.go vet ./models/db/andgo test -tags 'sqlite sqlite_unlock_notify' ./models/db/pass.gofmt -l models/db/is empty.8ba42e8f7965ed56c81c