Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: refactor V2 args for RebaseAutoID/Modify-table/Index-visibility/FK related DDL. #56164

Merged
merged 19 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 60 additions & 22 deletions pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2007,12 +2007,27 @@ func (e *executor) multiSchemaChange(ctx sessionctx.Context, ti ast.Ident, info
Mode: model.SharedInvolving,
})
case model.ActionAddForeignKey:
ref, ok := j.Args[0].(*model.FKInfo)
if !ok {
logFn("unexpected type of foreign key info",
zap.Any("args[0]", j.Args[0]),
zap.String("type", fmt.Sprintf("%T", j.Args[0])))
continue
var (
ref *model.FKInfo
ok bool
)
if j.Version == model.JobVersion1 {
ref, ok = j.Args[0].(*model.FKInfo)
if !ok {
logFn("unexpected type of foreign key info",
zap.Any("args[0]", j.Args[0]),
zap.String("type", fmt.Sprintf("%T", j.Args[0])))
continue
}
} else {
args, ok := j.Args[0].(*model.AddForeignKeyArgs)
if !ok {
logFn("unexpected type of foreign key info",
zap.Any("args[0]", j.Args[0]),
zap.String("type", fmt.Sprintf("%T", j.Args[0])))
continue
}
ref = args.FkInfo
}
involvingSchemaInfo = append(involvingSchemaInfo, model.InvolvingSchemaInfo{
Database: ref.RefSchema.L,
Expand Down Expand Up @@ -2132,17 +2147,23 @@ func (e *executor) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase
newBase = newBaseTemp
}
job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tbInfo.ID,
SchemaName: schema.Name.L,
TableName: tbInfo.Name.L,
Type: actionType,
BinlogInfo: &model.HistoryInfo{},
Args: []any{newBase, force},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}
err = e.DoDDLJob(ctx, job)
args := &model.RebaseAutoIDArgs{
NewBase: newBase,
Force: force,
}
// need fill args, the job will be pushed subjob.
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down Expand Up @@ -3525,18 +3546,19 @@ func (e *executor) AlterTableComment(ctx sessionctx.Context, ident ast.Ident, sp
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tb.Meta().ID,
SchemaName: schema.Name.L,
TableName: tb.Meta().Name.L,
Type: model.ActionModifyTableComment,
BinlogInfo: &model.HistoryInfo{},
Args: []any{spec.Comment},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}

err = e.DoDDLJob(ctx, job)
args := &model.ModifyTableCommentArgs{Comment: spec.Comment}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down Expand Up @@ -3607,17 +3629,24 @@ func (e *executor) AlterTableCharsetAndCollate(ctx sessionctx.Context, ident ast
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tb.Meta().ID,
SchemaName: schema.Name.L,
TableName: tb.Meta().Name.L,
Type: model.ActionModifyTableCharsetAndCollate,
BinlogInfo: &model.HistoryInfo{},
Args: []any{toCharset, toCollate, needsOverwriteCols},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}
err = e.DoDDLJob(ctx, job)

args := &model.ModifyTableCharsetAndCollateArgs{
ToCharset: toCharset,
ToCollate: toCollate,
NeedsOverwriteCols: needsOverwriteCols,
}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down Expand Up @@ -4949,13 +4978,13 @@ func (e *executor) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: t.Meta().ID,
SchemaName: schema.Name.L,
TableName: t.Meta().Name.L,
Type: model.ActionAddForeignKey,
BinlogInfo: &model.HistoryInfo{},
Args: []any{fkInfo, fkCheck},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
InvolvingSchemaInfo: []model.InvolvingSchemaInfo{
{
Expand All @@ -4970,8 +4999,12 @@ func (e *executor) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName
},
SQLMode: ctx.GetSessionVars().SQLMode,
}

err = e.DoDDLJob(ctx, job)
args := &model.AddForeignKeyArgs{
FkInfo: fkInfo,
FkCheck: fkCheck,
}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand All @@ -4988,19 +5021,20 @@ func (e *executor) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName p
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: t.Meta().ID,
SchemaName: schema.Name.L,
SchemaState: model.StatePublic,
TableName: t.Meta().Name.L,
Type: model.ActionDropForeignKey,
BinlogInfo: &model.HistoryInfo{},
Args: []any{fkName},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}

err = e.DoDDLJob(ctx, job)
args := &model.DropForeignKeyArgs{FkName: fkName}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down Expand Up @@ -5608,18 +5642,22 @@ func (e *executor) AlterIndexVisibility(ctx sessionctx.Context, ident ast.Ident,
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: schema.ID,
TableID: tb.Meta().ID,
SchemaName: schema.Name.L,
TableName: tb.Meta().Name.L,
Type: model.ActionAlterIndexVisibility,
BinlogInfo: &model.HistoryInfo{},
Args: []any{indexName, invisible},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}

