Skip to content

Commit df4a904

Browse files
authored
cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450) (#12457)
close #12449
1 parent da8d8b2 commit df4a904

File tree

6 files changed

+82
-120
lines changed

6 files changed

+82
-120
lines changed

cdc/entry/schema_storage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ func (s *schemaStorage) buildRenameEvents(
519519
tableInfo := model.WrapTableInfo(info.NewSchemaID, newSchemaName,
520520
job.BinlogInfo.FinishedTS, tableInfo)
521521
event.FromJobWithArgs(job, preTableInfo, tableInfo, oldSchemaName, newSchemaName)
522+
event.Seq = uint64(i)
522523
ddlEvents = append(ddlEvents, event)
523524
}
524525
return ddlEvents, nil

cdc/model/sink.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,10 @@ type DDLEvent struct {
10561056
// the DDL is executed by the primary cluster.
10571057
BDRRole string `msg:"-"`
10581058
SQLMode mysql.SQLMode `msg:"-"`
1059+
// Seq is used to order the DDLs with the same commit ts
1060+
// Only used in the splited DDLEvent generated by a multi-table DDL,
1061+
// we need to keep the order of the original multi-table DDL
1062+
Seq uint64 `msg:"seq"`
10591063
}
10601064

10611065
// FromJob fills the values with DDLEvent from DDL job

cdc/model/sink_gen.go

Lines changed: 43 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdc/owner/ddl_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ func (m *ddlManager) getNextDDL() *model.DDLEvent {
484484
delete(m.pendingDDLs, tb)
485485
continue
486486
}
487-
if res == nil || res.CommitTs > ddls[0].CommitTs {
487+
if res == nil || res.CommitTs > ddls[0].CommitTs || (res.CommitTs == ddls[0].CommitTs && res.Seq > ddls[0].Seq) {
488488
res = ddls[0]
489489
}
490490
}

tests/integration_tests/common_1/data/test.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,33 @@ VALUES (1),
175175
UPDATE `column_is_null`
176176
SET t = NULL
177177
WHERE id = 1;
178+
179+
-- rename tables DDL should keep the order of events
180+
181+
CREATE TABLE `rename_t3` (
182+
`id` INT PRIMARY KEY,
183+
`val` INT
184+
);
185+
186+
CREATE TABLE `rename_t1` (
187+
`id` INT PRIMARY KEY,
188+
`val` INT
189+
);
190+
191+
CREATE TABLE `rename_t2` (
192+
`id` INT PRIMARY KEY,
193+
`val` INT
194+
);
195+
196+
INSERT INTO `rename_t1` VALUES (1, 1);
197+
INSERT INTO `rename_t2` VALUES (1, 2);
198+
INSERT INTO `rename_t3` VALUES (1, 3);
199+
200+
RENAME TABLE
201+
`rename_t1` TO `rename_t4`,
202+
`rename_t2` TO `rename_t1`,
203+
`rename_t3` TO `rename_t2`;
204+
205+
INSERT INTO `rename_t4` VALUES (2, 4);
206+
INSERT INTO `rename_t1` VALUES (2, 1);
207+
INSERT INTO `rename_t2` VALUES (2, 2);

tests/integration_tests/common_1/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ EOF
8686
# sync_diff can't check non-exist table, so we check expected tables are created in downstream first
8787
check_table_exists common_1.v1 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
8888
check_table_exists common_1.recover_and_insert ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
89+
check_table_exists common_1.rename_t4 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
90+
check_table_exists common_1.rename_t1 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
91+
check_table_exists common_1.rename_t2 ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
8992
check_table_exists common_1.finish_mark ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT}
9093
check_sync_diff $WORK_DIR $CUR/conf/diff_config.toml
9194

0 commit comments

Comments
 (0)