-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: add job version 2 and use typed args for truncate table #55854
Changes from 1 commit
eb0584d
e8368ce
fc213b7
67ec37c
8cdc42c
345284e
a238153
99a5d0b
ecb3836
4c9cc7a
140069f
c0530a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4186,24 +4186,35 @@ func (e *executor) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error { | |
return errors.Trace(dbterror.ErrTruncateIllegalForeignKey.GenWithStackByArgs(msg)) | ||
} | ||
|
||
var partCount int | ||
var oldPartitionIDs []int64 | ||
if tblInfo.Partition != nil { | ||
partCount = len(tblInfo.Partition.Definitions) | ||
oldPartitionIDs = make([]int64, 0, len(tblInfo.Partition.Definitions)) | ||
for _, def := range tblInfo.Partition.Definitions { | ||
oldPartitionIDs = append(oldPartitionIDs, def.ID) | ||
} | ||
} | ||
job := &model.Job{ | ||
SchemaID: schema.ID, | ||
TableID: tblInfo.ID, | ||
SchemaName: schema.Name.L, | ||
TableName: tblInfo.Name.L, | ||
Type: model.ActionTruncateTable, | ||
BinlogInfo: &model.HistoryInfo{}, | ||
Version: model.GetJobVerInUse(), | ||
SchemaID: schema.ID, | ||
TableID: tblInfo.ID, | ||
SchemaName: schema.Name.L, | ||
TableName: tblInfo.Name.L, | ||
Type: model.ActionTruncateTable, | ||
BinlogInfo: &model.HistoryInfo{}, | ||
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource, | ||
SQLMode: ctx.GetSessionVars().SQLMode, | ||
} | ||
if job.Version == model.JobVersion1 { | ||
// Args[0] is the new table ID, args[2] is the ids for table partitions, we | ||
// add a placeholder here, they will be filled by job submitter. | ||
// the last param is not required for execution, we need it to calculate | ||
// number of new IDs to generate. | ||
Args: []any{int64(0), fkCheck, []int64{}, partCount}, | ||
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource, | ||
SQLMode: ctx.GetSessionVars().SQLMode, | ||
job.Args = []any{int64(0), fkCheck, []int64{}, len(oldPartitionIDs)} | ||
} else if job.Version == model.JobVersion2 { | ||
job.ArgsV2 = &model.TruncateTableArgs{ | ||
FKCheck: fkCheck, | ||
OldPartitionIDs: oldPartitionIDs, | ||
} | ||
} | ||
err = e.DoDDLJob(ctx, job) | ||
if err != nil { | ||
|
@@ -6426,7 +6437,12 @@ func (e *executor) DoDDLJobWrapper(ctx sessionctx.Context, jobW *JobWrapper) (re | |
func HandleLockTablesOnSuccessSubmit(ctx sessionctx.Context, jobW *JobWrapper) { | ||
if jobW.Type == model.ActionTruncateTable { | ||
if ok, lockTp := ctx.CheckTableLocked(jobW.TableID); ok { | ||
newTableID := jobW.Args[0].(int64) | ||
var newTableID int64 | ||
if jobW.Version == model.JobVersion1 { | ||
newTableID = jobW.Args[0].(int64) | ||
} else { | ||
newTableID = jobW.ArgsV2.(*model.TruncateTableArgs).NewTableID | ||
} | ||
ctx.AddTableLock([]model.TableLockTpInfo{{SchemaID: jobW.SchemaID, TableID: newTableID, Tp: lockTp}}) | ||
} | ||
} | ||
|
@@ -6437,7 +6453,12 @@ func HandleLockTablesOnSuccessSubmit(ctx sessionctx.Context, jobW *JobWrapper) { | |
func HandleLockTablesOnFinish(ctx sessionctx.Context, jobW *JobWrapper, ddlErr error) { | ||
if jobW.Type == model.ActionTruncateTable { | ||
if ddlErr != nil { | ||
newTableID := jobW.Args[0].(int64) | ||
var newTableID int64 | ||
if jobW.Version == model.JobVersion1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
newTableID = jobW.Args[0].(int64) | ||
} else { | ||
newTableID = jobW.ArgsV2.(*model.TruncateTableArgs).NewTableID | ||
} | ||
ctx.ReleaseTableLockByTableIDs([]int64{newTableID}) | ||
return | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -723,7 +723,13 @@ func job2UniqueIDs(job *model.Job, schema bool) string { | |
if schema { | ||
return strconv.FormatInt(job.SchemaID, 10) | ||
} | ||
return strconv.FormatInt(job.TableID, 10) + "," + strconv.FormatInt(job.Args[0].(int64), 10) | ||
var newTableID int64 | ||
if job.Version == model.JobVersion1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
newTableID = job.Args[0].(int64) | ||
} else { | ||
newTableID = job.ArgsV2.(*model.TruncateTableArgs).NewTableID | ||
} | ||
return strconv.FormatInt(job.TableID, 10) + "," + strconv.FormatInt(newTableID, 10) | ||
} | ||
if schema { | ||
return strconv.FormatInt(job.SchemaID, 10) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,7 +316,11 @@ func (s *JobSubmitter) addBatchDDLJobs2Table(jobWs []*JobWrapper) error { | |
|
||
for _, jobW := range jobWs { | ||
job := jobW.Job | ||
job.Version = currentVersion | ||
if job.Version == 0 { | ||
// if not set, fix it to version 1 | ||
// TODO remove this after we add code v2 for all jobs. | ||
job.Version = model.JobVersion1 | ||
} | ||
job.StartTS = startTS | ||
job.BDRRole = bdrRole | ||
|
||
|
@@ -359,6 +363,14 @@ func (s *JobSubmitter) addBatchDDLJobs2Queue(jobWs []*JobWrapper) error { | |
return kv.RunInNewTxn(ctx, s.store, true, func(_ context.Context, txn kv.Transaction) error { | ||
t := meta.NewMeta(txn) | ||
|
||
for _, jobW := range jobWs { | ||
if jobW.Version == 0 { | ||
// if not set, fix it to version 1 | ||
// TODO remove this after we add code v2 for all jobs. | ||
jobW.Version = model.JobVersion1 | ||
} | ||
} | ||
|
||
count := getRequiredGIDCount(jobWs) | ||
ids, err := t.GenGlobalIDs(count) | ||
if err != nil { | ||
|
@@ -372,7 +384,6 @@ func (s *JobSubmitter) addBatchDDLJobs2Queue(jobWs []*JobWrapper) error { | |
|
||
for _, jobW := range jobWs { | ||
job := jobW.Job | ||
job.Version = currentVersion | ||
job.StartTS = txn.StartTS() | ||
setJobStateToQueueing(job) | ||
if err = buildJobDependence(t, job); err != nil { | ||
|
@@ -508,8 +519,12 @@ func getRequiredGIDCount(jobWs []*JobWrapper) int { | |
pInfo := jobW.Args[1].(*model.PartitionInfo) | ||
count += len(pInfo.Definitions) | ||
case model.ActionTruncateTable: | ||
partCount := jobW.Args[3].(int) | ||
count += 1 + partCount | ||
if jobW.Version == model.JobVersion1 { | ||
partCount := jobW.Args[3].(int) | ||
lance6716 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
count += 1 + partCount | ||
} else if jobW.Version == model.JobVersion2 { | ||
count += 1 + len(jobW.ArgsV2.(*model.TruncateTableArgs).OldPartitionIDs) | ||
} | ||
} | ||
} | ||
return count | ||
|
@@ -579,13 +594,23 @@ func assignGIDsForJobs(jobWs []*JobWrapper, ids []int64) { | |
pInfo.NewTableID = pInfo.Definitions[0].ID | ||
case model.ActionTruncateTable: | ||
if !jobW.IDAllocated { | ||
jobW.Args[0] = alloc.next() | ||
partCount := jobW.Args[3].(int) | ||
partIDs := make([]int64, partCount) | ||
for i := range partIDs { | ||
partIDs[i] = alloc.next() | ||
if jobW.Version == model.JobVersion1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for these getter logic, I prefer we always use a function to convert all version representations to like if !jobW.IDAllocated {
arg := getTruncateTableArgs(job)
arg.NewTableID = alloc.next()
... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for new jobs, i plan put JobArgs into JobWrapper, and fill to job on insert, so we can use it directly here. |
||
jobW.Args[0] = alloc.next() | ||
partCount := jobW.Args[3].(int) | ||
partIDs := make([]int64, partCount) | ||
for i := range partIDs { | ||
partIDs[i] = alloc.next() | ||
} | ||
jobW.Args[2] = partIDs | ||
} else if jobW.Version == model.JobVersion2 { | ||
args := jobW.ArgsV2.(*model.TruncateTableArgs) | ||
args.NewTableID = alloc.next() | ||
partIDs := make([]int64, len(args.OldPartitionIDs)) | ||
for i := range partIDs { | ||
partIDs[i] = alloc.next() | ||
} | ||
args.NewPartitionIDs = partIDs | ||
} | ||
jobW.Args[2] = partIDs | ||
} | ||
} | ||
jobW.ID = alloc.next() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like
GetTruncateTableArgsBeforeRun
, this part can also be encapsulated?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c0530a1