-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
client.go
351 lines (287 loc) · 7.77 KB
/
client.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
/*
Copyright 2018 Pressinfra SRL
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 fake
import (
"database/sql"
"fmt"
"regexp"
"strings"
"sync"
// nolint: golint
. "github.com/bitpoke/mysql-operator/pkg/orchestrator"
)
// OrcFakeClient is a structure that implements orchestrator client interface used in
// testing
type OrcFakeClient struct {
Clusters map[string][]*Instance
Recoveries map[string][]TopologyRecovery
AckRec []int64
Discovered []InstanceKey
lock *sync.Mutex
reachable bool
}
var nextID int64
func getNextID() int64 {
nextID = nextID + 1
return nextID
}
// New fake orchestrator client
func New() *OrcFakeClient {
return &OrcFakeClient{
lock: &sync.Mutex{},
reachable: true,
}
}
// Reset removes all instances and ack from a client
func (o *OrcFakeClient) Reset() {
o.Clusters = *new(map[string][]*Instance)
o.Recoveries = *new(map[string][]TopologyRecovery)
o.AckRec = []int64{}
o.Discovered = []InstanceKey{}
}
// MakeOrcUnreachable makes every function return an error
func (o *OrcFakeClient) MakeOrcUnreachable() {
o.reachable = false
}
// AddInstance add a instance to orchestrator client
func (o *OrcFakeClient) AddInstance(instance Instance) *Instance {
o.lock.Lock()
defer o.lock.Unlock()
cluster := instance.ClusterName
instance.Key.Port = 3306
instance.MasterKey.Port = 3306
if o.Clusters == nil {
o.Clusters = make(map[string][]*Instance)
}
clusters, ok := o.Clusters[cluster]
if ok {
o.Clusters[cluster] = append(clusters, &instance)
} else {
o.Clusters[cluster] = []*Instance{&instance}
}
return &instance
}
// RemoveInstance deletes a instance from orchestrator
func (o *OrcFakeClient) RemoveInstance(cluster, host string) {
o.lock.Lock()
defer o.lock.Unlock()
instances, ok := o.Clusters[cluster]
if !ok {
return
}
index := -1
for i, inst := range instances {
if inst.Key.Hostname == host {
index = i
}
}
if index == -1 {
return
}
// remove instance from cluster
o.Clusters[cluster] = append(instances[:index], instances[index+1:]...)
// remove cluster key from map if has no instance
if len(o.Clusters[cluster]) == 0 {
delete(o.Clusters, cluster)
}
}
// AddRecoveries add a recovery for a cluster
func (o *OrcFakeClient) AddRecoveries(cluster string, ack bool) int64 {
o.lock.Lock()
defer o.lock.Unlock()
id := getNextID()
tr := TopologyRecovery{
Id: id,
Acknowledged: ack,
RecoveryStartTimestamp: "2018-05-16T13:15:05Z",
}
rs, ok := o.Recoveries[cluster]
if ok {
o.Recoveries[cluster] = append(rs, tr)
}
if o.Recoveries == nil {
o.Recoveries = make(map[string][]TopologyRecovery)
}
o.Recoveries[cluster] = []TopologyRecovery{tr}
return id
}
// CheckAck verify is an ack is present
func (o *OrcFakeClient) CheckAck(id int64) bool {
o.lock.Lock()
defer o.lock.Unlock()
for _, a := range o.AckRec {
if a == id {
return true
}
}
return false
}
// CheckDiscovered verify if a key was discovered or not
func (o *OrcFakeClient) CheckDiscovered(key string) bool {
for _, d := range o.Discovered {
if d.Hostname == key {
return true
}
}
return false
}
func (o *OrcFakeClient) getHostClusterAlias(host string) string {
// input: cluster-1943285891-mysql-0.mysql.default
// output: cluster-1943285891.default
re := regexp.MustCompile(`^([\w-]+)-mysql-\d*.mysql.([\w-]+)$`)
values := re.FindStringSubmatch(host)
return fmt.Sprintf("%s.%s", values[1], values[2])
}
// Discover register a host into orchestrator
func (o *OrcFakeClient) Discover(host string, port int) error {
if !o.reachable {
return NewErrorMsg("can't connect to orc", "/")
}
o.Discovered = append(o.Discovered, InstanceKey{
Hostname: host,
Port: port,
})
readOnly := true
if strings.Contains(host, "-0") {
// make node-0 as master alywas
readOnly = false
}
cluster := o.getHostClusterAlias(host)
o.AddInstance(Instance{
ClusterName: cluster,
Key: InstanceKey{Hostname: host},
ReadOnly: readOnly,
SlaveLagSeconds: sql.NullInt64{
Valid: false,
Int64: 0,
},
IsUpToDate: true,
IsRecentlyChecked: true,
IsLastCheckValid: true,
})
return nil
}
// Forget removes a host from orchestrator
func (o *OrcFakeClient) Forget(host string, port int) error {
if !o.reachable {
return NewErrorMsg("can't connect to orc", "/")
}
// determine cluster name
cluster := o.getHostClusterAlias(host)
o.RemoveInstance(cluster, host)
return nil
}
// Master returns the master of a cluster
func (o *OrcFakeClient) Master(clusterHint string) (*Instance, error) {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return nil, NewErrorMsg("can't connect to orc", "/")
}
insts, ok := o.Clusters[clusterHint]
if !ok {
return nil, NewErrorMsg("Unable to determine cluster name", "/master")
}
for _, inst := range insts {
if !inst.ReadOnly {
return inst, nil
}
}
return nil, NewErrorMsg("No masters found", "/master")
}
// Cluster returns the list of instances from a cluster
func (o *OrcFakeClient) Cluster(cluster string) ([]Instance, error) {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return nil, NewErrorMsg("can't connect to orc", "/")
}
instsPointers, ok := o.Clusters[cluster]
if !ok {
return nil, NewErrorMsg("Unable to determine cluster name", "/cluster")
}
insts := []Instance{}
for _, instP := range instsPointers {
insts = append(insts, *instP)
}
return insts, nil
}
// AuditRecovery returns recoveries for a cluster
func (o *OrcFakeClient) AuditRecovery(cluster string) ([]TopologyRecovery, error) {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return nil, NewErrorMsg("can't connect to orc", "/")
}
recoveries, ok := o.Recoveries[cluster]
if !ok {
return nil, NewErrorMsg("Unable to determine cluster name", "/audit-recovery")
}
return recoveries, nil
}
// AckRecovery ack a recovery
func (o *OrcFakeClient) AckRecovery(id int64, comment string) error {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return NewErrorMsg("can't connect to orc", "/")
}
o.AckRec = append(o.AckRec, id)
return nil
}
// SetHostWritable make a host writable
func (o *OrcFakeClient) SetHostWritable(key InstanceKey) error {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return NewErrorMsg("can't connect to orc", "/")
}
for _, instances := range o.Clusters {
for _, instance := range instances {
if instance.Key.Hostname == key.Hostname {
instance.ReadOnly = false
return nil
}
}
}
return fmt.Errorf("the desired host and port was not found")
}
// SetHostReadOnly make a host read only
func (o *OrcFakeClient) SetHostReadOnly(key InstanceKey) error {
o.lock.Lock()
defer o.lock.Unlock()
if !o.reachable {
return NewErrorMsg("can't connect to orc", "/")
}
for _, instances := range o.Clusters {
for _, instance := range instances {
if instance.Key.Hostname == key.Hostname {
instance.ReadOnly = true
return nil
}
}
}
return fmt.Errorf("the desired host and port was not found")
}
// BeginMaintenance set a host in maintenance
func (o *OrcFakeClient) BeginMaintenance(key InstanceKey, owner, reason string) error {
return nil
}
// EndMaintenance set a host in maintenance
func (o *OrcFakeClient) EndMaintenance(key InstanceKey) error {
return nil
}
// Maintenance put a node into maintenance
func (o *OrcFakeClient) Maintenance() ([]Maintenance, error) {
return nil, nil
}