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: make ToProxyJob return value to avoid heap allocation #35882

Merged
merged 2 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ func (d *ddl) DoDDLJob(ctx sessionctx.Context, job *model.Job) error {
failpoint.Inject("mockParallelSameDDLJobTwice", func(val failpoint.Value) {
if val.(bool) {
// The same job will be put to the DDL queue twice.
job = job.Clone()
task1 := &limitJobTask{job, make(chan error)}
d.limitJobCh <- task1
<-task.err
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func jobNeedGC(job *model.Job) bool {
case model.ActionMultiSchemaChange:
for _, sub := range job.MultiSchemaInfo.SubJobs {
proxyJob := sub.ToProxyJob(job)
needGC := jobNeedGC(proxyJob)
needGC := jobNeedGC(&proxyJob)
if needGC {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions ddl/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ func insertJobIntoDeleteRangeTableMultiSchema(ctx context.Context, sctx sessionc
var ea elementIDAlloc
for _, sub := range job.MultiSchemaInfo.SubJobs {
proxyJob := sub.ToProxyJob(job)
if jobNeedGC(proxyJob) {
err := insertJobIntoDeleteRangeTable(ctx, sctx, proxyJob, &ea)
if jobNeedGC(&proxyJob) {
err := insertJobIntoDeleteRangeTable(ctx, sctx, &proxyJob, &ea)
if err != nil {
return errors.Trace(err)
}
Expand Down
16 changes: 8 additions & 8 deletions ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func onMultiSchemaChange(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) (ve
continue
}
proxyJob := sub.ToProxyJob(job)
ver, err = w.runDDLJob(d, t, proxyJob)
sub.FromProxyJob(proxyJob)
ver, err = w.runDDLJob(d, t, &proxyJob)
sub.FromProxyJob(&proxyJob)
return ver, err
}
// The last rollback/cancelling sub-job is done.
Expand All @@ -87,8 +87,8 @@ func onMultiSchemaChange(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) (ve
continue
}
proxyJob := sub.ToProxyJob(job)
ver, err = w.runDDLJob(d, t, proxyJob)
sub.FromProxyJob(proxyJob)
ver, err = w.runDDLJob(d, t, &proxyJob)
sub.FromProxyJob(&proxyJob)
handleRevertibleException(job, sub, proxyJob.Error)
return ver, err
}
Expand All @@ -107,8 +107,8 @@ func onMultiSchemaChange(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) (ve
}
subJobs[i] = *sub
proxyJob := sub.ToProxyJob(job)
ver, err = w.runDDLJob(d, t, proxyJob)
sub.FromProxyJob(proxyJob)
ver, err = w.runDDLJob(d, t, &proxyJob)
sub.FromProxyJob(&proxyJob)
if err != nil || proxyJob.Error != nil {
for j := i - 1; j >= 0; j-- {
job.MultiSchemaInfo.SubJobs[j] = &subJobs[j]
Expand All @@ -129,8 +129,8 @@ func onMultiSchemaChange(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) (ve
continue
}
proxyJob := sub.ToProxyJob(job)
ver, err = w.runDDLJob(d, t, proxyJob)
sub.FromProxyJob(proxyJob)
ver, err = w.runDDLJob(d, t, &proxyJob)
sub.FromProxyJob(&proxyJob)
return ver, err
}
job.State = model.JobStateDone
Expand Down
2 changes: 1 addition & 1 deletion ddl/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func expectedDeleteRangeCnt(job *model.Job) (int, error) {
totalExpectedCnt := 0
for _, sub := range job.MultiSchemaInfo.SubJobs {
p := sub.ToProxyJob(job)
cnt, err := expectedDeleteRangeCnt(p)
cnt, err := expectedDeleteRangeCnt(&p)
if err != nil {
return 0, err
}
Expand Down
26 changes: 24 additions & 2 deletions parser/model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ func (sub *SubJob) IsFinished() bool {
}

// ToProxyJob converts a sub-job to a proxy job.
func (sub *SubJob) ToProxyJob(parentJob *Job) *Job {
return &Job{
func (sub *SubJob) ToProxyJob(parentJob *Job) Job {
return Job{
ID: parentJob.ID,
Type: sub.Type,
SchemaID: parentJob.SchemaID,
Expand Down Expand Up @@ -436,6 +436,28 @@ func (job *Job) MarkNonRevertible() {
}
}

// Clone returns a copy of the job.
func (job *Job) Clone() *Job {
encode, err := job.Encode(true)
if err != nil {
return nil
}
var clone Job
err = clone.Decode(encode)
if err != nil {
return nil
}
if len(job.Args) > 0 {
clone.Args = make([]interface{}, len(job.Args))
copy(clone.Args, job.Args)
}
for i, sub := range job.MultiSchemaInfo.SubJobs {
clone.MultiSchemaInfo.SubJobs[i].Args = make([]interface{}, len(sub.Args))
copy(clone.MultiSchemaInfo.SubJobs[i].Args, sub.Args)
}
return &clone
}

// TSConvert2Time converts timestamp to time.
func TSConvert2Time(ts uint64) time.Time {
t := int64(ts >> 18) // 18 is for the logical time.
Expand Down