cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)#12458
cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)#12458ti-chi-bot wants to merge 1 commit intopingcap:release-6.5from
Conversation
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
This cherry pick PR is for a release branch and has not yet been approved by triage owners. To merge this cherry pick:
DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
@wlwilliamx This PR has conflicts, I have hold it. |
|
@ti-chi-bot: ## If you want to know how to resolve it, please read the guide in TiDB Dev Guide. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository. |
Summary of ChangesHello @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 implements a crucial fix to ensure the strict ordering of Data Definition Language (DDL) events, particularly those originating from multi-table DDL statements like Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request aims to ensure strict ordering for multi-table DDLs by introducing a sequence number (Seq) to DDLEvent. This is a good approach to handle DDLs that share the same commit timestamp. However, the pull request contains several critical issues, including unresolved merge conflicts in multiple files, which will prevent the code from compiling. These must be addressed before merging.
| <<<<<<< HEAD | ||
| return !job.IsSynced() && !job.IsDone() | ||
| ======= | ||
| return !job.IsDone() | ||
| } | ||
|
|
||
| // BuildDDLEvents by parsing the DDL job | ||
| func (s *schemaStorage) BuildDDLEvents( | ||
| ctx context.Context, job *timodel.Job, | ||
| ) (ddlEvents []*model.DDLEvent, err error) { | ||
| switch job.Type { | ||
| case timodel.ActionRenameTables: | ||
| // The result contains more than one DDLEvent for a rename tables job. | ||
| ddlEvents, err = s.buildRenameEvents(ctx, job) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
| case timodel.ActionCreateTables: | ||
| if job.BinlogInfo != nil && job.BinlogInfo.MultipleTableInfos != nil { | ||
| querys, err := ddl.SplitQueries(job.Query) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
| multiTableInfos := job.BinlogInfo.MultipleTableInfos | ||
| for index, tableInfo := range multiTableInfos { | ||
| newTableInfo := model.WrapTableInfo(job.SchemaID, job.SchemaName, job.BinlogInfo.FinishedTS, tableInfo) | ||
| job.Query = querys[index] | ||
| event := new(model.DDLEvent) | ||
| event.FromJob(job, nil, newTableInfo) | ||
| ddlEvents = append(ddlEvents, event) | ||
| } | ||
| } else { | ||
| return nil, errors.Errorf("there is no multiple table infos in the create tables job: %s", job) | ||
| } | ||
| default: | ||
| // parse preTableInfo | ||
| preSnap, err := s.GetSnapshot(ctx, job.BinlogInfo.FinishedTS-1) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
| preTableInfo, err := preSnap.PreTableInfo(job) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
|
|
||
| // parse tableInfo | ||
| var tableInfo *model.TableInfo | ||
| err = preSnap.FillSchemaName(job) | ||
| if err != nil { | ||
| log.Error("build DDL event fail", zap.Any("job", job), zap.Error(err)) | ||
| return nil, errors.Trace(err) | ||
| } | ||
| // TODO: find a better way to refactor this. For example, drop table job should not | ||
| // have table info. | ||
| if job.BinlogInfo != nil && job.BinlogInfo.TableInfo != nil { | ||
| tableInfo = model.WrapTableInfo(job.SchemaID, job.SchemaName, job.BinlogInfo.FinishedTS, job.BinlogInfo.TableInfo) | ||
|
|
||
| // TODO: remove this after job is fixed by TiDB. | ||
| // ref: https://github.com/pingcap/tidb/issues/43819 | ||
| if job.Type == timodel.ActionExchangeTablePartition { | ||
| oldTableInfo, ok := preSnap.PhysicalTableByID(job.BinlogInfo.TableInfo.ID) | ||
| if !ok { | ||
| return nil, cerror.ErrSchemaStorageTableMiss.GenWithStackByArgs(job.TableID) | ||
| } | ||
| tableInfo.SchemaID = oldTableInfo.SchemaID | ||
| tableInfo.TableName = oldTableInfo.TableName | ||
| } | ||
| } else { | ||
| // Just retrieve the schema name for a DDL job that does not contain TableInfo. | ||
| // Currently supported by cdc are: ActionCreateSchema, ActionDropSchema, | ||
| // and ActionModifySchemaCharsetAndCollate. | ||
| tableInfo = &model.TableInfo{ | ||
| TableName: model.TableName{Schema: job.SchemaName}, | ||
| Version: job.BinlogInfo.FinishedTS, | ||
| } | ||
| } | ||
| event := new(model.DDLEvent) | ||
| event.FromJob(job, preTableInfo, tableInfo) | ||
| ddlEvents = append(ddlEvents, event) | ||
| } | ||
| return ddlEvents, nil | ||
| } | ||
|
|
||
| // GetNewJobWithArgs returns a new job with the given args | ||
| func GetNewJobWithArgs(job *timodel.Job, args timodel.JobArgs) (*timodel.Job, error) { | ||
| job.FillArgs(args) | ||
| bytes, err := job.Encode(true) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
| encodedJob := &timodel.Job{} | ||
| if err = encodedJob.Decode(bytes); err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
| return encodedJob, nil | ||
| } | ||
|
|
||
| // TODO: find a better way to refactor this function. | ||
| // buildRenameEvents gets a list of DDLEvent from a rename tables DDL job. | ||
| func (s *schemaStorage) buildRenameEvents( | ||
| ctx context.Context, job *timodel.Job, | ||
| ) ([]*model.DDLEvent, error) { | ||
| var ddlEvents []*model.DDLEvent | ||
| args, err := timodel.GetRenameTablesArgs(job) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
|
|
||
| multiTableInfos := job.BinlogInfo.MultipleTableInfos | ||
| if len(multiTableInfos) != len(args.RenameTableInfos) { | ||
| return nil, cerror.ErrInvalidDDLJob.GenWithStackByArgs(job.ID) | ||
| } | ||
|
|
||
| preSnap, err := s.GetSnapshot(ctx, job.BinlogInfo.FinishedTS-1) | ||
| if err != nil { | ||
| return nil, errors.Trace(err) | ||
| } | ||
|
|
||
| for i, tableInfo := range multiTableInfos { | ||
| info := args.RenameTableInfos[i] | ||
| newSchema, ok := preSnap.SchemaByID(info.NewSchemaID) | ||
| if !ok { | ||
| return nil, cerror.ErrSnapshotSchemaNotFound.GenWithStackByArgs( | ||
| info.NewSchemaID) | ||
| } | ||
| newSchemaName := newSchema.Name.O | ||
| oldSchemaName := info.OldSchemaName.O | ||
| event := new(model.DDLEvent) | ||
| preTableInfo, ok := preSnap.PhysicalTableByID(tableInfo.ID) | ||
| if !ok { | ||
| return nil, cerror.ErrSchemaStorageTableMiss.GenWithStackByArgs( | ||
| job.TableID) | ||
| } | ||
|
|
||
| tableInfo := model.WrapTableInfo(info.NewSchemaID, newSchemaName, | ||
| job.BinlogInfo.FinishedTS, tableInfo) | ||
| event.FromJobWithArgs(job, preTableInfo, tableInfo, oldSchemaName, newSchemaName) | ||
| event.Seq = uint64(i) | ||
| ddlEvents = append(ddlEvents, event) | ||
| } | ||
| return ddlEvents, nil | ||
| >>>>>>> 3c7fd0a1fd (cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)) | ||
| } |
There was a problem hiding this comment.
This file contains unresolved merge conflict markers (<<<<<<<, =======, >>>>>>>), which will cause a compilation failure. Please resolve the conflict.
Additionally, there are a couple of issues in the new code:
- The receiver for
BuildDDLEventsandbuildRenameEventsis*schemaStorage, which is an interface. This will cause a compilation error. It should probably be*schemaStorageImpl. - The function
BuildDDLEventscallsddl.SplitQueries, but theddlpackage (github.com/pingcap/tidb/ddl) is not imported.
Here is a suggested fix that resolves these issues.
return !job.IsDone()
}
// BuildDDLEvents by parsing the DDL job
func (s *schemaStorageImpl) BuildDDLEvents(
ctx context.Context, job *timodel.Job,
) (ddlEvents []*model.DDLEvent, err error) {
switch job.Type {
case timodel.ActionRenameTables:
// The result contains more than one DDLEvent for a rename tables job.
ddlEvents, err = s.buildRenameEvents(ctx, job)
if err != nil {
return nil, errors.Trace(err)
}
case timodel.ActionCreateTables:
if job.BinlogInfo != nil && job.BinlogInfo.MultipleTableInfos != nil {
querys, err := ddl.SplitQueries(job.Query)
if err != nil {
return nil, errors.Trace(err)
}
multiTableInfos := job.BinlogInfo.MultipleTableInfos
for index, tableInfo := range multiTableInfos {
newTableInfo := model.WrapTableInfo(job.SchemaID, job.SchemaName, job.BinlogInfo.FinishedTS, tableInfo)
job.Query = querys[index]
event := new(model.DDLEvent)
event.FromJob(job, nil, newTableInfo)
ddlEvents = append(ddlEvents, event)
}
} else {
return nil, errors.Errorf("there is no multiple table infos in the create tables job: %s", job)
}
default:
// parse preTableInfo
preSnap, err := s.GetSnapshot(ctx, job.BinlogInfo.FinishedTS-1)
if err != nil {
return nil, errors.Trace(err)
}
preTableInfo, err := preSnap.PreTableInfo(job)
if err != nil {
return nil, errors.Trace(err)
}
// parse tableInfo
var tableInfo *model.TableInfo
err = preSnap.FillSchemaName(job)
if err != nil {
log.Error("build DDL event fail", zap.Any("job", job), zap.Error(err))
return nil, errors.Trace(err)
}
// TODO: find a better way to refactor this. For example, drop table job should not
// have table info.
if job.BinlogInfo != nil && job.BinlogInfo.TableInfo != nil {
tableInfo = model.WrapTableInfo(job.SchemaID, job.SchemaName, job.BinlogInfo.FinishedTS, job.BinlogInfo.TableInfo)
// TODO: remove this after job is fixed by TiDB.
// ref: https://github.com/pingcap/tidb/issues/43819
if job.Type == timodel.ActionExchangeTablePartition {
oldTableInfo, ok := preSnap.PhysicalTableByID(job.BinlogInfo.TableInfo.ID)
if !ok {
return nil, cerror.ErrSchemaStorageTableMiss.GenWithStackByArgs(job.TableID)
}
tableInfo.SchemaID = oldTableInfo.SchemaID
tableInfo.TableName = oldTableInfo.TableName
}
} else {
// Just retrieve the schema name for a DDL job that does not contain TableInfo.
// Currently supported by cdc are: ActionCreateSchema, ActionDropSchema,
// and ActionModifySchemaCharsetAndCollate.
tableInfo = &model.TableInfo{
TableName: model.TableName{Schema: job.SchemaName},
Version: job.BinlogInfo.FinishedTS,
}
}
event := new(model.DDLEvent)
event.FromJob(job, preTableInfo, tableInfo)
ddlEvents = append(ddlEvents, event)
}
return ddlEvents, nil
}
// GetNewJobWithArgs returns a new job with the given args
func GetNewJobWithArgs(job *timodel.Job, args timodel.JobArgs) (*timodel.Job, error) {
job.FillArgs(args)
bytes, err := job.Encode(true)
if err != nil {
return nil, errors.Trace(err)
}
encodedJob := &timodel.Job{}
if err = encodedJob.Decode(bytes); err != nil {
return nil, errors.Trace(err)
}
return encodedJob, nil
}
// TODO: find a better way to refactor this function.
// buildRenameEvents gets a list of DDLEvent from a rename tables DDL job.
func (s *schemaStorageImpl) buildRenameEvents(
ctx context.Context, job *timodel.Job,
) ([]*model.DDLEvent, error) {
var ddlEvents []*model.DDLEvent
args, err := timodel.GetRenameTablesArgs(job)
if err != nil {
return nil, errors.Trace(err)
}
multiTableInfos := job.BinlogInfo.MultipleTableInfos
if len(multiTableInfos) != len(args.RenameTableInfos) {
return nil, cerror.ErrInvalidDDLJob.GenWithStackByArgs(job.ID)
}
preSnap, err := s.GetSnapshot(ctx, job.BinlogInfo.FinishedTS-1)
if err != nil {
return nil, errors.Trace(err)
}
for i, tableInfo := range multiTableInfos {
info := args.RenameTableInfos[i]
newSchema, ok := preSnap.SchemaByID(info.NewSchemaID)
if !ok {
return nil, cerror.ErrSnapshotSchemaNotFound.GenWithStackByArgs(
info.NewSchemaID)
}
newSchemaName := newSchema.Name.O
oldSchemaName := info.OldSchemaName.O
event := new(model.DDLEvent)
preTableInfo, ok := preSnap.PhysicalTableByID(tableInfo.ID)
if !ok {
return nil, cerror.ErrSchemaStorageTableMiss.GenWithStackByArgs(
job.TableID)
}
tableInfo := model.WrapTableInfo(info.NewSchemaID, newSchemaName,
job.BinlogInfo.FinishedTS, tableInfo)
event.FromJobWithArgs(job, preTableInfo, tableInfo, oldSchemaName, newSchemaName)
event.Seq = uint64(i)
ddlEvents = append(ddlEvents, event)
}
return ddlEvents, nil
}| <<<<<<< HEAD | ||
| ======= | ||
| IsBootstrap bool `msg:"-"` | ||
| // BDRRole is the role of the TiDB cluster, it is used to determine whether | ||
| // the DDL is executed by the primary cluster. | ||
| BDRRole string `msg:"-"` | ||
| SQLMode mysql.SQLMode `msg:"-"` | ||
| // 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"` | ||
| >>>>>>> 3c7fd0a1fd (cdc(ddl): ensure strict ordering for multi-table DDLs after split (#12450)) |
There was a problem hiding this comment.
This file contains unresolved merge conflict markers (<<<<<<<, =======, >>>>>>>), which will cause a compilation failure. Please resolve the conflict before merging.
IsBootstrap bool `msg:"-"`
// BDRRole is the role of the TiDB cluster, it is used to determine whether
// the DDL is executed by the primary cluster.
BDRRole string `msg:"-"`
SQLMode mysql.SQLMode `msg:"-"`
// 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"`|
@ti-chi-bot: The following tests failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
|
This pull request is closed because its related version has closed automatic cherry-picking. https://prow.tidb.net/command-help#cherrypick |
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
RENAMEstatement could be executed out of order downstream because they share the sameCommitTsand the order of ranging map is non-deterministic.Check List
Tests
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