Repository navigation
fix(databases): claim a transaction commit atomically - #14163
Conversation
The pending to committing transition now runs inside a database transaction that re-reads the transaction row with a lock, so only one concurrent commit request applies the staged operations. Requests that lose the claim fail with TRANSACTION_NOT_READY before they can mark the winner's transaction failed.
🟢 Tier S · Ready to merge
The transaction commit action now claims pending transactions atomically with a locked re-read, rejecting competing commits before they can enter failure handlers. The shared action covers all four database APIs. An E2E regression test stages an increment, sends four parallel commits, and checks that only one succeeds and the stored counter increases once. Latest changes: The newest commit adds an E2E test for concurrent commits of the same transaction, asserting one success, three transaction_not_ready errors, a committed response, and a single persisted increment.
📂 Walkthrough · 2
✅ Fixed since the last review · 1
Reviewed the commits since |
Four parallel commits of one staged increment must leave exactly one success, reject the rest with transaction_not_ready and apply the increment once.
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
Fixes #14142
Committing a transaction checked
status === 'pending'on a plain read and later wrotecommittingwith an unconditionalupdateDocument. ConcurrentPATCH {commit: true}requests on the same transaction all passed the check and all ran the staged operations, so an increment was applied once per request. With a create op, the losing requests hit a 409 conflict and their catch blocks then marked the winner's transactionfailed, even though its row had been written.The pending to committing transition now happens inside
$dbForProject->withTransaction(), after re-reading the transaction withgetDocument(..., forUpdate: true). If the status is no longerpending, the request throwsTRANSACTION_NOT_READY. The claim sits before thetryblock, so a request that loses it can no longer reach the catch blocks that mark the transaction failed. The TablesDB, DocumentsDB and VectorsDB transaction update actions extend this class, so all four APIs are covered. One visible change: for a create op, the losing requests now get 400transaction_not_readyinstead of 409transaction_conflict, and the winner's transaction stayscommitted.Checked against a local API container running this branch on Postgres, with a fresh transaction per round and N parallel commit requests:
transaction_not_ready, n=1, statuscommittedtransaction_conflict; row created but transaction endedfailedtransaction_not_ready, statuscommittedcommittedtestConcurrentCommitinTransactionsBasesends four parallel commits of one staged increment and asserts one 200, three 400transaction_not_ready, acommittedstatus and a single increment. Against main'sUpdate.phpit failed 6 of 6 runs ([200,200,200,200]or[200,200,400,400]); with this change it passed every run of the TablesDB, Legacy and DocumentsDB server, client and console suites.Only Postgres and a single API node were exercised. The lock is a database row lock, so multiple API nodes should behave the same. Two cases are not covered here: a rollback racing a commit can still overwrite it, because the rollback branch writes
failedwithout re-checking the status; and concurrent commits of an empty transaction (no operations) still all return 200, which applies nothing twice.