From aeec8f9f88a7398988b2fe49a9ea37f92d2c0d74 Mon Sep 17 00:00:00 2001 From: FanOne <294350394@qq.com> Date: Wed, 4 Oct 2023 22:00:57 +0800 Subject: [PATCH 1/2] feat:add code comments & refactor mysql config constant --- canal/config.go | 24 ++++++++++++------------ mysql/const.go | 8 ++++++++ mysql/position.go | 21 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/canal/config.go b/canal/config.go index 29c58f66b..8de350800 100644 --- a/canal/config.go +++ b/canal/config.go @@ -8,11 +8,12 @@ import ( "time" "github.com/BurntSushi/toml" - "github.com/go-mysql-org/go-mysql/client" - "github.com/go-mysql-org/go-mysql/mysql" "github.com/pingcap/errors" "github.com/siddontang/go-log/log" "github.com/siddontang/go-log/loggers" + + "github.com/go-mysql-org/go-mysql/client" + "github.com/go-mysql-org/go-mysql/mysql" ) type DumpConfig struct { @@ -91,13 +92,13 @@ type Config struct { // Set TLS config TLSConfig *tls.Config - //Set Logger + // Set Logger Logger loggers.Advanced - //Set Dialer + // Set Dialer Dialer client.Dialer - //Set Localhost + // Set Localhost Localhost string } @@ -121,19 +122,18 @@ func NewConfig(data string) (*Config, error) { return &c, nil } +// NewDefaultConfig Init some default config for Canal func NewDefaultConfig() *Config { c := new(Config) - c.Addr = "127.0.0.1:3306" - c.User = "root" - c.Password = "" - + c.Addr = mysql.DEFAULT_ADDR + c.User = mysql.DEFAULT_USER + c.Password = mysql.DEFAULT_PASSWORD c.Charset = mysql.DEFAULT_CHARSET c.ServerID = uint32(rand.New(rand.NewSource(time.Now().Unix())).Intn(1000)) + 1001 + c.Flavor = mysql.DEFAULT_FLAVOR - c.Flavor = "mysql" - - c.Dump.ExecutionPath = "mysqldump" + c.Dump.ExecutionPath = mysql.DEFAULT_DUMP_EXECUTION_PATH c.Dump.DiscardErr = true c.Dump.SkipMasterData = false diff --git a/mysql/const.go b/mysql/const.go index 34661294a..0cb7fc704 100644 --- a/mysql/const.go +++ b/mysql/const.go @@ -170,11 +170,19 @@ const ( ) const ( + DEFAULT_ADDR = "127.0.0.1:3306" + DEFAULT_USER = "root" + DEFAULT_PASSWORD = "" + DEFAULT_FLAVOR = "mysql" DEFAULT_CHARSET = "utf8" DEFAULT_COLLATION_ID uint8 = 33 DEFAULT_COLLATION_NAME string = "utf8_general_ci" ) +const ( + DEFAULT_DUMP_EXECUTION_PATH = "mysqldump" +) + // Like vitess, use flavor for different MySQL versions, const ( MySQLFlavor = "mysql" diff --git a/mysql/position.go b/mysql/position.go index c592d6363..107007818 100644 --- a/mysql/position.go +++ b/mysql/position.go @@ -6,12 +6,22 @@ import ( "strings" ) -// For binlog filename + position based replication +// Position for binlog filename + position based replication type Position struct { Name string Pos uint32 } +// Compare the position information between the p and o, +// if p > o return 1 means the position of p is further back than o. +// example is following +// id | project +// 1 | math +// 2 | cs +// 3 | ee +// p at id 3 and o at id 1. +// so this way we can determine from which position we should start syncing the data +// so that it is not consumed repeatedly. func (p Position) Compare(o Position) int { // First compare binlog name nameCmp := CompareBinlogFileName(p.Name, o.Name) @@ -32,6 +42,13 @@ func (p Position) String() string { return fmt.Sprintf("(%s, %d)", p.Name, p.Pos) } +// CompareBinlogFileName in this func, we'll compare the position between a and b by their filenames. +// if a>b will return 1. +// if b>a will return -1. +// the binlog filename consists of two parts (just like this --> [basename.00000x], for example bin_log.000001), +// one part is the `bin_log_basename` from the MySQL configuration file, +// and the other part is the number of the file's serial number, incrementing from 000001. +// you can use `show variables like 'log_%';` or `show variables like 'binlog%';` to show your configuration about binlog. func CompareBinlogFileName(a, b string) int { // sometimes it's convenient to construct a `Position` literal with no `Name` if a == "" && b == "" { @@ -61,9 +78,11 @@ func CompareBinlogFileName(a, b string) int { return n[:i], seq } + // get the basename(aBase) and the serial number(aSeq) aBase, aSeq := splitBinlogName(a) bBase, bSeq := splitBinlogName(b) + // aBase and bBase generally will be equal if they are both from the same database configuration. if aBase > bBase { return 1 } else if aBase < bBase { From d9f6ee9399431edcc09f538eb74f07b141d3f7af Mon Sep 17 00:00:00 2001 From: FanOne <294350394@qq.com> Date: Sat, 7 Oct 2023 22:33:46 +0800 Subject: [PATCH 2/2] feat: modify code comments --- canal/config.go | 2 +- mysql/position.go | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/canal/config.go b/canal/config.go index 8de350800..99694b801 100644 --- a/canal/config.go +++ b/canal/config.go @@ -122,7 +122,7 @@ func NewConfig(data string) (*Config, error) { return &c, nil } -// NewDefaultConfig Init some default config for Canal +// NewDefaultConfig initiates some default config for Canal func NewDefaultConfig() *Config { c := new(Config) diff --git a/mysql/position.go b/mysql/position.go index 107007818..09bd95f70 100644 --- a/mysql/position.go +++ b/mysql/position.go @@ -14,14 +14,6 @@ type Position struct { // Compare the position information between the p and o, // if p > o return 1 means the position of p is further back than o. -// example is following -// id | project -// 1 | math -// 2 | cs -// 3 | ee -// p at id 3 and o at id 1. -// so this way we can determine from which position we should start syncing the data -// so that it is not consumed repeatedly. func (p Position) Compare(o Position) int { // First compare binlog name nameCmp := CompareBinlogFileName(p.Name, o.Name) @@ -42,13 +34,9 @@ func (p Position) String() string { return fmt.Sprintf("(%s, %d)", p.Name, p.Pos) } -// CompareBinlogFileName in this func, we'll compare the position between a and b by their filenames. +// CompareBinlogFileName compares the binlog filename of a and b. // if a>b will return 1. // if b>a will return -1. -// the binlog filename consists of two parts (just like this --> [basename.00000x], for example bin_log.000001), -// one part is the `bin_log_basename` from the MySQL configuration file, -// and the other part is the number of the file's serial number, incrementing from 000001. -// you can use `show variables like 'log_%';` or `show variables like 'binlog%';` to show your configuration about binlog. func CompareBinlogFileName(a, b string) int { // sometimes it's convenient to construct a `Position` literal with no `Name` if a == "" && b == "" {