Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion canal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *Canal) runSyncBinlog() error {
}
case *replication.MariadbGTIDEvent:
// try to save the GTID later
gtid := e.GTID
gtid := &e.GTID
c.master.UpdateGTID(gtid)
if err := c.eventHandler.OnGTID(gtid); err != nil {
return errors.Trace(err)
Expand Down
2 changes: 1 addition & 1 deletion failover/mariadb_gtid_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (h *MariadbGTIDHandler) FindBestSlaves(slaves []*Server) ([]*Server, error)
return nil, errors.Trace(err)
}

seq = g.(MariadbGTID).SequenceNumber
seq = g.(*MariadbGTID).SequenceNumber
}

ps[i] = seq
Expand Down
24 changes: 12 additions & 12 deletions mysql/mariadb_gtid.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type MariadbGTID struct {
// We don't support multi source replication, so the mariadb gtid set may have only domain-server-sequence
func ParseMariadbGTIDSet(str string) (GTIDSet, error) {
if len(str) == 0 {
return MariadbGTID{0, 0, 0}, nil
return &MariadbGTID{0, 0, 0}, nil
}

seps := strings.Split(str, "-")

var gtid MariadbGTID
gtid := new(MariadbGTID)

if len(seps) != 3 {
return gtid, errors.Errorf("invalid Mariadb GTID %v, must domain-server-sequence", str)
Expand All @@ -43,49 +43,49 @@ func ParseMariadbGTIDSet(str string) (GTIDSet, error) {
return gtid, errors.Errorf("invalid MariaDB GTID Sequence number (%v): %v", seps[2], err)
}

return MariadbGTID{
return &MariadbGTID{
DomainID: uint32(domainID),
ServerID: uint32(serverID),
SequenceNumber: sequenceID}, nil
}

func (gtid MariadbGTID) String() string {
func (gtid *MariadbGTID) String() string {
if gtid.DomainID == 0 && gtid.ServerID == 0 && gtid.SequenceNumber == 0 {
return ""
}

return fmt.Sprintf("%d-%d-%d", gtid.DomainID, gtid.ServerID, gtid.SequenceNumber)
}

func (gtid MariadbGTID) Encode() []byte {
func (gtid *MariadbGTID) Encode() []byte {
return []byte(gtid.String())
}

func (gtid MariadbGTID) Equal(o GTIDSet) bool {
other, ok := o.(MariadbGTID)
func (gtid *MariadbGTID) Equal(o GTIDSet) bool {
other, ok := o.(*MariadbGTID)
if !ok {
return false
}

return gtid == other
return *gtid == *other
}

func (gtid MariadbGTID) Contain(o GTIDSet) bool {
other, ok := o.(MariadbGTID)
func (gtid *MariadbGTID) Contain(o GTIDSet) bool {
other, ok := o.(*MariadbGTID)
if !ok {
return false
}

return gtid.DomainID == other.DomainID && gtid.SequenceNumber >= other.SequenceNumber
}

func (gtid MariadbGTID) Update(GTIDStr string) error {
func (gtid *MariadbGTID) Update(GTIDStr string) error {
newGTID, err := ParseMariadbGTIDSet(GTIDStr)
if err != nil {
return err
}

gtid = newGTID.(MariadbGTID)
*gtid = *(newGTID.(*MariadbGTID))

return nil
}
48 changes: 48 additions & 0 deletions mysql/mariadb_gtid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mysql

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

type mariaDBTestSuite struct {
}

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

func (t *mariaDBTestSuite) SetUpSuite(c *check.C) {

}

func (t *mariaDBTestSuite) TearDownSuite(c *check.C) {

}

func (t *mariaDBTestSuite) TestParseMariadbGTIDSet(c *check.C) {
g1, err := ParseMariadbGTIDSet("0-1-1")
c.Assert(err, check.IsNil)

c.Assert(g1.String(), check.Equals, "0-1-1")
}

func (t *mariaDBTestSuite) TestMariaDBEqual(c *check.C) {
g1, err := ParseMariadbGTIDSet("0-1-1")
c.Assert(err, check.IsNil)

g2, err := ParseMariadbGTIDSet("0-1-1")
c.Assert(err, check.IsNil)

g3, err := ParseMariadbGTIDSet("0-1-2")
c.Assert(err, check.IsNil)

c.Assert(g1.Equal(g2), check.IsTrue)
c.Assert(g1.Equal(g3), check.IsFalse)
}

func (t *mariaDBTestSuite) TestMariaDBUpdate(c *check.C) {
g1, err := ParseMariadbGTIDSet("0-1-1")
c.Assert(err, check.IsNil)

g1.Update("0-1-2")

c.Assert(g1.String(), check.Equals, "0-1-2")
}
6 changes: 3 additions & 3 deletions mysql/mysql_gtid.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (s *UUIDSet) decode(data []byte) (int, error) {
}
pos += 16

n := int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
n := int64(binary.LittleEndian.Uint64(data[pos: pos+8]))
pos += 8
if len(data) < int(16*n)+pos {
return 0, errors.Errorf("invalid uuid set buffer, must %d, but %d", pos+int(16*n), len(data))
Expand All @@ -267,9 +267,9 @@ func (s *UUIDSet) decode(data []byte) (int, error) {

var in Interval
for i := int64(0); i < n; i++ {
in.Start = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
in.Start = int64(binary.LittleEndian.Uint64(data[pos: pos+8]))
pos += 8
in.Stop = int64(binary.LittleEndian.Uint64(data[pos : pos+8]))
in.Stop = int64(binary.LittleEndian.Uint64(data[pos: pos+8]))
pos += 8
s.Intervals = append(s.Intervals, in)
}
Expand Down
14 changes: 12 additions & 2 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysql

import (
"strings"
"testing"

"github.com/pingcap/check"
Expand All @@ -15,11 +16,11 @@ type mysqlTestSuite struct {

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

func (s *mysqlTestSuite) SetUpSuite(c *check.C) {
func (t *mysqlTestSuite) SetUpSuite(c *check.C) {

}

func (s *mysqlTestSuite) TearDownSuite(c *check.C) {
func (t *mysqlTestSuite) TearDownSuite(c *check.C) {

}

Expand Down Expand Up @@ -91,6 +92,15 @@ func (t *mysqlTestSuite) TestMysqlGTIDCodec(c *check.C) {
c.Assert(gs, check.DeepEquals, o)
}

func (t *mysqlTestSuite) TestMysqlUpdate(c *check.C) {
g1, err := ParseMysqlGTIDSet("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57")
c.Assert(err, check.IsNil)

g1.Update("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-58")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use 3E11FA47-71CA-11E1-9E33-C80AA9429562:58 is better


c.Assert(strings.ToUpper(g1.String()), check.Equals, "3E11FA47-71CA-11E1-9E33-C80AA9429562:21-58")
}

func (t *mysqlTestSuite) TestMysqlGTIDContain(c *check.C) {
g1, err := ParseMysqlGTIDSet("3E11FA47-71CA-11E1-9E33-C80AA9429562:23")
c.Assert(err, check.IsNil)
Expand Down