forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidb.go
645 lines (566 loc) · 17.2 KB
/
tidb.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
// 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 tidb
import (
"context"
"database/sql"
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/pingcap/br/pkg/lightning/backend"
"github.com/pingcap/br/pkg/lightning/backend/kv"
"github.com/pingcap/br/pkg/lightning/common"
"github.com/pingcap/br/pkg/lightning/config"
"github.com/pingcap/br/pkg/lightning/log"
"github.com/pingcap/br/pkg/lightning/verification"
"github.com/pingcap/br/pkg/redact"
"github.com/pingcap/br/pkg/version"
)
var extraHandleTableColumn = &table.Column{
ColumnInfo: kv.ExtraHandleColumnInfo,
GeneratedExpr: nil,
DefaultExpr: nil,
}
const (
writeRowsMaxRetryTimes = 3
)
type tidbRow string
type tidbRows []tidbRow
// MarshalLogArray implements the zapcore.ArrayMarshaler interface
func (rows tidbRows) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
for _, r := range rows {
encoder.AppendString(redact.String(string(r)))
}
return nil
}
type tidbEncoder struct {
mode mysql.SQLMode
tbl table.Table
se sessionctx.Context
// the index of table columns for each data field.
// index == len(table.columns) means this field is `_tidb_rowid`
columnIdx []int
columnCnt int
}
type tidbBackend struct {
db *sql.DB
onDuplicate string
}
// NewTiDBBackend creates a new TiDB backend using the given database.
//
// The backend does not take ownership of `db`. Caller should close `db`
// manually after the backend expired.
func NewTiDBBackend(db *sql.DB, onDuplicate string) backend.Backend {
switch onDuplicate {
case config.ReplaceOnDup, config.IgnoreOnDup, config.ErrorOnDup:
default:
log.L().Warn("unsupported action on duplicate, overwrite with `replace`")
onDuplicate = config.ReplaceOnDup
}
return backend.MakeBackend(&tidbBackend{db: db, onDuplicate: onDuplicate})
}
func (row tidbRow) Size() uint64 {
return uint64(len(row))
}
func (row tidbRow) ClassifyAndAppend(data *kv.Rows, checksum *verification.KVChecksum, _ *kv.Rows, _ *verification.KVChecksum) {
rows := (*data).(tidbRows)
// Cannot do `rows := data.(*tidbRows); *rows = append(*rows, row)`.
//nolint:gocritic
*data = append(rows, row)
cs := verification.MakeKVChecksum(uint64(len(row)), 1, 0)
checksum.Add(&cs)
}
func (rows tidbRows) SplitIntoChunks(splitSize int) []kv.Rows {
if len(rows) == 0 {
return nil
}
res := make([]kv.Rows, 0, 1)
i := 0
cumSize := 0
for j, row := range rows {
if i < j && cumSize+len(row) > splitSize {
res = append(res, rows[i:j])
i = j
cumSize = 0
}
cumSize += len(row)
}
return append(res, rows[i:])
}
func (rows tidbRows) Clear() kv.Rows {
return rows[:0]
}
func (enc *tidbEncoder) appendSQLBytes(sb *strings.Builder, value []byte) {
sb.Grow(2 + len(value))
sb.WriteByte('\'')
if enc.mode.HasNoBackslashEscapesMode() {
for _, b := range value {
if b == '\'' {
sb.WriteString(`''`)
} else {
sb.WriteByte(b)
}
}
} else {
for _, b := range value {
switch b {
case 0:
sb.WriteString(`\0`)
case '\b':
sb.WriteString(`\b`)
case '\n':
sb.WriteString(`\n`)
case '\r':
sb.WriteString(`\r`)
case '\t':
sb.WriteString(`\t`)
case 26:
sb.WriteString(`\Z`)
case '\'':
sb.WriteString(`''`)
case '\\':
sb.WriteString(`\\`)
default:
sb.WriteByte(b)
}
}
}
sb.WriteByte('\'')
}
// appendSQL appends the SQL representation of the Datum into the string builder.
// Note that we cannot use Datum.ToString since it doesn't perform SQL escaping.
func (enc *tidbEncoder) appendSQL(sb *strings.Builder, datum *types.Datum, _ *table.Column) error {
switch datum.Kind() {
case types.KindNull:
sb.WriteString("NULL")
case types.KindMinNotNull:
sb.WriteString("MINVALUE")
case types.KindMaxValue:
sb.WriteString("MAXVALUE")
case types.KindInt64:
// longest int64 = -9223372036854775808 which has 20 characters
var buffer [20]byte
value := strconv.AppendInt(buffer[:0], datum.GetInt64(), 10)
sb.Write(value)
case types.KindUint64, types.KindMysqlEnum, types.KindMysqlSet:
// longest uint64 = 18446744073709551615 which has 20 characters
var buffer [20]byte
value := strconv.AppendUint(buffer[:0], datum.GetUint64(), 10)
sb.Write(value)
case types.KindFloat32, types.KindFloat64:
// float64 has 16 digits of precision, so a buffer size of 32 is more than enough...
var buffer [32]byte
value := strconv.AppendFloat(buffer[:0], datum.GetFloat64(), 'g', -1, 64)
sb.Write(value)
case types.KindString:
// See: https://github.com/pingcap/tidb-lightning/issues/550
// if enc.mode.HasStrictMode() {
// d, err := table.CastValue(enc.se, *datum, col.ToInfo(), false, false)
// if err != nil {
// return errors.Trace(err)
// }
// datum = &d
// }
enc.appendSQLBytes(sb, datum.GetBytes())
case types.KindBytes:
enc.appendSQLBytes(sb, datum.GetBytes())
case types.KindMysqlJSON:
value, err := datum.GetMysqlJSON().MarshalJSON()
if err != nil {
return err
}
enc.appendSQLBytes(sb, value)
case types.KindBinaryLiteral:
value := datum.GetBinaryLiteral()
sb.Grow(3 + 2*len(value))
sb.WriteString("x'")
if _, err := hex.NewEncoder(sb).Write(value); err != nil {
return errors.Trace(err)
}
sb.WriteByte('\'')
case types.KindMysqlBit:
var buffer [20]byte
intValue, err := datum.GetBinaryLiteral().ToInt(nil)
if err != nil {
return err
}
value := strconv.AppendUint(buffer[:0], intValue, 10)
sb.Write(value)
// time, duration, decimal
default:
value, err := datum.ToString()
if err != nil {
return err
}
sb.WriteByte('\'')
sb.WriteString(value)
sb.WriteByte('\'')
}
return nil
}
func (*tidbEncoder) Close() {}
func getColumnByIndex(cols []*table.Column, index int) *table.Column {
if index == len(cols) {
return extraHandleTableColumn
}
return cols[index]
}
func (enc *tidbEncoder) Encode(logger log.Logger, row []types.Datum, _ int64, columnPermutation []int, _ int64) (kv.Row, error) {
cols := enc.tbl.Cols()
if len(enc.columnIdx) == 0 {
columnCount := 0
columnIdx := make([]int, len(columnPermutation))
for i, idx := range columnPermutation {
if idx >= 0 {
columnIdx[idx] = i
columnCount++
}
}
enc.columnIdx = columnIdx
enc.columnCnt = columnCount
}
// TODO: since the column count doesn't exactly reflect the real column names, we only check the upper bound currently.
// See: tests/generated_columns/data/gencol.various_types.0.sql this sql has no columns, so encodeLoop will fill the
// column permutation with default, thus enc.columnCnt > len(row).
if len(row) > enc.columnCnt {
logger.Error("column count mismatch", zap.Ints("column_permutation", columnPermutation),
zap.Array("data", kv.RowArrayMarshaler(row)))
return nil, errors.Errorf("column count mismatch, expected %d, got %d", enc.columnCnt, len(row))
}
var encoded strings.Builder
encoded.Grow(8 * len(row))
encoded.WriteByte('(')
for i, field := range row {
if i != 0 {
encoded.WriteByte(',')
}
datum := field
if err := enc.appendSQL(&encoded, &datum, getColumnByIndex(cols, enc.columnIdx[i])); err != nil {
logger.Error("tidb encode failed",
zap.Array("original", kv.RowArrayMarshaler(row)),
zap.Int("originalCol", i),
log.ShortError(err),
)
return nil, err
}
}
encoded.WriteByte(')')
return tidbRow(encoded.String()), nil
}
func (be *tidbBackend) Close() {
// *Not* going to close `be.db`. The db object is normally borrowed from a
// TidbManager, so we let the manager to close it.
}
func (be *tidbBackend) MakeEmptyRows() kv.Rows {
return tidbRows(nil)
}
func (be *tidbBackend) RetryImportDelay() time.Duration {
return 0
}
func (be *tidbBackend) MaxChunkSize() int {
failpoint.Inject("FailIfImportedSomeRows", func() {
failpoint.Return(1)
})
return 1048576
}
func (be *tidbBackend) ShouldPostProcess() bool {
return true
}
func (be *tidbBackend) CheckRequirements(ctx context.Context, _ *backend.CheckCtx) error {
log.L().Info("skipping check requirements for tidb backend")
return nil
}
func (be *tidbBackend) NewEncoder(tbl table.Table, options *kv.SessionOptions) (kv.Encoder, error) {
se := kv.NewSession(options)
if options.SQLMode.HasStrictMode() {
se.GetSessionVars().SkipUTF8Check = false
se.GetSessionVars().SkipASCIICheck = false
}
return &tidbEncoder{mode: options.SQLMode, tbl: tbl, se: se}, nil
}
func (be *tidbBackend) OpenEngine(context.Context, *backend.EngineConfig, uuid.UUID) error {
return nil
}
func (be *tidbBackend) CloseEngine(context.Context, *backend.EngineConfig, uuid.UUID) error {
return nil
}
func (be *tidbBackend) CleanupEngine(context.Context, uuid.UUID) error {
return nil
}
func (be *tidbBackend) CollectLocalDuplicateRows(ctx context.Context, tbl table.Table) error {
panic("Unsupported Operation")
}
func (be *tidbBackend) CollectRemoteDuplicateRows(ctx context.Context, tbl table.Table) error {
panic("Unsupported Operation")
}
func (be *tidbBackend) ImportEngine(context.Context, uuid.UUID) error {
return nil
}
func (be *tidbBackend) WriteRows(ctx context.Context, _ uuid.UUID, tableName string, columnNames []string, rows kv.Rows) error {
var err error
outside:
for _, r := range rows.SplitIntoChunks(be.MaxChunkSize()) {
for i := 0; i < writeRowsMaxRetryTimes; i++ {
err = be.WriteRowsToDB(ctx, tableName, columnNames, r)
switch {
case err == nil:
continue outside
case common.IsRetryableError(err):
// retry next loop
default:
return err
}
}
return errors.Annotatef(err, "[%s] write rows reach max retry %d and still failed", tableName, writeRowsMaxRetryTimes)
}
return nil
}
func (be *tidbBackend) WriteRowsToDB(ctx context.Context, tableName string, columnNames []string, r kv.Rows) error {
rows := r.(tidbRows)
if len(rows) == 0 {
return nil
}
var insertStmt strings.Builder
switch be.onDuplicate {
case config.ReplaceOnDup:
insertStmt.WriteString("REPLACE INTO ")
case config.IgnoreOnDup:
insertStmt.WriteString("INSERT IGNORE INTO ")
case config.ErrorOnDup:
insertStmt.WriteString("INSERT INTO ")
}
insertStmt.WriteString(tableName)
if len(columnNames) > 0 {
insertStmt.WriteByte('(')
for i, colName := range columnNames {
if i != 0 {
insertStmt.WriteByte(',')
}
common.WriteMySQLIdentifier(&insertStmt, colName)
}
insertStmt.WriteByte(')')
}
insertStmt.WriteString(" VALUES")
// Note: we are not going to do interpolation (prepared statements) to avoid
// complication arise from data length overflow of BIT and BINARY columns
for i, row := range rows {
if i != 0 {
insertStmt.WriteByte(',')
}
insertStmt.WriteString(string(row))
}
// Retry will be done externally, so we're not going to retry here.
_, err := be.db.ExecContext(ctx, insertStmt.String())
if err != nil && !common.IsContextCanceledError(err) {
log.L().Error("execute statement failed", zap.String("stmt", redact.String(insertStmt.String())),
zap.Array("rows", rows), zap.Error(err))
}
failpoint.Inject("FailIfImportedSomeRows", func() {
panic("forcing failure due to FailIfImportedSomeRows, before saving checkpoint")
})
return errors.Trace(err)
}
//nolint:nakedret // TODO: refactor
func (be *tidbBackend) FetchRemoteTableModels(ctx context.Context, schemaName string) (tables []*model.TableInfo, err error) {
s := common.SQLWithRetry{
DB: be.db,
Logger: log.L(),
}
err = s.Transact(ctx, "fetch table columns", func(c context.Context, tx *sql.Tx) error {
var versionStr string
if err = tx.QueryRowContext(ctx, "SELECT version()").Scan(&versionStr); err != nil {
return err
}
tidbVersion, err := version.ExtractTiDBVersion(versionStr)
if err != nil {
return err
}
rows, e := tx.Query(`
SELECT table_name, column_name, column_type, extra
FROM information_schema.columns
WHERE table_schema = ?
ORDER BY table_name, ordinal_position;
`, schemaName)
if e != nil {
return e
}
defer rows.Close()
var (
curTableName string
curColOffset int
curTable *model.TableInfo
)
for rows.Next() {
var tableName, columnName, columnType, columnExtra string
if e := rows.Scan(&tableName, &columnName, &columnType, &columnExtra); e != nil {
return e
}
if tableName != curTableName {
curTable = &model.TableInfo{
Name: model.NewCIStr(tableName),
State: model.StatePublic,
PKIsHandle: true,
}
tables = append(tables, curTable)
curTableName = tableName
curColOffset = 0
}
// see: https://github.com/pingcap/parser/blob/3b2fb4b41d73710bc6c4e1f4e8679d8be6a4863e/types/field_type.go#L185-L191
var flag uint
if strings.HasSuffix(columnType, "unsigned") {
flag |= mysql.UnsignedFlag
}
if strings.Contains(columnExtra, "auto_increment") {
flag |= mysql.AutoIncrementFlag
}
curTable.Columns = append(curTable.Columns, &model.ColumnInfo{
Name: model.NewCIStr(columnName),
Offset: curColOffset,
State: model.StatePublic,
FieldType: types.FieldType{
Flag: flag,
},
})
curColOffset++
}
if rows.Err() != nil {
return rows.Err()
}
// shard_row_id/auto random is only available after tidb v4.0.0
// `show table next_row_id` is also not available before tidb v4.0.0
if tidbVersion.Major < 4 {
return nil
}
// init auto id column for each table
for _, tbl := range tables {
tblName := common.UniqueTable(schemaName, tbl.Name.O)
autoIDInfos, err := FetchTableAutoIDInfos(ctx, tx, tblName)
if err != nil {
return errors.Trace(err)
}
for _, info := range autoIDInfos {
for _, col := range tbl.Columns {
if col.Name.O == info.Column {
switch info.Type {
case "AUTO_INCREMENT":
col.Flag |= mysql.AutoIncrementFlag
case "AUTO_RANDOM":
col.Flag |= mysql.PriKeyFlag
tbl.PKIsHandle = true
// set a stub here, since we don't really need the real value
tbl.AutoRandomBits = 1
}
}
}
}
}
return nil
})
return
}
func (be *tidbBackend) EngineFileSizes() []backend.EngineFileSize {
return nil
}
func (be *tidbBackend) FlushEngine(context.Context, uuid.UUID) error {
return nil
}
func (be *tidbBackend) FlushAllEngines(context.Context) error {
return nil
}
func (be *tidbBackend) ResetEngine(context.Context, uuid.UUID) error {
return errors.New("cannot reset an engine in TiDB backend")
}
func (be *tidbBackend) LocalWriter(
ctx context.Context,
cfg *backend.LocalWriterConfig,
engineUUID uuid.UUID,
) (backend.EngineWriter, error) {
return &Writer{be: be, engineUUID: engineUUID}, nil
}
type Writer struct {
be *tidbBackend
engineUUID uuid.UUID
}
func (w *Writer) Close(ctx context.Context) (backend.ChunkFlushStatus, error) {
return nil, nil
}
func (w *Writer) AppendRows(ctx context.Context, tableName string, columnNames []string, rows kv.Rows) error {
return w.be.WriteRows(ctx, w.engineUUID, tableName, columnNames, rows)
}
func (w *Writer) IsSynced() bool {
return true
}
type TableAutoIDInfo struct {
Column string
NextID int64
Type string
}
func FetchTableAutoIDInfos(ctx context.Context, exec common.QueryExecutor, tableName string) ([]*TableAutoIDInfo, error) {
rows, e := exec.QueryContext(ctx, fmt.Sprintf("SHOW TABLE %s NEXT_ROW_ID", tableName))
if e != nil {
return nil, errors.Trace(e)
}
var autoIDInfos []*TableAutoIDInfo
for rows.Next() {
var (
dbName, tblName, columnName, idType string
nextID int64
)
columns, err := rows.Columns()
if err != nil {
return nil, errors.Trace(err)
}
//+--------------+------------+-------------+--------------------+----------------+
//| DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID | ID_TYPE |
//+--------------+------------+-------------+--------------------+----------------+
//| testsysbench | t | _tidb_rowid | 1 | AUTO_INCREMENT |
//+--------------+------------+-------------+--------------------+----------------+
// if columns length is 4, it doesn't contains the last column `ID_TYPE`, and it will always be 'AUTO_INCREMENT'
// for v4.0.0~v4.0.2 show table t next_row_id only returns 4 columns.
if len(columns) == 4 {
err = rows.Scan(&dbName, &tblName, &columnName, &nextID)
idType = "AUTO_INCREMENT"
} else {
err = rows.Scan(&dbName, &tblName, &columnName, &nextID, &idType)
}
if err != nil {
return nil, errors.Trace(err)
}
autoIDInfos = append(autoIDInfos, &TableAutoIDInfo{
Column: columnName,
NextID: nextID,
Type: idType,
})
}
// Defer in for-loop would be costly, anyway, we don't need those rows after this turn of iteration.
//nolint:sqlclosecheck
if err := rows.Close(); err != nil {
return nil, errors.Trace(err)
}
if rows.Err() != nil {
return nil, errors.Trace(rows.Err())
}
return autoIDInfos, nil
}