@@ -65,7 +65,7 @@ type reorgCtx struct {
65
65
// If the reorganization job is done, we will use this channel to notify outer.
66
66
// TODO: Now we use goroutine to simulate reorganization jobs, later we may
67
67
// use a persistent job list.
68
- doneCh chan error
68
+ doneCh chan reorgFnResult
69
69
// rowCount is used to simulate a job's row count.
70
70
rowCount int64
71
71
jobState model.JobState
@@ -80,6 +80,13 @@ type reorgCtx struct {
80
80
references atomicutil.Int32
81
81
}
82
82
83
+ // reorgFnResult records the DDL owner TS before executing reorg function, in order to help
84
+ // receiver determine if the result is from reorg function of previous DDL owner in this instance.
85
+ type reorgFnResult struct {
86
+ ownerTS int64
87
+ err error
88
+ }
89
+
83
90
func newReorgExprCtx () exprctx.ExprContext {
84
91
evalCtx := contextstatic .NewStaticEvalContext (
85
92
contextstatic .WithSQLMode (mysql .ModeNone ),
@@ -251,11 +258,13 @@ func (w *worker) runReorgJob(
251
258
return dbterror .ErrCancelledDDLJob
252
259
}
253
260
261
+ beOwnerTS := w .ddlCtx .reorgCtx .getOwnerTS ()
254
262
rc = w .newReorgCtx (reorgInfo .Job .ID , reorgInfo .Job .GetRowCount ())
255
263
w .wg .Add (1 )
256
264
go func () {
257
265
defer w .wg .Done ()
258
- rc .doneCh <- reorgFn ()
266
+ err := reorgFn ()
267
+ rc .doneCh <- reorgFnResult {ownerTS : beOwnerTS , err : err }
259
268
}()
260
269
}
261
270
@@ -271,7 +280,16 @@ func (w *worker) runReorgJob(
271
280
272
281
// wait reorganization job done or timeout
273
282
select {
274
- case err := <- rc .doneCh :
283
+ case res := <- rc .doneCh :
284
+ err := res .err
285
+ curTS := w .ddlCtx .reorgCtx .getOwnerTS ()
286
+ if res .ownerTS != curTS {
287
+ d .removeReorgCtx (job .ID )
288
+ logutil .DDLLogger ().Warn ("owner ts mismatch, return timeout error and retry" ,
289
+ zap .Int64 ("prevTS" , res .ownerTS ),
290
+ zap .Int64 ("curTS" , curTS ))
291
+ return dbterror .ErrWaitReorgTimeout
292
+ }
275
293
// Since job is cancelled,we don't care about its partial counts.
276
294
if rc .isReorgCanceled () || terror .ErrorEqual (err , dbterror .ErrCancelledDDLJob ) {
277
295
d .removeReorgCtx (job .ID )
0 commit comments