Skip to content

ddl: reject FLASHBACK CLUSTER when affinity groups would no longer match - #72690

Open
JmPotato wants to merge 1 commit into
pingcap:masterfrom
JmPotato:fix-affinity-flashback-cluster
Open

JmPotato wants to merge 1 commit into
pingcap:masterfrom
JmPotato:fix-affinity-flashback-cluster

Conversation

@JmPotato

@JmPotato JmPotato commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: close #72058

Problem Summary:

FLASHBACK CLUSTER restores the table metadata to an earlier point but does not restore the affinity groups in PD. If tables gained, lost or changed affinity groups in between, for example through CREATE TABLE, DROP TABLE, TRUNCATE or ALTER TABLE ... AFFINITY, the groups in PD no longer match the restored metadata after the flashback.

What changed and how does it work?

  • Before the flashback, TiDB derives the affinity group IDs expected by all tables at the flashback point and now, and rejects the flashback if the two sets differ: Detected affinity changes of table <table> during [<ts>, now), can't do flashback.
  • It compares the net state of the two points rather than the DDL history in between. DDL that does not change the groups, such as ADD COLUMN on an affinity table, does not block it, and neither does a group created and deleted again in between.
  • If the metadata at the flashback point cannot be read, the flashback is rejected as well.

Boundaries:

  • The check reads and decodes the metadata of all tables at both points to derive the group sets, so its time grows with the number of tables; it has not been measured.
  • It compares metadata only. It does not read the groups in PD, so it neither detects nor repairs groups that already diverge from the metadata before the flashback.
  • The behavior does not depend on ddl, domain: test affinity DDL against a PD without affinity API #72677.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Manual test on a local cluster with TiKV v8.5.8 and PD master, running a tidb-server built from this PR:

  1. CREATE TABLE c2a ... AFFINITY after ts1, then FLASHBACK CLUSTER TO TIMESTAMP ts1 is rejected with Detected affinity changes of table rec.c2a during [...], can't do flashback.
  2. ALTER TABLE tp2 TRUNCATE PARTITION p0 on a partition-level affinity table after ts2, then flashback to ts2 is rejected for rec.tp2.
  3. ALTER TABLE t1 ADD COLUMN b INT on an affinity table after ts3, then flashback to ts3 succeeds; column b is gone, the table keeps AFFINITY, and the groups in PD match the tables.

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.

Reject `FLASHBACK CLUSTER` when the table affinity changed after the flashback point, to prevent the affinity groups in PD from no longer matching the restored tables.

Summary by CodeRabbit

  • Bug Fixes
    • Cluster flashback now rejects restores when affinity group metadata differs between the historical and current snapshots, including changes caused by creating or dropping groups, assigning affinity, or truncating a table or partition. Compatible changes, such as adding a column or creating a table without affinity, can still be restored. Errors identify the affected table and flashback time range.

@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 Nov 4, 2026
@coderabbitai

coderabbitai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

FLASHBACK CLUSTER derives affinity group IDs from historical and current table metadata. It rejects flashback when the sets differ. Tests cover DDL that changes or preserves affinity groups.

Changes: Flashback affinity validation

Layer / File(s) Summary
Derive and compare affinity groups
pkg/ddl/affinity.go
Metadata helpers derive expected affinity group IDs and compare the historical and current sets. They return errors if metadata scanning fails or the sets differ.
Guard flashback and test DDL cases
pkg/ddl/cluster.go, pkg/ddl/cluster_test.go
The cluster flashback path invokes the affinity group check and returns any error. Tests cover DDL that changes affinity groups and cases that preserve them.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: d3hunter, lhy1024

Merge Risk: Minimal · up to 193a4

