/
state_machine.go
373 lines (312 loc) · 11.2 KB
/
state_machine.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
// Copyright 2020 Chaos Mesh Authors.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package twophase
import (
"context"
"reflect"
"time"
"github.com/pkg/errors"
k8sError "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
)
const iterMax = 1e4
type chaosStateMachine struct {
Chaos v1alpha1.InnerSchedulerObject
Req ctrl.Request
*Reconciler
}
func unexpected(ctx context.Context, m *chaosStateMachine, targetPhase v1alpha1.ExperimentPhase, now time.Time) (bool, error) {
currentPhase := m.Chaos.GetStatus().Experiment.Phase
return true, errors.Errorf("turn from %s into %s is unexpected", currentPhase, targetPhase)
}
func noop(ctx context.Context, m *chaosStateMachine, targetPhase v1alpha1.ExperimentPhase, now time.Time) (bool, error) {
updated := false
currentPhase := m.Chaos.GetStatus().Experiment.Phase
if currentPhase != targetPhase {
m.Chaos.GetStatus().Experiment.Phase = targetPhase
updated = true
}
return updated, nil
}
func apply(ctx context.Context, m *chaosStateMachine, targetPhase v1alpha1.ExperimentPhase, startTime time.Time) (bool, error) {
duration, err := m.Chaos.GetDuration()
if err != nil {
m.Log.Error(err, "failed to get chaos duration")
return false, err
}
if duration == nil {
zero := time.Duration(0)
duration = &zero
}
currentPhase := m.Chaos.GetStatus().Experiment.Phase
status := m.Chaos.GetStatus()
m.Log.Info("applying", "current phase", currentPhase, "target phase", targetPhase)
err = m.Apply(ctx, m.Req, m.Chaos)
if err != nil {
m.Log.Error(err, "fail to apply")
status.Experiment.Phase = v1alpha1.ExperimentPhaseFailed
status.FailedMessage = err.Error()
return true, err
}
// reset failed message
status.FailedMessage = emptyString
status.Experiment.Phase = targetPhase
nextStart, nextRecover, err := m.IterateNextTime(startTime, *duration)
if err != nil {
m.Log.Error(err, "failed to get the next start time and recover time")
return true, err
}
m.Chaos.SetNextStart(*nextStart)
m.Chaos.SetNextRecover(*nextRecover)
status.Experiment.StartTime = &metav1.Time{Time: startTime}
status.Experiment.EndTime = nil
status.Experiment.Duration = duration.String()
return true, nil
}
func recover(ctx context.Context, m *chaosStateMachine, targetPhase v1alpha1.ExperimentPhase, now time.Time) (bool, error) {
duration, err := m.Chaos.GetDuration()
if err != nil {
m.Log.Error(err, "failed to get chaos duration")
return false, err
}
if duration == nil {
zero := time.Duration(0)
duration = &zero
}
currentPhase := m.Chaos.GetStatus().Experiment.Phase
status := m.Chaos.GetStatus()
m.Log.Info("recovering", "current phase", currentPhase, "target phase", targetPhase)
if err := m.Recover(ctx, m.Req, m.Chaos); err != nil {
status.FailedMessage = err.Error()
m.Log.Error(err, "fail to recover")
return true, err
}
status.Experiment.Phase = targetPhase
status.Experiment.EndTime = &metav1.Time{
Time: now,
}
if status.Experiment.StartTime != nil {
status.Experiment.Duration = now.Sub(status.Experiment.StartTime.Time).String()
}
// If this recover action is not called by pause action, reset recover time
if !now.Before(m.Chaos.GetNextRecover()) {
m.Chaos.SetNextRecover(m.Chaos.GetNextStart().Add(*duration))
}
return true, nil
}
func resume(ctx context.Context, m *chaosStateMachine, _ v1alpha1.ExperimentPhase, now time.Time) (bool, error) {
startTime := now
duration, err := m.Chaos.GetDuration()
if err != nil {
m.Log.Error(err, "failed to get chaos duration")
return false, err
}
if duration == nil {
zero := time.Duration(0)
duration = &zero
}
status := m.Chaos.GetStatus()
nextStart := m.Chaos.GetNextStart()
nextRecover := m.Chaos.GetNextRecover()
var lastStart time.Time
if status.Experiment.StartTime == nil {
// in this condition, the experiment has never executed
nextStart = now
lastStart = now
} else {
lastStart = status.Experiment.StartTime.Time
}
defer func() {
m.Chaos.SetNextStart(nextStart)
m.Chaos.SetNextRecover(nextRecover)
}()
counter := 0
for {
if nextRecover.After(now) && nextRecover.Before(nextStart) {
startTime = lastStart
return apply(ctx, m, v1alpha1.ExperimentPhaseRunning, startTime)
}
if nextStart.After(now) {
return noop(ctx, m, v1alpha1.ExperimentPhaseWaiting, now)
}
lastStart = nextStart
start, recover, err := m.IterateNextTime(nextStart, *duration)
if err != nil {
m.Log.Error(err, "failed to get the next start time and recover time")
return false, err
}
nextStart = *start
nextRecover = *recover
counter++
if counter > iterMax {
// If counter > iterMax, it means that chaos has been suspended for a long time,
// then directly restart chaos and set startTime to now.
startTime = now
start, recover, err = m.IterateNextTime(startTime, *duration)
if err != nil {
m.Log.Error(err, "failed to get the next start time and recover time")
return false, err
}
nextStart = *start
nextRecover = *recover
return apply(ctx, m, v1alpha1.ExperimentPhaseRunning, startTime)
}
}
}
// This method changes the phase of an object and do some side effects
// There are 6 different phases, so there could be 6 * 6 = 36 branches
func (m *chaosStateMachine) run(ctx context.Context, targetPhase v1alpha1.ExperimentPhase, now time.Time) (bool, error) {
currentPhase := m.Chaos.GetStatus().Experiment.Phase
m.Log.Info("change phase", "current phase", currentPhase, "target phase", targetPhase)
if phaseTransitionMap[currentPhase] == nil {
err := errors.Errorf("unexpected current phase '%s'", currentPhase)
return false, err
}
if phaseTransitionMap[currentPhase][targetPhase] == nil {
err := errors.Errorf("unexpected target phase '%s'", targetPhase)
return false, err
}
return phaseTransitionMap[currentPhase][targetPhase](ctx, m, targetPhase, now)
}
func (m *chaosStateMachine) Into(ctx context.Context, targetPhase v1alpha1.ExperimentPhase, now time.Time) error {
originFinalizers := m.Chaos.DeepCopyObject().(v1alpha1.InnerSchedulerObject).GetMeta().GetFinalizers()
updated, err := m.run(ctx, targetPhase, now)
if err != nil {
m.Log.Error(err, "error while executing state machine")
}
if updated {
updateError := m.Update(ctx, m.Chaos)
if updateError == nil {
return err
}
if !k8sError.IsConflict(updateError) {
return updateError
}
m.Log.Info("fail to update, and will retry on conflict")
// avoid panic
if m.Chaos.GetChaos() == nil || m.Chaos.GetStatus() == nil {
return updateError
}
namespacedName := types.NamespacedName{
Namespace: m.Chaos.GetChaos().Namespace,
Name: m.Chaos.GetChaos().Name,
}
updateError = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
// Fetch the resource
_chaos := m.Object()
if err := m.Client.Get(ctx, namespacedName, _chaos); err != nil {
m.Log.Error(err, "unable to get chaos")
return err
}
chaos := _chaos.(v1alpha1.InnerSchedulerObject)
// Make updates to the resource
status := chaos.GetStatus()
status.FailedMessage = m.Chaos.GetStatus().FailedMessage
status.Scheduler = m.Chaos.GetStatus().Scheduler
status.Experiment = m.Chaos.GetStatus().Experiment
newFinalizers := m.Chaos.GetMeta().GetFinalizers()
if !reflect.DeepEqual(originFinalizers, newFinalizers) {
chaos.GetMeta().SetFinalizers(newFinalizers)
}
// Try to update
return m.Update(ctx, chaos)
})
if updateError != nil {
return updateError
}
}
return err
}
func (m *chaosStateMachine) IterateNextTime(startTime time.Time, duration time.Duration) (*time.Time, *time.Time, error) {
scheduler := m.Chaos.GetScheduler()
if scheduler == nil {
return nil, nil, errors.Errorf("misdefined scheduler")
}
m.Log.Info("iterate nextStart and nextRecover", "startTime", startTime, "duration", duration, "scheduler", scheduler)
nextStart, err := nextTime(*scheduler, startTime)
if err != nil {
m.Log.Error(err, "failed to get the next start time")
return nil, nil, err
}
nextRecover := startTime.Add(duration)
counter := 0
// if the duration is too long, `nextRecover` could be after `nextStart`
// we can jump over a start to make sure `nextRecover` is before `nextStart`
for nextRecover.After(*nextStart) {
nextStart, err = nextTime(*scheduler, *nextStart)
if err != nil {
m.Log.Error(err, "failed to get the next start time")
return nil, nil, err
}
counter++
if counter > iterMax {
err = errors.Errorf("the number of iterations exceeded with nextRecover(%s) nextStart(%s)", nextRecover, nextStart)
return nil, nil, err
}
}
return nextStart, &nextRecover, nil
}
var phaseTransitionMap = map[v1alpha1.ExperimentPhase]map[v1alpha1.ExperimentPhase]func(ctx context.Context, m *chaosStateMachine, targetPhase v1alpha1.ExperimentPhase, now time.Time) (bool, error){
v1alpha1.ExperimentPhaseUninitialized: {
v1alpha1.ExperimentPhaseUninitialized: noop,
v1alpha1.ExperimentPhaseRunning: apply,
v1alpha1.ExperimentPhaseWaiting: noop,
v1alpha1.ExperimentPhasePaused: noop,
v1alpha1.ExperimentPhaseFailed: unexpected,
v1alpha1.ExperimentPhaseFinished: noop,
},
v1alpha1.ExperimentPhaseRunning: {
v1alpha1.ExperimentPhaseUninitialized: unexpected,
v1alpha1.ExperimentPhaseRunning: noop,
v1alpha1.ExperimentPhaseWaiting: recover,
v1alpha1.ExperimentPhasePaused: recover,
v1alpha1.ExperimentPhaseFailed: unexpected,
v1alpha1.ExperimentPhaseFinished: recover,
},
v1alpha1.ExperimentPhaseWaiting: {
v1alpha1.ExperimentPhaseUninitialized: unexpected,
v1alpha1.ExperimentPhaseRunning: apply,
v1alpha1.ExperimentPhaseWaiting: noop,
v1alpha1.ExperimentPhasePaused: noop,
v1alpha1.ExperimentPhaseFailed: unexpected,
v1alpha1.ExperimentPhaseFinished: noop,
},
v1alpha1.ExperimentPhasePaused: {
v1alpha1.ExperimentPhaseUninitialized: unexpected,
v1alpha1.ExperimentPhaseRunning: resume,
v1alpha1.ExperimentPhaseWaiting: resume,
v1alpha1.ExperimentPhasePaused: noop,
v1alpha1.ExperimentPhaseFailed: unexpected,
v1alpha1.ExperimentPhaseFinished: noop,
},
v1alpha1.ExperimentPhaseFailed: {
v1alpha1.ExperimentPhaseUninitialized: unexpected,
v1alpha1.ExperimentPhaseRunning: apply,
v1alpha1.ExperimentPhaseWaiting: noop,
v1alpha1.ExperimentPhasePaused: noop,
v1alpha1.ExperimentPhaseFailed: noop,
v1alpha1.ExperimentPhaseFinished: recover,
},
v1alpha1.ExperimentPhaseFinished: {
v1alpha1.ExperimentPhaseUninitialized: unexpected,
v1alpha1.ExperimentPhaseRunning: unexpected,
v1alpha1.ExperimentPhaseWaiting: unexpected,
v1alpha1.ExperimentPhasePaused: unexpected,
v1alpha1.ExperimentPhaseFailed: unexpected,
v1alpha1.ExperimentPhaseFinished: noop,
},
}