forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
464 lines (400 loc) · 14.2 KB
/
diff.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
// Copyright 2018 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 main
import (
"context"
"database/sql"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb-tools/pkg/dbutil"
"github.com/pingcap/tidb-tools/pkg/diff"
router "github.com/pingcap/tidb-tools/pkg/table-router"
"github.com/pingcap/tidb-tools/pkg/utils"
tidbconfig "github.com/pingcap/tidb/config"
"go.uber.org/zap"
)
// Diff contains two sql DB, used for comparing.
type Diff struct {
sourceDBs map[string]DBConfig
targetDB DBConfig
chunkSize int
sample int
checkThreadCount int
useChecksum bool
useCheckpoint bool
onlyUseChecksum bool
ignoreDataCheck bool
ignoreStructCheck bool
tables map[string]map[string]*TableConfig
fixSQLFile *os.File
report *Report
tidbInstanceID string
tableRouter *router.Table
cpDB *sql.DB
ctx context.Context
}
// NewDiff returns a Diff instance.
func NewDiff(ctx context.Context, cfg *Config) (diff *Diff, err error) {
diff = &Diff{
sourceDBs: make(map[string]DBConfig),
chunkSize: cfg.ChunkSize,
sample: cfg.Sample,
checkThreadCount: cfg.CheckThreadCount,
useChecksum: cfg.UseChecksum,
useCheckpoint: cfg.UseCheckpoint,
onlyUseChecksum: cfg.OnlyUseChecksum,
ignoreDataCheck: cfg.IgnoreDataCheck,
ignoreStructCheck: cfg.IgnoreStructCheck,
tables: make(map[string]map[string]*TableConfig),
report: NewReport(),
ctx: ctx,
}
if err = diff.init(cfg); err != nil {
diff.Close()
return nil, errors.Trace(err)
}
return diff, nil
}
func (df *Diff) init(cfg *Config) (err error) {
// create connection for source.
if err = df.CreateDBConn(cfg); err != nil {
return errors.Trace(err)
}
if err = df.AdjustTableConfig(cfg); err != nil {
return errors.Trace(err)
}
df.fixSQLFile, err = os.Create(cfg.FixSQLFile)
if err != nil {
return errors.Trace(err)
}
return nil
}
// CreateDBConn creates db connections for source and target.
func (df *Diff) CreateDBConn(cfg *Config) (err error) {
for _, source := range cfg.SourceDBCfg {
source.Conn, err = diff.CreateDB(df.ctx, source.DBConfig, cfg.CheckThreadCount)
if err != nil {
return errors.Errorf("create source db %s error %v", source.DBConfig.String(), err)
}
df.sourceDBs[source.InstanceID] = source
}
// create connection for target.
cfg.TargetDBCfg.Conn, err = diff.CreateDB(df.ctx, cfg.TargetDBCfg.DBConfig, cfg.CheckThreadCount)
if err != nil {
return errors.Errorf("create target db %s error %v", cfg.TargetDBCfg.DBConfig.String(), err)
}
df.targetDB = cfg.TargetDBCfg
df.cpDB, err = diff.CreateDBForCP(df.ctx, cfg.TargetDBCfg.DBConfig)
if err != nil {
return errors.Errorf("create checkpoint db %s error %v", cfg.TargetDBCfg.DBConfig.String(), err)
}
return nil
}
// AdjustTableConfig adjusts the table's config by check-tables and table-config.
func (df *Diff) AdjustTableConfig(cfg *Config) (err error) {
// to support table with auto_random in TiDB
tidbCfg := tidbconfig.GetGlobalConfig()
tidbCfg.Experimental.AllowAutoRandom = true
tidbconfig.StoreGlobalConfig(tidbCfg)
df.tableRouter, err = router.NewTableRouter(false, cfg.TableRules)
if err != nil {
return errors.Trace(err)
}
allTablesMap, err := df.GetAllTables(cfg)
if err != nil {
return errors.Trace(err)
}
// get all source table's matched target table
// target database name => target table name => all matched source table instance
sourceTablesMap := make(map[string]map[string][]TableInstance)
for instanceID, allSchemas := range allTablesMap {
if instanceID == df.targetDB.InstanceID {
continue
}
for schema, allTables := range allSchemas {
for table := range allTables {
targetSchema, targetTable, err := df.tableRouter.Route(schema, table)
if err != nil {
return errors.Errorf("get route result for %s.%s.%s failed, error %v", instanceID, schema, table, err)
}
if _, ok := sourceTablesMap[targetSchema]; !ok {
sourceTablesMap[targetSchema] = make(map[string][]TableInstance)
}
if _, ok := sourceTablesMap[targetSchema][targetTable]; !ok {
sourceTablesMap[targetSchema][targetTable] = make([]TableInstance, 0, 1)
}
sourceTablesMap[targetSchema][targetTable] = append(sourceTablesMap[targetSchema][targetTable], TableInstance{
InstanceID: instanceID,
Schema: schema,
Table: table,
})
}
}
}
// fill the table information.
// will add default source information, don't worry, we will use table config's info replace this later.
for _, schemaTables := range cfg.Tables {
df.tables[schemaTables.Schema] = make(map[string]*TableConfig)
tables := make([]string, 0, len(schemaTables.Tables))
allTables, ok := allTablesMap[df.targetDB.InstanceID][schemaTables.Schema]
if !ok {
return errors.NotFoundf("schema %s.%s", df.targetDB.InstanceID, schemaTables.Schema)
}
for _, table := range schemaTables.Tables {
matchedTables, err := df.GetMatchTable(df.targetDB, schemaTables.Schema, table, allTables)
if err != nil {
return errors.Trace(err)
}
//exclude those in "exclude-tables"
for _, t := range matchedTables {
if df.InExcludeTables(schemaTables.ExcludeTables, t) {
continue
} else {
tables = append(tables, t)
}
}
}
for _, tableName := range tables {
tableInfo, err := dbutil.GetTableInfo(df.ctx, df.targetDB.Conn, schemaTables.Schema, tableName, df.targetDB.SQLMode)
if err != nil {
return errors.Errorf("get table %s.%s's inforamtion error %s", schemaTables.Schema, tableName, errors.ErrorStack(err))
}
if _, ok := df.tables[schemaTables.Schema][tableName]; ok {
log.Error("duplicate config for one table", zap.String("table", dbutil.TableName(schemaTables.Schema, tableName)))
continue
}
sourceTables := make([]TableInstance, 0, 1)
if _, ok := sourceTablesMap[schemaTables.Schema][tableName]; ok {
log.Info("find matched source tables", zap.Reflect("source tables", sourceTablesMap[schemaTables.Schema][tableName]), zap.String("target schema", schemaTables.Schema), zap.String("table", tableName))
sourceTables = sourceTablesMap[schemaTables.Schema][tableName]
} else {
// use same database name and table name
sourceTables = append(sourceTables, TableInstance{
InstanceID: cfg.SourceDBCfg[0].InstanceID,
Schema: schemaTables.Schema,
Table: tableName,
})
}
df.tables[schemaTables.Schema][tableName] = &TableConfig{
TableInstance: TableInstance{
Schema: schemaTables.Schema,
Table: tableName,
},
TargetTableInfo: tableInfo,
Range: "TRUE",
SourceTables: sourceTables,
}
}
}
for _, table := range cfg.TableCfgs {
if _, ok := df.tables[table.Schema]; !ok {
return errors.NotFoundf("schema %s in check tables", table.Schema)
}
if _, ok := df.tables[table.Schema][table.Table]; !ok {
return errors.NotFoundf("table %s.%s in check tables", table.Schema, table.Table)
}
sourceTables := make([]TableInstance, 0, len(table.SourceTables))
for _, sourceTable := range table.SourceTables {
if _, ok := df.sourceDBs[sourceTable.InstanceID]; !ok {
return errors.Errorf("unkonwn database instance id %s", sourceTable.InstanceID)
}
allTables, ok := allTablesMap[df.sourceDBs[sourceTable.InstanceID].InstanceID][sourceTable.Schema]
if !ok {
return errors.Errorf("unknown schema %s in database %+v", sourceTable.Schema, df.sourceDBs[sourceTable.InstanceID])
}
tables, err := df.GetMatchTable(df.sourceDBs[sourceTable.InstanceID], sourceTable.Schema, sourceTable.Table, allTables)
if err != nil {
return errors.Trace(err)
}
for _, table := range tables {
sourceTables = append(sourceTables, TableInstance{
InstanceID: sourceTable.InstanceID,
Schema: sourceTable.Schema,
Table: table,
})
}
}
if len(sourceTables) != 0 {
df.tables[table.Schema][table.Table].SourceTables = sourceTables
}
if table.Range != "" {
df.tables[table.Schema][table.Table].Range = table.Range
}
df.tables[table.Schema][table.Table].IgnoreColumns = table.IgnoreColumns
df.tables[table.Schema][table.Table].Fields = table.Fields
df.tables[table.Schema][table.Table].Collation = table.Collation
}
return nil
}
// GetAllTables get all tables in all databases.
func (df *Diff) GetAllTables(cfg *Config) (map[string]map[string]map[string]interface{}, error) {
// instanceID => schema => table
allTablesMap := make(map[string]map[string]map[string]interface{})
allTablesMap[df.targetDB.InstanceID] = make(map[string]map[string]interface{})
targetSchemas, err := dbutil.GetSchemas(df.ctx, df.targetDB.Conn)
if err != nil {
return nil, errors.Annotatef(err, "get schemas from %s", df.targetDB.InstanceID)
}
for _, schema := range targetSchemas {
allTables, err := dbutil.GetTables(df.ctx, df.targetDB.Conn, schema)
if err != nil {
return nil, errors.Annotatef(err, "get tables from %s.%s", df.targetDB.InstanceID, schema)
}
allTablesMap[df.targetDB.InstanceID][schema] = utils.SliceToMap(allTables)
}
for _, source := range df.sourceDBs {
allTablesMap[source.InstanceID] = make(map[string]map[string]interface{})
sourceSchemas, err := dbutil.GetSchemas(df.ctx, source.Conn)
if err != nil {
return nil, errors.Annotatef(err, "get schemas from %s", source.InstanceID)
}
for _, schema := range sourceSchemas {
allTables, err := dbutil.GetTables(df.ctx, source.Conn, schema)
if err != nil {
return nil, errors.Annotatef(err, "get tables from %s.%s", source.InstanceID, schema)
}
allTablesMap[source.InstanceID][schema] = utils.SliceToMap(allTables)
}
}
return allTablesMap, nil
}
// GetMatchTable returns all the matched table.
func (df *Diff) GetMatchTable(db DBConfig, schema, table string, allTables map[string]interface{}) ([]string, error) {
tableNames := make([]string, 0, 1)
if table[0] == '~' {
tableRegex := regexp.MustCompile(fmt.Sprintf("(?i)%s", table[1:]))
for tableName := range allTables {
if !tableRegex.MatchString(tableName) {
continue
}
tableNames = append(tableNames, tableName)
}
} else {
if _, ok := allTables[table]; ok {
tableNames = append(tableNames, table)
} else {
return nil, errors.Errorf("%s.%s not found in %s", schema, table, db.InstanceID)
}
}
return tableNames, nil
}
// Close closes file and database connection.
func (df *Diff) Close() {
if df.fixSQLFile != nil {
df.fixSQLFile.Close()
}
for _, db := range df.sourceDBs {
if db.Conn != nil {
db.Conn.Close()
}
}
if df.targetDB.Conn != nil {
df.targetDB.Conn.Close()
}
if df.cpDB != nil {
df.cpDB.Close()
}
}
// Equal tests whether two database have same data and schema.
func (df *Diff) Equal() (err error) {
defer df.Close()
for _, schema := range df.tables {
for _, table := range schema {
sourceTables := make([]*diff.TableInstance, 0, len(table.SourceTables))
for _, sourceTable := range table.SourceTables {
sourceTableInstance := &diff.TableInstance{
Conn: df.sourceDBs[sourceTable.InstanceID].Conn,
Schema: sourceTable.Schema,
Table: sourceTable.Table,
InstanceID: sourceTable.InstanceID,
SQLMode: df.sourceDBs[sourceTable.InstanceID].SQLMode,
}
sourceTables = append(sourceTables, sourceTableInstance)
}
targetTableInstance := &diff.TableInstance{
Conn: df.targetDB.Conn,
Schema: table.Schema,
Table: table.Table,
InstanceID: df.targetDB.InstanceID,
SQLMode: df.targetDB.SQLMode,
}
// find tidb instance for getting statistical information to split chunk
var tidbStatsSource *diff.TableInstance
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
isTiDB, err := dbutil.IsTiDB(ctx, targetTableInstance.Conn)
if err != nil {
log.Warn("judge instance is tidb failed", zap.Error(err))
} else if isTiDB {
tidbStatsSource = targetTableInstance
} else if len(sourceTables) == 1 {
isTiDB, err := dbutil.IsTiDB(ctx, sourceTables[0].Conn)
if err != nil {
log.Warn("judge instance is tidb failed", zap.Error(err))
} else if isTiDB {
tidbStatsSource = sourceTables[0]
}
}
td := &diff.TableDiff{
SourceTables: sourceTables,
TargetTable: targetTableInstance,
IgnoreColumns: table.IgnoreColumns,
Fields: table.Fields,
Range: table.Range,
Collation: table.Collation,
ChunkSize: df.chunkSize,
Sample: df.sample,
CheckThreadCount: df.checkThreadCount,
UseChecksum: df.useChecksum,
UseCheckpoint: df.useCheckpoint,
OnlyUseChecksum: df.onlyUseChecksum,
IgnoreStructCheck: df.ignoreStructCheck,
IgnoreDataCheck: df.ignoreDataCheck,
TiDBStatsSource: tidbStatsSource,
CpDB: df.cpDB,
}
structEqual, dataEqual, err := td.Equal(df.ctx, func(dml string) error {
_, err := df.fixSQLFile.WriteString(fmt.Sprintf("%s\n", dml))
return errors.Trace(err)
})
if err != nil {
log.Error("check failed", zap.String("table", dbutil.TableName(table.Schema, table.Table)), zap.Error(err))
df.report.SetTableMeetError(table.Schema, table.Table, err)
df.report.FailedNum++
continue
}
df.report.SetTableStructCheckResult(table.Schema, table.Table, structEqual)
df.report.SetTableDataCheckResult(table.Schema, table.Table, dataEqual)
if structEqual && dataEqual {
df.report.PassNum++
} else {
df.report.FailedNum++
}
}
}
return
}
// Judge if a table is in "exclude-tables" list
func (df *Diff) InExcludeTables(exclude_tables []string, table string) bool {
for _, exclude_table := range exclude_tables {
if strings.EqualFold(exclude_table, table) {
return true
}
}
return false
}