Skip to content

Commit 546ae8c

Browse files
committed
Initial support for structurized oracle gtid entries
1 parent 47902f8 commit 546ae8c

File tree

4 files changed

+167
-2
lines changed

4 files changed

+167
-2
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
set -e
88

9-
RELEASE_VERSION="1.4.541"
9+
RELEASE_VERSION="1.4.542"
1010
TOPDIR=/tmp/orchestrator-release
1111
export RELEASE_VERSION TOPDIR
1212

go/inst/instance_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (s *TestSuite) TestInstanceKeyValid(c *C) {
227227
c.Assert(err, IsNil)
228228
c.Assert(i.IsValid(), Equals, false)
229229
i, err = inst.ParseInstanceKey("//myhost:3306")
230-
c.Assert(err, IsNil)
230+
c.Assert(err, Not(IsNil))
231231
c.Assert(i.IsValid(), Equals, false)
232232
}
233233

@@ -304,6 +304,52 @@ func (s *TestSuite) TestNextGTID(c *C) {
304304
}
305305
}
306306

307+
func (s *TestSuite) TestOracleGTIDSet(c *C) {
308+
gtidSetString := `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539,
309+
316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596,
310+
321f5c0d-70e5-11e5-adb2-ecf4bb2262ff:1-56`
311+
gtidSet, err := inst.ParseGtidSet(gtidSetString)
312+
c.Assert(err, IsNil)
313+
c.Assert(len(gtidSet.GtidEntries), Equals, 3)
314+
c.Assert(gtidSet.String(), Equals, `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539,
315+
316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596,
316+
321f5c0d-70e5-11e5-adb2-ecf4bb2262ff:1-56`)
317+
318+
c.Assert(gtidSet.GtidEntries[0].UUID, Equals, `230ea8ea-81e3-11e4-972a-e25ec4bd140a`)
319+
c.Assert(gtidSet.GtidEntries[1].UUID, Equals, `316d193c-70e5-11e5-adb2-ecf4bb2262ff`)
320+
c.Assert(gtidSet.GtidEntries[2].UUID, Equals, `321f5c0d-70e5-11e5-adb2-ecf4bb2262ff`)
321+
c.Assert(gtidSet.GtidEntries[1].Ranges, Equals, `1-8935:8984-6124596`)
322+
323+
removed := gtidSet.RemoveUUID(`ffffffff-70e5-11e5-adb2-ecf4bb2262ff`)
324+
c.Assert(removed, Equals, false)
325+
c.Assert(len(gtidSet.GtidEntries), Equals, 3)
326+
327+
removed = gtidSet.RemoveUUID(`316d193c-70e5-11e5-adb2-ecf4bb2262ff`)
328+
c.Assert(removed, Equals, true)
329+
c.Assert(len(gtidSet.GtidEntries), Equals, 2)
330+
331+
c.Assert(gtidSet.String(), Equals, `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539,
332+
321f5c0d-70e5-11e5-adb2-ecf4bb2262ff:1-56`)
333+
334+
removed = gtidSet.RemoveUUID(`316d193c-70e5-11e5-adb2-ecf4bb2262ff`)
335+
c.Assert(removed, Equals, false)
336+
c.Assert(len(gtidSet.GtidEntries), Equals, 2)
337+
338+
removed = gtidSet.RemoveUUID(`321f5c0d-70e5-11e5-adb2-ecf4bb2262ff`)
339+
c.Assert(removed, Equals, true)
340+
c.Assert(len(gtidSet.GtidEntries), Equals, 1)
341+
c.Assert(gtidSet.String(), Equals, `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539`)
342+
343+
removed = gtidSet.RemoveUUID(`230ea8ea-81e3-11e4-972a-e25ec4bd140a`)
344+
c.Assert(removed, Equals, true)
345+
c.Assert(len(gtidSet.GtidEntries), Equals, 0)
346+
347+
removed = gtidSet.RemoveUUID(`230ea8ea-81e3-11e4-972a-e25ec4bd140a`)
348+
c.Assert(removed, Equals, false)
349+
c.Assert(len(gtidSet.GtidEntries), Equals, 0)
350+
c.Assert(gtidSet.String(), Equals, ``)
351+
}
352+
307353
func (s *TestSuite) TestRemoveInstance(c *C) {
308354
{
309355
instances := [](*inst.Instance){&instance1, &instance2}

go/inst/oracle_gtid_set.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2015 Shlomi Noach, courtesy Booking.com
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package inst
18+
19+
import (
20+
"strings"
21+
)
22+
23+
// OracleGtidSet represents a set of GTID ranges as depicted by Retrieved_Gtid_Set, Executed_Gtid_Set or @@gtid_purged.
24+
type OracleGtidSet struct {
25+
GtidEntries [](*OracleGtidSetEntry)
26+
}
27+
28+
// Example input: `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539,
29+
// 316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596,
30+
// 321f5c0d-70e5-11e5-adb2-ecf4bb2262ff:1-56`
31+
func ParseGtidSet(gtidSet string) (res *OracleGtidSet, err error) {
32+
res = &OracleGtidSet{}
33+
34+
gtidSet = strings.TrimSpace(gtidSet)
35+
if gtidSet == "" {
36+
return res, nil
37+
}
38+
entries := strings.Split(gtidSet, ",")
39+
for _, entry := range entries {
40+
if gtidRange, err := NewOracleGtidSetEntry(entry); err == nil {
41+
res.GtidEntries = append(res.GtidEntries, gtidRange)
42+
} else {
43+
return res, err
44+
}
45+
}
46+
return res, nil
47+
}
48+
49+
// RemoveUUID removes entries that belong to given UUID.
50+
// By way of how this works there can only be one entry matching our UUID, but we generalize.
51+
// We keep order of entries.
52+
func (this *OracleGtidSet) RemoveUUID(uuid string) (removed bool) {
53+
filteredEntries := [](*OracleGtidSetEntry){}
54+
for _, entry := range this.GtidEntries {
55+
if entry.UUID == uuid {
56+
removed = true
57+
} else {
58+
filteredEntries = append(filteredEntries, entry)
59+
}
60+
}
61+
if removed {
62+
this.GtidEntries = filteredEntries
63+
}
64+
return removed
65+
}
66+
67+
func (this OracleGtidSet) String() string {
68+
tokens := []string{}
69+
for _, entry := range this.GtidEntries {
70+
tokens = append(tokens, entry.String())
71+
}
72+
return strings.Join(tokens, ",\n")
73+
}

go/inst/oracle_gtid_set_entry.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2015 Shlomi Noach, courtesy Booking.com
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package inst
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
)
23+
24+
// OracleGtidSetEntry represents an entry in a set of GTID ranges,
25+
// for example, the entry: "316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596" (may include gaps)
26+
type OracleGtidSetEntry struct {
27+
UUID string
28+
Ranges string
29+
}
30+
31+
32+
// NewOracleGtidSetEntry parses a single entry text
33+
func NewOracleGtidSetEntry(gtidRangeString string) (*OracleGtidSetEntry, error) {
34+
gtidRangeString = strings.TrimSpace(gtidRangeString)
35+
tokens := strings.SplitN(gtidRangeString, ":", 2)
36+
if len(tokens) != 2 {
37+
return nil, fmt.Errorf("Cannot parse OracleGtidSetEntry from %s", gtidRangeString)
38+
}
39+
gtidRange := &OracleGtidSetEntry{UUID: tokens[0], Ranges: tokens[1]}
40+
return gtidRange, nil
41+
}
42+
43+
// String returns a user-friendly string representation of this entry
44+
func (this OracleGtidSetEntry) String() string {
45+
return fmt.Sprintf("%s:%s", this.UUID, this.Ranges)
46+
}

0 commit comments

Comments
 (0)