From d2abc39ae8ea805cedec4bb1e428a404f1a4c2c8 Mon Sep 17 00:00:00 2001 From: Guillem Date: Fri, 2 May 2025 21:30:36 +0800 Subject: [PATCH 1/4] fix canal.GetMasterPos() for mariadb --- canal/sync.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/canal/sync.go b/canal/sync.go index ff515a06a..6c0a7fe5a 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -325,12 +325,21 @@ 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" + query := "SHOW BINARY LOG STATUS" + switch c.cfg.Flavor { + case mysql.MariaDBFlavor: + query = "SHOW BINLOG STATUS" + + if eq, err := c.conn.CompareServerVersion("10.5.2"); (err == nil) && (eq < 0) { + query = "SHOW MASTER STATUS" + } + case mysql.MySQLFlavor: + if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq < 0) { + query = "SHOW MASTER STATUS" + } } - rr, err := c.Execute(showBinlogStatus) + rr, err := c.Execute(query) if err != nil { return mysql.Position{}, errors.Trace(err) } From db9a7295e5d12fab95cf3416ac3872f9b28a60ac Mon Sep 17 00:00:00 2001 From: Guillem Date: Mon, 5 May 2025 21:16:17 +0800 Subject: [PATCH 2/4] add getShowBinaryLogQuery --- canal/sync.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/canal/sync.go b/canal/sync.go index 6c0a7fe5a..4d70c43fd 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -324,21 +324,26 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error { } } -func (c *Canal) GetMasterPos() (mysql.Position, error) { - query := "SHOW BINARY LOG STATUS" +func (c *Canal) getShowBinaryLogQuery() string { switch c.cfg.Flavor { case mysql.MariaDBFlavor: - query = "SHOW BINLOG STATUS" - - if eq, err := c.conn.CompareServerVersion("10.5.2"); (err == nil) && (eq < 0) { - query = "SHOW MASTER STATUS" + // Source: https://mariadb.com/kb/en/show-binlog-status/#:~:text=SHOW%20BINLOG%20STATUS%20%2D%2D-,From%20MariaDB%2010.5.2,-Description + if eq, err := c.conn.CompareServerVersion("10.5.2"); (err == nil) && (eq >= 0) { + return "SHOW BINLOG STATUS" } case mysql.MySQLFlavor: - if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq < 0) { - query = "SHOW MASTER STATUS" + // Source: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html#:~:text=AND%20GTIDS)%3B-,SHOW%20MASTER%20STATUS,-(SHOW%20BINARY + if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq >= 0) { + return "SHOW BINARY LOG STATUS" } } + return "SHOW MASTER STATUS" +} + +func (c *Canal) GetMasterPos() (mysql.Position, error) { + query := c.getShowBinaryLogQuery() + rr, err := c.Execute(query) if err != nil { return mysql.Position{}, errors.Trace(err) From b290e28f642953440d9cf18d81c8d1330bcfe000 Mon Sep 17 00:00:00 2001 From: Guillem Date: Mon, 5 May 2025 21:28:26 +0800 Subject: [PATCH 3/4] add test --- canal/sync.go | 13 ++++++++----- canal/sync_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 canal/sync_test.go diff --git a/canal/sync.go b/canal/sync.go index 4d70c43fd..815f77a05 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -324,16 +324,19 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error { } } -func (c *Canal) getShowBinaryLogQuery() string { - switch c.cfg.Flavor { +func getShowBinaryLogQuery(flavor, serverVersion string) string { + switch flavor { case mysql.MariaDBFlavor: // Source: https://mariadb.com/kb/en/show-binlog-status/#:~:text=SHOW%20BINLOG%20STATUS%20%2D%2D-,From%20MariaDB%2010.5.2,-Description - if eq, err := c.conn.CompareServerVersion("10.5.2"); (err == nil) && (eq >= 0) { + + eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2") + if (err == nil) && (eq >= 0) { return "SHOW BINLOG STATUS" } case mysql.MySQLFlavor: // Source: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html#:~:text=AND%20GTIDS)%3B-,SHOW%20MASTER%20STATUS,-(SHOW%20BINARY - if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq >= 0) { + eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0") + if (err == nil) && (eq >= 0) { return "SHOW BINARY LOG STATUS" } } @@ -342,7 +345,7 @@ func (c *Canal) getShowBinaryLogQuery() string { } func (c *Canal) GetMasterPos() (mysql.Position, error) { - query := c.getShowBinaryLogQuery() + query := getShowBinaryLogQuery(c.cfg.Flavor, c.conn.GetServerVersion()) rr, err := c.Execute(query) if err != nil { diff --git a/canal/sync_test.go b/canal/sync_test.go new file mode 100644 index 000000000..cd3a6711d --- /dev/null +++ b/canal/sync_test.go @@ -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) + }) + } +} From c32c655b1c9b53d93e9eed90be3a84b30d0973c3 Mon Sep 17 00:00:00 2001 From: Guillem Date: Mon, 5 May 2025 21:42:53 +0800 Subject: [PATCH 4/4] improve docs --- canal/sync.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/canal/sync.go b/canal/sync.go index 815f77a05..e93826663 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -324,17 +324,21 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error { } } +// 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: - // Source: https://mariadb.com/kb/en/show-binlog-status/#:~:text=SHOW%20BINLOG%20STATUS%20%2D%2D-,From%20MariaDB%2010.5.2,-Description - eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2") if (err == nil) && (eq >= 0) { return "SHOW BINLOG STATUS" } case mysql.MySQLFlavor: - // Source: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html#:~:text=AND%20GTIDS)%3B-,SHOW%20MASTER%20STATUS,-(SHOW%20BINARY eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0") if (err == nil) && (eq >= 0) { return "SHOW BINARY LOG STATUS"