Skip to content

Commit

Permalink
feat:add code comments & refactor mysql config constant (#826)
Browse files Browse the repository at this point in the history
* feat:add code comments & refactor mysql config constant

* feat: modify code comments
  • Loading branch information
CocaineCong authored Oct 8, 2023
1 parent 93df998 commit 3606141
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
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 initiates 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
9 changes: 8 additions & 1 deletion mysql/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ 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.
func (p Position) Compare(o Position) int {
// First compare binlog name
nameCmp := CompareBinlogFileName(p.Name, o.Name)
Expand All @@ -32,6 +34,9 @@ func (p Position) String() string {
return fmt.Sprintf("(%s, %d)", p.Name, p.Pos)
}

// CompareBinlogFileName compares the binlog filename of a and b.
// if a>b will return 1.
// if b>a will return -1.
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 +66,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

0 comments on commit 3606141

Please sign in to comment.