err = e.DoDDLJob(ctx, job)
args := &model.AlterIndexVisibilityArgs{
IndexName: indexName,
Invisible: invisible,
}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down
17 changes: 8 additions & 9 deletions pkg/ddl/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,34 @@ func (w *worker) onCreateForeignKey(jobCtx *jobContext, t *meta.Meta, job *model
return ver, errors.Trace(err)
}

var fkInfo model.FKInfo
var fkCheck bool
err = job.DecodeArgs(&fkInfo, &fkCheck)
args, err := model.GetAddForeignKeyArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
fkInfo, fkCheck := args.FkInfo, args.FkCheck

if job.IsRollingback() {
return dropForeignKey(jobCtx, t, job, tblInfo, fkInfo.Name)
}
switch job.SchemaState {
case model.StateNone:
err = checkAddForeignKeyValidInOwner(jobCtx.infoCache, job.SchemaName, tblInfo, &fkInfo, fkCheck)
err = checkAddForeignKeyValidInOwner(jobCtx.infoCache, job.SchemaName, tblInfo, fkInfo, fkCheck)
if err != nil {
job.State = model.JobStateCancelled
return ver, err
}
fkInfo.State = model.StateWriteOnly
fkInfo.ID = allocateFKIndexID(tblInfo)
tblInfo.ForeignKeys = append(tblInfo.ForeignKeys, &fkInfo)
tblInfo.ForeignKeys = append(tblInfo.ForeignKeys, fkInfo)
ver, err = updateVersionAndTableInfo(jobCtx, t, job, tblInfo, true)
if err != nil {
return ver, errors.Trace(err)
}
job.SchemaState = model.StateWriteOnly
return ver, nil
case model.StateWriteOnly:
err = checkForeignKeyConstrain(w, job.SchemaName, tblInfo.Name.L, &fkInfo, fkCheck)
err = checkForeignKeyConstrain(w, job.SchemaName, tblInfo.Name.L, fkInfo, fkCheck)
if err != nil {
job.State = model.JobStateRollingback
return ver, err
Expand Down Expand Up @@ -99,13 +99,12 @@ func onDropForeignKey(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int
return ver, errors.Trace(err)
}

var fkName pmodel.CIStr
err = job.DecodeArgs(&fkName)
args, err := model.GetDropForeignKeyArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
return dropForeignKey(jobCtx, t, job, tblInfo, fkName)
return dropForeignKey(jobCtx, t, job, tblInfo, args.FkName)
}

func dropForeignKey(jobCtx *jobContext, t *meta.Meta, job *model.Job, tblInfo *model.TableInfo, fkName pmodel.CIStr) (ver int64, err error) {
Expand Down
14 changes: 10 additions & 4 deletions pkg/ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,40 @@ func testCreateForeignKey(t *testing.T, d ddl.ExecutorForTest, ctx sessionctx.Co
}

job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: dbInfo.ID,
SchemaName: dbInfo.Name.L,
TableID: tblInfo.ID,
TableName: tblInfo.Name.L,
Type: model.ActionAddForeignKey,
BinlogInfo: &model.HistoryInfo{},
Args: []any{fkInfo},
}
err := sessiontxn.NewTxn(context.Background(), ctx)
require.NoError(t, err)
ctx.SetValue(sessionctx.QueryString, "skip")
err = d.DoDDLJobWrapper(ctx, ddl.NewJobWrapper(job, true))

args := &model.AddForeignKeyArgs{FkInfo: fkInfo}
job.FillArgs(args)
err = d.DoDDLJobWrapper(ctx, ddl.NewJobWrapperWithArgs(job, args, true))
require.NoError(t, err)
return job
}

