Skip to content

mysql: use numeric comparison for binlog filename #547

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 7 commits into from
Apr 9, 2021
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
67 changes: 57 additions & 10 deletions mysql/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mysql

import (
"fmt"
"strconv"
"strings"
)

// For binlog filename + position based replication
Expand All @@ -12,22 +14,67 @@ type Position struct {

func (p Position) Compare(o Position) int {
// First compare binlog name
if p.Name > o.Name {
nameCmp := CompareBinlogFileName(p.Name, o.Name)
if nameCmp != 0 {
return nameCmp
}
// Same binlog file, compare position
if p.Pos > o.Pos {
return 1
} else if p.Name < o.Name {
} else if p.Pos < o.Pos {
return -1
} else {
// Same binlog file, compare position
if p.Pos > o.Pos {
return 1
} else if p.Pos < o.Pos {
return -1
} else {
return 0
}
return 0
}
}

func (p Position) String() string {
return fmt.Sprintf("(%s, %d)", p.Name, p.Pos)
}

func CompareBinlogFileName(a, b string) int {
// sometimes it's convenient to construct a `Position` literal with no `Name`
if a == "" && b == "" {
return 0
} else if a == "" {
return -1
} else if b == "" {
return 1
}

splitBinlogName := func(n string) (string, int) {
// mysqld appends a numeric extension to the binary log base name to generate binary log file names
// ...
// If you supply an extension in the log name (for example, --log-bin=base_name.extension),
// the extension is silently removed and ignored.
// ref: https://dev.mysql.com/doc/refman/8.0/en/binary-log.html
i := strings.LastIndexByte(n, '.')
if i == -1 {
// try keeping backward compatibility
return n, 0
}

seq, err := strconv.Atoi(n[i+1:])
if err != nil {
panic(fmt.Sprintf("binlog file %s doesn't contain numeric extension", err))
}
return n[:i], seq
}

aBase, aSeq := splitBinlogName(a)
bBase, bSeq := splitBinlogName(b)

if aBase > bBase {
return 1
} else if aBase < bBase {
return -1
}

if aSeq > bSeq {
return 1
} else if aSeq < bSeq {
return -1
} else {
return 0
}
}
51 changes: 51 additions & 0 deletions mysql/position_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mysql

import (
"github.com/pingcap/check"
)

type positionCompareSuite struct {
}

var _ = check.Suite(&positionCompareSuite{})

func (t *positionCompareSuite) TestPosCompare(c *check.C) {
ascendingPositions := []Position{
{
"",
4,
},
{
"",
100,
},
{
"mysql-bin.000001",
4,
},
{
"mysql-bin.000001",
100,
},
{
"mysql-bin.000002",
4,
},
{
"mysql-bin.999999",
4,
},
{
"mysql-bin.1000000",
4,
},
}

for i := 1; i < len(ascendingPositions); i++ {
c.Assert(ascendingPositions[i-1].Compare(ascendingPositions[i]), check.Equals, -1)
}

for _, p := range ascendingPositions {
c.Assert(p.Compare(p), check.Equals, 0)
}
}