Skip to content

cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)#12457

Merged
ti-chi-bot[bot] merged 6 commits intopingcap:release-8.5from
ti-chi-bot:cherry-pick-12450-to-release-8.5
Dec 11, 2025
Merged

cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)#12457
ti-chi-bot[bot] merged 6 commits intopingcap:release-8.5from
ti-chi-bot:cherry-pick-12450-to-release-8.5

Conversation

@ti-chi-bot
Copy link
Member

This is an automated cherry-pick of #12450

What problem does this PR solve?

Issue Number: close #12449

What is changed and how it works?

This PR addresses an issue where split DDLs from a multi-table RENAME statement could be executed out of order downstream because they share the same CommitTs and the order of ranging map is non-deterministic.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test

Questions

Will it cause performance regression or break compatibility?

None

Do you need to update user documentation, design documentation or monitoring documentation?

None

Release note

Fix the incorrect execution order of split DDLs generated from a multi-table DDL statement (e.g., RENAME TABLE).

@ti-chi-bot ti-chi-bot added lgtm 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. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR. labels Dec 10, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @ti-chi-bot, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical data consistency problem in the Change Data Capture (CDC) system where DDL operations affecting multiple tables, such as RENAME TABLE, could be processed in an unpredictable order by downstream consumers. By introducing a sequential identifier for DDL events that share the same commit timestamp, the system now guarantees that these complex DDLs are applied in their intended order, thereby preventing potential data integrity issues and ensuring reliable replication.

Highlights

  • DDL Ordering Fix: Addressed an issue where split DDLs from multi-table RENAME statements could execute out of order downstream due to shared CommitTs and non-deterministic map iteration.
  • Seq Field Introduction: A new Seq (sequence) field was added to the DDLEvent struct to provide a deterministic ordering for DDLs that share the same CommitTs.
  • Seq Assignment: The Seq field is populated in cdc/entry/schema_storage.go during the generation of DDL events for multi-table operations like RENAME TABLE.
  • DDL Manager Logic Update: The ddlManager in cdc/owner/ddl_manager.go now considers the Seq field when selecting the next DDL to process, ensuring strict ordering for events with identical CommitTs.
  • Serialization Updates: The msgp serialization and deserialization logic for DDLEvent and RedoDDLEvent in cdc/model/sink_gen.go has been updated to correctly handle the new Seq field.
  • Integration Test Coverage: New integration tests were added in tests/integration_tests/common_1/data/test.sql and tests/integration_tests/common_1/run.sh to validate the correct ordering of multi-table RENAME DDLs.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively resolves the issue of out-of-order execution for split DDLs originating from multi-table RENAME statements. The solution introduces a Seq field to the DDLEvent structure, which is then used to enforce strict ordering when multiple DDLs share the same CommitTs. The changes are implemented by assigning the sequence number during DDL event building in schema_storage.go and incorporating this Seq field into the DDL selection logic in ddl_manager.go. Additionally, the msgp serialization code has been updated, and a dedicated integration test has been added to validate the fix.