func testDropForeignKey(t *testing.T, ctx sessionctx.Context, d ddl.ExecutorForTest, dbInfo *model.DBInfo, tblInfo *model.TableInfo, foreignKeyName string) *model.Job {
job := &model.Job{
Version: model.GetJobVerInUse(),
SchemaID: dbInfo.ID,
SchemaName: dbInfo.Name.L,
TableID: tblInfo.ID,
TableName: tblInfo.Name.L,
Type: model.ActionDropForeignKey,
BinlogInfo: &model.HistoryInfo{},
Args: []any{pmodel.NewCIStr(foreignKeyName)},
Args: []any{},
joccau marked this conversation as resolved.
Show resolved Hide resolved
}
ctx.SetValue(sessionctx.QueryString, "skip")
err := d.DoDDLJobWrapper(ctx, ddl.NewJobWrapper(job, true))
args := &model.DropForeignKeyArgs{FkName: pmodel.NewCIStr(foreignKeyName)}
job.FillArgs(args)
err := d.DoDDLJobWrapper(ctx, ddl.NewJobWrapperWithArgs(job, args, true))
require.NoError(t, err)
v := getSchemaVer(t, ctx)
checkHistoryJobArgs(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
Expand Down
4 changes: 3 additions & 1 deletion pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1327,10 +1327,12 @@ func checkAlterIndexVisibility(t *meta.Meta, job *model.Job) (*model.TableInfo,
return nil, indexName, invisible, errors.Trace(err)
}

if err := job.DecodeArgs(&indexName, &invisible); err != nil {
args, err := model.GetAlterIndexVisibilityArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return nil, indexName, invisible, errors.Trace(err)
}
indexName, invisible = args.IndexName, args.Invisible

skip, err := validateAlterIndexVisibility(nil, indexName, invisible, tblInfo)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions pkg/ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func appendToSubJobs(m *model.MultiSchemaInfo, jobW *JobWrapper) error {
reorgTp = jobW.ReorgMeta.ReorgTp
}
m.SubJobs = append(m.SubJobs, &model.SubJob{
Version: jobW.Version,
Type: jobW.Type,
Args: jobW.Args,
RawArgs: jobW.RawArgs,
Expand Down Expand Up @@ -251,11 +252,22 @@ func fillMultiSchemaInfo(info *model.MultiSchemaInfo, job *JobWrapper) error {
col := job.Args[0].(*table.Column)
info.ModifyColumns = append(info.ModifyColumns, col.Name)
case model.ActionAlterIndexVisibility:
idxName := job.Args[0].(pmodel.CIStr)
var idxName pmodel.CIStr
if job.Version == model.JobVersion1 {
idxName = job.Args[0].(pmodel.CIStr)
} else {
idxName = job.Args[0].(*model.AlterIndexVisibilityArgs).IndexName
}
joccau marked this conversation as resolved.
Show resolved Hide resolved
info.AlterIndexes = append(info.AlterIndexes, idxName)
case model.ActionRebaseAutoID, model.ActionModifyTableComment, model.ActionModifyTableCharsetAndCollate:
case model.ActionAddForeignKey:
fkInfo := job.Args[0].(*model.FKInfo)
var fkInfo *model.FKInfo
if job.Version == model.JobVersion1 {
fkInfo = job.Args[0].(*model.FKInfo)
} else {
args := job.Args[0].(*model.AddForeignKeyArgs)
fkInfo = args.FkInfo
}
joccau marked this conversation as resolved.
Show resolved Hide resolved
info.AddForeignKeys = append(info.AddForeignKeys, model.AddForeignKeyInfo{
Name: fkInfo.Name,
Cols: fkInfo.Cols,
Expand Down
22 changes: 10 additions & 12 deletions pkg/ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,17 +588,15 @@ func onRebaseAutoRandomType(jobCtx *jobContext, t *meta.Meta, job *model.Job) (v
}

func onRebaseAutoID(jobCtx *jobContext, t *meta.Meta, job *model.Job, tp autoid.AllocatorType) (ver int64, _ error) {
schemaID := job.SchemaID
var (
newBase int64
force bool
)
err := job.DecodeArgs(&newBase, &force)
args, err := model.GetRebaseAutoIDArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}

schemaID := job.SchemaID
newBase, force := args.NewBase, args.Force

if job.MultiSchemaInfo != nil && job.MultiSchemaInfo.Revertible {
job.MarkNonRevertible()
return ver, nil
Expand Down Expand Up @@ -983,8 +981,8 @@ func finishJobRenameTables(jobCtx *jobContext, t *meta.Meta, job *model.Job, arg
}

func onModifyTableComment(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int64, _ error) {
var comment string
if err := job.DecodeArgs(&comment); err != nil {
args, err := model.GetModifyTableCommentArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
Expand All @@ -999,7 +997,7 @@ func onModifyTableComment(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver
return ver, nil
}

tblInfo.Comment = comment
tblInfo.Comment = args.Comment
ver, err = updateVersionAndTableInfo(jobCtx, t, job, tblInfo, true)
if err != nil {
return ver, errors.Trace(err)
Expand All @@ -1009,12 +1007,12 @@ func onModifyTableComment(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver
}

func onModifyTableCharsetAndCollate(jobCtx *jobContext, t *meta.Meta, job *model.Job) (ver int64, _ error) {
var toCharset, toCollate string
var needsOverwriteCols bool
if err := job.DecodeArgs(&toCharset, &toCollate, &needsOverwriteCols); err != nil {
args, err := model.GetModifyTableCharsetAndCollateArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
toCharset, toCollate, needsOverwriteCols := args.ToCharset, args.ToCollate, args.NeedsOverwriteCols

dbInfo, err := checkSchemaExistAndCancelNotExistJob(t, job)
if err != nil {
Expand Down
Loading