generated from cybozu-go/neco-template
/
status.go
504 lines (454 loc) · 10.7 KB
/
status.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package clustering
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"sync"
"time"
mocov1beta1 "github.com/cybozu-go/moco/api/v1beta1"
"github.com/cybozu-go/moco/pkg/constants"
"github.com/cybozu-go/moco/pkg/dbop"
"github.com/cybozu-go/moco/pkg/password"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
statusCheckRetryMax = 3
statusCheckRetryInterval = 3 * time.Second
)
func init() {
intervalStr := os.Getenv("MOCO_CHECK_INTERVAL")
if intervalStr == "" {
return
}
interval, err := time.ParseDuration(intervalStr)
if err != nil {
return
}
statusCheckRetryInterval = interval
}
// ClusterState represents the state of a MySQL cluster.
// Consult docs/clustering.md for details.
type ClusterState int
// List of possible ClusterState.
const (
StateUndecided ClusterState = iota
StateIncomplete
StateHealthy
StateCloning
StateRestoring
StateDegraded
StateFailed
StateLost
)
// String returns a unique string for each ClusterState.
func (s ClusterState) String() string {
switch s {
case StateUndecided:
return "Undecided"
case StateIncomplete:
return "Incomplete"
case StateHealthy:
return "Healthy"
case StateCloning:
return "Cloning"
case StateRestoring:
return "Restoring"
case StateDegraded:
return "Degraded"
case StateFailed:
return "Failed"
case StateLost:
return "Lost"
}
panic(int(s))
}
// StatusSet represents the set of information to determine the ClusterState
// and later operations.
type StatusSet struct {
Primary int
Cluster *mocov1beta1.MySQLCluster
Password *password.MySQLPassword
Pods []*corev1.Pod
DBOps []dbop.Operator
MySQLStatus []*dbop.MySQLInstanceStatus
ExecutedGTID string
Errants []int
Candidates []int
NeedSwitch bool
Candidate int
State ClusterState
}
// Close closes `ss.DBOps`.
func (ss *StatusSet) Close() {
for _, op := range ss.DBOps {
if op != nil {
op.Close()
}
}
}
// DecideState decides the ClusterState and set it to `ss.State`.
// It may also set `ss.NeedSwitch` and `ss.Candidate` for switchover.
func (ss *StatusSet) DecideState() {
switch {
case isCloning(ss):
ss.State = StateCloning
case isRestoring(ss):
ss.State = StateRestoring
case isHealthy(ss):
ss.State = StateHealthy
case isDegraded(ss):
ss.State = StateDegraded
case isFailed(ss):
ss.State = StateFailed
case isLost(ss):
ss.State = StateLost
default:
ss.State = StateIncomplete
}
if len(ss.Candidates) > 0 {
ss.NeedSwitch = needSwitch(ss.Pods[ss.Primary])
// Choose the lowest ordinal for a switchover target.
sort.Ints(ss.Candidates)
ss.Candidate = ss.Candidates[0]
}
}
// GatherStatus collects information and Kubernetes resources and construct
// StatusSet. It calls `StatusSet.DecideState` before returning.
func (p *managerProcess) GatherStatus(ctx context.Context) (*StatusSet, error) {
ss := &StatusSet{}
cluster := &mocov1beta1.MySQLCluster{}
if err := p.reader.Get(ctx, p.name, cluster); err != nil {
return nil, fmt.Errorf("failed to get MySQLCluster: %w", err)
}
ss.Cluster = cluster
ss.Primary = cluster.Status.CurrentPrimaryIndex
passwdSecret := &corev1.Secret{}
if err := p.client.Get(ctx, client.ObjectKey{Namespace: p.name.Namespace, Name: cluster.UserSecretName()}, passwdSecret); err != nil {
return nil, fmt.Errorf("failed to get password secret: %w", err)
}
passwd, err := password.NewMySQLPasswordFromSecret(passwdSecret)
if err != nil {
return nil, err
}
ss.Password = passwd
pods := &corev1.PodList{}
if err := p.client.List(ctx, pods, client.InNamespace(p.name.Namespace), client.MatchingLabels{
constants.LabelAppName: constants.AppNameMySQL,
constants.LabelAppInstance: p.name.Name,
}); err != nil {
return nil, fmt.Errorf("failed to list Pods: %w", err)
}
if int(cluster.Spec.Replicas) != len(pods.Items) {
return nil, fmt.Errorf("too few pods; only %d pods exist", len(pods.Items))
}
ss.Pods = make([]*corev1.Pod, cluster.Spec.Replicas)
for i, pod := range pods.Items {
fields := strings.Split(pod.Name, "-")
index, err := strconv.Atoi(fields[len(fields)-1])
if err != nil {
return nil, fmt.Errorf("bad pod name: %s", pod.Name)
}
if index < 0 || index >= len(pods.Items) {
return nil, fmt.Errorf("index out of range: %d", index)
}
ss.Pods[index] = &pods.Items[i]
}
ss.DBOps = make([]dbop.Operator, cluster.Spec.Replicas)
defer func() {
if ss.State == StateUndecided {
ss.Close()
}
}()
for i := 0; i < int(cluster.Spec.Replicas); i++ {
op, err := p.dbf.New(ctx, cluster, passwd, i)
if err != nil {
return nil, err
}
ss.DBOps[i] = op
}
ss.MySQLStatus = make([]*dbop.MySQLInstanceStatus, cluster.Spec.Replicas)
var wg sync.WaitGroup
for i := 0; i < len(ss.MySQLStatus); i++ {
wg.Add(1)
go func(index int) {
defer wg.Done()
for j := 0; j < statusCheckRetryMax; j++ {
ist, err := ss.DBOps[index].GetStatus(ctx)
if err != nil {
p.log.Error(err, "failed to get mysqld status")
time.Sleep(statusCheckRetryInterval)
continue
}
ss.MySQLStatus[index] = ist
return
}
}(i)
}
wg.Wait()
// re-check the primary MySQL status to retrieve the latest executed GTID set
if ss.MySQLStatus[ss.Primary] != nil {
time.Sleep(100 * time.Millisecond)
pst, err := ss.DBOps[ss.Primary].GetStatus(ctx)
if err != nil {
return nil, fmt.Errorf("failed to re-check the primary instance: %w", err)
}
ss.MySQLStatus[ss.Primary] = pst
ss.ExecutedGTID = pst.GlobalVariables.ExecutedGTID
}
if ss.ExecutedGTID != "" {
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
continue
}
ok, err := ss.DBOps[ss.Primary].IsSubsetGTID(ctx, ist.GlobalVariables.ExecutedGTID, ss.ExecutedGTID)
if err != nil {
return nil, fmt.Errorf("failed to compare GTID %s and %s: %w", ist.GlobalVariables.ExecutedGTID, ss.ExecutedGTID, err)
}
if !ok {
ist.IsErrant = true
ss.Errants = append(ss.Errants, i)
}
}
} else {
// restore errant replica status from information stored in MySQLCluster
// when the primary is down or possibly lost data.
for _, index := range cluster.Status.ErrantReplicaList {
ss.MySQLStatus[index].IsErrant = true
ss.Errants = append(ss.Errants, index)
}
}
ss.DecideState()
return ss, nil
}
func isPodReady(pod *corev1.Pod) bool {
for _, cond := range pod.Status.Conditions {
if cond.Type != corev1.PodReady {
continue
}
if cond.Status == corev1.ConditionTrue {
return true
}
}
return false
}
func replicasInCluster(cluster *mocov1beta1.MySQLCluster, replicas []dbop.ReplicaHost) int32 {
var n int32
for _, r := range replicas {
if r.ServerID >= cluster.Spec.ServerIDBase && r.ServerID < (cluster.Spec.ServerIDBase+cluster.Spec.Replicas) {
n++
}
}
return n
}
func lostData(ss *StatusSet) bool {
if ss.ExecutedGTID != "" {
return false
}
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
continue
}
if ist.GlobalVariables.ExecutedGTID != "" {
return true
}
}
return false
}
func isHealthy(ss *StatusSet) bool {
for _, pod := range ss.Pods {
if !isPodReady(pod) {
return false
}
}
primaryHostname := ss.Cluster.PodHostname(ss.Primary)
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
return false
}
if ist.IsErrant {
return false
}
if !ist.GlobalVariables.SuperReadOnly {
return false
}
if ist.ReplicaStatus == nil {
return false
}
if ist.ReplicaStatus.MasterHost != primaryHostname {
return false
}
ss.Candidates = append(ss.Candidates, i)
}
pst := ss.MySQLStatus[ss.Primary]
if pst == nil {
return false
}
if replicasInCluster(ss.Cluster, pst.ReplicaHosts) != (ss.Cluster.Spec.Replicas - 1) {
return false
}
if ss.Cluster.Spec.ReplicationSourceSecretName != nil {
if !pst.GlobalVariables.SuperReadOnly {
return false
}
} else {
if pst.GlobalVariables.ReadOnly {
return false
}
}
return true
}
func isCloning(ss *StatusSet) bool {
if ss.Cluster.Spec.ReplicationSourceSecretName == nil {
return false
}
if ss.Cluster.Status.Cloned {
return false
}
pst := ss.MySQLStatus[ss.Primary]
if pst == nil {
return true
}
if pst.CloneStatus != nil && pst.CloneStatus.State.String != "Completed" {
return true
}
if pst.CloneStatus == nil && pst.GlobalVariables.ExecutedGTID == "" {
return true
}
return false
}
func isRestoring(ss *StatusSet) bool {
if ss.Cluster.Spec.Restore == nil {
return false
}
if ss.Cluster.Status.RestoredTime != nil {
return false
}
return true
}
func isDegraded(ss *StatusSet) bool {
ppod := ss.Pods[ss.Primary]
if !isPodReady(ppod) {
return false
}
if lostData(ss) {
return false
}
pst := ss.MySQLStatus[ss.Primary]
if pst == nil {
return false
}
if ss.Cluster.Spec.ReplicationSourceSecretName != nil {
if !pst.GlobalVariables.SuperReadOnly {
return false
}
} else {
if pst.GlobalVariables.ReadOnly {
return false
}
}
if replicasInCluster(ss.Cluster, pst.ReplicaHosts) < (ss.Cluster.Spec.Replicas / 2) {
return false
}
primaryHostname := ss.Cluster.PodHostname(ss.Primary)
var okReplicas int
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
continue
}
if !isPodReady(ss.Pods[i]) {
continue
}
if !ist.GlobalVariables.SuperReadOnly {
continue
}
if ist.ReplicaStatus == nil {
continue
}
if ist.ReplicaStatus.MasterHost != primaryHostname {
continue
}
if ist.IsErrant {
continue
}
okReplicas++
ss.Candidates = append(ss.Candidates, i)
}
return okReplicas >= (int(ss.Cluster.Spec.Replicas)/2) && okReplicas != int(ss.Cluster.Spec.Replicas-1)
}
func isFailed(ss *StatusSet) bool {
pst := ss.MySQLStatus[ss.Primary]
if pst != nil && !lostData(ss) {
return false
}
var okReplicas int
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
continue
}
if ist.ReplicaStatus == nil {
continue
}
if ist.IsErrant {
continue
}
if ist.GlobalVariables.ExecutedGTID == "" {
continue
}
okReplicas++
}
return okReplicas > (int(ss.Cluster.Spec.Replicas) / 2)
}
func isLost(ss *StatusSet) bool {
pst := ss.MySQLStatus[ss.Primary]
if pst != nil && !lostData(ss) {
return false
}
var okReplicas int
for i, ist := range ss.MySQLStatus {
if i == ss.Primary {
continue
}
if ist == nil {
continue
}
if ist.ReplicaStatus == nil {
continue
}
if ist.IsErrant {
continue
}
if ist.GlobalVariables.ExecutedGTID == "" {
continue
}
okReplicas++
}
return okReplicas <= (int(ss.Cluster.Spec.Replicas) / 2)
}
func needSwitch(pod *corev1.Pod) bool {
if pod.DeletionTimestamp != nil {
return true
}
if pod.Annotations[constants.AnnDemote] == "true" {
return true
}
return false
}