continue
}
if res == nil || res.CommitTs > ddls[0].CommitTs {
if res == nil || res.CommitTs > ddls[0].CommitTs || (res.CommitTs == ddls[0].CommitTs && res.Seq > ddls[0].Seq) {

Choose a reason for hiding this comment

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

high

The modification to include (res.CommitTs == ddls[0].CommitTs && res.Seq > ddls[0].Seq) in the DDL selection logic is crucial. This ensures that when multiple DDLs share the same CommitTs, they are processed in the strict order defined by their Seq field, preventing out-of-order execution for multi-table DDLs.

func (z DDLEvent) Msgsize() (s int) {
s = 1 + 9 + msgp.Uint64Size + 10 + msgp.Uint64Size + 6 + msgp.StringPrefixSize + len(z.Query)
func (z *DDLEvent) Msgsize() (s int) {
s = 1 + 9 + msgp.Uint64Size + 10 + msgp.Uint64Size + 6 + msgp.StringPrefixSize + len(z.Query) + 4 + msgp.Uint64Size

Choose a reason for hiding this comment

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

medium

The Msgsize calculation for DDLEvent has been correctly updated to include the size of the new Seq field.

}
// write "query"
err = en.Append(0xa5, 0x71, 0x75, 0x65, 0x72, 0x79)
err = z.DDL.EncodeMsg(en)

Choose a reason for hiding this comment

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

medium

Refactoring RedoDDLEvent.EncodeMsg to delegate to z.DDL.EncodeMsg(en) is a good improvement. It reduces code duplication and makes the generated code cleaner and easier to maintain as DDLEvent fields change.

func (z DDLEvent) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 3
func (z *DDLEvent) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 4

Choose a reason for hiding this comment

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

medium

The map header size for DDLEvent encoding has been correctly updated from 3 to 4 to account for the new Seq field.

Comment on lines +573 to +582
// write "seq"
err = en.Append(0xa3, 0x73, 0x65, 0x71)
if err != nil {
return
}
err = en.WriteUint64(z.Seq)
if err != nil {
err = msgp.WrapError(err, "Seq")
return
}

Choose a reason for hiding this comment

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

medium

The Seq field is correctly appended during the encoding of DDLEvents, ensuring it is included in the serialized message.

}
var zb0002 uint32
zb0002, bts, err = msgp.ReadMapHeaderBytes(bts)
bts, err = z.DDL.UnmarshalMsg(bts)

Choose a reason for hiding this comment

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

medium

Refactoring RedoDDLEvent.UnmarshalMsg to delegate to z.DDL.UnmarshalMsg(bts) is a good improvement. It reduces code duplication and makes the generated code cleaner and easier to maintain as DDLEvent fields change.

s += msgp.NilSize
} else {
s += 1 + 9 + msgp.Uint64Size + 10 + msgp.Uint64Size + 6 + msgp.StringPrefixSize + len(z.DDL.Query)
s += z.DDL.Msgsize()

Choose a reason for hiding this comment

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

medium

Refactoring RedoDDLEvent.Msgsize to use z.DDL.Msgsize() is a good improvement. It reduces code duplication and makes the generated code cleaner and easier to maintain as DDLEvent fields change.

Comment on lines +1059 to +1062
// Seq is used to order the DDLs with the same commit ts
// Only used in the splited DDLEvent generated by a multi-table DDL,
// we need to keep the order of the original multi-table DDL
Seq uint64 `msg:"seq"`

Choose a reason for hiding this comment

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

medium

The addition of the Seq field to DDLEvent is a clear and effective way to ensure deterministic ordering for split DDLs with the same CommitTs. The comments clearly explain its purpose.

Comment on lines +179 to +207
-- rename tables DDL should keep the order of events

CREATE TABLE `rename_t3` (
`id` INT PRIMARY KEY,
`val` INT
);

CREATE TABLE `rename_t1` (
`id` INT PRIMARY KEY,
`val` INT
);

CREATE TABLE `rename_t2` (
`id` INT PRIMARY KEY,
`val` INT
);

INSERT INTO `rename_t1` VALUES (1, 1);
INSERT INTO `rename_t2` VALUES (1, 2);
INSERT INTO `rename_t3` VALUES (1, 3);

RENAME TABLE
`rename_t1` TO `rename_t4`,
`rename_t2` TO `rename_t1`,
`rename_t3` TO `rename_t2`;

INSERT INTO `rename_t4` VALUES (2, 4);
INSERT INTO `rename_t1` VALUES (2, 1);
INSERT INTO `rename_t2` VALUES (2, 2);

Choose a reason for hiding this comment

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

medium

The added integration test case for RENAME TABLE statements is valuable. It directly verifies that the fix correctly handles multi-table DDLs and maintains the expected order of events downstream.

Comment on lines +89 to +91
check_table_exists common_1.rename_t4 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_table_exists common_1.rename_t1 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
check_table_exists common_1.rename_t2 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}

Choose a reason for hiding this comment

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

medium

Adding check_table_exists for the newly renamed tables (rename_t4, rename_t1, rename_t2) in the run.sh script is a necessary step to ensure the integration test correctly validates the DDL execution. This confirms that the tables are created and renamed as expected in the downstream.

@codecov
Copy link

codecov bot commented Dec 10, 2025

Codecov Report

❌ Patch coverage is 43.18182% with 25 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release-8.5@da8d8b2). Learn more about missing BASE report.

Additional details and impacted files
Components Coverage Δ
cdc 59.6648% <0.0000%> (?)
dm 50.0308% <0.0000%> (?)
engine 53.2110% <0.0000%> (?)
Flag Coverage Δ
unit 55.1725% <43.1818%> (?)

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

@@               Coverage Diff                @@
##             release-8.5     #12457   +/-   ##
================================================
  Coverage               ?   55.1725%           
================================================
  Files                  ?       1003           
  Lines                  ?     136858           
  Branches               ?          0           
================================================
  Hits                   ?      75508           
  Misses                 ?      55800           
  Partials               ?       5550           
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ti-chi-bot
Copy link
Contributor

ti-chi-bot bot commented Dec 11, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wlwilliamx

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

The pull request process is described 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

@ti-chi-bot ti-chi-bot bot added approved cherry-pick-approved Cherry pick PR approved by release team. and removed do-not-merge/cherry-pick-not-approved labels Dec 11, 2025
@ti-chi-bot ti-chi-bot bot merged commit df4a904 into pingcap:release-8.5 Dec 11, 2025
23 of 26 checks passed
@ti-chi-bot ti-chi-bot bot deleted the cherry-pick-12450-to-release-8.5 branch December 11, 2025 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved cherry-pick-approved Cherry pick PR approved by release team. lgtm 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. type/cherry-pick-for-release-8.5 This PR is cherry-picked to release-8.5 from a source PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants