forked from vitessio/vitess
/
mysql56_gtid_set.go
353 lines (296 loc) · 8.78 KB
/
mysql56_gtid_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package replication
import (
"bytes"
"encoding/binary"
"fmt"
"sort"
"strconv"
"strings"
)
type interval struct {
start, end int64
}
func (iv interval) contains(other interval) bool {
return iv.start <= other.start && other.end <= iv.end
}
type intervalList []interval
// Len implements sort.Interface.
func (s intervalList) Len() int { return len(s) }
// Less implements sort.Interface.
func (s intervalList) Less(i, j int) bool { return s[i].start < s[j].start }
// Swap implements sort.Interface.
func (s intervalList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func parseInterval(s string) (interval, error) {
parts := strings.Split(s, "-")
start, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return interval{}, fmt.Errorf("invalid interval (%q): %v", s, err)
}
if start < 1 {
return interval{}, fmt.Errorf("invalid interval (%q): start must be > 0", s)
}
switch len(parts) {
case 1:
return interval{start: start, end: start}, nil
case 2:
end, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return interval{}, fmt.Errorf("invalid interval (%q): %v", s, err)
}
return interval{start: start, end: end}, nil
default:
return interval{}, fmt.Errorf("invalid interval (%q): expected start-end or single number", s)
}
}
// parseMysql56GTIDSet is registered as a GTIDSet parser.
//
// https://dev.mysql.com/doc/refman/5.6/en/replication-gtids-concepts.html
func parseMysql56GTIDSet(s string) (GTIDSet, error) {
set := Mysql56GTIDSet{}
// gtid_set: uuid_set [, uuid_set] ...
for _, uuidSet := range strings.Split(s, ",") {
uuidSet = strings.TrimSpace(uuidSet)
if uuidSet == "" {
continue
}
// uuid_set: uuid:interval[:interval]...
parts := strings.Split(uuidSet, ":")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid MySQL 5.6 GTID set (%q): expected uuid:interval", s)
}
// Parse Server ID.
sid, err := ParseSID(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid MySQL 5.6 GTID set (%q): %v", s, err)
}
// Parse Intervals.
intervals := make([]interval, 0, len(parts)-1)
for _, part := range parts[1:] {
iv, err := parseInterval(part)
if err != nil {
return nil, fmt.Errorf("invalid MySQL 5.6 GTID set (%q): %v", s, err)
}
if iv.end < iv.start {
// According to MySQL 5.6 code:
// "The end of an interval may be 0, but any interval that has an
// endpoint that is smaller than the start is discarded."
continue
}
intervals = append(intervals, iv)
}
if len(intervals) == 0 {
// We might have discarded all the intervals.
continue
}
// Internally we expect intervals to be stored in order.
sort.Sort(intervalList(intervals))
set[sid] = intervals
}
return set, nil
}
// Mysql56GTIDSet implements GTIDSet for MySQL 5.6.
type Mysql56GTIDSet map[SID][]interval
// SIDs returns a sorted list of SIDs in the set.
func (set Mysql56GTIDSet) SIDs() []SID {
sids := make([]SID, 0, len(set))
for sid := range set {
sids = append(sids, sid)
}
sort.Sort(sidList(sids))
return sids
}
type sidList []SID
// Len implements sort.Interface.
func (s sidList) Len() int { return len(s) }
// Less implements sort.Interface.
func (s sidList) Less(i, j int) bool { return bytes.Compare(s[i][:], s[j][:]) < 0 }
// Swap implements sort.Interface.
func (s sidList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// String implements GTIDSet.
func (set Mysql56GTIDSet) String() string {
buf := &bytes.Buffer{}
for i, sid := range set.SIDs() {
if i != 0 {
buf.WriteByte(',')
}
buf.WriteString(sid.String())
for _, interval := range set[sid] {
buf.WriteByte(':')
buf.WriteString(strconv.FormatInt(interval.start, 10))
if interval.end != interval.start {
buf.WriteByte('-')
buf.WriteString(strconv.FormatInt(interval.end, 10))
}
}
}
return buf.String()
}
// Flavor implements GTIDSet.
func (Mysql56GTIDSet) Flavor() string { return mysql56FlavorID }
// ContainsGTID implements GTIDSet.
func (set Mysql56GTIDSet) ContainsGTID(gtid GTID) bool {
gtid56, ok := gtid.(Mysql56GTID)
if !ok {
return false
}
for _, iv := range set[gtid56.Server] {
if iv.start > gtid56.Sequence {
// We assume intervals are sorted, so we can skip the rest.
return false
}
if gtid56.Sequence <= iv.end {
// Now we know that: start <= Sequence <= end.
return true
}
}
// Server wasn't in the set, or no interval contained gtid.
return false
}
// Contains implements GTIDSet.
func (set Mysql56GTIDSet) Contains(other GTIDSet) bool {
other56, ok := other.(Mysql56GTIDSet)
if !ok {
return false
}
// Check each SID in the other set.
for sid, otherIntervals := range other56 {
i := 0
intervals := set[sid]
count := len(intervals)
// Check each interval for this SID in the other set.
for _, iv := range otherIntervals {
// Check that interval against each of our intervals.
// Intervals are monotonically increasing,
// so we don't need to reset the index each time.
for {
if i >= count {
// We ran out of intervals to check against.
return false
}
if intervals[i].contains(iv) {
// Yes it's covered. Go on to the next one.
break
}
i++
}
}
}
// No uncovered intervals were found.
return true
}
// Equal implements GTIDSet.
func (set Mysql56GTIDSet) Equal(other GTIDSet) bool {
other56, ok := other.(Mysql56GTIDSet)
if !ok {
return false
}
// Check for same number of SIDs.
if len(set) != len(other56) {
return false
}
// Compare each SID.
for sid, intervals := range set {
otherIntervals := other56[sid]
// Check for same number of intervals.
if len(intervals) != len(otherIntervals) {
return false
}
// Compare each interval.
// Since intervals are sorted, they have to be in the same order.
for i, iv := range intervals {
if iv != otherIntervals[i] {
return false
}
}
}
// No discrepancies were found.
return true
}
// AddGTID implements GTIDSet.
func (set Mysql56GTIDSet) AddGTID(gtid GTID) GTIDSet {
gtid56, ok := gtid.(Mysql56GTID)
if !ok {
return set
}
// If it's already in the set, we can return the same instance.
// This is safe because GTIDSets are immutable.
if set.ContainsGTID(gtid) {
return set
}
// Make a copy and add the new GTID in the proper place.
// This function is not supposed to modify the original set.
newSet := make(Mysql56GTIDSet)
added := false
for sid, intervals := range set {
newIntervals := make([]interval, 0, len(intervals))
if sid == gtid56.Server {
// Look for the right place to add this GTID.
for _, iv := range intervals {
if !added {
switch {
case gtid56.Sequence == iv.start-1:
// Expand the interval at the beginning.
iv.start = gtid56.Sequence
added = true
case gtid56.Sequence == iv.end+1:
// Expand the interval at the end.
iv.end = gtid56.Sequence
added = true
case gtid56.Sequence < iv.start-1:
// The next interval is beyond the new GTID, but it can't
// be expanded, so we have to insert a new interval.
newIntervals = append(newIntervals, interval{start: gtid56.Sequence, end: gtid56.Sequence})
added = true
}
}
// Check if this interval can be merged with the previous one.
count := len(newIntervals)
if count != 0 && iv.start == newIntervals[count-1].end+1 {
// Merge instead of appending.
newIntervals[count-1].end = iv.end
} else {
// Can't be merged.
newIntervals = append(newIntervals, iv)
}
}
} else {
// Just copy everything.
newIntervals = append(newIntervals, intervals...)
}
newSet[sid] = newIntervals
}
if !added {
// There wasn't any place to insert the new GTID, so just append it
// as a new interval.
newSet[gtid56.Server] = append(newSet[gtid56.Server], interval{start: gtid56.Sequence, end: gtid56.Sequence})
}
return newSet
}
// SIDBlock returns the binary encoding of a MySQL 5.6 GTID set as expected
// by internal commands that refer to an "SID block".
//
// e.g. https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html
func (set Mysql56GTIDSet) SIDBlock() []byte {
buf := &bytes.Buffer{}
// Number of SIDs.
binary.Write(buf, binary.LittleEndian, uint64(len(set)))
for _, sid := range set.SIDs() {
buf.Write(sid[:])
// Number of intervals.
intervals := set[sid]
binary.Write(buf, binary.LittleEndian, uint64(len(intervals)))
for _, iv := range intervals {
binary.Write(buf, binary.LittleEndian, iv.start)
// MySQL's internal form for intervals adds 1 to the end value.
// See Gtid_set::add_gtid_text() in rpl_gtid_set.cc for example.
binary.Write(buf, binary.LittleEndian, iv.end+1)
}
}
return buf.Bytes()
}
func init() {
gtidSetParsers[mysql56FlavorID] = parseMysql56GTIDSet
}