Skip to content

Commit

Permalink
add serverID to Mariadb GTID set
Browse files Browse the repository at this point in the history
  • Loading branch information
okJiang committed Mar 11, 2024
1 parent 4187985 commit 1becded
Showing 1 changed file with 53 additions and 30 deletions.
83 changes: 53 additions & 30 deletions mysql/mariadb_gtid.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func (gtid *MariadbGTID) forward(newer *MariadbGTID) error {

// MariadbGTIDSet is a set of mariadb gtid
type MariadbGTIDSet struct {
Sets map[uint32]*MariadbGTID
Sets map[uint32]map[uint32]*MariadbGTID
}

// ParseMariadbGTIDSet parses str into mariadb gtid sets
func ParseMariadbGTIDSet(str string) (GTIDSet, error) {
s := new(MariadbGTIDSet)
s.Sets = make(map[uint32]*MariadbGTID)
s.Sets = make(map[uint32]map[uint32]*MariadbGTID)
if str == "" {
return s, nil
}
Expand All @@ -126,14 +126,17 @@ func (s *MariadbGTIDSet) AddSet(gtid *MariadbGTID) error {
return nil
}

o, ok := s.Sets[gtid.DomainID]
if ok {
if serverSets, ok := s.Sets[gtid.DomainID]; !ok {
s.Sets[gtid.DomainID] = map[uint32]*MariadbGTID{
gtid.ServerID: gtid,
}
} else if o, ok := serverSets[gtid.ServerID]; !ok {
serverSets[gtid.ServerID] = gtid
} else {
err := o.forward(gtid)
if err != nil {
return errors.Trace(err)
}
} else {
s.Sets[gtid.DomainID] = gtid
}

return nil
Expand All @@ -159,7 +162,9 @@ func (s *MariadbGTIDSet) Update(GTIDStr string) error {
func (s *MariadbGTIDSet) String() string {
sets := make([]string, 0, len(s.Sets))
for _, set := range s.Sets {
sets = append(sets, set.String())
for _, gtid := range set {
sets = append(sets, gtid.String())
}
}
sort.Strings(sets)

Expand All @@ -170,10 +175,12 @@ func (s *MariadbGTIDSet) String() string {
func (s *MariadbGTIDSet) Encode() []byte {
var buf bytes.Buffer
sep := ""
for _, gtid := range s.Sets {
buf.WriteString(sep)
buf.WriteString(gtid.String())
sep = ","
for _, set := range s.Sets {
for _, gtid := range set {
buf.WriteString(sep)
buf.WriteString(gtid.String())
sep = ","
}
}

return buf.Bytes()
Expand All @@ -182,10 +189,15 @@ func (s *MariadbGTIDSet) Encode() []byte {
// Clone clones a mariadb gtid set
func (s *MariadbGTIDSet) Clone() GTIDSet {
clone := &MariadbGTIDSet{
Sets: make(map[uint32]*MariadbGTID),
Sets: make(map[uint32]map[uint32]*MariadbGTID),
}
for domainID, gtid := range s.Sets {
clone.Sets[domainID] = gtid.Clone()
for domainID, set := range s.Sets {
for serverID, gtid := range set {
if clone.Sets[domainID] == nil {
clone.Sets[domainID] = make(map[uint32]*MariadbGTID)
}
clone.Sets[domainID][serverID] = gtid.Clone()
}
}

return clone
Expand All @@ -202,14 +214,21 @@ func (s *MariadbGTIDSet) Equal(o GTIDSet) bool {
return false
}

for domainID, gtid := range other.Sets {
o, ok := s.Sets[domainID]
if !ok {
return false
}

if *gtid != *o {
return false
for domainID, set := range other.Sets {
for serverID, gtid := range set {
serverSet, ok := s.Sets[domainID]
if !ok {
return false
}

o, ok := serverSet[serverID]
if !ok {
return false
}

if *gtid != *o {
return false
}
}
}

Expand All @@ -223,14 +242,18 @@ func (s *MariadbGTIDSet) Contain(o GTIDSet) bool {
return false
}

for doaminID, gtid := range other.Sets {
o, ok := s.Sets[doaminID]
if !ok {
return false
}

if !o.Contain(gtid) {
return false
for doaminID, set := range other.Sets {
for serverID, gtid := range set {
serverSet, ok := s.Sets[doaminID]
if !ok {
return false
}

if o, ok := serverSet[serverID]; !ok {
return false
} else if !o.Contain(gtid) {
return false
}
}
}

Expand Down

0 comments on commit 1becded

Please sign in to comment.