forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_structure.go
500 lines (429 loc) · 13.8 KB
/
table_structure.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
// 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 check
import (
"bytes"
"context"
"database/sql"
"fmt"
"strings"
"github.com/pingcap/tidb-tools/pkg/column-mapping"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb-tools/pkg/dbutil"
)
// AutoIncrementKeyChecking is an identification for auto increment key checking
const AutoIncrementKeyChecking = "auto-increment key checking"
// hold information of incompatibility option
type incompatibilityOption struct {
state State
instruction string
errMessage string
}
// String returns raw text of this incompatibility option
func (o *incompatibilityOption) String() string {
var text bytes.Buffer
if len(o.errMessage) > 0 {
fmt.Fprintf(&text, "information: %s\n", o.errMessage)
}
if len(o.instruction) > 0 {
fmt.Fprintf(&text, "instruction: %s\n", o.instruction)
}
return text.String()
}
// TablesChecker checks compatibility of table structures, there are differences between MySQL and TiDB.
// In generally we need to check definitions of columns, constraints and table options.
// Because of the early TiDB engineering design, we did not have a complete list of check items, which are all based on experience now.
type TablesChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
tables map[string][]string // schema => []table; if []table is empty, query tables from db
}
// NewTablesChecker returns a Checker
func NewTablesChecker(db *sql.DB, dbinfo *dbutil.DBConfig, tables map[string][]string) Checker {
return &TablesChecker{
db: db,
dbinfo: dbinfo,
tables: tables,
}
}
// Check implements Checker interface
func (c *TablesChecker) Check(ctx context.Context) *Result {
r := &Result{
Name: c.Name(),
Desc: "check compatibility of table structure",
State: StateSuccess,
Extra: fmt.Sprintf("address of db instance - %s:%d", c.dbinfo.Host, c.dbinfo.Port),
}
var (
err error
options = make(map[string][]*incompatibilityOption)
statements = make(map[string]string)
)
for schema, tables := range c.tables {
if len(tables) == 0 {
tables, err = dbutil.GetTables(ctx, c.db, schema)
if err != nil {
markCheckError(r, err)
return r
}
}
for _, table := range tables {
tableName := dbutil.TableName(schema, table)
statement, err := dbutil.GetCreateTableSQL(ctx, c.db, schema, table)
if err != nil {
markCheckError(r, err)
return r
}
opts := c.checkCreateSQL(statement)
if len(opts) > 0 {
options[tableName] = opts
statements[tableName] = statement
}
}
}
var information bytes.Buffer
for name, opts := range options {
if len(opts) == 0 {
continue
}
var (
errMessages = make([]string, 0, len(opts))
warningMessages = make([]string, 0, len(opts))
)
fmt.Fprintf(&information, "********** table %s **********\n", name)
fmt.Fprintf(&information, "statement: %s\n", statements[name])
for _, option := range opts {
switch option.state {
case StateWarning:
if len(r.State) == 0 {
r.State = StateWarning
}
warningMessages = append(warningMessages, option.String())
case StateFailure:
r.State = StateFailure
errMessages = append(errMessages, option.String())
}
}
if len(errMessages) > 0 {
fmt.Fprint(&information, "---------- error options ----------\n")
fmt.Fprintf(&information, "%s\n", strings.Join(errMessages, "\n"))
}
if len(warningMessages) > 0 {
fmt.Fprint(&information, "---------- warning options ----------\n")
fmt.Fprintf(&information, "%s\n", strings.Join(warningMessages, "\n"))
}
}
r.ErrorMsg = information.String()
return r
}
// Name implements Checker interface
func (c *TablesChecker) Name() string {
return "table structure compatibility check"
}
func (c *TablesChecker) checkCreateSQL(statement string) []*incompatibilityOption {
stmt, err := parser.New().ParseOneStmt(statement, "", "")
if err != nil {
return []*incompatibilityOption{
{
state: StateFailure,
errMessage: err.Error(),
},
}
}
// Analyze ast
return c.checkAST(stmt)
}
func (c *TablesChecker) checkAST(stmt ast.StmtNode) []*incompatibilityOption {
st, ok := stmt.(*ast.CreateTableStmt)
if !ok {
return []*incompatibilityOption{
{
state: StateFailure,
errMessage: fmt.Sprintf("Expect CreateTableStmt but got %T", stmt),
},
}
}
var options []*incompatibilityOption
// check columns
for _, def := range st.Cols {
option := c.checkColumnDef(def)
if option != nil {
options = append(options, option)
}
}
// check constrains
for _, cst := range st.Constraints {
option := c.checkConstraint(cst)
if option != nil {
options = append(options, option)
}
}
// check options
for _, opt := range st.Options {
option := c.checkTableOption(opt)
if option != nil {
options = append(options, option)
}
}
return options
}
func (c *TablesChecker) checkColumnDef(def *ast.ColumnDef) *incompatibilityOption {
return nil
}
func (c *TablesChecker) checkConstraint(cst *ast.Constraint) *incompatibilityOption {
switch cst.Tp {
case ast.ConstraintForeignKey:
return &incompatibilityOption{
state: StateWarning,
instruction: fmt.Sprintf("please ref document: https://github.com/pingcap/docs-cn/blob/master/sql/ddl.md"),
errMessage: fmt.Sprintf("Foreign Key %s is parsed but ignored by TiDB.", cst.Name),
}
}
return nil
}
func (c *TablesChecker) checkTableOption(opt *ast.TableOption) *incompatibilityOption {
switch opt.Tp {
case ast.TableOptionCharset:
// Check charset
cs := strings.ToLower(opt.StrValue)
if cs != "binary" && !charset.ValidCharsetAndCollation(cs, "") {
return &incompatibilityOption{
state: StateFailure,
instruction: fmt.Sprintf("please ref document: https://github.com/pingcap/docs-cn/blob/master/sql/character-set-support.md"),
errMessage: fmt.Sprintf("unsupport charset %s", opt.StrValue),
}
}
}
return nil
}
// ShardingTablesCheck checks consistency of table structures of one sharding group
// * check whether they have same column list
// * check whether they have auto_increment key
type ShardingTablesCheck struct {
name string
dbs map[string]*sql.DB
tables map[string]map[string][]string // instance => {schema: [table1, table2, ...]}
mapping map[string]*column.Mapping
checkAutoIncrementPrimaryKey bool
}
// NewShardingTablesCheck returns a Checker
func NewShardingTablesCheck(name string, dbs map[string]*sql.DB, tables map[string]map[string][]string, mapping map[string]*column.Mapping, checkAutoIncrementPrimaryKey bool) Checker {
return &ShardingTablesCheck{
name: name,
dbs: dbs,
tables: tables,
mapping: mapping,
checkAutoIncrementPrimaryKey: checkAutoIncrementPrimaryKey,
}
}
// Check implements Checker interface
func (c *ShardingTablesCheck) Check(ctx context.Context) *Result {
r := &Result{
Name: c.Name(),
Desc: "check consistency of sharding table structures",
State: StateSuccess,
Extra: fmt.Sprintf("sharding %s", c.name),
}
var (
stmtNode *ast.CreateTableStmt
tableName string
)
for instance, schemas := range c.tables {
db, ok := c.dbs[instance]
if !ok {
markCheckError(r, errors.NotFoundf("client for instance %s", instance))
return r
}
for schema, tables := range schemas {
for _, table := range tables {
statement, err := dbutil.GetCreateTableSQL(ctx, db, schema, table)
if err != nil {
markCheckError(r, err)
r.Extra = fmt.Sprintf("instance %s on sharding %s", instance, c.name)
return r
}
info, err := dbutil.GetTableInfoBySQL(statement)
if err != nil {
markCheckError(r, err)
r.Extra = fmt.Sprintf("instance %s on sharding %s", instance, c.name)
return r
}
stmt, err := parser.New().ParseOneStmt(statement, "", "")
if err != nil {
markCheckError(r, errors.Annotatef(err, "statement %s", statement))
r.Extra = fmt.Sprintf("instance %s on sharding %s", instance, c.name)
return r
}
ctStmt, ok := stmt.(*ast.CreateTableStmt)
if !ok {
markCheckError(r, errors.Errorf("Expect CreateTableStmt but got %T", stmt))
r.Extra = fmt.Sprintf("instance %s on sharding %s", instance, c.name)
return r
}
if c.checkAutoIncrementPrimaryKey {
passed := c.checkAutoIncrementKey(instance, schema, table, ctStmt, info, r)
if !passed {
return r
}
}
if stmtNode == nil {
stmtNode = ctStmt
tableName = dbutil.TableName(schema, table)
continue
}
err = c.checkConsistency(stmtNode, ctStmt, tableName, dbutil.TableName(schema, table))
if err != nil {
markCheckError(r, err)
r.Extra = fmt.Sprintf("instance %s on sharding %s", instance, c.name)
r.Instruction = "please set same table structure for sharding tables"
return r
}
}
}
}
return r
}
func (c *ShardingTablesCheck) checkAutoIncrementKey(instance, schema, table string, ctStmt *ast.CreateTableStmt, info *model.TableInfo, r *Result) bool {
autoIncrementKeys := c.findAutoIncrementKey(ctStmt, info)
for columnName, isBigInt := range autoIncrementKeys {
hasMatchedRule := false
if cm, ok1 := c.mapping[instance]; ok1 {
ruleSet := cm.Selector.Match(schema, table)
for _, rule := range ruleSet {
r, ok2 := rule.(*column.Rule)
if !ok2 {
continue
}
if r.Expression == column.PartitionID && r.TargetColumn == columnName {
hasMatchedRule = true
break
}
}
if hasMatchedRule && !isBigInt {
r.State = StateFailure
r.ErrorMsg = fmt.Sprintf("instance %s table `%s`.`%s` of sharding %s have auto-increment key %s and column mapping, but type of %s should be bigint", instance, schema, table, c.name, columnName, columnName)
r.Instruction = "please set auto-increment key type to bigint"
r.Extra = AutoIncrementKeyChecking
return false
}
}
if !hasMatchedRule {
r.State = StateFailure
r.ErrorMsg = fmt.Sprintf("instance %s table `%s`.`%s` of sharding %s have auto-increment key, would conflict with each other to cause data corruption", instance, schema, table, c.name)
r.Instruction = "please set column mapping rules for them, or handle it by yourself"
r.Extra = AutoIncrementKeyChecking
return false
}
}
return true
}
func (c *ShardingTablesCheck) findAutoIncrementKey(stmt *ast.CreateTableStmt, info *model.TableInfo) map[string]bool {
autoIncrementKeys := make(map[string]bool)
autoIncrementCols := make(map[string]bool)
for _, col := range stmt.Cols {
var (
hasAutoIncrementOpt bool
isUnique bool
)
for _, opt := range col.Options {
switch opt.Tp {
case ast.ColumnOptionAutoIncrement:
hasAutoIncrementOpt = true
case ast.ColumnOptionPrimaryKey, ast.ColumnOptionUniqKey:
isUnique = true
}
}
if hasAutoIncrementOpt {
if isUnique {
autoIncrementKeys[col.Name.Name.O] = col.Tp.Tp == mysql.TypeLonglong
} else {
autoIncrementCols[col.Name.Name.O] = col.Tp.Tp == mysql.TypeLonglong
}
}
}
for _, index := range info.Indices {
if index.Unique || index.Primary {
if len(index.Columns) == 1 {
if isBigInt, ok := autoIncrementCols[index.Columns[0].Name.O]; ok {
autoIncrementKeys[index.Columns[0].Name.O] = isBigInt
}
}
}
}
return autoIncrementKeys
}
type briefColumnInfo struct {
name string
tp string
isUniqueKey bool
isPrimaryKey bool
}
func (c *briefColumnInfo) String() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%s %s", c.name, c.tp)
if c.isPrimaryKey {
fmt.Fprintln(&buf, " primary key")
} else if c.isUniqueKey {
fmt.Fprintln(&buf, " unique key")
}
return buf.String()
}
type briefColumnInfos []*briefColumnInfo
func (cs briefColumnInfos) String() string {
var colStrs = make([]string, 0, len(cs))
for _, col := range cs {
colStrs = append(colStrs, col.String())
}
return strings.Join(colStrs, "\n")
}
func (c *ShardingTablesCheck) checkConsistency(self, other *ast.CreateTableStmt, selfName, otherName string) error {
selfColumnList := getBriefColumnList(self)
otherColumnList := getBriefColumnList(other)
if len(selfColumnList) != len(otherColumnList) {
return errors.Errorf("column length mismatch (%d vs %d)\n table %s\ncolumns %s\n\ntable%s\ncolumns %s", len(selfColumnList), len(otherColumnList), selfName, selfColumnList, otherName, otherColumnList)
}
for i := range selfColumnList {
if *selfColumnList[i] != *otherColumnList[i] {
return errors.Errorf("different column definition\ncolumn %s on table %s\ncolumn %s on table %s", selfColumnList[i], selfName, otherColumnList[i], otherName)
}
}
return nil
}
func getBriefColumnList(stmt *ast.CreateTableStmt) briefColumnInfos {
columnList := make(briefColumnInfos, 0, len(stmt.Cols))
for _, col := range stmt.Cols {
bc := &briefColumnInfo{
name: col.Name.Name.L,
tp: col.Tp.String(),
}
for _, opt := range col.Options {
switch opt.Tp {
case ast.ColumnOptionPrimaryKey:
bc.isPrimaryKey = true
case ast.ColumnOptionUniqKey:
bc.isUniqueKey = true
}
}
columnList = append(columnList, bc)
}
return columnList
}
// Name implements Checker interface
func (c *ShardingTablesCheck) Name() string {
return fmt.Sprintf("sharding table %s consistency checking", c.name)
}