forked from openark/orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_utils.go
184 lines (165 loc) · 5.99 KB
/
instance_utils.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
/*
Copyright 2015 Shlomi Noach, courtesy Booking.com
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 inst
import (
"regexp"
"strconv"
"strings"
)
var (
DowntimeLostInRecoveryMessage = "lost-in-recovery"
)
type InstancesSorterByExec struct {
instances [](*Instance)
dataCenter string
}
func NewInstancesSorterByExec(instances [](*Instance), dataCenter string) *InstancesSorterByExec {
return &InstancesSorterByExec{
instances: instances,
dataCenter: dataCenter,
}
}
func (this *InstancesSorterByExec) Len() int { return len(this.instances) }
func (this *InstancesSorterByExec) Swap(i, j int) {
this.instances[i], this.instances[j] = this.instances[j], this.instances[i]
}
func (this *InstancesSorterByExec) Less(i, j int) bool {
// Returning "true" in this function means [i] is "smaller" than [j],
// which will lead to [j] be a better candidate for promotion
// Sh*t happens. We just might get nil while attempting to discover/recover
if this.instances[i] == nil {
return false
}
if this.instances[j] == nil {
return true
}
if this.instances[i].ExecBinlogCoordinates.Equals(&this.instances[j].ExecBinlogCoordinates) {
// Secondary sorting: "smaller" if not logging replica updates
if this.instances[j].LogSlaveUpdatesEnabled && !this.instances[i].LogSlaveUpdatesEnabled {
return true
}
// Next sorting: "smaller" if of higher version (this will be reversed eventually)
// Idea is that given 5.6 a& 5.7 both of the exact position, we will want to promote
// the 5.6 on top of 5.7, as the other way around is invalid
if this.instances[j].IsSmallerMajorVersion(this.instances[i]) {
return true
}
// Next sorting: "smaller" if of larger binlog-format (this will be reversed eventually)
// Idea is that given ROW & STATEMENT both of the exact position, we will want to promote
// the STATEMENT on top of ROW, as the other way around is invalid
if this.instances[j].IsSmallerBinlogFormat(this.instances[i]) {
return true
}
if this.instances[j].DataCenter == this.dataCenter && this.instances[i].DataCenter != this.dataCenter {
return true
}
}
return this.instances[i].ExecBinlogCoordinates.SmallerThan(&this.instances[j].ExecBinlogCoordinates)
}
// filterInstancesByPattern will filter given array of instances according to regular expression pattern
func filterInstancesByPattern(instances [](*Instance), pattern string) [](*Instance) {
if pattern == "" {
return instances
}
filtered := [](*Instance){}
for _, instance := range instances {
if matched, _ := regexp.MatchString(pattern, instance.Key.DisplayString()); matched {
filtered = append(filtered, instance)
}
}
return filtered
}
// removeInstance will remove an instance from a list of instances
func RemoveInstance(instances [](*Instance), instanceKey *InstanceKey) [](*Instance) {
if instanceKey == nil {
return instances
}
for i := len(instances) - 1; i >= 0; i-- {
if instances[i].Key.Equals(instanceKey) {
instances = append(instances[:i], instances[i+1:]...)
}
}
return instances
}
// removeBinlogServerInstances will remove all binlog servers from given lsit
func RemoveBinlogServerInstances(instances [](*Instance)) [](*Instance) {
for i := len(instances) - 1; i >= 0; i-- {
if instances[i].IsBinlogServer() {
instances = append(instances[:i], instances[i+1:]...)
}
}
return instances
}
// removeNilInstances
func RemoveNilInstances(instances [](*Instance)) [](*Instance) {
for i := len(instances) - 1; i >= 0; i-- {
if instances[i] == nil {
instances = append(instances[:i], instances[i+1:]...)
}
}
return instances
}
// SemicolonTerminated is a utility function that makes sure a statement is terminated with
// a semicolon, if it isn't already
func SemicolonTerminated(statement string) string {
statement = strings.TrimSpace(statement)
statement = strings.TrimRight(statement, ";")
statement = statement + ";"
return statement
}
// MajorVersion returns a MySQL major version number (e.g. given "5.5.36" it returns "5.5")
func MajorVersion(version string) []string {
tokens := strings.Split(version, ".")
if len(tokens) < 2 {
return []string{"0", "0"}
}
return tokens[:2]
}
// IsSmallerMajorVersion tests two versions against another and returns true if
// the former is a smaller "major" varsion than the latter.
// e.g. 5.5.36 is NOT a smaller major version as comapred to 5.5.40, but IS as compared to 5.6.9
func IsSmallerMajorVersion(version string, otherVersion string) bool {
thisMajorVersion := MajorVersion(version)
otherMajorVersion := MajorVersion(otherVersion)
for i := 0; i < len(thisMajorVersion); i++ {
thisToken, _ := strconv.Atoi(thisMajorVersion[i])
otherToken, _ := strconv.Atoi(otherMajorVersion[i])
if thisToken < otherToken {
return true
}
if thisToken > otherToken {
return false
}
}
return false
}
// IsSmallerBinlogFormat tests two binlog formats and sees if one is "smaller" than the other.
// "smaller" binlog format means you can replicate from the smaller to the larger.
func IsSmallerBinlogFormat(binlogFormat string, otherBinlogFormat string) bool {
if binlogFormat == "STATEMENT" {
return (otherBinlogFormat == "ROW" || otherBinlogFormat == "MIXED")
}
if binlogFormat == "MIXED" {
return otherBinlogFormat == "ROW"
}
return false
}
// RegexpMatchPatterns returns true if s matches any of the provided regexpPatterns
func RegexpMatchPatterns(s string, regexpPatterns []string) bool {
for _, filter := range regexpPatterns {
if matched, err := regexp.MatchString(filter, s); err == nil && matched {
return true
}
}
return false
}