FLASHBACK CLUSTER now refuses to run when table metadata at the target time implies different affinity groups than the current metadata. This prevents the cluster from being restored to a state that no longer matches PD. No actionable merge-blocking risk was found. The cost of scanning all table metadata on large clusters has not been measured.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: FLASHBACK CLUSTER is rejected when affinity groups would no longer match. The raw summary and PR objectives both describe this check on affinity group sets before flas…
Description check Passed The description follows the repository template. It has an issue link (close #72058), a problem summary, a 'What changed and how does it work?' section with boundaries, a checklist with unit and manua…
Linked Issues check Passed Issue #72058 requires that, after FLASHBACK CLUSTER, PD affinity groups match the restored table metadata. The reported failure comes from affinity DDL (CREATE, DROP, TRUNCATE, ALTER ... AFFINITY) tha…
Out of Scope Changes check Passed The PR changes three areas, and each connects to #72058. pkg/ddl/affinity.go adds expectedAffinityGroups and checkFlashbackAffinityGroups, which perform the metadata comparison the issue requires. pkg…
Full details: Title check

Explanation

The title names the main change: FLASHBACK CLUSTER is rejected when affinity groups would no longer match. The raw summary and PR objectives both describe this check on affinity group sets before flashback. The title follows the repository's 'pkg: what's changed' format.

Full details: Description check

Explanation

The description follows the repository template. It has an issue link (close #72058), a problem summary, a 'What changed and how does it work?' section with boundaries, a checklist with unit and manual tests marked, documentation impact marked, and a release note. The integration test box is unchecked, which the template allows because at least one test type is required and unit tests are checked. The PR title format is not part of the description itself, so no change is needed there.

Full details: Linked Issues check

Explanation

Issue #72058 requires that, after FLASHBACK CLUSTER, PD affinity groups match the restored table metadata. The reported failure comes from affinity DDL (CREATE, DROP, TRUNCATE, ALTER ... AFFINITY) that runs after the target timestamp. At the reviewed head, pkg/ddl/affinity.go defines expectedAffinityGroups, which collects the group IDs implied by table metadata at a given snapshot, and checkFlashbackAffinityGroups, which compares the flashback-point set with the current set. It returns 'Detected affinity changes of table <table> during [<ts>, now), can't do flashback' for any difference in either direction, and returns an error when either metadata scan fails. Comparing sets gives the net-state behavior described in the PR: a group created and deleted again in between does not block flashback, and ADD COLUMN does not block flashback. The previous assessment reported that pkg/ddl/cluster.go calls this check in checkAndSetFlashbackClusterInfo before the flashback job is created, and that TestFlashbackClusterWithAffinityChanges covers the expected reject and accept cases. The incremental diff since the previous reviewed head could not be read, so the cluster.go call and the test file were not re-read at the current head. The previous conclusion is kept on that basis. The PR does not implement the background reconcile in #72061. The issue cites that work as a separate proposal, so it is not a requirement here. Limit: the check does not read PD, so pre-existing drift is not detected. The issue's scenario does not require that.

Full details: Out of Scope Changes check

Explanation

The PR changes three areas, and each connects to #72058. pkg/ddl/affinity.go adds expectedAffinityGroups and checkFlashbackAffinityGroups, which perform the metadata comparison the issue requires. pkg/ddl/cluster.go adds a short call to that check in checkAndSetFlashbackClusterInfo, which places the check on the flashback path. pkg/ddl/cluster_test.go adds TestFlashbackClusterWithAffinityChanges, which tests the new check. The affinity.go content at the reviewed head matches this description, and it contains no unrelated changes. The PR does not change isFlashbackSupportedDDLAction or the PD access paths. The incremental diff since the previous reviewed head could not be read, so the cluster.go and test changes were not re-inspected at the current head. Minor coverage gap, not a scope issue: the net-zero case and the historical metadata read failure path are described in the PR but are not confirmed by the added test in the available evidence.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

golangci-lint (2.15.0)

golangci-lint failed without an exit code
golangci-lint (2.15.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

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

A rabbit checks the groups at night
Through older tables, IDs gleam bright
If sets diverge, the flashback waits
The tests check columns, tables, gates
Then hops away beneath the moon

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5e1b6b0f-3f77-4c57-92cf-00ad1ceb7f95
Commits

Reviewing files that changed from the base of the PR and between 39993d3 and 193a45e.

Files selected for processing: 3

  • pkg/ddl/affinity.go
  • pkg/ddl/cluster.go
  • pkg/ddl/cluster_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@ti-chi-bot

ti-chi-bot Bot commented Nov 4, 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 wjhuang2016 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

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.5907%. Comparing base (0103aff) to head (193a45e).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #72690        +/-   ##
================================================
+ Coverage   72.1875%   73.5907%   +1.4031%     
================================================
  Files          2173       2221        +48     
  Lines        602622     614186     +11564     
================================================
+ Hits         435018     451984     +16966     
+ Misses       167602     162197      -5405     
- Partials          2          5         +3     
Flag Coverage Δ
integration 51.8760% <0.0000%> (+5.8493%) ⬆️

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

Components Coverage Δ
dumpling 59.9240% <ø> (ø)
parser ∅ <ø> (∅)
br 60.8079% <ø> (+17.4969%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

FLASHBACK CLUSTER restores the table metadata to an earlier point but
does not restore the affinity groups in PD. If tables gained, lost or
changed affinity groups in between, for example through CREATE TABLE,
DROP TABLE, TRUNCATE or ALTER TABLE ... AFFINITY, the groups in PD no
longer match the restored metadata after the flashback.

Before the flashback, TiDB now derives the affinity group IDs expected
by all tables at the flashback point and now, and rejects the flashback
if the two sets differ. It compares the net state of the two points
rather than the DDL history in between, so DDL that does not change the
groups, such as ADD COLUMN on an affinity table, does not block it, and
a group created and deleted again in between does not either. Reading
the metadata at the flashback point failing also rejects the flashback.

Fixes pingcap#72058

Signed-off-by: JmPotato <github@ipotato.me>
@JmPotato
JmPotato force-pushed the fix-affinity-flashback-cluster branch from 39993d3 to 193a45e Compare November 26, 2026 03:00

This branch has not been deployed

No deployments
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.

ddl: FLASHBACK CLUSTER leaves affinity groups inconsistent with restored metadata

1 participant