Skip to content

Commit 61638c4

Browse files
alarbadaGuillem
and
Guillem
authored
fix canal.GetMasterPos() for mariadb (#1030)
* fix canal.GetMasterPos() for mariadb * add getShowBinaryLogQuery * add test * improve docs --------- Co-authored-by: Guillem <[email protected]>
1 parent 8c60af8 commit 61638c4

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

canal/sync.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,34 @@ func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error {
324324
}
325325
}
326326

327-
func (c *Canal) GetMasterPos() (mysql.Position, error) {
328-
showBinlogStatus := "SHOW BINARY LOG STATUS"
329-
if eq, err := c.conn.CompareServerVersion("8.4.0"); (err == nil) && (eq < 0) {
330-
showBinlogStatus = "SHOW MASTER STATUS"
327+
// getShowBinaryLogQuery returns the correct SQL statement to query binlog status
328+
// for the given database flavor and server version.
329+
//
330+
// Sources:
331+
//
332+
// MySQL: https://dev.mysql.com/doc/relnotes/mysql/8.4/en/news-8-4-0.html
333+
// MariaDB: https://mariadb.com/kb/en/show-binlog-status
334+
func getShowBinaryLogQuery(flavor, serverVersion string) string {
335+
switch flavor {
336+
case mysql.MariaDBFlavor:
337+
eq, err := mysql.CompareServerVersions(serverVersion, "10.5.2")
338+
if (err == nil) && (eq >= 0) {
339+
return "SHOW BINLOG STATUS"
340+
}
341+
case mysql.MySQLFlavor:
342+
eq, err := mysql.CompareServerVersions(serverVersion, "8.4.0")
343+
if (err == nil) && (eq >= 0) {
344+
return "SHOW BINARY LOG STATUS"
345+
}
331346
}
332347

333-
rr, err := c.Execute(showBinlogStatus)
348+
return "SHOW MASTER STATUS"
349+
}
350+
351+
func (c *Canal) GetMasterPos() (mysql.Position, error) {
352+
query := getShowBinaryLogQuery(c.cfg.Flavor, c.conn.GetServerVersion())
353+
354+
rr, err := c.Execute(query)
334355
if err != nil {
335356
return mysql.Position{}, errors.Trace(err)
336357
}

canal/sync_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package canal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestGetShowBinaryLogQuery(t *testing.T) {
10+
tests := []struct {
11+
flavor string
12+
serverVersion string
13+
expected string
14+
}{
15+
{flavor: "mariadb", serverVersion: "10.5.2", expected: "SHOW BINLOG STATUS"},
16+
{flavor: "mariadb", serverVersion: "10.6.0", expected: "SHOW BINLOG STATUS"},
17+
{flavor: "mariadb", serverVersion: "10.4.0", expected: "SHOW MASTER STATUS"},
18+
{flavor: "mysql", serverVersion: "8.4.0", expected: "SHOW BINARY LOG STATUS"},
19+
{flavor: "mysql", serverVersion: "8.4.1", expected: "SHOW BINARY LOG STATUS"},
20+
{flavor: "mysql", serverVersion: "8.0.33", expected: "SHOW MASTER STATUS"},
21+
{flavor: "mysql", serverVersion: "5.7.41", expected: "SHOW MASTER STATUS"},
22+
{flavor: "other", serverVersion: "1.0.0", expected: "SHOW MASTER STATUS"},
23+
}
24+
25+
for _, tt := range tests {
26+
t.Run(tt.flavor+"_"+tt.serverVersion, func(t *testing.T) {
27+
got := getShowBinaryLogQuery(tt.flavor, tt.serverVersion)
28+
require.Equal(t, tt.expected, got)
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)