Repository navigation
fix(lock): preserve bounded ranges for large DML batches - #26706
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
c24747a to
d0d2aef
Compare
9817e6c to
a151b4f
Compare
|
Updated exact head Systematic rebase/fix summary:
Fresh exact-tree validation:
The force update used an exact lease against old remote head |
aunjgr
left a comment
There was a problem hiding this comment.
Re-reviewed exact head 7f440e24db522e635b83e16f4eccc00cd21f2a19 after the generation-fence update.
[P1] Apply the generation snapshot to the owner-side remote Lock paths too. The local service.Lock now captures getActiveTxnWithGeneration and verifies both generation and ID after acquiring the transaction mutex (service.go:483-494), which fixes the reported ABA. However, both remote handlers still call getActiveTxn(..., true, ...), wait on the pooled mutex, and validate only bytes.Equal(txn.txnID, req.Lock.TxnID) (service_remote.go:419-426 and 569-576).
The same concrete interleaving remains reachable on a physical owner: remote request R1 fetches generation N's pointer and blocks on its mutex; Unlock deletes N and returns that still-locked object to the pool; a new remote Lock with the same transaction ID reuses and initializes the object as generation N+1; then R1 acquires the mutex, the ID comparison succeeds, and the stale N request mutates N+1. Depending on ordering, old work can acquire/publish locks in the new transaction or affect its terminal result.
Use getActiveTxnWithGeneration (or an equivalent sealed admission token) in both ordinary and forwarded remote Lock handlers and reject a changed generation after locking, exactly as the local path does. Extend the deterministic forced-reuse regression through each remote handler; the current test exercises only ordinary service.Unlock/local holder access and does not close the remote mutation paths.
There was a problem hiding this comment.
Deep review completed for exact head 7f440e24db522e635b83e16f4eccc00cd21f2a19 against merge base 53f6aa6f0bbdc59f924072e823cdcd48ff43a981.
Requesting changes for one confirmed P1 correctness/lifecycle defect.
[P1] Fence both owner-side remote Lock handlers by transaction generation.
At pkg/lockservice/service_remote.go:419-426 and pkg/lockservice/service_remote.go:569-576, handleRemoteLock and handleForwardLock fetch an activeTxn, wait for its pooled mutex, and validate only txnID. The local service.Lock path now captures getActiveTxnWithGeneration and validates both generation and ID after locking (pkg/lockservice/service.go:483-494), but that lifetime fence was not applied to these two remote mutation paths.
A concrete interleaving remains reachable: R1 obtains the generation N pointer and blocks on its mutex; Unlock closes N, deletes it from the holder, and returns the still-locked object to the pool; a new remote Lock with the same transaction ID reuses and initializes that exact pointer as generation N+1; then R1 acquires the mutex. Because the ID matches again, the current checks pass and the stale N request calls beginLockOpLocked, coarsens, and locks against N+1. Old remote work can therefore acquire or publish locks in the new transaction generation or change its terminal result.
Capture the holder-published generation under the shard barrier and reject a changed generation after taking the transaction mutex in both handlers, matching service.Lock. Extend the deterministic forced-reuse regression through ordinary remote Lock and forwarded Lock. The current TestDelayedOrdinaryUnlockDoesNotCloseReusedSameIDGeneration exercises only ordinary Unlock and local holder access, so all 26 green terminal checks do not cover these paths.
I also rechecked the complete diff covering planner fallback, owner ledger and range replacement, proxy handoff, remote cleanup, v27 compatibility, waiter cleanup, reuse-checker scopes, and Q1-Q3 termination and bounds. No additional confirmed finding remained. This P1 is independent of the current main rebase conflict reported by GitHub.
aptend
left a comment
There was a problem hiding this comment.
Deep re-review completed on exact head 7f440e2. Since my prior approval at 72ace6f, I read all historical reviews, author follow-ups and resolved thread state, compared the nine-commit range-diff, and rechecked the complete current diff. The local generation snapshot closes the reported synchronous-close ABA, but both owner-side remote mutation handlers still use an unfenced pooled transaction pointer; requesting changes for the inline P1s. Independent validation passed for the full lockservice suite normally and under race, all other affected packages normally and under race, go vet, and git diff --check. A deterministic forced-reuse counterexample proved both handlers mutate the replacement generation under -race -count=20; its temporary test was removed. Current main also has separate merge conflicts in deadlock.go and deadlock_test.go.
| s.incRef(bind.Group, bind.Table) | ||
| } | ||
| txnID := append([]byte(nil), req.Lock.TxnID...) | ||
| ctx, finishLockOp := txn.beginLockOpLocked(ctx) |
There was a problem hiding this comment.
[P1] Capture and validate the transaction generation before admitting this remote operation
txn was fetched at line 419 with getActiveTxn, so this beginLockOpLocked can run on a recycled generation after waiting for the pooled mutex. I forced this interleaving on the exact head: an old-origin remote Lock captured generation N, concurrent Unlock deleted/reset it, the exact pointer was republished for a new-origin transaction with the same ID as N+1, and then the delayed request resumed. The txn-ID-only check passed, the RPC returned success, and the requested row appeared in N+1 ledger and physical ownership. This reproduced 20/20 under race. Capture the holder-published generation with the pointer and validate both generation and ID after txn.Lock, as the local service.Lock path now does.
| s.incRef(bind.Group, bind.Table) | ||
| } | ||
| txnID := append([]byte(nil), req.Lock.TxnID...) | ||
| ctx, finishLockOp := txn.beginLockOpLocked(ctx) |
There was a problem hiding this comment.
[P1] Apply the same generation fence to forwarded owner operations
This handler repeats the same unfenced getActiveTxn plus txn-ID-only validation at lines 569-576. The forced-reuse counterexample through handleForwardLock also reproduced 20/20 under race: a request from the retired origin generation acquired a row in the republished same-ID generation and returned success. Please snapshot and revalidate the generation here before calling beginLockOpLocked or mutating bookkeeping.
Rebase the final ownership and lifecycle closure onto current main. Reserve MORPC v27 for owner-local lock snapshots because current main already uses v18-v26 for unrelated protocol features. Keep transaction-closure admission cancellable and exact while restoring the ordinary Lock+Unlock path to zero additional allocations.
aunjgr
left a comment
There was a problem hiding this comment.
Re-reviewed exact head 3bbda1a577c3c3e8a51b4d09d4bcb4a282826a2d against merge base b89a639b0c4ac30ae143cae3b67925678d347d3b.
The previous owner-side remote ABA blocker is closed. Both handleRemoteLock and handleForwardLock now snapshot pointer plus generation under the holder publication barrier and verify generation and transaction ID after acquiring the pooled transaction mutex. The deterministic regression forces deletion, reset, reuse, and republication of the exact pointer with the same ID through both handlers, and proves the stale request returns ErrTxnNotFound without marking, locking, or deleting the replacement generation.
I re-walked the N → close/delete → pool reset → N+1 publication boundary, async lock-operation drain, service admission ownership, callback completion, and bind fencing. The rebase also moves the capability consistently to v28, leaving main's v27 ASOF protocol slot untouched, and preserves main's canonical one-victim deadlock semantics. Q1 generation ownership, Q2 callback/cancellation termination, and Q3 admission/ledger bounds are closed. No remaining blocker found. Approved.
Fixes #26630
Root cause
The former planner-side whole-table promotion serialized disjoint large DML, while a per-call row threshold could not bound keys accumulated by a large or multi-statement transaction. The enforceable budget belongs to the physical lock owner and must preserve ownership semantics: only non-sharded Exclusive ownership can be conservatively replaced by its observed
[min,max]range.The original implementation closed the row-budget path but left system-level gaps around remote deadlock snapshots, mixed-version ownership bookkeeping, table-by-table owner cleanup, proxy handoff atomicity, transaction generations, bind refresh, and Shared-to-Exclusive reentry.
Design and correctness closure
FOR UPDATErow-scoped in the planner. At the authoritative owner, cumulatively coarsen only all-Exclusive, non-sharded ownership after distinct retained keys exceedMaxLockRowCount.mainalready uses v18-v26 for unrelated protocol features.NewLockAdd=false, existing proxies dynamically bypass their cache after protocol downgrade, and proxy mode is enabled only when table-scoped owner cleanup is negotiated.ReplaceToparticipant, without coupling unrelated hash collisions. Source generations remain registered and closing until every physical table acknowledges cleanup; successful tables are removed from the retry ledger and are never replayed.Deep-review fixes
mainand integrated the upstream stale empty-lock repair, irregular UPDATE lock ordering, and remoteLAST_INSERT_IDchanges.Unhappy-path closure
Compatibility
MaxLockRowCount <= MaxFixedSliceSizeconfigurations remain valid.Validation
Final rebased head:
28f80f34c144d0583a10b0add8eed1c619660066(basecdf7888344df65136f6d0c9c35ead4940d69a793)pkg/lockservice: PASS, 102.385spkg/lockservice -race: PASS, 108.281s-race -count=100: PASSpkg/sql/colexec/lockopandpkg/sql/plan: PASS; both also PASS under-racego build,go vet, andgit diff --check: PASSThe remaining roughly 5-10% single-core CPU overhead versus
mainis the measured cost of the required closing-generation fence, cumulative budget check, and retryable cleanup lifecycle. The avoidable allocation regression has been removed.