Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 26 additions & 5 deletions canal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,34 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error {
}
}

func (c *Canal) GetMasterPos() (mysql.Position, error) {
showBinlogStatus := "SHOW BINARY LOG STATUS"
if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq < 0) {
showBinlogStatus = "SHOW MASTER STATUS"
// getShowBinaryLogQuery returns the correct SQL statement to query binlog status
// for the given database flavor and server version.
//
// Sources:
//
// MySQL: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html
// MariaDB: https://mariadb.com/kb/en/show-binlog-status
func getShowBinaryLogQuery(flavor, serverVersion string) string {
switch flavor {
case mysql.MariaDBFlavor:
eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2")
if (err == nil) && (eq >= 0) {
return "SHOW BINLOG STATUS"
}
case mysql.MySQLFlavor:
eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0")
if (err == nil) && (eq >= 0) {
return "SHOW BINARY LOG STATUS"
}
}

rr, err := c.Execute(showBinlogStatus)
return "SHOW MASTER STATUS"
}

func (c *Canal) GetMasterPos() (mysql.Position, error) {
query := getShowBinaryLogQuery(c.cfg.Flavor, c.conn.GetServerVersion())

rr, err := c.Execute(query)
if err != nil {
return mysql.Position{}, errors.Trace(err)
}
Expand Down
31 changes: 31 additions & 0 deletions canal/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package canal

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetShowBinaryLogQuery(t *testing.T) {
tests := []struct {
flavor string
serverVersion string
expected string
}{
{flavor: "mariadb", serverVersion: "10.5.2", expected: "SHOW BINLOG STATUS"},
{flavor: "mariadb", serverVersion: "10.6.0", expected: "SHOW BINLOG STATUS"},
{flavor: "mariadb", serverVersion: "10.4.0", expected: "SHOW MASTER STATUS"},
{flavor: "mysql", serverVersion: "8.4.0", expected: "SHOW BINARY LOG STATUS"},
{flavor: "mysql", serverVersion: "8.4.1", expected: "SHOW BINARY LOG STATUS"},
{flavor: "mysql", serverVersion: "8.0.33", expected: "SHOW MASTER STATUS"},
{flavor: "mysql", serverVersion: "5.7.41", expected: "SHOW MASTER STATUS"},
{flavor: "other", serverVersion: "1.0.0", expected: "SHOW MASTER STATUS"},
}

for _, tt := range tests {
t.Run(tt.flavor+"_"+tt.serverVersion, func(t *testing.T) {
got := getShowBinaryLogQuery(tt.flavor, tt.serverVersion)
require.Equal(t, tt.expected, got)
})
}
}