Skip to content

Commit 034c7ab

Browse files
authored
separate serverID of Mariadb GTID set (#852)
* add serverID to Mariadb GTID set * fix ut * fix comment
1 parent 4187985 commit 034c7ab

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

mysql/mariadb_gtid.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func (gtid *MariadbGTID) forward(newer *MariadbGTID) error {
103103

104104
// MariadbGTIDSet is a set of mariadb gtid
105105
type MariadbGTIDSet struct {
106-
Sets map[uint32]*MariadbGTID
106+
Sets map[uint32]map[uint32]*MariadbGTID
107107
}
108108

109109
// ParseMariadbGTIDSet parses str into mariadb gtid sets
110110
func ParseMariadbGTIDSet(str string) (GTIDSet, error) {
111111
s := new(MariadbGTIDSet)
112-
s.Sets = make(map[uint32]*MariadbGTID)
112+
s.Sets = make(map[uint32]map[uint32]*MariadbGTID)
113113
if str == "" {
114114
return s, nil
115115
}
@@ -126,14 +126,17 @@ func (s *MariadbGTIDSet) AddSet(gtid *MariadbGTID) error {
126126
return nil
127127
}
128128

129-
o, ok := s.Sets[gtid.DomainID]
130-
if ok {
129+
if serverSets, ok := s.Sets[gtid.DomainID]; !ok {
130+
s.Sets[gtid.DomainID] = map[uint32]*MariadbGTID{
131+
gtid.ServerID: gtid,
132+
}
133+
} else if o, ok := serverSets[gtid.ServerID]; !ok {
134+
serverSets[gtid.ServerID] = gtid
135+
} else {
131136
err := o.forward(gtid)
132137
if err != nil {
133138
return errors.Trace(err)
134139
}
135-
} else {
136-
s.Sets[gtid.DomainID] = gtid
137140
}
138141

139142
return nil
@@ -159,7 +162,9 @@ func (s *MariadbGTIDSet) Update(GTIDStr string) error {
159162
func (s *MariadbGTIDSet) String() string {
160163
sets := make([]string, 0, len(s.Sets))
161164
for _, set := range s.Sets {
162-
sets = append(sets, set.String())
165+
for _, gtid := range set {
166+
sets = append(sets, gtid.String())
167+
}
163168
}
164169
sort.Strings(sets)
165170

@@ -170,10 +175,12 @@ func (s *MariadbGTIDSet) String() string {
170175
func (s *MariadbGTIDSet) Encode() []byte {
171176
var buf bytes.Buffer
172177
sep := ""
173-
for _, gtid := range s.Sets {
174-
buf.WriteString(sep)
175-
buf.WriteString(gtid.String())
176-
sep = ","
178+
for _, set := range s.Sets {
179+
for _, gtid := range set {
180+
buf.WriteString(sep)
181+
buf.WriteString(gtid.String())
182+
sep = ","
183+
}
177184
}
178185

179186
return buf.Bytes()
@@ -182,10 +189,13 @@ func (s *MariadbGTIDSet) Encode() []byte {
182189
// Clone clones a mariadb gtid set
183190
func (s *MariadbGTIDSet) Clone() GTIDSet {
184191
clone := &MariadbGTIDSet{
185-
Sets: make(map[uint32]*MariadbGTID),
192+
Sets: make(map[uint32]map[uint32]*MariadbGTID),
186193
}
187-
for domainID, gtid := range s.Sets {
188-
clone.Sets[domainID] = gtid.Clone()
194+
for domainID, set := range s.Sets {
195+
clone.Sets[domainID] = make(map[uint32]*MariadbGTID)
196+
for serverID, gtid := range set {
197+
clone.Sets[domainID][serverID] = gtid.Clone()
198+
}
189199
}
190200

191201
return clone
@@ -202,14 +212,17 @@ func (s *MariadbGTIDSet) Equal(o GTIDSet) bool {
202212
return false
203213
}
204214

205-
for domainID, gtid := range other.Sets {
206-
o, ok := s.Sets[domainID]
215+
for domainID, set := range other.Sets {
216+
serverSet, ok := s.Sets[domainID]
207217
if !ok {
208218
return false
209219
}
210-
211-
if *gtid != *o {
212-
return false
220+
for serverID, gtid := range set {
221+
if o, ok := serverSet[serverID]; !ok {
222+
return false
223+
} else if *gtid != *o {
224+
return false
225+
}
213226
}
214227
}
215228

@@ -223,14 +236,17 @@ func (s *MariadbGTIDSet) Contain(o GTIDSet) bool {
223236
return false
224237
}
225238

226-
for doaminID, gtid := range other.Sets {
227-
o, ok := s.Sets[doaminID]
239+
for doaminID, set := range other.Sets {
240+
serverSet, ok := s.Sets[doaminID]
228241
if !ok {
229242
return false
230243
}
231-
232-
if !o.Contain(gtid) {
233-
return false
244+
for serverID, gtid := range set {
245+
if o, ok := serverSet[serverID]; !ok {
246+
return false
247+
} else if !o.Contain(gtid) {
248+
return false
249+
}
234250
}
235251
}
236252

mysql/mariadb_gtid_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ func TestMariaDBForward(t *testing.T) {
9191
func TestParseMariaDBGTIDSet(t *testing.T) {
9292
cases := []struct {
9393
gtidStr string
94-
subGTIDs map[uint32]string //domain ID => gtid string
95-
expectedStr []string // test String()
94+
subGTIDs map[uint32]map[uint32]string //domain ID => gtid string
95+
expectedStr []string // test String()
9696
hasError bool
9797
}{
98-
{"0-1-1", map[uint32]string{0: "0-1-1"}, []string{"0-1-1"}, false},
98+
{"0-1-1", map[uint32]map[uint32]string{0: {1: "0-1-1"}}, []string{"0-1-1"}, false},
9999
{"", nil, []string{""}, false},
100-
{"0-1-1,1-2-3", map[uint32]string{0: "0-1-1", 1: "1-2-3"}, []string{"0-1-1,1-2-3", "1-2-3,0-1-1"}, false},
100+
{"0-1-1,1-2-3", map[uint32]map[uint32]string{0: {1: "0-1-1"}, 1: {2: "1-2-3"}}, []string{"0-1-1,1-2-3", "1-2-3,0-1-1"}, false},
101101
{"0-1--1", nil, nil, true},
102102
}
103103

@@ -112,9 +112,12 @@ func TestParseMariaDBGTIDSet(t *testing.T) {
112112

113113
// check sub gtid
114114
require.Len(t, mariadbGTIDSet.Sets, len(cs.subGTIDs))
115-
for domainID, gtid := range mariadbGTIDSet.Sets {
115+
for domainID, set := range mariadbGTIDSet.Sets {
116116
require.Contains(t, mariadbGTIDSet.Sets, domainID)
117-
require.Equal(t, cs.subGTIDs[domainID], gtid.String())
117+
for serverID, gtid := range set {
118+
require.Contains(t, cs.subGTIDs, domainID)
119+
require.Equal(t, cs.subGTIDs[domainID][serverID], gtid.String())
120+
}
118121
}
119122

120123
// check String() function
@@ -135,13 +138,13 @@ func TestMariaDBGTIDSetUpdate(t *testing.T) {
135138
cases := []struct {
136139
isNilGTID bool
137140
gtidStr string
138-
subGTIDs map[uint32]string
141+
subGTIDs map[uint32]map[uint32]string
139142
}{
140-
{true, "", map[uint32]string{1: "1-1-1", 2: "2-2-2"}},
141-
{false, "1-2-2", map[uint32]string{1: "1-2-2", 2: "2-2-2"}},
142-
{false, "1-2-1", map[uint32]string{1: "1-2-1", 2: "2-2-2"}},
143-
{false, "3-2-1", map[uint32]string{1: "1-1-1", 2: "2-2-2", 3: "3-2-1"}},
144-
{false, "3-2-1,4-2-1", map[uint32]string{1: "1-1-1", 2: "2-2-2", 3: "3-2-1", 4: "4-2-1"}},
143+
{true, "", map[uint32]map[uint32]string{1: {1: "1-1-1"}, 2: {2: "2-2-2"}}},
144+
{false, "1-2-2", map[uint32]map[uint32]string{1: {1: "1-1-1", 2: "1-2-2"}, 2: {2: "2-2-2"}}},
145+
{false, "1-2-1", map[uint32]map[uint32]string{1: {1: "1-1-1", 2: "1-2-1"}, 2: {2: "2-2-2"}}},
146+
{false, "3-2-1", map[uint32]map[uint32]string{1: {1: "1-1-1"}, 2: {2: "2-2-2"}, 3: {2: "3-2-1"}}},
147+
{false, "3-2-1,4-2-1", map[uint32]map[uint32]string{1: {1: "1-1-1"}, 2: {2: "2-2-2"}, 3: {2: "3-2-1"}, 4: {2: "4-2-1"}}},
145148
}
146149

147150
for _, cs := range cases {
@@ -158,9 +161,12 @@ func TestMariaDBGTIDSetUpdate(t *testing.T) {
158161
}
159162
// check sub gtid
160163
require.Len(t, mariadbGTIDSet.Sets, len(cs.subGTIDs))
161-
for domainID, gtid := range mariadbGTIDSet.Sets {
164+
for domainID, set := range mariadbGTIDSet.Sets {
162165
require.Contains(t, mariadbGTIDSet.Sets, domainID)
163-
require.Equal(t, cs.subGTIDs[domainID], gtid.String())
166+
for serverID, gtid := range set {
167+
require.Contains(t, cs.subGTIDs, domainID)
168+
require.Equal(t, cs.subGTIDs[domainID][serverID], gtid.String())
169+
}
164170
}
165171
}
166172
}

0 commit comments

Comments
 (0)