forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
376 lines (309 loc) · 10.9 KB
/
config.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
// 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 (
"database/sql"
"encoding/json"
"flag"
"net/url"
"strconv"
"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb-tools/pkg/dbutil"
router "github.com/pingcap/tidb-tools/pkg/table-router"
"go.uber.org/zap"
)
const (
percent0 = 0
percent100 = 100
)
var sourceInstanceMap map[string]interface{} = make(map[string]interface{})
// DBConfig is the config of database, and keep the connection.
type DBConfig struct {
dbutil.DBConfig
InstanceID string `toml:"instance-id" json:"instance-id"`
Conn *sql.DB
}
// Valid returns true if database's config is valide.
func (c *DBConfig) Valid() bool {
if c.InstanceID == "" {
log.Error("must specify source database's instance id")
return false
}
sourceInstanceMap[c.InstanceID] = struct{}{}
return true
}
// CheckTables saves the tables need to check.
type CheckTables struct {
// schema name
Schema string `toml:"schema" json:"schema"`
// table list
Tables []string `toml:"tables" json:"tables"`
ExcludeTables []string `toml:"exclude-tables" json:"exclude-tables"`
}
// TableConfig is the config of table.
type TableConfig struct {
// table's origin information
TableInstance
// columns be ignored, will not check this column's data
IgnoreColumns []string `toml:"ignore-columns"`
// field should be the primary key, unique key or field with index
Fields string `toml:"index-fields"`
// select range, for example: "age > 10 AND age < 20"
Range string `toml:"range"`
// set true if comparing sharding tables with target table, should have more than one source tables.
IsSharding bool `toml:"is-sharding"`
// saves the source tables's info.
// may have more than one source for sharding tables.
// or you want to compare table with different schema and table name.
// SourceTables can be nil when source and target is one-to-one correspondence.
SourceTables []TableInstance `toml:"source-tables"`
TargetTableInfo *model.TableInfo
// collation config in mysql/tidb
Collation string `toml:"collation"`
}
// Valid returns true if table's config is valide.
func (t *TableConfig) Valid() bool {
if t.Schema == "" || t.Table == "" {
log.Error("schema and table's name can't be empty")
return false
}
if t.IsSharding {
if len(t.SourceTables) <= 1 {
log.Error("must have more than one source tables if comparing sharding tables")
return false
}
} else {
if len(t.SourceTables) > 1 {
log.Error("have more than one source table in no sharding mode")
return false
}
}
for _, sourceTable := range t.SourceTables {
if !sourceTable.Valid() {
return false
}
}
return true
}
// TableInstance saves the base information of table.
type TableInstance struct {
// database's instance id
InstanceID string `toml:"instance-id" json:"instance-id"`
// schema name
Schema string `toml:"schema"`
// table name
Table string `toml:"table"`
}
// Valid returns true if table instance's info is valide.
// should be executed after source database's check.
func (t *TableInstance) Valid() bool {
if t.InstanceID == "" {
log.Error("must specify the database's instance id for source table")
return false
}
if _, ok := sourceInstanceMap[t.InstanceID]; !ok {
log.Error("unknown database instance id", zap.String("instance id", t.InstanceID))
return false
}
if t.Schema == "" || t.Table == "" {
log.Error("schema and table's name can't be empty")
return false
}
return true
}
// Config is the configuration.
type Config struct {
*flag.FlagSet `json:"-"`
// log level
LogLevel string `toml:"log-level" json:"log-level"`
// source database's config
SourceDBCfg []DBConfig `toml:"source-db" json:"source-db"`
// target database's config
TargetDBCfg DBConfig `toml:"target-db" json:"target-db"`
// for example, the whole data is [1...100]
// we can split these data to [1...10], [11...20], ..., [91...100]
// the [1...10] is a chunk, and it's chunk size is 10
// size of the split chunk
ChunkSize int `toml:"chunk-size" json:"chunk-size"`
// sampling check percent, for example 10 means only check 10% data
Sample int `toml:"sample-percent" json:"sample-percent"`
// how many goroutines are created to check data
CheckThreadCount int `toml:"check-thread-count" json:"check-thread-count"`
// set false if want to comapre the data directly
UseChecksum bool `toml:"use-checksum" json:"use-checksum"`
// set true if just want compare data by checksum, will skip select data when checksum is not equal.
OnlyUseChecksum bool `toml:"only-use-checksum" json:"only-use-checksum"`
// the name of the file which saves sqls used to fix different data
FixSQLFile string `toml:"fix-sql-file" json:"fix-sql-file"`
// the tables to be checked
Tables []*CheckTables `toml:"check-tables" json:"check-tables"`
// TableRules defines table name and database name's conversion relationship between source database and target database
TableRules []*router.TableRule `toml:"table-rules" json:"table-rules"`
// the config of table
TableCfgs []*TableConfig `toml:"table-config" json:"table-config"`
// ignore check table's struct
IgnoreStructCheck bool `toml:"ignore-struct-check" json:"ignore-struct-check"`
// ignore tidb stats only use randomSpliter to split chunks
IgnoreStats bool `toml:"ignore-stats" json:"ignore-stats"`
// ignore check table's data
IgnoreDataCheck bool `toml:"ignore-data-check" json:"ignore-data-check"`
// set true will continue check from the latest checkpoint
UseCheckpoint bool `toml:"use-checkpoint" json:"use-checkpoint"`
// DMAddr is dm-master's address, the format should like "http://127.0.0.1:8261"
DMAddr string `toml:"dm-addr" json:"dm-addr"`
// DMTask is dm's task name
DMTask string `toml:"dm-task" json:"dm-task"`
// config file
ConfigFile string
// print version if set true
PrintVersion bool
}
// NewConfig creates a new config.
func NewConfig() *Config {
cfg := &Config{}
cfg.FlagSet = flag.NewFlagSet("diff", flag.ContinueOnError)
fs := cfg.FlagSet
fs.StringVar(&cfg.ConfigFile, "config", "", "Config file")
fs.StringVar(&cfg.LogLevel, "L", "info", "log level: debug, info, warn, error, fatal")
fs.IntVar(&cfg.ChunkSize, "chunk-size", 1000, "diff check chunk size")
fs.IntVar(&cfg.Sample, "sample", 100, "the percent of sampling check")
fs.IntVar(&cfg.CheckThreadCount, "check-thread-count", 1, "how many goroutines are created to check data")
fs.BoolVar(&cfg.UseChecksum, "use-checksum", true, "set false if want to comapre the data directly")
fs.StringVar(&cfg.FixSQLFile, "fix-sql-file", "fix.sql", "the name of the file which saves sqls used to fix different data")
fs.BoolVar(&cfg.PrintVersion, "V", false, "print version of sync_diff_inspector")
fs.BoolVar(&cfg.IgnoreDataCheck, "ignore-data-check", false, "ignore check table's data")
fs.BoolVar(&cfg.IgnoreStructCheck, "ignore-struct-check", false, "ignore check table's struct")
fs.BoolVar(&cfg.IgnoreStats, "ignore-stats", false, "don't use tidb stats to split chunks")
fs.BoolVar(&cfg.UseCheckpoint, "use-checkpoint", true, "set true will continue check from the latest checkpoint")
return cfg
}
// Parse parses flag definitions from the argument list.
func (c *Config) Parse(arguments []string) error {
// Parse first to get config file.
err := c.FlagSet.Parse(arguments)
if err != nil {
return errors.Trace(err)
}
// Load config file if specified.
if c.ConfigFile != "" {
err = c.configFromFile(c.ConfigFile)
if err != nil {
return errors.Trace(err)
}
}
// Parse again to replace with command line options.
err = c.FlagSet.Parse(arguments)
if err != nil {
return errors.Trace(err)
}
if len(c.FlagSet.Args()) != 0 {
return errors.Errorf("'%s' is an invalid flag", c.FlagSet.Arg(0))
}
return nil
}
func (c *Config) String() string {
cfg, err := json.Marshal(c)
if err != nil {
return "<nil>"
}
return string(cfg)
}
// configFromFile loads config from file.
func (c *Config) configFromFile(path string) error {
meta, err := toml.DecodeFile(path, c)
if err != nil {
return errors.Trace(err)
}
if len(meta.Undecoded()) > 0 {
return errors.Errorf("unknown keys in config file %s: %v", path, meta.Undecoded())
}
return nil
}
func (c *Config) checkConfig() bool {
if c.Sample > percent100 || c.Sample < percent0 {
log.Error("sample must be greater than 0 and less than or equal to 100!")
return false
}
if c.CheckThreadCount <= 0 {
log.Error("check-thcount must greater than 0!")
return false
}
if len(c.DMAddr) != 0 {
u, err := url.Parse(c.DMAddr)
if err != nil || u.Scheme == "" || u.Host == "" {
log.Error("dm-addr's format should like 'http://127.0.0.1:8261'")
return false
}
if len(c.DMTask) == 0 {
log.Error("must set the `dm-task` if set `dm-addr`")
return false
}
emptyDBConfig := DBConfig{}
// source DB, target DB and check table's information will get from DM, should not set them
if len(c.SourceDBCfg) != 0 || c.TargetDBCfg != emptyDBConfig {
log.Error("should not set `source-db` or `target-db`, diff will generate them automatically when set `dm-addr` and `dm-task`")
return false
}
if len(c.Tables) != 0 || len(c.TableRules) != 0 || len(c.TableCfgs) != 0 {
log.Error("should not set `check-tables`, `table-rules` or `table-config`, diff will generate them automatically when set `dm-addr` and `dm-task`")
return false
}
} else {
if len(c.SourceDBCfg) == 0 {
log.Error("must have at least one source database")
return false
}
for i := range c.SourceDBCfg {
if !c.SourceDBCfg[i].Valid() {
return false
}
if c.SourceDBCfg[i].Snapshot != "" {
c.SourceDBCfg[i].Snapshot = strconv.Quote(c.SourceDBCfg[i].Snapshot)
}
}
if c.TargetDBCfg.InstanceID == "" {
c.TargetDBCfg.InstanceID = "target"
}
if c.TargetDBCfg.Snapshot != "" {
c.TargetDBCfg.Snapshot = strconv.Quote(c.TargetDBCfg.Snapshot)
}
if _, ok := sourceInstanceMap[c.TargetDBCfg.InstanceID]; ok {
log.Error("target has same instance id in source", zap.String("instance id", c.TargetDBCfg.InstanceID))
return false
}
if len(c.Tables) == 0 {
log.Error("must specify check tables")
return false
}
for _, tableCfg := range c.TableCfgs {
if !tableCfg.Valid() {
return false
}
}
}
if c.OnlyUseChecksum {
if !c.UseChecksum {
log.Error("need set use-checksum = true")
return false
}
} else {
if len(c.FixSQLFile) == 0 {
log.Warn("fix-sql-file is invalid, will use default value 'fix.sql'")
c.FixSQLFile = "fix.sql"
}
}
return true
}