-
Notifications
You must be signed in to change notification settings - Fork 367
/
checker.go
253 lines (227 loc) · 6.67 KB
/
checker.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
package redisfailover
import (
"errors"
"time"
redisfailoverv1 "github.com/spotahome/redis-operator/api/redisfailover/v1"
)
const (
timeToPrepare = 2 * time.Minute
)
//UpdateRedisesPods if the running version of pods are equal to the statefulset one
func (r *RedisFailoverHandler) UpdateRedisesPods(rf *redisfailoverv1.RedisFailover) error {
redises, err := r.rfChecker.GetRedisesIPs(rf)
if err != nil {
return err
}
masterIP := ""
if !rf.Bootstrapping() {
masterIP, _ = r.rfChecker.GetMasterIP(rf)
}
// No perform updates when nodes are syncing, still not connected, etc.
for _, rp := range redises {
if rp != masterIP {
ready, err := r.rfChecker.CheckRedisSlavesReady(rp, rf)
if err != nil {
return err
}
if !ready {
return nil
}
}
}
ssUR, err := r.rfChecker.GetStatefulSetUpdateRevision(rf)
if err != nil {
return err
}
redisesPods, err := r.rfChecker.GetRedisesSlavesPods(rf)
if err != nil {
return err
}
// Update stale pods with slave role
for _, pod := range redisesPods {
revision, err := r.rfChecker.GetRedisRevisionHash(pod, rf)
if err != nil {
return err
}
if revision != ssUR {
//Delete pod and wait next round to check if the new one is synced
r.rfHealer.DeletePod(pod, rf)
return nil
}
}
if !rf.Bootstrapping() {
// Update stale pod with role master
master, err := r.rfChecker.GetRedisesMasterPod(rf)
if err != nil {
return err
}
masterRevision, err := r.rfChecker.GetRedisRevisionHash(master, rf)
if masterRevision != ssUR {
r.rfHealer.DeletePod(master, rf)
return nil
}
}
return nil
}
// CheckAndHeal runs verifcation checks to ensure the RedisFailover is in an expected and healthy state.
// If the checks do not match up to expectations, an attempt will be made to "heal" the RedisFailover into a healthy state.
func (r *RedisFailoverHandler) CheckAndHeal(rf *redisfailoverv1.RedisFailover) error {
if rf.Bootstrapping() {
return r.checkAndHealBootstrapMode(rf)
}
// Number of redis is equal as the set on the RF spec
// Number of sentinel is equal as the set on the RF spec
// Check only one master
// Number of redis master is 1
// All redis slaves have the same master
// All sentinels points to the same redis master
// Sentinel has not death nodes
// Sentinel knows the correct slave number
if err := r.rfChecker.CheckRedisNumber(rf); err != nil {
r.logger.Debug("Number of redis mismatch, this could be for a change on the statefulset")
return nil
}
if err := r.rfChecker.CheckSentinelNumber(rf); err != nil {
r.logger.Debug("Number of sentinel mismatch, this could be for a change on the deployment")
return nil
}
nMasters, err := r.rfChecker.GetNumberMasters(rf)
if err != nil {
return err
}
switch nMasters {
case 0:
redisesIP, err := r.rfChecker.GetRedisesIPs(rf)
if err != nil {
return err
}
if len(redisesIP) == 1 {
if err := r.rfHealer.MakeMaster(redisesIP[0], rf); err != nil {
return err
}
break
}
minTime, err2 := r.rfChecker.GetMinimumRedisPodTime(rf)
if err2 != nil {
return err2
}
if minTime > timeToPrepare {
r.logger.Debugf("time %.f more than expected. Not even one master, fixing...", minTime.Round(time.Second).Seconds())
// We can consider there's an error
if err2 := r.rfHealer.SetOldestAsMaster(rf); err2 != nil {
return err2
}
} else {
// We'll wait until failover is done
r.logger.Debug("No master found, wait until failover")
return nil
}
case 1:
break
default:
return errors.New("More than one master, fix manually")
}
master, err := r.rfChecker.GetMasterIP(rf)
if err != nil {
return err
}
if err2 := r.rfChecker.CheckAllSlavesFromMaster(master, rf); err2 != nil {
r.logger.Debug("Not all slaves have the same master")
if err3 := r.rfHealer.SetMasterOnAll(master, rf); err3 != nil {
return err3
}
}
if err := r.applyRedisCustomConfig(rf); err != nil {
return err
}
err = r.UpdateRedisesPods(rf)
if err != nil {
return err
}
sentinels, err := r.rfChecker.GetSentinelsIPs(rf)
if err != nil {
return err
}
for _, sip := range sentinels {
if err := r.rfChecker.CheckSentinelMonitor(sip, master); err != nil {
r.logger.Debug("Sentinel is not monitoring the correct master")
if err := r.rfHealer.NewSentinelMonitor(sip, master, rf); err != nil {
return err
}
}
}
return r.checkAndHealSentinels(rf, sentinels)
}
func (r *RedisFailoverHandler) checkAndHealBootstrapMode(rf *redisfailoverv1.RedisFailover) error {
if err := r.rfChecker.CheckRedisNumber(rf); err != nil {
r.logger.Debug("Number of redis mismatch, this could be for a change on the statefulset")
return nil
}
err := r.UpdateRedisesPods(rf)
if err != nil {
return err
}
if err := r.applyRedisCustomConfig(rf); err != nil {
return err
}
bootstrapSettings := rf.Spec.BootstrapNode
if err := r.rfHealer.SetExternalMasterOnAll(bootstrapSettings.Host, bootstrapSettings.Port, rf); err != nil {
return err
}
if rf.SentinelsAllowed() {
if err := r.rfChecker.CheckSentinelNumber(rf); err != nil {
r.logger.Debug("Number of sentinel mismatch, this could be for a change on the deployment")
return nil
}
sentinels, err := r.rfChecker.GetSentinelsIPs(rf)
if err != nil {
return err
}
for _, sip := range sentinels {
if err := r.rfChecker.CheckSentinelMonitor(sip, bootstrapSettings.Host, bootstrapSettings.Port); err != nil {
r.logger.Debug("Sentinel is not monitoring the correct master")
if err := r.rfHealer.NewSentinelMonitorWithPort(sip, bootstrapSettings.Host, bootstrapSettings.Port, rf); err != nil {
return err
}
}
}
return r.checkAndHealSentinels(rf, sentinels)
}
return nil
}
func (r *RedisFailoverHandler) applyRedisCustomConfig(rf *redisfailoverv1.RedisFailover) error {
redises, err := r.rfChecker.GetRedisesIPs(rf)
if err != nil {
return err
}
for _, rip := range redises {
if err := r.rfHealer.SetRedisCustomConfig(rip, rf); err != nil {
return err
}
}
return nil
}
func (r *RedisFailoverHandler) checkAndHealSentinels(rf *redisfailoverv1.RedisFailover, sentinels []string) error {
for _, sip := range sentinels {
if err := r.rfChecker.CheckSentinelNumberInMemory(sip, rf); err != nil {
r.logger.Debug("Sentinel has more sentinel in memory than spected")
if err := r.rfHealer.RestoreSentinel(sip); err != nil {
return err
}
}
}
for _, sip := range sentinels {
if err := r.rfChecker.CheckSentinelSlavesNumberInMemory(sip, rf); err != nil {
r.logger.Debug("Sentinel has more slaves in memory than spected")
if err := r.rfHealer.RestoreSentinel(sip); err != nil {
return err
}
}
}
for _, sip := range sentinels {
if err := r.rfHealer.SetSentinelCustomConfig(sip, rf); err != nil {
return err
}
}
return nil
}