-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mariadb_gtid.go
272 lines (236 loc) · 6.69 KB
/
mariadb_gtid.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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"fmt"
"sort"
"strconv"
"strings"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
// MariadbFlavorID is the string identifier for the MariaDB flavor.
const MariadbFlavorID = "MariaDB"
// parseMariadbGTID is registered as a GTID parser.
func parseMariadbGTID(s string) (GTID, error) {
// Split into parts.
parts := strings.Split(s, "-")
if len(parts) != 3 {
return nil, vterrors.Errorf(vtrpc.Code_INTERNAL, "invalid MariaDB GTID (%v): expecting Domain-Server-Sequence", s)
}
// Parse Domain ID.
Domain, err := strconv.ParseUint(parts[0], 10, 32)
if err != nil {
return nil, vterrors.Wrapf(err, "invalid MariaDB GTID Domain ID (%v)", parts[0])
}
// Parse Server ID.
Server, err := strconv.ParseUint(parts[1], 10, 32)
if err != nil {
return nil, vterrors.Wrapf(err, "invalid MariaDB GTID Server ID (%v)", parts[1])
}
// Parse Sequence number.
Sequence, err := strconv.ParseUint(parts[2], 10, 64)
if err != nil {
return nil, vterrors.Wrapf(err, "invalid MariaDB GTID Sequence number (%v)", parts[2])
}
return MariadbGTID{
Domain: uint32(Domain),
Server: uint32(Server),
Sequence: Sequence,
}, nil
}
// parseMariadbGTIDSet is registered as a GTIDSet parser.
func parseMariadbGTIDSet(s string) (GTIDSet, error) {
gtidStrings := strings.Split(s, ",")
gtidSet := make(MariadbGTIDSet, len(gtidStrings))
for _, gtidString := range gtidStrings {
gtid, err := parseMariadbGTID(gtidString)
if err != nil {
return nil, err
}
mdbGTID := gtid.(MariadbGTID)
gtidSet[mdbGTID.Domain] = mdbGTID
}
return gtidSet, nil
}
// MariadbGTID implements GTID.
type MariadbGTID struct {
// Domain is the ID number of the domain within which sequence numbers apply.
Domain uint32
// Server is the ID of the server that generated the transaction.
Server uint32
// Sequence is the sequence number of the transaction within the domain.
Sequence uint64
}
// String implements GTID.String().
func (gtid MariadbGTID) String() string {
return fmt.Sprintf("%d-%d-%d", gtid.Domain, gtid.Server, gtid.Sequence)
}
// Flavor implements GTID.Flavor().
func (gtid MariadbGTID) Flavor() string {
return MariadbFlavorID
}
// SequenceDomain implements GTID.SequenceDomain().
func (gtid MariadbGTID) SequenceDomain() interface{} {
return gtid.Domain
}
// SourceServer implements GTID.SourceServer().
func (gtid MariadbGTID) SourceServer() interface{} {
return gtid.Server
}
// SequenceNumber implements GTID.SequenceNumber().
func (gtid MariadbGTID) SequenceNumber() interface{} {
return gtid.Sequence
}
// GTIDSet implements GTID.GTIDSet().
func (gtid MariadbGTID) GTIDSet() GTIDSet {
return MariadbGTIDSet{gtid.Domain: gtid}
}
// MariadbGTIDSet implements GTIDSet.
type MariadbGTIDSet map[uint32]MariadbGTID
// String implements GTIDSet.String()
func (gtidSet MariadbGTIDSet) String() string {
// Sort domains so the string format is deterministic.
domains := make([]uint32, 0, len(gtidSet))
for domain := range gtidSet {
domains = append(domains, domain)
}
sort.Slice(domains, func(i, j int) bool {
return domains[i] < domains[j]
})
// Convert each domain's GTID to a string and join all with comma.
s := make([]string, len(gtidSet))
for i, domain := range domains {
s[i] = gtidSet[domain].String()
}
return strings.Join(s, ",")
}
// Flavor implements GTIDSet.Flavor()
func (gtidSet MariadbGTIDSet) Flavor() string {
return MariadbFlavorID
}
// ContainsGTID implements GTIDSet.ContainsGTID().
func (gtidSet MariadbGTIDSet) ContainsGTID(other GTID) bool {
if other == nil {
return true
}
mdbOther, ok := other.(MariadbGTID)
if !ok {
return false
}
gtid, ok := gtidSet[mdbOther.Domain]
if !ok {
return false
}
return gtid.Sequence >= mdbOther.Sequence
}
// Contains implements GTIDSet.Contains().
func (gtidSet MariadbGTIDSet) Contains(other GTIDSet) bool {
if other == nil {
return false
}
mdbOther, ok := other.(MariadbGTIDSet)
if !ok {
return false
}
for _, gtid := range mdbOther {
if !gtidSet.ContainsGTID(gtid) {
return false
}
}
return true
}
// Equal implements GTIDSet.Equal().
func (gtidSet MariadbGTIDSet) Equal(other GTIDSet) bool {
mdbOther, ok := other.(MariadbGTIDSet)
if !ok {
return false
}
if len(gtidSet) != len(mdbOther) {
return false
}
for domain, gtid := range gtidSet {
otherGTID, ok := mdbOther[domain]
if !ok {
return false
}
if gtid != otherGTID {
return false
}
}
return true
}
// AddGTID implements GTIDSet.AddGTID().
func (gtidSet MariadbGTIDSet) AddGTID(other GTID) GTIDSet {
if other == nil {
return gtidSet
}
mdbOther, ok := other.(MariadbGTID)
if !ok {
return gtidSet
}
newSet := gtidSet.deepCopy()
newSet.addGTID(mdbOther)
return newSet
}
// Union implements GTIDSet.Union(). This is a pure method, and does not mutate the receiver.
func (gtidSet MariadbGTIDSet) Union(other GTIDSet) GTIDSet {
if gtidSet == nil && other != nil {
return other
}
if gtidSet == nil || other == nil {
return gtidSet
}
mdbOther, ok := other.(MariadbGTIDSet)
if !ok {
return gtidSet
}
newSet := gtidSet.deepCopy()
for _, otherGTID := range mdbOther {
newSet.addGTID(otherGTID)
}
return newSet
}
//Last returns the last gtid
func (gtidSet MariadbGTIDSet) Last() string {
// Sort domains so the string format is deterministic.
domains := make([]uint32, 0, len(gtidSet))
for domain := range gtidSet {
domains = append(domains, domain)
}
sort.Slice(domains, func(i, j int) bool {
return domains[i] < domains[j]
})
lastGTID := domains[len(gtidSet)-1]
return gtidSet[lastGTID].String()
}
// deepCopy returns a deep copy of the set.
func (gtidSet MariadbGTIDSet) deepCopy() MariadbGTIDSet {
newSet := make(MariadbGTIDSet, len(gtidSet))
for domain, gtid := range gtidSet {
newSet[domain] = gtid
}
return newSet
}
// addGTID is an internal method that adds a GTID to the set.
// Unlike the exported methods, this mutates the receiver.
func (gtidSet MariadbGTIDSet) addGTID(otherGTID MariadbGTID) {
gtid, ok := gtidSet[otherGTID.Domain]
if !ok || otherGTID.Sequence > gtid.Sequence {
gtidSet[otherGTID.Domain] = otherGTID
}
}
func init() {
gtidParsers[MariadbFlavorID] = parseMariadbGTID
gtidSetParsers[MariadbFlavorID] = parseMariadbGTIDSet
}