-
Notifications
You must be signed in to change notification settings - Fork 269
/
dml_worker.go
336 lines (303 loc) · 10.6 KB
/
dml_worker.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
// Copyright 2021 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package syncer
import (
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
tcontext "github.com/pingcap/tiflow/dm/pkg/context"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/dm/pkg/utils"
"github.com/pingcap/tiflow/dm/syncer/dbconn"
"github.com/pingcap/tiflow/dm/syncer/metrics"
"github.com/pingcap/tiflow/pkg/sqlmodel"
"go.uber.org/zap"
)
// DMLWorker is used to sync dml.
type DMLWorker struct {
compact bool
batch int
workerCount int
chanSize int
multipleRows bool
toDBConns []*dbconn.DBConn
syncCtx *tcontext.Context
logger log.Logger
metricProxies *metrics.Proxies
// for MetricsProxies
task string
source string
worker string
// callback func
// TODO: refine callback func
successFunc func(int, int, []*job)
fatalFunc func(*job, error)
lagFunc func(*job, int)
updateJobMetricsFunc func(bool, string, *job)
// channel
inCh chan *job
flushCh chan *job
}
// dmlWorkerWrap creates and runs a dmlWorker instance and returns flush job channel.
func dmlWorkerWrap(inCh chan *job, syncer *Syncer) chan *job {
chanSize := syncer.cfg.QueueSize / 2
if syncer.cfg.Compact {
chanSize /= 2
}
dmlWorker := &DMLWorker{
compact: syncer.cfg.Compact,
batch: syncer.cfg.Batch,
workerCount: syncer.cfg.WorkerCount,
chanSize: chanSize,
multipleRows: syncer.cfg.MultipleRows,
task: syncer.cfg.Name,
source: syncer.cfg.SourceID,
worker: syncer.cfg.WorkerName,
logger: syncer.tctx.Logger.WithFields(zap.String("component", "dml_worker")),
successFunc: syncer.successFunc,
fatalFunc: syncer.fatalFunc,
lagFunc: syncer.updateReplicationJobTS,
updateJobMetricsFunc: syncer.updateJobMetrics,
syncCtx: syncer.syncCtx, // this ctx can be used to cancel all the workers
metricProxies: syncer.metricsProxies,
toDBConns: syncer.toDBConns,
inCh: inCh,
flushCh: make(chan *job),
}
go func() {
dmlWorker.run()
dmlWorker.close()
}()
return dmlWorker.flushCh
}
// close closes outer channel.
func (w *DMLWorker) close() {
close(w.flushCh)
}
// run distribute jobs by queueBucket.
func (w *DMLWorker) run() {
jobChs := make([]chan *job, w.workerCount)
for i := 0; i < w.workerCount; i++ {
jobChs[i] = make(chan *job, w.chanSize)
go w.executeJobs(i, jobChs[i])
}
defer func() {
for i := 0; i < w.workerCount; i++ {
close(jobChs[i])
}
}()
queueBucketMapping := make([]string, w.workerCount)
for i := 0; i < w.workerCount; i++ {
queueBucketMapping[i] = queueBucketName(i)
}
for j := range w.inCh {
w.metricProxies.QueueSizeGauge.WithLabelValues(w.task, "dml_worker_input", w.source).Set(float64(len(w.inCh)))
switch j.tp {
case flush:
w.updateJobMetricsFunc(false, adminQueueName, j)
w.sendJobToAllDmlQueue(j, jobChs, queueBucketMapping)
j.flushWg.Wait()
w.updateJobMetricsFunc(true, adminQueueName, j)
w.flushCh <- j
case asyncFlush:
w.updateJobMetricsFunc(false, adminQueueName, j)
w.sendJobToAllDmlQueue(j, jobChs, queueBucketMapping)
w.flushCh <- j
case conflict:
w.updateJobMetricsFunc(false, adminQueueName, j)
w.sendJobToAllDmlQueue(j, jobChs, queueBucketMapping)
j.flushWg.Wait()
w.updateJobMetricsFunc(true, adminQueueName, j)
default:
queueBucket := int(utils.GenHashKey(j.dmlQueueKey)) % w.workerCount
w.updateJobMetricsFunc(false, queueBucketMapping[queueBucket], j)
startTime := time.Now()
w.logger.Debug("queue for key", zap.Int("queue", queueBucket), zap.String("key", j.dmlQueueKey))
jobChs[queueBucket] <- j
w.metricProxies.AddJobDurationHistogram.WithLabelValues(j.tp.String(), w.task, queueBucketMapping[queueBucket], w.source).Observe(time.Since(startTime).Seconds())
}
}
}
func (w *DMLWorker) sendJobToAllDmlQueue(j *job, jobChs []chan *job, queueBucketMapping []string) {
// flush for every DML queue
for i, jobCh := range jobChs {
startTime := time.Now()
jobCh <- j
w.metricProxies.AddJobDurationHistogram.WithLabelValues(j.tp.String(), w.task, queueBucketMapping[i], w.source).Observe(time.Since(startTime).Seconds())
}
}
// executeJobs execute jobs in same queueBucket
// All the jobs received should be executed consecutively.
func (w *DMLWorker) executeJobs(queueID int, jobCh chan *job) {
jobs := make([]*job, 0, w.batch)
workerJobIdx := dmlWorkerJobIdx(queueID)
queueBucket := queueBucketName(queueID)
for j := range jobCh {
w.metricProxies.QueueSizeGauge.WithLabelValues(w.task, queueBucket, w.source).Set(float64(len(jobCh)))
if j.tp != flush && j.tp != asyncFlush && j.tp != conflict {
if len(jobs) == 0 {
// set job TS when received first job of this batch.
w.lagFunc(j, workerJobIdx)
}
jobs = append(jobs, j)
if len(jobs) < w.batch && len(jobCh) > 0 {
continue
}
}
failpoint.Inject("syncDMLBatchNotFull", func() {
if len(jobCh) == 0 && len(jobs) < w.batch {
w.logger.Info("execute not full job queue")
}
})
w.executeBatchJobs(queueID, jobs)
if j.tp == conflict || j.tp == flush || j.tp == asyncFlush {
j.flushWg.Done()
}
jobs = jobs[0:0]
if len(jobCh) == 0 {
failpoint.Inject("noJobInQueueLog", func() {
w.logger.Debug("no job in queue, update lag to zero", zap.Int(
"workerJobIdx", workerJobIdx), zap.Int64("current ts", time.Now().Unix()))
})
w.lagFunc(nil, workerJobIdx)
}
}
}
// executeBatchJobs execute jobs with batch size.
func (w *DMLWorker) executeBatchJobs(queueID int, jobs []*job) {
var (
affect int
queries []string
args [][]interface{}
db = w.toDBConns[queueID]
err error
dmls = make([]*sqlmodel.RowChange, 0, len(jobs))
)
defer func() {
if err == nil {
w.successFunc(queueID, len(dmls), jobs)
} else {
if len(queries) == len(jobs) {
w.fatalFunc(jobs[affect], err)
} else {
w.logger.Warn("length of queries not equals length of jobs, cannot determine which job failed", zap.Int("queries", len(queries)), zap.Int("jobs", len(jobs)))
newJob := job{
startLocation: jobs[0].startLocation,
currentLocation: jobs[len(jobs)-1].currentLocation,
}
w.fatalFunc(&newJob, err)
}
}
}()
if len(jobs) == 0 {
return
}
failpoint.Inject("failSecondJob", func() {
if failExecuteSQLForTest && failOnceForTest.CAS(false, true) {
w.logger.Info("trigger failSecondJob")
err = terror.ErrDBExecuteFailed.Delegate(errors.New("failSecondJob"), "mock")
failpoint.Return()
}
})
queries, args = w.genSQLs(jobs)
failpoint.Inject("BlockExecuteSQLs", func(v failpoint.Value) {
t := v.(int) // sleep time
w.logger.Info("BlockExecuteSQLs", zap.Any("job", jobs[0]), zap.Int("sleep time", t))
for _, query := range queries {
if strings.Contains(query, "UPDATE") && strings.Contains(query, "MetricsProxies") {
t = 10
w.logger.Info("BlockExecuteSQLs block for update sleep 10s for MetricsProxies it test", zap.Any("query", query))
}
}
time.Sleep(time.Second * time.Duration(t))
})
failpoint.Inject("WaitUserCancel", func(v failpoint.Value) {
t := v.(int)
time.Sleep(time.Duration(t) * time.Second)
})
// use background context to execute sqls as much as possible
// set timeout to maxDMLConnectionDuration to make sure dmls can be replicated to downstream event if the latency is high
// if users need to quit this asap, we can support pause-task/stop-task --force in the future
ctx, cancel := w.syncCtx.WithTimeout(maxDMLConnectionDuration)
defer cancel()
affect, err = db.ExecuteSQL(ctx, w.metricProxies, queries, args...)
failpoint.Inject("SafeModeExit", func(val failpoint.Value) {
if intVal, ok := val.(int); ok && intVal == 4 && len(jobs) > 0 {
w.logger.Warn("fail to exec DML", zap.String("failpoint", "SafeModeExit"))
affect, err = 0, terror.ErrDBExecuteFailed.Delegate(errors.New("SafeModeExit"), "mock")
}
})
failpoint.Inject("ErrorOnLastDML", func(_ failpoint.Value) {
if len(queries) > len(jobs) {
w.logger.Error("error on last queries", zap.Int("queries", len(queries)), zap.Int("jobs", len(jobs)))
affect, err = len(queries)-1, terror.ErrDBExecuteFailed.Delegate(errors.New("ErrorOnLastDML"), "mock")
}
})
if w.judgeKeyNotFound(affect, jobs) {
// throw an error if needed in the future.
// err = terror.ErrDBExecuteFailed.Delegate(errors.New("key not found"), "mock")
w.logger.Warn("no matching record is found to update/delete, ER_KEY_NOT_FOUND", zap.Int("affect", affect), zap.Int("jobs", len(jobs)), zap.Stringer("start from", jobs[0].startLocation), zap.Stringer("end at", jobs[len(jobs)-1].currentLocation))
}
}
// genSQLs generate SQLs in single row mode or multiple rows mode.
func (w *DMLWorker) genSQLs(jobs []*job) ([]string, [][]interface{}) {
if w.multipleRows {
return genDMLsWithSameOp(jobs)
}
queries := make([]string, 0, len(jobs))
args := make([][]interface{}, 0, len(jobs))
for _, j := range jobs {
var query string
var arg []interface{}
appendQueryAndArg := func() {
queries = append(queries, query)
args = append(args, arg)
}
switch j.dml.Type() {
case sqlmodel.RowChangeInsert:
if j.safeMode {
query, arg = j.dml.GenSQL(sqlmodel.DMLReplace)
} else {
query, arg = j.dml.GenSQL(sqlmodel.DMLInsert)
}
case sqlmodel.RowChangeUpdate:
if j.safeMode {
query, arg = j.dml.GenSQL(sqlmodel.DMLDelete)
appendQueryAndArg()
query, arg = j.dml.GenSQL(sqlmodel.DMLReplace)
} else {
query, arg = j.dml.GenSQL(sqlmodel.DMLUpdate)
}
case sqlmodel.RowChangeDelete:
query, arg = j.dml.GenSQL(sqlmodel.DMLDelete)
}
appendQueryAndArg()
}
return queries, args
}
func (w *DMLWorker) judgeKeyNotFound(affect int, jobs []*job) bool {
// TODO: support compact and multiple rows
// In compact mode, we need to calculate the expected affected rows based on the compacted job
// while in multiple-rows, we need to calculate the affected rows based on the sql type
if w.compact || w.multipleRows {
return false
}
for _, j := range jobs {
if j.safeMode {
return false
}
}
return affect < len(jobs)
}