Skip to content

feat:add code comments & refactor mysql config constant #826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions canal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion mysql/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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 {
Expand Down