-
Notifications
You must be signed in to change notification settings - Fork 65
/
config.go
594 lines (489 loc) · 17.5 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
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
package ghostferry
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"time"
sql "github.com/Shopify/ghostferry/sqlwrapper"
"github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
)
const (
VerifierTypeChecksumTable = "ChecksumTable"
VerifierTypeIterative = "Iterative"
VerifierTypeInline = "Inline"
VerifierTypeNoVerification = "NoVerification"
DefaultNet = "tcp"
DefaultMarginalia = "application:ghostferry"
)
type TLSConfig struct {
CertPath string
ServerName string
tlsConfig *tls.Config
}
func (this *TLSConfig) BuildConfig() (*tls.Config, error) {
if this.tlsConfig == nil {
certPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(this.CertPath)
if err != nil {
return nil, err
}
if ok := certPool.AppendCertsFromPEM(pem); !ok {
return nil, errors.New("unable to append pem")
}
this.tlsConfig = &tls.Config{
RootCAs: certPool,
ServerName: this.ServerName,
}
}
return this.tlsConfig, nil
}
type DatabaseConfig struct {
Host string
Port uint16
Net string
User string
Pass string
Collation string
Params map[string]string
TLS *TLSConfig
// SQL annotations used to differentiate Ghostferry's DMLs
// against other actor's. This will default to the defaultMarginalia
// constant above if not set.
//
// This is used to ensure any changes to the Target during the move process
// are performed only by Ghostferry (until cutover). Otherwise, the modification
// will be identified as data corruption and fail the move.
Marginalia string
}
func (c *DatabaseConfig) MySQLConfig() (*mysql.Config, error) {
var addr string
if c.Net == "unix" {
addr = c.Host
} else {
addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
}
cfg := &mysql.Config{
User: c.User,
Passwd: c.Pass,
Net: c.Net,
Addr: addr,
Collation: c.Collation,
Params: c.Params,
AllowNativePasswords: true,
MultiStatements: true,
}
if c.TLS != nil {
tlsConfig, err := c.TLS.BuildConfig()
if err != nil {
return nil, fmt.Errorf("failed to build TLS config: %v", err)
}
cfgName := fmt.Sprintf("%s@%s:%s", c.User, c.Host, c.Port)
err = mysql.RegisterTLSConfig(cfgName, tlsConfig)
if err != nil {
return nil, fmt.Errorf("failed to register TLS config: %v", err)
}
cfg.TLSConfig = cfgName
}
return cfg, nil
}
func (c *DatabaseConfig) Validate() error {
if c.Host == "" {
return fmt.Errorf("host is empty")
}
if c.Port == 0 {
return fmt.Errorf("port is not specified")
}
if c.Net == "" {
c.Net = DefaultNet
} else if c.Net == "unix" {
c.Port = 0
} else if c.Net != "tcp" && c.Net != "unix" {
return fmt.Errorf("net is unknown (valid modes: tcp, unix)")
}
if c.User == "" {
return fmt.Errorf("user is empty")
}
err := c.assertParamSet("time_zone", "'+00:00'")
if err != nil {
return err
}
err = c.assertParamSet("sql_mode", "'STRICT_ALL_TABLES,NO_BACKSLASH_ESCAPES'")
if err != nil {
return err
}
if c.Marginalia == "" {
c.Marginalia = DefaultMarginalia
}
return nil
}
func (c *DatabaseConfig) SqlDB(logger *logrus.Entry) (*sql.DB, error) {
dbCfg, err := c.MySQLConfig()
if err != nil {
return nil, fmt.Errorf("failed to build database config: %s", err)
}
if logger != nil {
logger.WithField("dsn", MaskedDSN(dbCfg)).Info("connecting to database")
}
return sql.Open("mysql", dbCfg.FormatDSN(), c.Marginalia)
}
func (c *DatabaseConfig) assertParamSet(param, value string) error {
if c.Params == nil {
c.Params = make(map[string]string)
}
if c.Params[param] != "" && c.Params[param] != value {
return fmt.Errorf("%s must be set to %s", param, value)
}
c.Params[param] = value
return nil
}
type InlineVerifierConfig struct {
// The maximum expected downtime during cutover, in the format of
// time.ParseDuration. If nothing is specified, the InlineVerifier will not
// try to estimate the downtime and will always allow cutover.
MaxExpectedDowntime string
// The interval at which the periodic binlog reverification occurs, in the
// format of time.ParseDuration. Default: 1s.
VerifyBinlogEventsInterval string
verifyBinlogEventsInterval time.Duration
maxExpectedDowntime time.Duration
}
func (c *InlineVerifierConfig) Validate() error {
var err error
if c.MaxExpectedDowntime != "" {
c.maxExpectedDowntime, err = time.ParseDuration(c.MaxExpectedDowntime)
if err != nil {
return err
}
} else {
c.maxExpectedDowntime = time.Duration(0)
}
if c.VerifyBinlogEventsInterval == "" {
c.VerifyBinlogEventsInterval = "1s"
}
c.verifyBinlogEventsInterval, err = time.ParseDuration(c.VerifyBinlogEventsInterval)
if err != nil {
return err
}
return nil
}
type IterativeVerifierConfig struct {
// List of tables that should be ignored by the IterativeVerifier.
IgnoredTables []string
// List of columns that should be ignored by the IterativeVerifier.
// This is in the format of table_name -> [list of column names]
IgnoredColumns map[string][]string
// The number of concurrent verifiers. Note that a single table can only be
// assigned to one goroutine and currently multiple goroutines per table
// is not supported.
Concurrency int
// The maximum expected downtime during cutover, in the format of
// time.ParseDuration.
MaxExpectedDowntime string
// Map of the table and column identifying the compression type
// (if any) of the column. This is used during verification to ensure
// the data was successfully copied as some compression algorithms can
// output different compressed data with the same input data.
//
// The data structure is a map of table names to a map of column names
// to the compression algorithm.
// ex: {books: {contents: snappy}}
//
// Currently supported compression algorithms are:
// 1. Snappy (https://google.github.io/snappy/) as "SNAPPY"
//
// Optional: defaults to empty map/no compression
//
// Note that the IterativeVerifier is in the process of being deprecated.
// If this is specified, ColumnCompressionConfig should also be filled out in
// the main Config.
TableColumnCompression TableColumnCompressionConfig
}
func (c *IterativeVerifierConfig) Validate() error {
if c.MaxExpectedDowntime != "" {
_, err := time.ParseDuration(c.MaxExpectedDowntime)
if err != nil {
return err
}
}
if c.Concurrency == 0 {
c.Concurrency = 4
}
return nil
}
// SchemaName => TableName => ColumnName => CompressionAlgorithm
// Example: blog1 => articles => body => snappy
// (SELECT body FROM blog1.articles => returns compressed blob)
type ColumnCompressionConfig map[string]map[string]map[string]string
func (c ColumnCompressionConfig) CompressedColumnsFor(schemaName, tableName string) map[string]string {
tableConfig, found := c[schemaName]
if !found {
return nil
}
columnsConfig, found := tableConfig[tableName]
if !found {
return nil
}
return columnsConfig
}
// SchemaName => TableName => ColumnName => struct{}{}
// These columns will be ignored during InlineVerification
type ColumnIgnoreConfig map[string]map[string]map[string]struct{}
func (c ColumnIgnoreConfig) IgnoredColumnsFor(schemaName, tableName string) map[string]struct{} {
tableConfig, found := c[schemaName]
if !found {
return nil
}
columnsConfig, found := tableConfig[tableName]
if !found {
return nil
}
return columnsConfig
}
// CascadingPaginationColumnConfig to configure pagination columns to be
// used. The term `Cascading` to denote that greater specificity takes
// precedence.
type CascadingPaginationColumnConfig struct {
// PerTable has greatest specificity and takes precedence over the other options
PerTable map[string]map[string]string // SchemaName => TableName => ColumnName
// FallbackColumn is a global default to fallback to and is less specific than the
// default, which is the Primary Key
FallbackColumn string
}
// PaginationColumnFor is a helper function to retrieve the column name to paginate by
func (c *CascadingPaginationColumnConfig) PaginationColumnFor(schemaName, tableName string) (string, bool) {
if c == nil {
return "", false
}
tableConfig, found := c.PerTable[schemaName]
if !found {
return "", false
}
column, found := tableConfig[tableName]
if !found {
return "", false
}
return column, true
}
// FallbackPaginationColumnName retreives the column name specified as a fallback when the Primary Key isn't suitable for pagination
func (c *CascadingPaginationColumnConfig) FallbackPaginationColumnName() (string, bool) {
if c == nil || c.FallbackColumn == "" {
return "", false
}
return c.FallbackColumn, true
}
type Config struct {
// Source database connection configuration
//
// Required
Source *DatabaseConfig
// Target database connection configuration
//
// Required
Target *DatabaseConfig
// Map database name on the source database (key of the map) to a
// different name on the target database (value of the associated key).
// This allows one to move data and change the database name in the
// process.
//
// Optional: defaults to empty map/no rewrites
DatabaseRewrites map[string]string
// Map the table name on the source database to a different name on
// the target database. See DatabaseRewrite.
//
// Optional: defaults to empty map/no rewrites
TableRewrites map[string]string
// The maximum number of retries for writes if the writes failed on
// the target database.
//
// Optional: defaults to 5.
DBWriteRetries int
// Filter out the databases/tables when detecting the source databases
// and tables.
//
// Required
TableFilter TableFilter
// Filter out unwanted data/events from being copied.
//
// Optional: defaults to nil/no filter.
CopyFilter CopyFilter
// The server id used by Ghostferry to connect to MySQL as a replication
// slave. This id must be unique on the MySQL server. If 0 is specified,
// a random id will be generated upon connecting to the MySQL server.
//
// Optional: defaults to an automatically generated one
MyServerId uint32
// The maximum number of binlog events to write at once. Note this is a
// maximum: if there are not a lot of binlog events, they will be written
// one at a time such the binlog streamer lag is as low as possible. This
// batch size will only be hit if there is a log of binlog at the same time.
//
// Optional: defaults to 100
BinlogEventBatchSize int
// The batch size used to iterate the data during data copy. This batch size
// is always used: if this is specified to be 100, 100 rows will be copied
// per iteration.
//
// With the current implementation of Ghostferry, we need to lock the rows
// we select. This means, the larger this number is, the longer we need to
// hold this lock. On the flip side, the smaller this number is, the slower
// the copy will likely be.
//
// Optional: defaults to 200
DataIterationBatchSize uint64
// The maximum number of retries for reads if the reads fail on the source
// database.
//
// Optional: defaults to 5
DBReadRetries int
// This specify the number of concurrent goroutines, each iterating over
// a single table.
//
// At this point in time, parallelize iteration within a single table. This
// may be possible to add to the future.
//
// Optional: defaults to 4
DataIterationConcurrency int
// This specifies if Ghostferry will pause before cutover or not.
//
// Optional: defaults to false
AutomaticCutover bool
// This specifies whether or not Ferry.Run will handle SIGINT and SIGTERM
// by dumping the current state to stdout and the error HTTP callback.
// The dumped state can be used to resume Ghostferry.
DumpStateOnSignal bool
// Config for the ControlServer
ServerBindAddr string
WebBasedir string
// Report progress via an HTTP callback. The Payload field of the callback
// will be sent to the server as the CustomPayload field in the Progress
// struct. The unit of ProgressReportFrequency is in milliseconds.
ProgressCallback HTTPCallback
ProgressReportFrequency int
// Report state via an HTTP callback. The SerializedState struct will be
// sent as the Payload parameter. The unit of StateReportFrequency is
// in milliseconds.
StateCallback HTTPCallback
StateReportFrequency int
// The state to resume from as dumped by the PanicErrorHandler.
// If this is null, a new Ghostferry run will be started. Otherwise, the
// reconciliation process will start and Ghostferry will resume after that.
StateToResumeFrom *SerializableState
// The verifier to use during the run. Valid choices are:
// ChecksumTable
// Iterative
// NoVerification
//
// If it is left blank, the Verifier member variable on the Ferry will be
// used. If that member variable is nil, no verification will be done.
VerifierType string
// Only useful if VerifierType == Iterative.
// This specifies the configurations to the IterativeVerifier.
//
// This option is in the process of being deprecated.
IterativeVerifierConfig IterativeVerifierConfig
// Only useful if VerifierType == Inline.
// This specifies the configurations to the InlineVerifierConfig.
InlineVerifierConfig InlineVerifierConfig
// For old versions mysql<5.6.2, MariaDB<10.1.6 which has no related var
// Make sure you have binlog_row_image=FULL when turning on this
SkipBinlogRowImageCheck bool
// This config is necessary for inline verification for a special case of
// Ghostferry:
//
// - If you are copying a table where the data is already partially on the
// target through some other means.
// - Specifically, the PaginationKey of this row on both the source and the target are
// the same. Thus, INSERT IGNORE will skip copying this row, leaving the
// data on the target unchanged.
// - If the data on the target is already identical to the source, then
// verification will pass and all is well.
// - However, if this data is compressed with a non-determinstic algorithm
// such as snappy, the compressed blob may not be equal even when the
// uncompressed data is equal.
// - This column signals to the InlineVerifier that it needs to decompress
// the data to compare identity.
//
// Note: a similar option exists in IterativeVerifier. However, the
// IterativeVerifier is being deprecated and this will be the correct place
// to specify it if you don't need the IterativeVerifier.
CompressedColumnsForVerification ColumnCompressionConfig
// This config is also for inline verification for the same special case of
// Ghostferry as documented with the CompressedColumnsForVerification option:
//
// - If you're copying a table where the data is partially already on the
// the target through some other means.
// - A difference in a particular column could be acceptable.
// - An example would be a table with a data field and a created_at field.
// Maybe the created_at field is not important for data integrity as long
// as the data field is correct.
// - Putting the column in this config will cause the InlineVerifier to skip
// this column for verification.
IgnoredColumnsForVerification ColumnIgnoreConfig
// Ghostferry requires a single numeric column to paginate over tables. Inferring that column is done in the following exact order:
// 1. Use the PerTable pagination column, if configured for a table. Fail if we cannot find this column in the table.
// 2. Use the table's primary key column as the pagination column. Fail if the primary key is not numeric or is a composite key without a FallbackColumn specified.
// 3. Use the FallbackColumn pagination column, if configured. Fail if we cannot find this column in the table.
CascadingPaginationColumnConfig *CascadingPaginationColumnConfig
// SkipTargetVerification is used to enable or disable target verification during moves.
// This feature is currently only available while using the InlineVerifier.
//
// This does so by inspecting the annotations (configured as Marginalia in the DatabaseConfig above)
// and will fail the move unless all applicable DMLs (as identified by the sharding key) sent to the
// Target were sent from Ghostferry.
//
// NOTE:
// The Target database must be configured with binlog_rows_query_log_events
// set to "ON" for this to function properly. Ghostferry not allow the move
// process to begin if this is enabled and the above option is set to "OFF".
//
// Required: defaults to false
SkipTargetVerification bool
}
func (c *Config) ValidateConfig() error {
if err := c.Source.Validate(); err != nil {
return fmt.Errorf("source: %s", err)
}
if err := c.Target.Validate(); err != nil {
return fmt.Errorf("target: %s", err)
}
if c.TableFilter == nil {
return fmt.Errorf("Table filter function must be provided")
}
if c.StateToResumeFrom != nil && c.StateToResumeFrom.GhostferryVersion != VersionString {
return fmt.Errorf("StateToResumeFrom version mismatch: resume = %s, current = %s", c.StateToResumeFrom.GhostferryVersion, VersionString)
}
if c.VerifierType == VerifierTypeIterative {
if err := c.IterativeVerifierConfig.Validate(); err != nil {
return fmt.Errorf("IterativeVerifierConfig invalid: %v", err)
}
} else if c.VerifierType == VerifierTypeInline {
if err := c.InlineVerifierConfig.Validate(); err != nil {
return fmt.Errorf("InlineVerifierConfig invalid: %v", err)
}
}
if c.DBWriteRetries == 0 {
c.DBWriteRetries = 5
}
if c.DataIterationBatchSize == 0 {
c.DataIterationBatchSize = 200
}
if c.BinlogEventBatchSize == 0 {
c.BinlogEventBatchSize = 100
}
if c.DataIterationConcurrency == 0 {
c.DataIterationConcurrency = 4
}
if c.DBReadRetries == 0 {
c.DBReadRetries = 5
}
if c.ServerBindAddr == "" {
c.ServerBindAddr = "0.0.0.0:8000"
}
if c.WebBasedir == "" {
c.WebBasedir = "."
}
return nil
}