Skip to content

fix(frontend): prevent publication grants from recreating dropped subscriber IDs - #32936

Merged
XuPeng-SH merged 14 commits into
matrixorigin:mainfrom
LeftHandCold:fix/32900-pubsub-drop-verify
Nov 22, 2026
Merged

XuPeng-SH merged 14 commits into
matrixorigin:mainfrom
LeftHandCold:fix/32900-pubsub-drop-verify

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

Related to #32900 (proven stale-grant path; deliberately not a closing reference).

What this PR does / why we need it:

Two different CNs can both report success for DROP ACCOUNT A and ALTER PUBLICATION ... ACCOUNT ADD B, yet leave a mo_subs row for A's deleted ID. ALTER computes its final account map before fanout. If DROP commits after that lookup, ALTER sees that the subscription no longer exists and its batch insert recreates it using the old account map. The same lookup-to-insert gap exists for CREATE PUBLICATION.

Before source database validation and fanout, protect the sorted physical mo_account primary keys of explicitly named recipients in the publication transaction, then verify that all selected IDs still exist. Named requests wait for foreign covering ranges to clear and acquire points. A compatible covering lock already held by the publication transaction satisfies the read without widening its scope. A missing ID or lock error aborts the statement. For ACCOUNT ALL, keep the existing shared locking read over its resolved IDs; this PR does not address concurrent account creation (#32941). If any ID disappeared, roll back the statement with a retryable conflict instead of writing a partial grant or looking up the replacement by name. DROP already locks the immutable account ID exclusively before inspecting subscriptions; reuse that lock. No schema migration, historical orphan sweep, SHOW change or change to ordinary queries. The lock set scales with the effective publication targets (including ACCOUNT ALL); a conflicting DROP/grant may wait or require a new statement.

Scope: The original QA rows were revoked (status=1) with their old subscription name and time. The reproduced stale insert is a normal grant (status=0, no subscription name/time). It proves a real violation of the same invariant, not the exact row history of either QA incident. Do not close #32900 or treat this as a repair of existing metadata.

Tested with (original fix before lock-scope review)

  • Pre-fix on 4cec607: controlled live cross-CN DDL; A previously subscribed, ALTER only added B, DROP A committed first, both DDLs succeeded, one orphan for old A. A pre-revoked named subscriber was a negative control and did not reproduce the QA signature.
  • Post-fix: same stale selection rolled back (20628), old-ID orphan count zero. No temporary scheduling hooks in the PR.
  • Full go test ./pkg/frontend via the repository CGo test wrapper.
  • Full pkg/frontend and the new cross-CN case also passed under -race.
  • Reverse-order control: ALTER held the resolved target before DROP could finish; both statements succeeded, the final orphan count was zero, and the unaffected grant remained.
  • TestDropAccountConcurrentPublicationGrant and TestShowPublicationsMissingAccountsSQL, three repetitions via the CGo wrapper. Asserts the actual catalog rows, failed grant rollback, successful new statement, unaffected subscriber data, grant list and same-name recreation with a new ID.

Lock-scope review follow-up (51fd169490)

  • Two-CN real SQL under -race: with max-row-lock-count=16, tested both 1 and 17 explicit recipients. An unrelated DROP committed while publication still waited for its source DB (including a non-target inside the 17-ID span). A target DROP waited for publication; afterwards zero grants referenced its old ID. The original concurrent DROP/ALTER ADD regression also passed.
  • 84 focused top-level frontend publication/restore tests passed under -race. A separate full frontend -race run on macOS hit the previously reported time.Local test race ([Bug]: TestConvertRowsIntoBatchTemporalText races with Go timers when writing time.Local #32893); it was not a green full-suite race run.

Shared covering range follow-up

  • At a row-lock threshold of 4, two CNs reproduced MySQL 20707 on the reviewed commit for both a named CREATE behind a foreign shared range and a cluster restore replaying ACCOUNT ALL followed by a named publication. The restore required no concurrent SQL.
  • The same race-enabled SQL regression now waits for the foreign holder to release and reads back exactly the two named grants. The cluster restore reads back five active grants for ACCOUNT ALL and exactly the two named grants.
  • Owner, remote proxy, and forward-route tests retain strict exact-row admission for its other callers. A separate writer can acquire the gap between the named rows while a writer on a named row conflicts until the reader unlocks.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Nov 19, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Nov 19, 2026
@mergify

mergify Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Independent two-CN review of 07d9e10: the old-ID fix works (baseline retained one normal grant after DROP, PR retained zero, healthy subscriber still read value 7). Canceling a waiting publication released DROP and left no partial catalog rows. This is not proof of the revoked, named QA orphan in #32900; please keep that issue open.

Merge decision needed: a one-target publication can block unrelated DROP ACCOUNT. With max-row-lock-count=16 and the same small catalog, head selected Table level lock for a one-ID locking read; the held shared range was visible, an unrelated existing account could not get an exclusive NOWAIT lock (3572), and a separate unrelated DROP waited until publication was released. The baseline publication did not acquire that lock and DROP completed before release. On head with the default 20,000 and the same catalog, the plan used a row lock and the unrelated DROP completed before release. A large catalog at the default threshold has not been tested. This is not data loss, and normal unrelated SELECTs were not shown to block.

The planner compares the lock node’s estimated output, not the size of the resolved ID set. The PR acquires the lock before source DB validation and can hold it across that independent wait. Recommendation: retain same-ID exclusion; before merging, demonstrate that a one-ID grant under the low threshold lets an unrelated DROP finish while still blocking DROP of the target, or explicitly accept the broader DDL blocking as a trade-off. Do not disable escalation globally. The independent pre-existing missing-grant interleaving is tracked separately in #32941.

@XuPeng-SH

XuPeng-SH commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 07d9e1073817b70848e6db52fd09c076d0d7e2c0

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 07d9e1073817b70848e6db52fd09c076d0d7e2c0

Assessment: request_changes

The shared account-ID lock prevents the reproduced stale grant. At a supported low lock threshold, the planner widens even a one-recipient grant to a table-wide lock, delaying DROP ACCOUNT for an unrelated tenant.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

  • P2 pkg/frontend/publication_subscription.go:2946: CREATE PUBLICATION targets one account with max-row-lock-count=16 and waits for its source database lock while another CN drops a different account.
    Impact: The unrelated DROP ACCOUNT waits behind the publication transaction because the shared lock covers all of mo_account. The low-threshold probe reproduced the wait. At the default threshold, the unrelated DROP completed while the publication still waited on its source database.
    Evidence: The catalog scan estimates 1,000 rows for a single ID, so pkg/sql/plan/build_util.go:82 promotes the shared lock before source database validation. EXPLAIN VERBOSE and the two-CN wait observation are retained in lock-scope-verbose.jsonl with receipt 1795088648882406157-2.json. Use exact primary-key locks for bounded recipient sets and keep the post-lock existence check so DROP of a selected recipient still conflicts.

Validation:

  • The committed two-CN stale-grant regression passed normally and under -race. The focused race run also passed publication and restore tests. See attempt-1/embed-normal.jsonl and attempt-1/focused-race.jsonl with receipts 1795088528847747580-2.json and 1795088632596011998-2.json.

  • The corrected low-threshold probe confirmed the table lock and unrelated DROP wait. The default-threshold subtest passed its DROP control in attempt-1/lock-scope.jsonl. The initial low-threshold assertion used plain EXPLAIN, which omits lock granularity.

  • The frontend suite passed after excluding Test_openNewFile_nilCtx, which tried to write into the read-only checkout. See frontend-normal.jsonl in the model-switch directory and receipt 1795088672355114183-2.json.

Limitations:

  • The default-threshold control used a small catalog. Large-catalog lock behavior at the default threshold remains unmeasured.

  • The stale-grant result does not establish the cause of the historical revoked rows in #32900. The missing ACCOUNT ALL grant in #32941 also occurs on the baseline.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 8660e4408463daaac20fd37c4cf978c43748e6a6

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 8660e4408463daaac20fd37c4cf978c43748e6a6

Assessment: request_changes

The exact account-ID locks prevent grants to deleted accounts and resolve the prior contention with unrelated account drops. The early existence check introduces a separate race in CREATE PUBLICATION IF NOT EXISTS.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

  • P2 pkg/frontend/publication_subscription.go:397: Run CREATE PUBLICATION IF NOT EXISTS for an existing name, specifying a table that does not yet exist in a different source database. After the early name lookup, drop the old publication and commit, then create the requested table before CREATE validates it.
    Impact: CREATE can return success while the publication is absent. The requested source was invalid while the old publication existed, and once the source became valid the statement needed to create the publication.
    Evidence: Line 316 reads the publication without locking it. Source validation runs later and can see newer commits in the read-committed transaction. Line 397 returns success using the earlier result. The merge base checked the publisher's name after source validation. Recheck the name there before returning success. If the name disappeared and the recipient locks were skipped, return a retryable conflict so the next attempt can acquire those locks before validating the source.

Validation:

  • Verified the frozen checkout, patch, merge base and pinned policy. See attempt-1/verify-inputs.log and receipt 1795254528118679344-2.json.

  • The focused frontend package passed, and TestDropAccountConcurrentPublicationGrant reached PASS. The combined command later timed out during the lock-scope test. See attempt-1/focused.log and receipt 1795254364477402317-2.json.

  • TestCreatePublicationExplicitTargetLockScope passed in isolation. It covered the existing-publication no-op and verified that unrelated drops completed while target drops waited for both 1 and 17 recipients. See attempt-1/lock-scope.log and receipt 1795254612785171364-2.json.

Limitations:

  • The conditional CREATE finding is based on source analysis. Both supplemental SQL probe runs reached their check deadlines before producing a test result. See attempt-1/conditional-publication-probe.log and attempt-1/conditional-publication-retry.log with receipts 1795254831949951480-2.json and 1795255198449460425-2.json.

  • The cause of the historical revoked rows in #32900 remains unverified. The concurrent account-creation gap in #32941 remains outside this fix.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Addressed the P2 in review 6004515836 in 4e30a13de5.

I reproduced the false success on 8660e44084 over two CNs using the MySQL protocol. The new regression pauses CN1 after it obtains the shared source database lock. CN0 drops the old publication, then creates the previously missing source table. On resuming, the original conditional CREATE returned success with zero publications and zero grants. The scheduling hook leaves the database's lock result untouched.

The early lookup now only determines whether to acquire recipient locks. After source validation, CREATE rechecks the name in the publisher's account. If the publication disappeared after recipient locks were skipped, the transaction returns retry code 20628. Under the same schedule, the fixed statement returned that code with both catalogs empty. Retrying the identical SQL created exactly one publication on the requested database and table, and exactly one active grant for the requested account.

Local race-enabled validation passed for the full frontend package, ten repetitions of the conditional-publication unit tests, all four two-CN lock-scope cases, and the separate concurrent account-drop/publication-grant regression.

@XuPeng-SH

XuPeng-SH commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 4e30a13de5aed0ae1250cdd7f69f384ae0572b89

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 4e30a13de5aed0ae1250cdd7f69f384ae0572b89

Assessment: request_changes

Reusing the account ID lock addresses the reproduced stale grant, and the conditional CREATE recheck closes the earlier false success. The new recipient lock path still rejects valid publication creation and snapshot replay when a shared range covers the named recipients.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

  • P2 pkg/frontend/publication_account_lock.go:72: At max-row-lock-count=4, create a publication for two accounts covered by another transaction's shared mo_account range, or restore a snapshot whose ACCOUNT ALL publication takes a shared range before a named publication is replayed in the same transaction.
    Impact: CREATE PUBLICATION and RESTORE CLUSTER both returned MySQL error 20707. The restore failed without concurrent SQL, so retrying the same replay order with the same lock settings hits the same guard.
    Evidence: pkg/lockservice/lock_table_local.go:1117 rejects an exact row request when the caller already holds the covering range. A request can first join another transaction's shared range through the code at line 1120, then hit that guard on its second recipient. Snapshot replay shares one transaction across publications at pkg/frontend/snapshot.go:4420. Preserve the named account lock scope when another transaction owns a range, and recognize compatible coverage already held by the replay transaction. Keep the post-lock recipient existence check.

Validation:

  • 120 selected frontend tests passed under the race detector. See frontend-publication-race.jsonl and receipt 1795283654042467758-2.json.

  • All four committed two-CN lock scope cases passed in the dedicated race run. The committed concurrent DROP and grant test also passed in the earlier combined run, which later timed out during setup of the lock scope test. See publication-lock-scope-race.jsonl and focused-race.jsonl with receipts 1795283961253721786-2.json and 1795283757889564645-2.json.

  • Both two-CN probes returned error 20707 on the PR head and passed when only publication_subscription.go was replaced with its merge base version. See publication-range-race.jsonl, publication-snapshot-race.jsonl, and publication-baseline-race.jsonl with receipts 1795283784694094202-2.json, 1795284024379650928-2.json, and 1795283971732753842-2.json. Logs and receipts are under attempt-2/retained-validation.

Limitations:

  • The shared range failures were reproduced with a lowered lock threshold. Their frequency at default settings was not measured.

  • The #32900 evidence covers the reproduced stale grant with status 0. The cause of the historical revoked QA rows and the separate account creation race in #32941 remain open.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Fixed in 93a36cc. I reproduced both failures under the race detector on 4e30a13 with two CNs and a row-lock threshold of 4. The named CREATE and the cluster restore each returned MySQL 20707.

The lock owner now makes the distinction at acquisition. An exact point request waits for a foreign covering range to release. A named publication can also reuse compatible coverage already held by its own transaction. It still checks the physical recipient IDs after locking, and it cannot widen the request on capacity exhaustion.

With the same SQL, the named CREATE waited for the foreign range to release, committed, and yielded exactly the two requested grants. After dropping both publications and restoring the cluster, I read back five active grants for ACCOUNT ALL and the two expected active grants for the named publication. A separate lock-service test verifies that a writer can take the gap between the named IDs while a writer on a named ID conflicts until unlock.

The 1- and 17-target CREATE lock-scope cases and the concurrent account DROP/publication grant regression passed. The scope test now checks that the unrelated DROP commits and that the account is absent while the publisher is still gated, so an unrelated CN-wide wait sample cannot fail it. The full lockservice, lock operator, and frontend packages passed with -race. Incremental vet and golangci-lint also passed.

@XuPeng-SH

XuPeng-SH commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 93a36cc7c227604cf4a2731cebad8072bc6462f9

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 93a36cc7c227604cf4a2731cebad8072bc6462f9

Assessment: approve

Publication writes now lock and revalidate recipient IDs using the same account keys as DROP ACCOUNT. The prior findings are resolved. Named grants retain exact lock scope, conditional CREATE rechecks publication existence before writing, and restore can reuse a covering lock it already owns.

Model: gpt-6.2-sol / xhigh

Session: 01a1f0a0-5126-78f3-9ab8-ef4e87f2f119 · slot 0 · generation 3

Validation:

  • Verified the frozen checkout, patch, and target merge. The generated LockOptions descriptor adds only optional bool field 16. See scope.log, target-merge-verification.log, and lock-descriptor.log.

  • The lock-service and SQL lock-operator suites passed under the race detector. Four focused exact-row tests also passed 100 repetitions each. See lock-race.jsonl and the four Test*-race.jsonl logs with their checks/ receipts.

  • The focused frontend race run passed 179 top-level tests. See frontend-race.jsonl and checks/1795319022192507323-2.json.

  • Three embedded two-CN race tests passed, covering concurrent DROP and ALTER, conditional CREATE and exact recipient scope, foreign covering-range waits, and restore with an owned covering range. See embed-race.jsonl and checks/1795319196684675217-2.json.

Limitations:

  • The full frontend race run failed in the unchanged Test_openNewFile_nilCtx fixture because it writes inside the read-only checkout. See frontend-package-race.jsonl and checks/1795319244814135458-2.json.

  • The fix covers the reproduced stale grant. The revoked-orphan QA cases in #32900 and the concurrent account-creation case in #32941 remain outside this review's scope.

@mergify

mergify Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request dequeued

1 h 50 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Sun 22 Nov, 03:48 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 05:38 · dca97a2

Reason

Pull request #32936 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

To get back in the queue

  • Push a change if one is needed: the pull request is queued again once its queue conditions match.
  • If no change is needed, tick the box below to queue it again as it is.

Merge conditions not met when it left the queue

  • any of · required by GitHub branch protection:
    • check-neutral = CI Required
    • check-skipped = CI Required
    • check-success = CI Required

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request
2 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection

Times in UTC · dashboard · how the merge queue works

This branch was successfully deployed

No deployments
ci — dca97a27 Deployed Nov 22, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DROP ACCOUNT 成功后仍残留引用已删除订阅账号的 mo_subs 记录

3 participants