-
Notifications
You must be signed in to change notification settings - Fork 269
/
binlog.go
394 lines (352 loc) · 13.6 KB
/
binlog.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
// Copyright 2021 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 checker
import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"os"
"strings"
"time"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/go-mysql-org/go-mysql/replication"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/dbutil"
"github.com/pingcap/tiflow/dm/config"
"github.com/pingcap/tiflow/dm/config/dbconfig"
"github.com/pingcap/tiflow/dm/pkg/conn"
tcontext "github.com/pingcap/tiflow/dm/pkg/context"
"github.com/pingcap/tiflow/dm/pkg/gtid"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/dm/pkg/utils"
"github.com/pingcap/tiflow/pkg/errors"
)
// MySQLBinlogEnableChecker checks whether `log_bin` variable is enabled in MySQL.
type MySQLBinlogEnableChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogEnableChecker returns a RealChecker.
func NewMySQLBinlogEnableChecker(db *sql.DB, dbinfo *dbutil.DBConfig) RealChecker {
return &MySQLBinlogEnableChecker{db: db, dbinfo: dbinfo}
}
// Check implements the RealChecker interface.
func (pc *MySQLBinlogEnableChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog is enabled",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
value, err := dbutil.ShowLogBin(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "ON" {
result.Errors = append(result.Errors, NewError("log_bin is %s, and should be ON", value))
result.Instruction = "MySQL as source: please refer to the document to enable the binlog https://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html"
return result
}
result.State = StateSuccess
return result
}
// Name implements the RealChecker interface.
func (pc *MySQLBinlogEnableChecker) Name() string {
return "mysql_binlog_enable"
}
/*****************************************************/
// MySQLBinlogFormatChecker checks mysql binlog_format.
type MySQLBinlogFormatChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogFormatChecker returns a RealChecker.
func NewMySQLBinlogFormatChecker(db *sql.DB, dbinfo *dbutil.DBConfig) RealChecker {
return &MySQLBinlogFormatChecker{db: db, dbinfo: dbinfo}
}
// Check implements the RealChecker interface.
func (pc *MySQLBinlogFormatChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog_format is ROW",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
value, err := dbutil.ShowBinlogFormat(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "ROW" {
result.Errors = append(result.Errors, NewError("binlog_format is %s, and should be ROW", value))
result.Instruction = "MySQL as source: please execute 'set global binlog_format=ROW;'; AWS Aurora (MySQL)/RDS MySQL as source: please refer to the document to create a new DB parameter group and set the binlog_format=row: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_WorkingWithDBInstanceParamGroups.html. Then modify the instance to use the new DB parameter group and restart the instance to take effect."
return result
}
result.State = StateSuccess
return result
}
// Name implements the RealChecker interface.
func (pc *MySQLBinlogFormatChecker) Name() string {
return "mysql_binlog_format"
}
/*****************************************************/
var (
mysqlBinlogRowImageRequired MySQLVersion = [3]uint{5, 6, 2}
mariaDBBinlogRowImageRequired MySQLVersion = [3]uint{10, 1, 6}
)
// MySQLBinlogRowImageChecker checks mysql binlog_row_image.
type MySQLBinlogRowImageChecker struct {
db *sql.DB
dbinfo *dbutil.DBConfig
}
// NewMySQLBinlogRowImageChecker returns a RealChecker.
func NewMySQLBinlogRowImageChecker(db *sql.DB, dbinfo *dbutil.DBConfig) RealChecker {
return &MySQLBinlogRowImageChecker{db: db, dbinfo: dbinfo}
}
// Check implements the RealChecker interface.
// 'binlog_row_image' is introduced since mysql 5.6.2, and mariadb 10.1.6.
// > In MySQL 5.5 and earlier, full row images are always used for both before images and after images.
// So we need check 'binlog_row_image' after mysql 5.6.2 version and mariadb 10.1.6.
// ref:
// - https://dev.mysql.com/doc/refman/5.6/en/replication-options-binary-log.html#sysvar_binlog_row_image
// - https://mariadb.com/kb/en/library/replication-and-binary-log-server-system-variables/#binlog_row_image
func (pc *MySQLBinlogRowImageChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: pc.Name(),
Desc: "check whether mysql binlog_row_image is FULL",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
}
// check version firstly
value, err := dbutil.ShowVersion(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
version, err := toMySQLVersion(value)
if err != nil {
markCheckError(result, err)
return result
}
// for mysql.version < 5.6.2, we don't need to check binlog_row_image.
if !version.Ge(mysqlBinlogRowImageRequired) {
result.State = StateSuccess
return result
}
// for mariadb.version < 10.1.6., we don't need to check binlog_row_image.
if conn.IsMariaDB(value) && !version.Ge(mariaDBBinlogRowImageRequired) {
result.State = StateSuccess
return result
}
value, err = dbutil.ShowBinlogRowImage(ctx, pc.db)
if err != nil {
markCheckError(result, err)
return result
}
if strings.ToUpper(value) != "FULL" {
result.Errors = append(result.Errors, NewError("binlog_row_image is %s, and should be FULL", value))
result.Instruction = "MySQL as source: please execute 'set global binlog_row_image = FULL;'; AWS Aurora (MySQL)/RDS MySQL as source: please refer to the document to create a new DB parameter group and set the binlog_row_image = FULL: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_WorkingWithDBInstanceParamGroups.html Then modify the instance to use the new DB parameter group and restart the instance to take effect."
return result
}
result.State = StateSuccess
return result
}
// Name implements the RealChecker interface.
func (pc *MySQLBinlogRowImageChecker) Name() string {
return "mysql_binlog_row_image"
}
// BinlogDBChecker checks if migrated dbs are in binlog_do_db or binlog_ignore_db.
type BinlogDBChecker struct {
db *conn.BaseDB
dbinfo *dbutil.DBConfig
schemas map[string]struct{}
caseSensitive bool
}
// NewBinlogDBChecker returns a RealChecker.
func NewBinlogDBChecker(db *conn.BaseDB, dbinfo *dbutil.DBConfig, schemas map[string]struct{}, caseSensitive bool) RealChecker {
newSchemas := make(map[string]struct{}, len(schemas))
for schema := range schemas {
newSchemas[schema] = struct{}{}
}
return &BinlogDBChecker{db: db, dbinfo: dbinfo, schemas: newSchemas, caseSensitive: caseSensitive}
}
// Check implements the RealChecker interface.
func (c *BinlogDBChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: c.Name(),
Desc: "check whether migrated dbs are in binlog_do_db/binlog_ignore_db",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", c.dbinfo.Host, c.dbinfo.Port),
}
flavor, err := conn.GetFlavor(ctx, c.db)
if err != nil {
markCheckError(result, err)
return result
}
tctx := tcontext.NewContext(ctx, log.L())
binlogDoDB, binlogIgnoreDB, err := conn.GetBinlogDB(tctx, c.db, flavor)
if err != nil {
markCheckError(result, err)
return result
}
if !c.caseSensitive {
binlogDoDB = strings.ToLower(binlogDoDB)
binlogIgnoreDB = strings.ToLower(binlogIgnoreDB)
}
binlogDoDBs := strings.Split(binlogDoDB, ",")
binlogIgnoreDBs := strings.Split(binlogIgnoreDB, ",")
// MySQL will check –binlog-do-db first, if there are any options,
// it will apply this one and ignore –binlog-ignore-db. If the
// –binlog-do-db is NOT set, then mysql will check –binlog-ignore-db.
// If both of them are empty, it will log changes for all DBs.
if len(binlogDoDB) != 0 {
for _, doDB := range binlogDoDBs {
delete(c.schemas, doDB)
}
if len(c.schemas) > 0 {
dbs := utils.SetToSlice(c.schemas)
result.Errors = append(result.Errors, NewWarn("these dbs [%s] are not in binlog_do_db[%s]", strings.Join(dbs, ","), binlogDoDB))
result.Instruction = "Ensure that the do_dbs contains the dbs you want to migrate"
return result
}
} else {
ignoreDBs := []string{}
for _, ignoreDB := range binlogIgnoreDBs {
if _, ok := c.schemas[ignoreDB]; ok {
ignoreDBs = append(ignoreDBs, ignoreDB)
}
}
if len(ignoreDBs) > 0 {
result.Errors = append(result.Errors, NewWarn("these dbs [%s] are in binlog_ignore_db[%s]", strings.Join(ignoreDBs, ","), binlogIgnoreDB))
result.Instruction = "Ensure that the ignore_dbs does not contain the dbs you want to migrate"
return result
}
}
result.State = StateSuccess
return result
}
// Name implements the RealChecker interface.
func (c *BinlogDBChecker) Name() string {
return "binlog_do_db/binlog_ignore_db check"
}
// MetaPositionChecker checks if meta position for given source database is valid.
type MetaPositionChecker struct {
db *conn.BaseDB
sourceCfg dbconfig.DBConfig
enableGTID bool
meta *config.Meta
}
// NewBinlogDBChecker returns a RealChecker.
func NewMetaPositionChecker(db *conn.BaseDB, sourceCfg dbconfig.DBConfig, enableGTID bool, meta *config.Meta) RealChecker {
return &MetaPositionChecker{db: db, sourceCfg: sourceCfg, enableGTID: enableGTID, meta: meta}
}
// Check implements the RealChecker interface.
func (c *MetaPositionChecker) Check(ctx context.Context) *Result {
result := &Result{
Name: c.Name(),
Desc: "check whether meta position is valid for db",
State: StateFailure,
Extra: fmt.Sprintf("address of db instance - %s:%d", c.sourceCfg.Host, c.sourceCfg.Port),
}
var tlsConfig *tls.Config
var err error
if c.sourceCfg.Security != nil {
if loadErr := c.sourceCfg.Security.LoadTLSContent(); loadErr != nil {
markCheckError(result, loadErr)
result.Instruction = "please check upstream tls config"
return result
}
tlsConfig, err = util.NewTLSConfig(
util.WithCAContent(c.sourceCfg.Security.SSLCABytes),
util.WithCertAndKeyContent(c.sourceCfg.Security.SSLCertBytes, c.sourceCfg.Security.SSLKeyBytes),
util.WithVerifyCommonName(c.sourceCfg.Security.CertAllowedCN),
util.WithMinTLSVersion(tls.VersionTLS10),
)
if err != nil {
markCheckError(result, err)
result.Instruction = "please check upstream tls config"
return result
}
}
flavor, err := conn.GetFlavor(ctx, c.db)
if err != nil {
markCheckError(result, err)
result.Instruction = "please check upstream database config"
return result
}
// always use a new random serverID
randomServerID, err := conn.GetRandomServerID(tcontext.NewContext(ctx, log.L()), c.db)
if err != nil {
// should never happened unless the master has too many slave
markCheckError(result, terror.Annotate(err, "fail to get random server id for relay reader"))
return result
}
h, _ := os.Hostname()
h = "dm-checker-" + h
// https://github.com/mysql/mysql-server/blob/1bfe02bdad6604d54913c62614bde57a055c8332/include/my_hostname.h#L33-L42
if len(h) > 60 {
h = h[:60]
}
syncCfg := replication.BinlogSyncerConfig{
ServerID: randomServerID,
Flavor: flavor,
Host: c.sourceCfg.Host,
Port: uint16(c.sourceCfg.Port),
User: c.sourceCfg.User,
Password: c.sourceCfg.Password,
TLSConfig: tlsConfig,
Localhost: h,
}
syncer := replication.NewBinlogSyncer(syncCfg)
defer syncer.Close()
var streamer *replication.BinlogStreamer
if c.enableGTID {
gtidSet, err2 := gtid.ParserGTID(flavor, c.meta.BinLogGTID)
if err2 != nil {
markCheckError(result, err2)
result.Instruction = "you should check your BinlogGTID's format, "
if flavor == mysql.MariaDBFlavor {
result.Instruction += "it should consist of three numbers separated with dashes '-', see https://mariadb.com/kb/en/gtid/"
} else {
result.Instruction += "it should be any combination of single GTIDs and ranges of GTID, see https://dev.mysql.com/doc/refman/8.0/en/replication-gtids-concepts.html"
}
return result
}
streamer, err = syncer.StartSyncGTID(gtidSet)
} else {
streamer, err = syncer.StartSync(mysql.Position{Name: c.meta.BinLogName, Pos: c.meta.BinLogPos})
}
if err != nil {
markCheckError(result, err)
result.Instruction = "you should make sure your meta's binlog position is valid and not purged, and the user has REPLICATION SLAVE privilege"
return result
}
// if we don't get a new event after 15s, it means there is no new event in the binlog
ctx2, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
_, err = streamer.GetEvent(ctx2)
if err != nil && errors.Cause(err) != context.DeadlineExceeded {
markCheckError(result, err)
result.Instruction = "you should make sure your meta's binlog position is valid and not purged, and the user has REPLICATION SLAVE privilege"
return result
}
result.State = StateSuccess
return result
}
// Name implements the RealChecker interface.
func (c *MetaPositionChecker) Name() string {
return "meta position check"
}