Skip to content

session: ensure masking policy table before old upgrades#69738

Open
expxiaoli wants to merge 1 commit into
pingcap:masterfrom
expxiaoli:fix_69464_v2
Open

session: ensure masking policy table before old upgrades#69738
expxiaoli wants to merge 1 commit into
pingcap:masterfrom
expxiaoli:fix_69464_v2

Conversation

@expxiaoli

@expxiaoli expxiaoli commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #69464

Problem Summary:

During rolling upgrade, a newly started TiDB may repeatedly crash in bootstrap upgrade when upgrading from an old bootstrap version.

upgradeToVer190 can run reentrant MODIFY COLUMN DDLs before upgradeToVer260 creates mysql.tidb_masking_policy. However, the masking policy sync path used by ordinary DDL may query mysql.tidb_masking_policy. If the table does not exist yet, the DDL job fails with Table 'mysql.tidb_masking_policy' doesn't exist, and bootstrap upgrade exits fatally.

What changed and how does it work?

Before running the versioned bootstrap upgrade loop, this PR ensures mysql.tidb_masking_policy exists when the stored bootstrap version is older than version260.

The table is created with CREATE TABLE IF NOT EXISTS, so the operation is idempotent. This keeps the fix local to bootstrap upgrade ordering and avoids adding long-lived DDL context state.

This PR also extends the existing masking policy bootstrap upgrade test to cover upgrading from bootstrap version 189, which reproduces the actual failing upgradeToVer190 path. The existing 254 coverage is kept.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test

Executed:

  • GOPATH="$HOME/go" GOSUMDB=sum.golang.org ./tools/check/failpoint-go-test.sh pkg/session/test/bootstraptest -run 'TestUpgradeVersion260MaskingPolicy/from_version_189' -count=1
    • Verified the new regression case fails without the fix and passes with the fix.
  • GOPATH="$HOME/go" GOSUMDB=sum.golang.org ./tools/check/failpoint-go-test.sh pkg/session/test/bootstraptest -run TestUpgradeVersion260MaskingPolicy -count=1
  • GOPATH="$HOME/go" GOSUMDB=sum.golang.org ./tools/check/failpoint-go-test.sh pkg/session -run TestForIssue23387 -count=1
  • GOPATH="$HOME/go" GOSUMDB=sum.golang.org make bazel_prepare
    • No Bazel metadata files were changed.
  • GOPATH="$HOME/go" GOSUMDB=sum.golang.org make lint
  • git diff --check

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Fix an issue that TiDB might repeatedly crash during rolling upgrade when a bootstrap DDL runs before mysql.tidb_masking_policy is created.

Summary by CodeRabbit

  • Bug Fixes
    • Improved upgrade handling to ensure the masking policy system table is available during older-version bootstrap and upgrade flows.
    • Fixed masking-policy schema verification across multiple upgrade paths, helping confirm consistent behavior after re-bootstrap.

@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 9, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign tiancaiamao for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 132da1c6-8ebd-43b6-b3e4-164f80bc8847

📥 Commits

Reviewing files that changed from the base of the PR and between 60996fd and c389d19.

📒 Files selected for processing (2)
  • pkg/session/test/bootstraptest/bootstrap_upgrade_test.go
  • pkg/session/upgrade_run.go

📝 Walkthrough

Walkthrough

This change fixes a bootstrap upgrade crash by ensuring the tidb_masking_policy system table is created before earlier reentrant upgrade DDLs execute, when upgrading from versions prior to 260. The masking-policy upgrade test was extended to cover both version 189 and 254.

Changes

Masking policy table upgrade fix

Layer / File(s) Summary
Ensure masking policy table exists before upgrade DDLs
pkg/session/upgrade_run.go
Adds ensureTiDBMaskingPolicyTableBeforeUpgradeDDL, which creates mysql.tidb_masking_policy via metadef.CreateTiDBMaskingPolicyTable when ver < version260, called during upgrade() right after upgradeToVer99Before.
Test coverage for multiple bootstrap versions
pkg/session/test/bootstraptest/bootstrap_upgrade_test.go
Replaces the single fromVersion = 254 test run with a loop over {189, 254} in t.Run subtests, each verifying re-bootstrap and masking-policy table schema.

Estimated code review effort: 2 (Simple) | ~15 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Session
  participant upgrade
  participant ensureTiDBMaskingPolicyTableBeforeUpgradeDDL
  participant metadef

  Session->>upgrade: upgrade(s)
  upgrade->>upgrade: upgradeToVer99Before(s)
  upgrade->>ensureTiDBMaskingPolicyTableBeforeUpgradeDDL: ensureTiDBMaskingPolicyTableBeforeUpgradeDDL(s, ver)
  alt ver < version260
    ensureTiDBMaskingPolicyTableBeforeUpgradeDDL->>metadef: CreateTiDBMaskingPolicyTable
  else ver >= version260
    ensureTiDBMaskingPolicyTableBeforeUpgradeDDL-->>upgrade: return
  end
  upgrade->>upgrade: run reentrant upgrade DDLs (e.g. upgradeToVer190)
Loading

Suggested labels: sig/planner

Suggested reviewers: bb7133, yudongusa, Leavrth, D3Hunter

Poem

A table was missing, DDLs cried in vain,
So a rabbit hopped in to ease the pain 🐇
"Create it first!" she squeaked with glee,
Now upgrades run smooth as can be,
From version 189 clear to 254,
No more crash loops knocking at the door! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately summarizes the main change: ensuring the masking policy table before older upgrade paths.
Linked Issues check ✅ Passed The code adds the missing table before pre-version-260 upgrade DDLs and extends tests to cover the failing 189 upgrade path.
Out of Scope Changes check ✅ Passed The PR stays focused on bootstrap upgrade ordering and related test coverage, with no obvious unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.2987%. Comparing base (60996fd) to head (c389d19).

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #69738        +/-   ##
================================================
- Coverage   76.3225%   74.2987%   -2.0239%     
================================================
  Files          2041       2050         +9     
  Lines        560226     575276     +15050     
================================================
- Hits         427579     427423       -156     
- Misses       131746     147609     +15863     
+ Partials        901        244       -657     
Flag Coverage Δ
integration 41.0129% <0.0000%> (+1.3076%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 60.4471% <ø> (ø)
parser ∅ <ø> (∅)
br 47.4060% <ø> (-15.3154%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TiDB upgrade to master repeatedly crashes in upgradeToVer190 doReentrantDDL on tidb_masking_policy

1 participant