forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_table.go
556 lines (511 loc) · 17.6 KB
/
job_table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl
import (
"bytes"
"context"
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/logutil"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
var (
addingDDLJobConcurrent = "/tidb/ddl/add_ddl_job_general"
)
func (dc *ddlCtx) insertRunningDDLJobMap(id int64) {
dc.runningJobs.Lock()
defer dc.runningJobs.Unlock()
dc.runningJobs.ids[id] = struct{}{}
}
func (dc *ddlCtx) deleteRunningDDLJobMap(id int64) {
dc.runningJobs.Lock()
defer dc.runningJobs.Unlock()
delete(dc.runningJobs.ids, id)
}
func (dc *ddlCtx) excludeJobIDs() string {
dc.runningJobs.RLock()
defer dc.runningJobs.RUnlock()
if len(dc.runningJobs.ids) == 0 {
return ""
}
dc.runningJobIDs = dc.runningJobIDs[:0]
for id := range dc.runningJobs.ids {
dc.runningJobIDs = append(dc.runningJobIDs, strconv.Itoa(int(id)))
}
return fmt.Sprintf("and job_id not in (%s)", strings.Join(dc.runningJobIDs, ","))
}
const (
getJobSQL = "select job_meta, processing from mysql.tidb_ddl_job where job_id in (select min(job_id) from mysql.tidb_ddl_job group by schema_ids, table_ids) and %s reorg %s order by processing desc, job_id"
)
type jobType int
func (t jobType) String() string {
switch t {
case general:
return "general"
case reorg:
return "reorg"
}
return "unknown job type: " + strconv.Itoa(int(t))
}
const (
general jobType = iota
reorg
)
func (d *ddl) getJob(sess *session, tp jobType, filter func(*model.Job) (bool, error)) (*model.Job, error) {
not := "not"
label := "get_job_general"
if tp == reorg {
not = ""
label = "get_job_reorg"
}
sql := fmt.Sprintf(getJobSQL, not, d.excludeJobIDs())
rows, err := sess.execute(context.Background(), sql, label)
if err != nil {
return nil, errors.Trace(err)
}
for _, row := range rows {
jobBinary := row.GetBytes(0)
runJob := model.Job{}
err := runJob.Decode(jobBinary)
if err != nil {
return nil, errors.Trace(err)
}
if row.GetInt64(1) == 1 {
return &runJob, nil
}
b, err := filter(&runJob)
if err != nil {
return nil, errors.Trace(err)
}
if b {
if err := d.markJobProcessing(sess, &runJob); err != nil {
logutil.BgLogger().Warn("[ddl] handle ddl job failed: mark job is processing meet error", zap.Error(err), zap.String("job", runJob.String()))
return nil, errors.Trace(err)
}
return &runJob, nil
}
}
return nil, nil
}
func (d *ddl) getGeneralJob(sess *session) (*model.Job, error) {
return d.getJob(sess, general, func(job *model.Job) (bool, error) {
if job.Type == model.ActionDropSchema {
sql := fmt.Sprintf("select job_id from mysql.tidb_ddl_job where find_in_set(%s, schema_ids) != 0 and processing limit 1", strconv.Quote(strconv.FormatInt(job.SchemaID, 10)))
return d.checkJobIsRunnable(sess, sql)
}
// For general job, there is only 1 general worker to handle it, so at this moment the processing job must be reorg job and the reorg job must only contain one table id.
// So it's not possible the find_in_set("1,2", "1,2,3") occurs.
sql := fmt.Sprintf("select job_id from mysql.tidb_ddl_job t1, (select table_ids from mysql.tidb_ddl_job where job_id = %d) t2 where processing and find_in_set(t1.table_ids, t2.table_ids) != 0", job.ID)
return d.checkJobIsRunnable(sess, sql)
})
}
func (d *ddl) checkJobIsRunnable(sess *session, sql string) (bool, error) {
rows, err := sess.execute(context.Background(), sql, "check_runnable")
return len(rows) == 0, err
}
func (d *ddl) getReorgJob(sess *session) (*model.Job, error) {
return d.getJob(sess, reorg, func(job *model.Job) (bool, error) {
sql := fmt.Sprintf("select job_id from mysql.tidb_ddl_job where (find_in_set(%s, schema_ids) != 0 and type = %d and processing) or (find_in_set(%s, table_ids) != 0 and processing) limit 1",
strconv.Quote(strconv.FormatInt(job.SchemaID, 10)), model.ActionDropSchema, strconv.Quote(strconv.FormatInt(job.TableID, 10)))
return d.checkJobIsRunnable(sess, sql)
})
}
func (d *ddl) startDispatchLoop() {
se, err := d.sessPool.get()
if err != nil {
logutil.BgLogger().Fatal("dispatch loop get session failed, it should not happen, please try restart TiDB", zap.Error(err))
}
defer d.sessPool.put(se)
sess := newSession(se)
var notifyDDLJobByEtcdCh clientv3.WatchChan
if d.etcdCli != nil {
notifyDDLJobByEtcdCh = d.etcdCli.Watch(d.ctx, addingDDLJobConcurrent)
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
if isChanClosed(d.ctx.Done()) {
return
}
if !variable.EnableConcurrentDDL.Load() || !d.isOwner() || d.waiting.Load() {
d.once.Store(true)
time.Sleep(time.Second)
continue
}
select {
case <-d.ddlJobCh:
case <-ticker.C:
case _, ok := <-notifyDDLJobByEtcdCh:
if !ok {
logutil.BgLogger().Warn("[ddl] start worker watch channel closed", zap.String("watch key", addingDDLJobConcurrent))
notifyDDLJobByEtcdCh = d.etcdCli.Watch(d.ctx, addingDDLJobConcurrent)
time.Sleep(time.Second)
continue
}
case <-d.ctx.Done():
return
}
d.loadDDLJobAndRun(sess, d.generalDDLWorkerPool, d.getGeneralJob)
d.loadDDLJobAndRun(sess, d.reorgWorkerPool, d.getReorgJob)
}
}
func (d *ddl) loadDDLJobAndRun(sess *session, pool *workerPool, getJob func(*session) (*model.Job, error)) {
wk, err := pool.get()
if err != nil || wk == nil {
logutil.BgLogger().Debug(fmt.Sprintf("[ddl] no %v worker available now", pool.tp()), zap.Error(err))
return
}
d.mu.RLock()
d.mu.hook.OnGetJobBefore(pool.tp().String())
d.mu.RUnlock()
job, err := getJob(sess)
if job == nil || err != nil {
if err != nil {
logutil.BgLogger().Warn("[ddl] get job met error", zap.Error(err))
}
pool.put(wk)
return
}
d.mu.RLock()
d.mu.hook.OnGetJobAfter(pool.tp().String(), job)
d.mu.RUnlock()
d.delivery2worker(wk, pool, job)
}
func (d *ddl) delivery2worker(wk *worker, pool *workerPool, job *model.Job) {
injectFailPointForGetJob(job)
d.insertRunningDDLJobMap(job.ID)
d.wg.Run(func() {
metrics.DDLRunningJobCount.WithLabelValues(pool.tp().String()).Inc()
defer func() {
pool.put(wk)
d.deleteRunningDDLJobMap(job.ID)
asyncNotify(d.ddlJobCh)
metrics.DDLRunningJobCount.WithLabelValues(pool.tp().String()).Dec()
}()
// we should wait 2 * d.lease time to guarantee all TiDB server have finished the schema change.
// see waitSchemaSynced for more details.
if !d.isSynced(job) || d.once.Load() {
wk.waitSchemaSynced(d.ddlCtx, job, 2*d.lease)
d.once.Store(false)
}
if err := wk.HandleDDLJobTable(d.ddlCtx, job); err != nil {
logutil.BgLogger().Info("[ddl] handle ddl job failed", zap.Error(err), zap.String("job", job.String()))
}
})
}
func (d *ddl) markJobProcessing(sess *session, job *model.Job) error {
sess.SetDiskFullOpt(kvrpcpb.DiskFullOpt_AllowedOnAlmostFull)
_, err := sess.execute(context.Background(), fmt.Sprintf("update mysql.tidb_ddl_job set processing = 1 where job_id = %d", job.ID), "mark_job_processing")
return errors.Trace(err)
}
const (
addDDLJobSQL = "insert into mysql.tidb_ddl_job(job_id, reorg, schema_ids, table_ids, job_meta, type, processing) values"
updateDDLJobSQL = "update mysql.tidb_ddl_job set job_meta = %s where job_id = %d"
)
func insertDDLJobs2Table(sess *session, jobs []*model.Job, updateRawArgs bool) error {
failpoint.Inject("mockAddBatchDDLJobsErr", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(errors.Errorf("mockAddBatchDDLJobsErr"))
}
})
if len(jobs) == 0 {
return nil
}
var sql bytes.Buffer
sql.WriteString(addDDLJobSQL)
for i, job := range jobs {
b, err := job.Encode(updateRawArgs)
if err != nil {
return err
}
if i != 0 {
sql.WriteString(",")
}
sql.WriteString(fmt.Sprintf("(%d, %t, %s, %s, %s, %d, %t)", job.ID, job.MayNeedReorg(), strconv.Quote(job2SchemaIDs(job)), strconv.Quote(job2TableIDs(job)), wrapKey2String(b), job.Type, !job.NotStarted()))
}
sess.SetDiskFullOpt(kvrpcpb.DiskFullOpt_AllowedOnAlmostFull)
ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL)
_, err := sess.execute(ctx, sql.String(), "insert_job")
logutil.BgLogger().Debug("[ddl] add job to mysql.tidb_ddl_job table", zap.String("sql", sql.String()))
return errors.Trace(err)
}
func job2SchemaIDs(job *model.Job) string {
return job2UniqueIDs(job, true)
}
func job2TableIDs(job *model.Job) string {
return job2UniqueIDs(job, false)
}
func job2UniqueIDs(job *model.Job, schema bool) string {
switch job.Type {
case model.ActionExchangeTablePartition, model.ActionRenameTables, model.ActionRenameTable:
var ids []int64
if schema {
ids = job.CtxVars[0].([]int64)
} else {
ids = job.CtxVars[1].([]int64)
}
set := make(map[int64]struct{}, len(ids))
for _, id := range ids {
set[id] = struct{}{}
}
s := make([]string, 0, len(set))
for id := range set {
s = append(s, strconv.FormatInt(id, 10))
}
slices.Sort(s)
return strings.Join(s, ",")
}
if schema {
return strconv.FormatInt(job.SchemaID, 10)
}
return strconv.FormatInt(job.TableID, 10)
}
func (w *worker) deleteDDLJob(job *model.Job) error {
sql := fmt.Sprintf("delete from mysql.tidb_ddl_job where job_id = %d", job.ID)
_, err := w.sess.execute(context.Background(), sql, "delete_job")
return errors.Trace(err)
}
func updateDDLJob2Table(sctx *session, job *model.Job, updateRawArgs bool) error {
b, err := job.Encode(updateRawArgs)
if err != nil {
return err
}
sql := fmt.Sprintf(updateDDLJobSQL, wrapKey2String(b), job.ID)
_, err = sctx.execute(context.Background(), sql, "update_job")
return errors.Trace(err)
}
// getDDLReorgHandle gets DDL reorg handle.
func getDDLReorgHandle(sess *session, job *model.Job) (element *meta.Element, startKey, endKey kv.Key, physicalTableID int64, err error) {
sql := fmt.Sprintf("select ele_id, ele_type, start_key, end_key, physical_id from mysql.tidb_ddl_reorg where job_id = %d", job.ID)
rows, err := sess.execute(context.Background(), sql, "get_handle")
if err != nil {
return nil, nil, nil, 0, err
}
if len(rows) == 0 {
return nil, nil, nil, 0, meta.ErrDDLReorgElementNotExist
}
id := rows[0].GetInt64(0)
tp := rows[0].GetBytes(1)
element = &meta.Element{
ID: id,
TypeKey: tp,
}
startKey = rows[0].GetBytes(2)
endKey = rows[0].GetBytes(3)
physicalTableID = rows[0].GetInt64(4)
// physicalTableID may be 0, because older version TiDB (without table partition) doesn't store them.
// update them to table's in this case.
if physicalTableID == 0 {
if job.ReorgMeta != nil {
endKey = kv.IntHandle(job.ReorgMeta.EndHandle).Encoded()
} else {
endKey = kv.IntHandle(math.MaxInt64).Encoded()
}
physicalTableID = job.TableID
logutil.BgLogger().Warn("new TiDB binary running on old TiDB DDL reorg data",
zap.Int64("partition ID", physicalTableID),
zap.Stringer("startHandle", startKey),
zap.Stringer("endHandle", endKey))
}
return
}
// updateDDLReorgStartHandle update the startKey of the handle.
func updateDDLReorgStartHandle(sess *session, job *model.Job, element *meta.Element, startKey kv.Key) error {
sql := fmt.Sprintf("update mysql.tidb_ddl_reorg set ele_id = %d, ele_type = %s, start_key = %s where job_id = %d",
element.ID, wrapKey2String(element.TypeKey), wrapKey2String(startKey), job.ID)
_, err := sess.execute(context.Background(), sql, "update_start_handle")
return err
}
// updateDDLReorgHandle update startKey, endKey physicalTableID and element of the handle.
func updateDDLReorgHandle(sess *session, jobID int64, startKey kv.Key, endKey kv.Key, physicalTableID int64, element *meta.Element) error {
sql := fmt.Sprintf("update mysql.tidb_ddl_reorg set ele_id = %d, ele_type = %s, start_key = %s, end_key = %s, physical_id = %d where job_id = %d",
element.ID, wrapKey2String(element.TypeKey), wrapKey2String(startKey), wrapKey2String(endKey), physicalTableID, jobID)
_, err := sess.execute(context.Background(), sql, "update_handle")
return err
}
// initDDLReorgHandle initializes the handle for ddl reorg.
func initDDLReorgHandle(sess *session, jobID int64, startKey kv.Key, endKey kv.Key, physicalTableID int64, element *meta.Element) error {
sql := fmt.Sprintf("insert into mysql.tidb_ddl_reorg(job_id, ele_id, ele_type, start_key, end_key, physical_id) values (%d, %d, %s, %s, %s, %d)",
jobID, element.ID, wrapKey2String(element.TypeKey), wrapKey2String(startKey), wrapKey2String(endKey), physicalTableID)
_, err := sess.execute(context.Background(), sql, "update_handle")
return err
}
// deleteDDLReorgHandle deletes the handle for ddl reorg.
func removeDDLReorgHandle(sess *session, job *model.Job, elements []*meta.Element) error {
if len(elements) == 0 {
return nil
}
sql := fmt.Sprintf("delete from mysql.tidb_ddl_reorg where job_id = %d", job.ID)
_, err := sess.execute(context.Background(), sql, "remove_handle")
return err
}
// removeReorgElement removes the element from ddl reorg, it is the same with removeDDLReorgHandle, only used in failpoint
func removeReorgElement(sess *session, job *model.Job) error {
sql := fmt.Sprintf("delete from mysql.tidb_ddl_reorg where job_id = %d", job.ID)
_, err := sess.execute(context.Background(), sql, "remove_handle")
return err
}
func wrapKey2String(key []byte) string {
if len(key) == 0 {
return "''"
}
return fmt.Sprintf("0x%x", key)
}
func getJobsBySQL(sess *session, tbl, condition string) ([]*model.Job, error) {
rows, err := sess.execute(context.Background(), fmt.Sprintf("select job_meta from mysql.%s where %s", tbl, condition), "get_job")
if err != nil {
return nil, errors.Trace(err)
}
jobs := make([]*model.Job, 0, 16)
for _, row := range rows {
jobBinary := row.GetBytes(0)
job := model.Job{}
err := job.Decode(jobBinary)
if err != nil {
return nil, errors.Trace(err)
}
jobs = append(jobs, &job)
}
return jobs, nil
}
// MoveJobFromQueue2Table move existing DDLs in queue to table.
func (d *ddl) MoveJobFromQueue2Table(force bool) error {
sess, err := d.sessPool.get()
if err != nil {
return err
}
defer d.sessPool.put(sess)
return runInTxn(newSession(sess), func(se *session) error {
txn, err := se.txn()
if err != nil {
return errors.Trace(err)
}
t := meta.NewMeta(txn)
isConcurrentDDL, err := t.IsConcurrentDDL()
if !force && (isConcurrentDDL || err != nil) {
return errors.Trace(err)
}
for _, tp := range []workerType{addIdxWorker, generalWorker} {
t := newMetaWithQueueTp(txn, tp)
jobs, err := t.GetAllDDLJobsInQueue()
if err != nil {
return errors.Trace(err)
}
err = insertDDLJobs2Table(se, jobs, false)
if err != nil {
return errors.Trace(err)
}
if tp == generalWorker {
// general job do not have reorg info.
continue
}
for _, job := range jobs {
element, start, end, pid, err := t.GetDDLReorgHandle(job)
if meta.ErrDDLReorgElementNotExist.Equal(err) {
continue
}
if err != nil {
return errors.Trace(err)
}
err = initDDLReorgHandle(se, job.ID, start, end, pid, element)
if err != nil {
return errors.Trace(err)
}
}
}
if err = t.ClearALLDDLJob(); err != nil {
return errors.Trace(err)
}
if err = t.ClearAllDDLReorgHandle(); err != nil {
return errors.Trace(err)
}
return t.SetConcurrentDDL(true)
})
}
// MoveJobFromTable2Queue move existing DDLs in table to queue.
func (d *ddl) MoveJobFromTable2Queue() error {
sess, err := d.sessPool.get()
if err != nil {
return err
}
defer d.sessPool.put(sess)
return runInTxn(newSession(sess), func(se *session) error {
txn, err := se.txn()
if err != nil {
return errors.Trace(err)
}
t := meta.NewMeta(txn)
isConcurrentDDL, err := t.IsConcurrentDDL()
if !isConcurrentDDL || err != nil {
return errors.Trace(err)
}
jobs, err := getJobsBySQL(se, "tidb_ddl_job", "1 order by job_id")
if err != nil {
return errors.Trace(err)
}
for _, job := range jobs {
jobListKey := meta.DefaultJobListKey
if job.MayNeedReorg() {
jobListKey = meta.AddIndexJobListKey
}
if err := t.EnQueueDDLJobNoUpdate(job, jobListKey); err != nil {
return errors.Trace(err)
}
}
reorgHandle, err := se.execute(context.Background(), "select job_id, start_key, end_key, physical_id, ele_id, ele_type from mysql.tidb_ddl_reorg", "get_handle")
if err != nil {
return errors.Trace(err)
}
for _, row := range reorgHandle {
if err := t.UpdateDDLReorgHandle(row.GetInt64(0), row.GetBytes(1), row.GetBytes(2), row.GetInt64(3), &meta.Element{ID: row.GetInt64(4), TypeKey: row.GetBytes(5)}); err != nil {
return errors.Trace(err)
}
}
// clean up these 2 tables.
_, err = se.execute(context.Background(), "delete from mysql.tidb_ddl_job", "delete_old_ddl")
if err != nil {
return errors.Trace(err)
}
_, err = se.execute(context.Background(), "delete from mysql.tidb_ddl_reorg", "delete_old_reorg")
if err != nil {
return errors.Trace(err)
}
return t.SetConcurrentDDL(false)
})
}
func runInTxn(se *session, f func(*session) error) (err error) {
err = se.begin()
if err != nil {
return err
}
err = f(se)
if err != nil {
se.rollback()
return
}
return errors.Trace(se.commit())
}