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: record get owner TS and compare it before runReorgJob quit (#55049) #56909

Merged
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
15 changes: 14 additions & 1 deletion ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,19 @@
sync.RWMutex
// reorgCtxMap maps job ID to reorg context.
reorgCtxMap map[int64]*reorgCtx
beOwnerTS int64
}

func (r *reorgContexts) getOwnerTS() int64 {
r.RLock()
defer r.RUnlock()
return r.beOwnerTS
}

func (r *reorgContexts) setOwnerTS(ts int64) {
r.Lock()
r.beOwnerTS = ts
r.Unlock()

Check warning on line 541 in ddl/ddl.go

View check run for this annotation

Codecov / codecov/patch

ddl/ddl.go#L539-L541

Added lines #L539 - L541 were not covered by tests
}

func (dc *ddlCtx) getReorgCtx(jobID int64) *reorgCtx {
Expand All @@ -543,7 +556,7 @@
return existedRC
}
rc := &reorgCtx{}
rc.doneCh = make(chan error, 1)
rc.doneCh = make(chan reorgFnResult, 1)
// initial reorgCtx
rc.setRowCount(rowCount)
rc.mu.warnings = make(map[errors.ErrorID]*terror.Error)
Expand Down
24 changes: 21 additions & 3 deletions ddl/reorg.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
// If the reorganization job is done, we will use this channel to notify outer.
// TODO: Now we use goroutine to simulate reorganization jobs, later we may
// use a persistent job list.
doneCh chan error
doneCh chan reorgFnResult
// rowCount is used to simulate a job's row count.
rowCount int64
jobState model.JobState
Expand All @@ -78,6 +78,13 @@
references atomicutil.Int32
}

// reorgFnResult records the DDL owner TS before executing reorg function, in order to help
// receiver determine if the result is from reorg function of previous DDL owner in this instance.
type reorgFnResult struct {
ownerTS int64
err error
}

// newContext gets a context. It is only used for adding column in reorganization state.
func newContext(store kv.Storage) sessionctx.Context {
c := mock.NewContext()
Expand Down Expand Up @@ -246,11 +253,13 @@
return dbterror.ErrCancelledDDLJob
}

beOwnerTS := w.ddlCtx.reorgCtx.getOwnerTS()
rc = w.newReorgCtx(reorgInfo.Job.ID, reorgInfo.Job.GetRowCount())
w.wg.Add(1)
go func() {
defer w.wg.Done()
rc.doneCh <- f()
err := f()
rc.doneCh <- reorgFnResult{ownerTS: beOwnerTS, err: err}
}()
}

Expand All @@ -266,7 +275,16 @@

// wait reorganization job done or timeout
select {
case err := <-rc.doneCh:
case res := <-rc.doneCh:
err := res.err
curTS := w.ddlCtx.reorgCtx.getOwnerTS()
if res.ownerTS != curTS {
d.removeReorgCtx(job.ID)
logutil.BgLogger().Warn("owner ts mismatch, return timeout error and retry",
zap.Int64("prevTS", res.ownerTS),
zap.Int64("curTS", curTS))
return dbterror.ErrWaitReorgTimeout
}

Check warning on line 287 in ddl/reorg.go

View check run for this annotation

Codecov / codecov/patch

ddl/reorg.go#L282-L287

Added lines #L282 - L287 were not covered by tests
// Since job is cancelled,we don't care about its partial counts.
if rc.isReorgCanceled() || terror.ErrorEqual(err, dbterror.ErrCancelledDDLJob) {
d.removeReorgCtx(job.ID)
Expand Down