Skip to content

Commit 4dd640d

Browse files
authored
Merge branch 'master' into master
2 parents d49212d + 8801d83 commit 4dd640d

File tree

5 files changed

+128
-13
lines changed

5 files changed

+128
-13
lines changed

mysql/mariadb_gtid.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package mysql
33
import (
44
"bytes"
55
"fmt"
6+
"sort"
67
"strconv"
78
"strings"
89

910
"github.com/pingcap/errors"
1011
"github.com/siddontang/go-log/log"
11-
"github.com/siddontang/go/hack"
1212
)
1313

1414
// MariadbGTID represent mariadb gtid, [domain ID]-[server-id]-[sequence]
@@ -157,7 +157,13 @@ func (s *MariadbGTIDSet) Update(GTIDStr string) error {
157157
}
158158

159159
func (s *MariadbGTIDSet) String() string {
160-
return hack.String(s.Encode())
160+
sets := make([]string, 0, len(s.Sets))
161+
for _, set := range s.Sets {
162+
sets = append(sets, set.String())
163+
}
164+
sort.Strings(sets)
165+
166+
return strings.Join(sets, ",")
161167
}
162168

163169
// Encode encodes mariadb gtid set

mysql/mariadb_gtid_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,14 @@ func (t *mariaDBTestSuite) TestMariaDBGTIDSetClone(c *check.C) {
232232
c.Assert(gtidSet.Clone(), check.DeepEquals, gtidSet)
233233
}
234234
}
235+
236+
func (t *mariaDBTestSuite) TestMariaDBGTIDSetSortedString(c *check.C) {
237+
cases := [][]string{{"", ""}, {"1-1-1", "1-1-1"},
238+
{"2-2-2,1-1-1,3-2-1", "1-1-1,2-2-2,3-2-1"}}
239+
240+
for _, strs := range cases {
241+
gtidSet, err := ParseMariadbGTIDSet(strs[0])
242+
c.Assert(err, check.IsNil)
243+
c.Assert(gtidSet.String(), check.Equals, strs[1])
244+
}
245+
}

mysql/mysql_gtid.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (s *UUIDSet) Decode(data []byte) error {
294294
func (s *UUIDSet) Clone() *UUIDSet {
295295
clone := new(UUIDSet)
296296

297-
clone.SID, _ = uuid.FromString(s.SID.String())
297+
copy(clone.SID.Bytes(), s.SID.Bytes())
298298
clone.Intervals = s.Intervals.Normalize()
299299

300300
return clone

mysql/position.go

+57-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package mysql
22

33
import (
44
"fmt"
5+
"strconv"
6+
"strings"
57
)
68

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

1315
func (p Position) Compare(o Position) int {
1416
// First compare binlog name
15-
if p.Name > o.Name {
17+
nameCmp := CompareBinlogFileName(p.Name, o.Name)
18+
if nameCmp != 0 {
19+
return nameCmp
20+
}
21+
// Same binlog file, compare position
22+
if p.Pos > o.Pos {
1623
return 1
17-
} else if p.Name < o.Name {
24+
} else if p.Pos < o.Pos {
1825
return -1
1926
} else {
20-
// Same binlog file, compare position
21-
if p.Pos > o.Pos {
22-
return 1
23-
} else if p.Pos < o.Pos {
24-
return -1
25-
} else {
26-
return 0
27-
}
27+
return 0
2828
}
2929
}
3030

3131
func (p Position) String() string {
3232
return fmt.Sprintf("(%s, %d)", p.Name, p.Pos)
3333
}
34+
35+
func CompareBinlogFileName(a, b string) int {
36+
// sometimes it's convenient to construct a `Position` literal with no `Name`
37+
if a == "" && b == "" {
38+
return 0
39+
} else if a == "" {
40+
return -1
41+
} else if b == "" {
42+
return 1
43+
}
44+
45+
splitBinlogName := func(n string) (string, int) {
46+
// mysqld appends a numeric extension to the binary log base name to generate binary log file names
47+
// ...
48+
// If you supply an extension in the log name (for example, --log-bin=base_name.extension),
49+
// the extension is silently removed and ignored.
50+
// ref: https://dev.mysql.com/doc/refman/8.0/en/binary-log.html
51+
i := strings.LastIndexByte(n, '.')
52+
if i == -1 {
53+
// try keeping backward compatibility
54+
return n, 0
55+
}
56+
57+
seq, err := strconv.Atoi(n[i+1:])
58+
if err != nil {
59+
panic(fmt.Sprintf("binlog file %s doesn't contain numeric extension", err))
60+
}
61+
return n[:i], seq
62+
}
63+
64+
aBase, aSeq := splitBinlogName(a)
65+
bBase, bSeq := splitBinlogName(b)
66+
67+
if aBase > bBase {
68+
return 1
69+
} else if aBase < bBase {
70+
return -1
71+
}
72+
73+
if aSeq > bSeq {
74+
return 1
75+
} else if aSeq < bSeq {
76+
return -1
77+
} else {
78+
return 0
79+
}
80+
}

mysql/position_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package mysql
2+
3+
import (
4+
"github.com/pingcap/check"
5+
)
6+
7+
type positionCompareSuite struct {
8+
}
9+
10+
var _ = check.Suite(&positionCompareSuite{})
11+
12+
func (t *positionCompareSuite) TestPosCompare(c *check.C) {
13+
ascendingPositions := []Position{
14+
{
15+
"",
16+
4,
17+
},
18+
{
19+
"",
20+
100,
21+
},
22+
{
23+
"mysql-bin.000001",
24+
4,
25+
},
26+
{
27+
"mysql-bin.000001",
28+
100,
29+
},
30+
{
31+
"mysql-bin.000002",
32+
4,
33+
},
34+
{
35+
"mysql-bin.999999",
36+
4,
37+
},
38+
{
39+
"mysql-bin.1000000",
40+
4,
41+
},
42+
}
43+
44+
for i := 1; i < len(ascendingPositions); i++ {
45+
c.Assert(ascendingPositions[i-1].Compare(ascendingPositions[i]), check.Equals, -1)
46+
}
47+
48+
for _, p := range ascendingPositions {
49+
c.Assert(p.Compare(p), check.Equals, 0)
50+
}
51+
}

0 commit comments

Comments
 (0)