-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathexecutor.go
More file actions
384 lines (325 loc) · 9.43 KB
/
executor.go
File metadata and controls
384 lines (325 loc) · 9.43 KB
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
// Copyright 2019 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 loader
import (
"context"
gosql "database/sql"
"fmt"
"strings"
"sync/atomic"
"time"
"github.com/pingcap/tidb-binlog/drainer/loopbacksync"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/errors"
"github.com/pingcap/log"
pkgsql "github.com/pingcap/tidb-binlog/pkg/sql"
"github.com/pingcap/tidb-binlog/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
var (
defaultBatchSize = 128
defaultWorkerCount = 16
index int64
)
type executor struct {
db *gosql.DB
batchSize int
workerCount int
info *loopbacksync.LoopBackSync
queryHistogramVec *prometheus.HistogramVec
refreshTableInfo func(schema string, table string) (info *tableInfo, err error)
}
func newExecutor(db *gosql.DB) *executor {
exe := &executor{
db: db,
batchSize: defaultBatchSize,
workerCount: defaultWorkerCount,
}
return exe
}
func (e *executor) withRefreshTableInfo(fn func(schema string, table string) (info *tableInfo, err error)) *executor {
e.refreshTableInfo = fn
return e
}
func (e *executor) withBatchSize(batchSize int) *executor {
e.batchSize = batchSize
return e
}
func (e *executor) setSyncInfo(info *loopbacksync.LoopBackSync) {
e.info = info
}
func (e *executor) setWorkerCount(workerCount int) {
e.workerCount = workerCount
}
func (e *executor) withQueryHistogramVec(queryHistogramVec *prometheus.HistogramVec) *executor {
e.queryHistogramVec = queryHistogramVec
return e
}
func (e *executor) execTableBatchRetry(ctx context.Context, dmls []*DML, retryNum int, backoff time.Duration) error {
err := util.RetryContext(ctx, retryNum, backoff, 1, func(context.Context) error {
return e.execTableBatch(ctx, dmls)
})
return errors.Trace(err)
}
// a wrap of *sql.Tx with metrics
type tx struct {
*gosql.Tx
queryHistogramVec *prometheus.HistogramVec
}
// wrap of sql.Tx.Exec()
func (tx *tx) exec(query string, args ...interface{}) (gosql.Result, error) {
start := time.Now()
res, err := tx.Tx.Exec(query, args...)
if tx.queryHistogramVec != nil {
tx.queryHistogramVec.WithLabelValues("exec").Observe(time.Since(start).Seconds())
}
return res, err
}
func (tx *tx) autoRollbackExec(query string, args ...interface{}) (res gosql.Result, err error) {
res, err = tx.exec(query, args...)
if err != nil {
log.Error("Exec fail, will rollback", zap.String("query", query), zap.Reflect("args", args), zap.Error(err))
if rbErr := tx.Rollback(); rbErr != nil {
log.Error("Auto rollback", zap.Error(rbErr))
}
err = errors.Trace(err)
}
return
}
// wrap of sql.Tx.Commit()
func (tx *tx) commit() error {
start := time.Now()
err := tx.Tx.Commit()
if tx.queryHistogramVec != nil {
tx.queryHistogramVec.WithLabelValues("commit").Observe(time.Since(start).Seconds())
}
return errors.Trace(err)
}
func (e *executor) addIndex() int64 {
return atomic.AddInt64(&index, 1) % ((int64)(e.workerCount))
}
// return a wrap of sql.Tx
func (e *executor) begin() (*tx, error) {
sqlTx, err := e.db.Begin()
if err != nil {
return nil, errors.Trace(err)
}
var tx = &tx{
Tx: sqlTx,
queryHistogramVec: e.queryHistogramVec,
}
if e.info != nil && e.info.LoopbackControl {
start := time.Now()
err = loopbacksync.UpdateMark(tx.Tx, e.addIndex(), e.info.ChannelID)
if err != nil {
rerr := tx.Rollback()
if rerr != nil {
log.Error("fail to rollback", zap.Error(rerr))
}
return nil, errors.Annotate(err, "failed to update mark data")
}
if tx.queryHistogramVec != nil {
tx.queryHistogramVec.WithLabelValues("update_mark_table").Observe(time.Since(start).Seconds())
}
}
return tx, nil
}
func (e *executor) bulkDelete(deletes []*DML) error {
if len(deletes) == 0 {
return nil
}
var sqls strings.Builder
argss := make([]interface{}, 0, len(deletes))
for _, dml := range deletes {
sql, args := dml.sql()
sqls.WriteString(sql)
sqls.WriteByte(';')
argss = append(argss, args...)
}
tx, err := e.begin()
if err != nil {
return errors.Trace(err)
}
sql := sqls.String()
_, err = tx.autoRollbackExec(sql, argss...)
if err != nil {
return errors.Trace(err)
}
err = tx.commit()
return errors.Trace(err)
}
func (e *executor) bulkReplace(inserts []*DML) error {
if len(inserts) == 0 {
return nil
}
info := inserts[0].info
var builder strings.Builder
cols := "(" + BuildColumnList(info.columns) + ")"
builder.WriteString("REPLACE INTO " + inserts[0].TableName() + cols + " VALUES ")
holder := fmt.Sprintf("(%s)", holderString(len(info.columns)))
for i := 0; i < len(inserts); i++ {
if i > 0 {
builder.WriteByte(',')
}
builder.WriteString(holder)
}
args := make([]interface{}, 0, len(inserts)*len(info.columns))
for _, insert := range inserts {
for _, name := range info.columns {
v := insert.Values[name]
args = append(args, v)
}
}
tx, err := e.begin()
if err != nil {
return errors.Trace(err)
}
_, err = tx.autoRollbackExec(builder.String(), args...)
if err != nil {
return errors.Trace(err)
}
err = tx.commit()
return errors.Trace(err)
}
// we merge dmls by primary key, after merge by key, we
// have only one dml for one primary key which contains the newest value(like a kv store),
// to avoid other column's duplicate entry, we should apply delete dmls first, then insert&update
// use replace to handle the update unique index case(see https://github.com/pingcap/tidb-binlog/pull/437/files)
// or we can simply check if it update unique index column or not, and for update change to (delete + insert)
// the final result should has no duplicate entry or the origin dmls is wrong.
func (e *executor) execTableBatch(ctx context.Context, dmls []*DML) error {
if len(dmls) == 0 {
return nil
}
types, err := mergeByPrimaryKey(dmls)
if err != nil {
return errors.Trace(err)
}
log.Debug("merge dmls", zap.Reflect("dmls", dmls), zap.Reflect("merged", types))
if allDeletes, ok := types[DeleteDMLType]; ok {
if err := e.splitExecDML(ctx, allDeletes, e.bulkDelete); err != nil {
return errors.Trace(err)
}
}
if allInserts, ok := types[InsertDMLType]; ok {
if err := e.splitExecDML(ctx, allInserts, e.bulkReplace); err != nil {
return errors.Trace(err)
}
}
if allUpdates, ok := types[UpdateDMLType]; ok {
if err := e.splitExecDML(ctx, allUpdates, e.bulkReplace); err != nil {
return errors.Trace(err)
}
}
return nil
}
// splitExecDML split dmls to size of e.batchSize and call exec concurrently
func (e *executor) splitExecDML(ctx context.Context, dmls []*DML, exec func(dmls []*DML) error) error {
errg, _ := errgroup.WithContext(ctx)
for _, split := range splitDMLs(dmls, e.batchSize) {
split := split
errg.Go(func() error {
err := exec(split)
if err != nil {
return errors.Trace(err)
}
return nil
})
}
return errors.Trace(errg.Wait())
}
func tryRefreshTableErr(err error) bool {
errCode, ok := pkgsql.GetSQLErrCode(err)
if !ok {
return false
}
switch errCode {
case infoschema.ErrColumnNotExists.Code():
return true
}
return false
}
func (e *executor) singleExecRetry(ctx context.Context, allDMLs []*DML, safeMode bool, retryNum int, backoff time.Duration) error {
for _, dmls := range splitDMLs(allDMLs, e.batchSize) {
err := util.RetryContext(ctx, retryNum, backoff, 1, func(context.Context) error {
execErr := e.singleExec(dmls, safeMode)
if execErr == nil {
return nil
}
if tryRefreshTableErr(execErr) && e.refreshTableInfo != nil {
log.Info("try refresh table info")
name2info := make(map[string]*tableInfo)
for _, dml := range dmls {
name := dml.TableName()
info, ok := name2info[name]
if !ok {
var err error
info, err = e.refreshTableInfo(dml.Database, dml.Table)
if err != nil {
log.Error("fail to refresh table info", zap.Error(err))
continue
}
name2info[name] = info
}
if len(dml.info.columns) != len(info.columns) {
log.Info("columns change", zap.Strings("old", dml.info.columns),
zap.Strings("new", info.columns))
removeOrphanCols(info, dml)
}
dml.info = info
}
}
return execErr
})
if err != nil {
return errors.Trace(err)
}
}
return nil
}
func (e *executor) singleExec(dmls []*DML, safeMode bool) error {
tx, err := e.begin()
if err != nil {
return errors.Trace(err)
}
for _, dml := range dmls {
if safeMode && dml.Tp == UpdateDMLType {
sql, args := dml.deleteSQL()
_, err := tx.autoRollbackExec(sql, args...)
if err != nil {
return errors.Trace(err)
}
sql, args = dml.replaceSQL()
_, err = tx.autoRollbackExec(sql, args...)
if err != nil {
return errors.Trace(err)
}
} else if safeMode && dml.Tp == InsertDMLType {
sql, args := dml.replaceSQL()
_, err := tx.autoRollbackExec(sql, args...)
if err != nil {
return errors.Trace(err)
}
} else {
sql, args := dml.sql()
_, err := tx.autoRollbackExec(sql, args...)
if err != nil {
return errors.Trace(err)
}
}
}
err = tx.commit()
return errors.Trace(err)
}