From 495e225dc70002c2418107d1524daf291876d089 Mon Sep 17 00:00:00 2001 From: tangenta Date: Thu, 9 Feb 2023 21:26:41 +0800 Subject: [PATCH] ddl: fix data race on job.SetWarnings (#41231) close pingcap/tidb#40751 --- ddl/reorg.go | 6 ++++-- parser/model/ddl.go | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/ddl/reorg.go b/ddl/reorg.go index 3eaa8080f1f73..e0b60887104a4 100644 --- a/ddl/reorg.go +++ b/ddl/reorg.go @@ -277,10 +277,12 @@ func (w *worker) runReorgJob(rh *reorgHandler, reorgInfo *reorgInfo, tblInfo *mo func (w *worker) mergeWarningsIntoJob(job *model.Job) { rc := w.getReorgCtx(job.ID) rc.mu.Lock() - defer rc.mu.Unlock() partWarnings := rc.mu.warnings partWarningsCount := rc.mu.warningsCount - job.SetWarnings(mergeWarningsAndWarningsCount(partWarnings, job.ReorgMeta.Warnings, partWarningsCount, job.ReorgMeta.WarningsCount)) + rc.mu.Unlock() + warnings, warningsCount := job.GetWarnings() + warnings, warningsCount = mergeWarningsAndWarningsCount(partWarnings, warnings, partWarningsCount, warningsCount) + job.SetWarnings(warnings, warningsCount) } func updateBackfillProgress(w *worker, reorgInfo *reorgInfo, tblInfo *model.TableInfo, diff --git a/parser/model/ddl.go b/parser/model/ddl.go index 7ca33341258c5..574e8d932193c 100644 --- a/parser/model/ddl.go +++ b/parser/model/ddl.go @@ -598,13 +598,18 @@ func (job *Job) GetRowCount() int64 { // SetWarnings sets the warnings of rows handled. func (job *Job) SetWarnings(warnings map[errors.ErrorID]*terror.Error, warningsCount map[errors.ErrorID]int64) { + job.Mu.Lock() job.ReorgMeta.Warnings = warnings job.ReorgMeta.WarningsCount = warningsCount + job.Mu.Unlock() } // GetWarnings gets the warnings of the rows handled. func (job *Job) GetWarnings() (map[errors.ErrorID]*terror.Error, map[errors.ErrorID]int64) { - return job.ReorgMeta.Warnings, job.ReorgMeta.WarningsCount + job.Mu.Lock() + w, wc := job.ReorgMeta.Warnings, job.ReorgMeta.WarningsCount + job.Mu.Unlock() + return w, wc } // Encode encodes job with json format. @@ -672,7 +677,8 @@ func (job *Job) String() string { ret := fmt.Sprintf("ID:%d, Type:%s, State:%s, SchemaState:%s, SchemaID:%d, TableID:%d, RowCount:%d, ArgLen:%d, start time: %v, Err:%v, ErrCount:%d, SnapshotVersion:%v", job.ID, job.Type, job.State, job.SchemaState, job.SchemaID, job.TableID, rowCount, len(job.Args), TSConvert2Time(job.StartTS), job.Error, job.ErrorCount, job.SnapshotVer) if job.ReorgMeta != nil { - ret += fmt.Sprintf(", UniqueWarnings:%d", len(job.ReorgMeta.Warnings)) + warnings, _ := job.GetWarnings() + ret += fmt.Sprintf(", UniqueWarnings:%d", len(warnings)) } if job.Type != ActionMultiSchemaChange && job.MultiSchemaInfo != nil { ret += fmt.Sprintf(", Multi-Schema Change:true, Revertible:%v", job.MultiSchemaInfo.Revertible)