Repository navigation
fix(ddl): align lifecycle locks for truncate and alter copy - #29347
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
P2: Keep lifecycle and table lock ordering consistent with ALTER COPY. At pkg/sql/compile/ddl.go:3745, TRUNCATE now acquires the SNAPSHOT lifecycle row before table metadata. ALTER COPY still acquires the table exclusively (alter.go:1173), its storage lock (1184), then the SNAPSHOT lifecycle barrier (1250). Concurrent ALTER COPY and TRUNCATE on the same ordinary table can therefore deadlock: ALTER owns the table and waits for SNAPSHOT while TRUNCATE owns SNAPSHOT and waits for the table. Please close this competing path and add a deterministic concurrent regression; the current single-call order test does not cover it. Exact-head CI run 35990048643 passed enabled checks, but the PR notes no live two-connection replay. COMMENT only because this is self-authored.
6d16db9 to
de54aff
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep review of de54aff6d879fb3d7742eca99822f000c619cec9 against main (verified base ref 9841d29e2f1ee953daec281d508048b0ce485f5c, merge base 44e5226a21d7905a1d1e528d257321d2696c807b).
Recommendation: approve / no blocking findings. COMMENT is used because the authenticated account is also the PR author. The earlier COPY ALTER lock-order finding is resolved at this head.
Scope and correctness
Reviewed every changed hunk in all three files, #29335, the previous review, and the connected lock/transaction/cleanup paths. The change map covers persistent pessimistic TRUNCATE and COPY ALTER, the existing lifecycle SQL executor, and the deterministic test fixture. This is a focused bug fix, not a feature requiring a separate design document.
The new order is lifecycle row -> catalog/table locks -> RC snapshot advancement -> existing lineage write barrier. DROP ACCOUNT already enters the lifecycle before cluster-table cleanup; COPY ALTER now follows the same direction, closing the competing path identified in the previous review. DROP TABLE/DATABASE already enter the lifecycle before their target locks. The relevant in-place index removal path uses parent-owned relation deletion rather than a nested DROP TABLE gate.
The ordinary DELETE compiler has an early CanTruncate return that constructs the TRUNCATE scope before ordinary DELETE metadata-lock collection. Thus the new gate is reached before the target-table locks for the original issue's optimized DELETE too; this is not merely a Scope-only ordering change hidden behind an earlier DELETE lock.
The gate query runs as the system account with the original transaction and disabled statement increment. Its result is closed. The existing post-snapshot write barrier must remain: the early locking read avoids publishing a new MVCC version before snapshot advancement, while the later write maintains lineage publication/conflict semantics. Temporary/optimistic behavior and public SQL/storage formats are preserved.
Unhappy paths and bounds
| Check | Result |
|---|---|
| Q1: ownership / cleanup | Transaction owns the lifecycle lock through commit/rollback; the helper does not create a second transaction. SQL results close on success. Gate errors return before target-table mutation. Existing rollback attempts lock release even if workspace cleanup errors; lineage snapshot restoration and ALTER cleanup remain in place. |
| Q2: waits / termination | The two edited DDL paths and DROP ACCOUNT no longer create the reported table/gate cycle. They use the existing context-aware lock wait and transaction release machinery. The concurrent test cancels/unblocks/joins workers before restoring stubs or releasing compiles; result channels are buffered. This does not claim to eliminate arbitrary user-created multi-statement lock cycles. |
| Q3: growth / reuse | No new queue, cache, worker or unbounded retained state in production. The test executor protects concurrent recording with a mutex, invokes its callback outside that mutex, and is restored after the scenario. No parallel package test was found using the modified global stubs. |
Performance and engineering quality
There is one additional internal SELECT ... FOR UPDATE per affected persistent pessimistic DDL, and the existing global gate is acquired earlier while waiting for table locks. No per-row work or new global serialization domain is added: these operations already crossed the same lifecycle write barrier. The extra statement and earlier hold window are real costs, so this is not a claim of zero overhead. No throughput/DDL-contention benchmark was run.
The helper reuses existing lifecycle SQL, executor and transaction ownership. The implementation adds no retry framework, background worker or alternate state machine. The two focused UTs prove distinct contracts: full TRUNCATE gate/catalog/storage/barrier order, and overlapping COPY ALTER/TRUNCATE admission. Channel synchronization, tiny fixtures and joined cleanup keep the tests deterministic and inexpensive.
Validation at this head
- Both new UTs selected and passed locally under
mo-cgo-test -mod=readonly -race -count=1 -timeout 120s -json; terminal compile package elapsed 1.174s. - Exact
TestAlterCopyAndTruncateSerializeBeforeTableLocksrepeated under-race -count=100: 100 passes, zero failures, terminal package elapsed 1.431s. Initial measured test duration was below timer resolution, so the adaptive 30s budget selects the capped 100 repetitions. - Filled the PR's stated public-SQL evidence gap: a review-only Go overlay reused the existing single-CN SQL integration fixture, asserted pessimistic mode on an actual transaction, and opened two independent sys SQL connections. On the same instance it executed two serial controls (both orders) and eight concurrent paired launches of DROP ACCOUNT and unconditional cluster-table DELETE. The input matches the reported minimal case: one test account, one sys-owned cluster row, no rows for the dropped account. All ten rounds succeeded. Each round asserted zero account rows, zero table rows, then dropped the test table and asserted catalog removal before the next round. The scenario took 10.757s; the complete test including fixture setup took 21.01s, and the package exited successfully after fixture shutdown (21.700s).
- The public replay used a shared start channel, not sleep-based scheduling. It validates the real SQL/transaction path; the permanent UT supplies the deterministic internal interleaving proof. The replay does not claim all possible schedules or multi-CN failure modes were exhaustively tested.
- Reused current-head green CI for owning-package/static checks and both multi-CN BVT variants: https://github.com/matrixorigin/matrixone/actions/runs/36041013406 . No failing or pending enabled checks; PR is mergeable.
git diff --checkpassed. No tracked/untracked source changes were left in the PR worktree. The review-only SQL probe was loaded through an external overlay and was not added to this PR.
The target issue now has both deterministic lock-order evidence and a successful current-head public SQL replay. No unresolved blocking correctness, cleanup, boundedness or engineering-quality finding remains.
What type of PR is this?
What this PR does / why we need it
An unconditional DELETE on a cluster table can run through TRUNCATE. Previously, pessimistic TRUNCATE acquired table locks before the SNAPSHOT lifecycle gate, while DROP ACCOUNT acquired the gate before cluster-table cleanup. That inverse order caused the concurrent deadlock reported in #29335.
Pessimistic TRUNCATE now takes the existing SNAPSHOT lifecycle row lock before table locks. COPY ALTER on a persistent table follows the same order; without that change, COPY ALTER could hold a table lock while waiting for the gate held by TRUNCATE. Both paths retain the existing lineage write barrier after RC snapshot advancement, preserving the catalog view used for lineage publication. Temporary and optimistic paths are unchanged.
The deterministic tests check the TRUNCATE gate → catalog lock → storage lock → write-barrier order and overlap COPY ALTER with TRUNCATE to verify that each enters the gate before locking the table. The original issue recorded 10/10 concurrent deadlocks on its base revision and 10/10 successful serial controls. A live two-connection SQL replay on this candidate was not run.
Validation
pkg/sql/compilefull package test: passed.-race, 3 repetitions: passed.go vet ./pkg/sql/compile: passed.pkg/sql/compile: 0 issues.git diff --check: passed.Which issue(s) this PR fixes
Fixes #29335