forked from argoproj/argo-rollouts
/
analysis.go
487 lines (458 loc) · 17.3 KB
/
analysis.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
package analysis
import (
"fmt"
"sync"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
analysisutil "github.com/argoproj/argo-rollouts/utils/analysis"
logutil "github.com/argoproj/argo-rollouts/utils/log"
)
const (
// DefaultMeasurementHistoryLimit is the default maximum number of measurements to retain per metric,
// before trimming the list.
DefaultMeasurementHistoryLimit = 10
// DefaultConsecutiveErrorLimit is the default number times a metric can error in sequence before
// erroring the entire metric.
DefaultConsecutiveErrorLimit int32 = 4
// DefaultErrorRetryInterval is the default interval to retry a measurement upon error, in the
// event an interval was not specified
DefaultErrorRetryInterval time.Duration = 10 * time.Second
)
// Event reasons for analysis events
const (
EventReasonStatusFailed = "Failed"
EventReasonStatusCompleted = "Complete"
)
// metricTask holds the metric which need to be measured during this reconciliation along with
// an in-progress measurement
type metricTask struct {
metric v1alpha1.Metric
incompleteMeasurement *v1alpha1.Measurement
}
func (c *AnalysisController) reconcileAnalysisRun(origRun *v1alpha1.AnalysisRun) *v1alpha1.AnalysisRun {
if origRun.Status.Phase.Completed() {
return origRun
}
log := logutil.WithAnalysisRun(origRun)
run := origRun.DeepCopy()
if run.Status.MetricResults == nil {
run.Status.MetricResults = make([]v1alpha1.MetricResult, 0)
err := analysisutil.ValidateMetrics(run.Spec.Metrics)
if err != nil {
message := fmt.Sprintf("analysis spec invalid: %v", err)
log.Warn(message)
run.Status.Phase = v1alpha1.AnalysisPhaseError
run.Status.Message = message
c.recorder.Eventf(run, corev1.EventTypeWarning, EventReasonStatusFailed, "analysis completed %s", run.Status.Phase)
return run
}
}
tasks := generateMetricTasks(run)
log.Infof("taking %d measurements", len(tasks))
c.runMeasurements(run, tasks)
newStatus := c.asssessRunStatus(run)
if newStatus != run.Status.Phase {
message := fmt.Sprintf("analysis transitioned from %s -> %s", run.Status.Phase, newStatus)
if newStatus.Completed() {
switch newStatus {
case v1alpha1.AnalysisPhaseError, v1alpha1.AnalysisPhaseFailed:
c.recorder.Eventf(run, corev1.EventTypeWarning, EventReasonStatusFailed, "analysis completed %s", newStatus)
default:
c.recorder.Eventf(run, corev1.EventTypeNormal, EventReasonStatusCompleted, "analysis completed %s", newStatus)
}
}
log.Info(message)
run.Status.Phase = newStatus
}
err := c.garbageCollectMeasurements(run, DefaultMeasurementHistoryLimit)
if err != nil {
// TODO(jessesuen): surface errors to controller so they can be retried
log.Warnf("Failed to garbage collect measurements: %v", err)
}
nextReconcileTime := calculateNextReconcileTime(run)
if nextReconcileTime != nil {
enqueueSeconds := nextReconcileTime.Sub(time.Now())
if enqueueSeconds < 0 {
enqueueSeconds = 0
}
log.Infof("enqueuing analysis after %v", enqueueSeconds)
c.enqueueAnalysisAfter(run, enqueueSeconds)
}
return run
}
// generateMetricTasks generates a list of metrics tasks needed to be measured as part of this
// sync, based on the last completion times that metric was measured (if ever). If the run is
// terminating (e.g. due to manual termination or failing metric), will not schedule further
// measurements other than to resume any in-flight measurements.
func generateMetricTasks(run *v1alpha1.AnalysisRun) []metricTask {
log := logutil.WithAnalysisRun(run)
var tasks []metricTask
terminating := analysisutil.IsTerminating(run)
for _, metric := range run.Spec.Metrics {
if analysisutil.MetricCompleted(run, metric.Name) {
continue
}
logCtx := log.WithField("metric", metric.Name)
lastMeasurement := analysisutil.LastMeasurement(run, metric.Name)
if lastMeasurement != nil && lastMeasurement.FinishedAt == nil {
now := metav1.Now()
if lastMeasurement.ResumeAt != nil && lastMeasurement.ResumeAt.After(now.Time) {
continue
}
// last measurement is still in-progress. need to complete it
logCtx.Infof("resuming in-progress measurement")
tasks = append(tasks, metricTask{
metric: metric,
incompleteMeasurement: lastMeasurement,
})
continue
}
if terminating {
logCtx.Infof("skipping measurement: run is terminating")
continue
}
if lastMeasurement == nil {
if metric.InitialDelay != "" {
if run.Status.StartedAt == nil {
continue
}
duration, err := metric.InitialDelay.Duration()
if err != nil {
logCtx.Warnf("failed to parse duration: %v", err)
continue
}
if run.Status.StartedAt.Add(duration).After(time.Now()) {
logCtx.Infof("waiting until start delay duration passes")
continue
}
}
// measurement never taken
tasks = append(tasks, metricTask{metric: metric})
logCtx.Infof("running initial measurement")
continue
}
metricResult := analysisutil.GetResult(run, metric.Name)
effectiveCount := metric.EffectiveCount()
if effectiveCount != nil && metricResult.Count >= *effectiveCount {
// we have reached desired count
continue
}
// if we get here, we know we need to take a measurement (eventually). check last measurement
// to decide if it should be taken now. metric.Interval can be null because we may be
// retrying a metric due to error.
interval := DefaultErrorRetryInterval
if metric.Interval != "" {
metricInterval, err := metric.Interval.Duration()
if err != nil {
logCtx.Warnf("failed to parse interval: %v", err)
continue
}
interval = metricInterval
}
if time.Now().After(lastMeasurement.FinishedAt.Add(interval)) {
tasks = append(tasks, metricTask{metric: metric})
logCtx.Infof("running overdue measurement")
continue
}
}
return tasks
}
// runMeasurements iterates a list of metric tasks, and runs, resumes, or terminates measurements
func (c *AnalysisController) runMeasurements(run *v1alpha1.AnalysisRun, tasks []metricTask) {
var wg sync.WaitGroup
// resultsLock should be held whenever we are accessing or setting status.metricResults since
// we are performing queries in parallel
var resultsLock sync.Mutex
terminating := analysisutil.IsTerminating(run)
for _, task := range tasks {
wg.Add(1)
go func(t metricTask) {
defer wg.Done()
log := logutil.WithAnalysisRun(run).WithField("metric", t.metric.Name)
resultsLock.Lock()
metricResult := analysisutil.GetResult(run, t.metric.Name)
resultsLock.Unlock()
if metricResult == nil {
metricResult = &v1alpha1.MetricResult{
Name: t.metric.Name,
Phase: v1alpha1.AnalysisPhaseRunning,
}
}
var newMeasurement v1alpha1.Measurement
provider, err := c.newProvider(*log, t.metric)
if err != nil {
if t.incompleteMeasurement != nil {
newMeasurement = *t.incompleteMeasurement
} else {
startedAt := metav1.Now()
newMeasurement.StartedAt = &startedAt
}
newMeasurement.Phase = v1alpha1.AnalysisPhaseError
newMeasurement.Message = err.Error()
} else {
if t.incompleteMeasurement == nil {
newMeasurement = provider.Run(run, t.metric)
} else {
// metric is incomplete. either terminate or resume it
if terminating {
log.Infof("terminating in-progress measurement")
newMeasurement = provider.Terminate(run, t.metric, *t.incompleteMeasurement)
if newMeasurement.Phase == v1alpha1.AnalysisPhaseSuccessful {
newMeasurement.Message = "metric terminated"
}
} else {
newMeasurement = provider.Resume(run, t.metric, *t.incompleteMeasurement)
}
}
}
if newMeasurement.Phase.Completed() {
log.Infof("measurement completed %s", newMeasurement.Phase)
if newMeasurement.FinishedAt == nil {
finishedAt := metav1.Now()
newMeasurement.FinishedAt = &finishedAt
}
switch newMeasurement.Phase {
case v1alpha1.AnalysisPhaseSuccessful:
metricResult.Successful++
metricResult.Count++
metricResult.ConsecutiveError = 0
case v1alpha1.AnalysisPhaseFailed:
metricResult.Failed++
metricResult.Count++
metricResult.ConsecutiveError = 0
case v1alpha1.AnalysisPhaseInconclusive:
metricResult.Inconclusive++
metricResult.Count++
metricResult.ConsecutiveError = 0
case v1alpha1.AnalysisPhaseError:
metricResult.Error++
metricResult.ConsecutiveError++
log.Warnf("measurement had error: %s", newMeasurement.Message)
}
}
if t.incompleteMeasurement == nil {
metricResult.Measurements = append(metricResult.Measurements, newMeasurement)
} else {
metricResult.Measurements[len(metricResult.Measurements)-1] = newMeasurement
}
resultsLock.Lock()
analysisutil.SetResult(run, *metricResult)
resultsLock.Unlock()
}(task)
}
wg.Wait()
}
// asssessRunStatus assesses the overall status of this AnalysisRun
// If any metric is not yet completed, the AnalysisRun is still considered Running
// Once all metrics are complete, the worst status is used as the overall AnalysisRun status
func (c *AnalysisController) asssessRunStatus(run *v1alpha1.AnalysisRun) v1alpha1.AnalysisPhase {
var worstStatus v1alpha1.AnalysisPhase
terminating := analysisutil.IsTerminating(run)
everythingCompleted := true
if run.Status.StartedAt == nil {
now := metav1.Now()
run.Status.StartedAt = &now
}
// Iterate all metrics and update MetricResult.Phase fields based on lastest measurement(s)
for _, metric := range run.Spec.Metrics {
if result := analysisutil.GetResult(run, metric.Name); result != nil {
log := logutil.WithAnalysisRun(run).WithField("metric", metric.Name)
metricStatus := assessMetricStatus(metric, *result, terminating)
if result.Phase != metricStatus {
log.Infof("metric transitioned from %s -> %s", result.Phase, metricStatus)
if metricStatus.Completed() {
switch metricStatus {
case v1alpha1.AnalysisPhaseError, v1alpha1.AnalysisPhaseFailed:
c.recorder.Eventf(run, corev1.EventTypeWarning, EventReasonStatusFailed, "metric '%s' completed %s", metric.Name, metricStatus)
default:
c.recorder.Eventf(run, corev1.EventTypeNormal, EventReasonStatusCompleted, "metric '%s' completed %s", metric.Name, metricStatus)
}
}
if lastMeasurement := analysisutil.LastMeasurement(run, metric.Name); lastMeasurement != nil {
result.Message = lastMeasurement.Message
}
result.Phase = metricStatus
analysisutil.SetResult(run, *result)
}
if !metricStatus.Completed() {
// if any metric is in-progress, then entire analysis run will be considered running
everythingCompleted = false
} else {
// otherwise, remember the worst status of all completed metric results
if worstStatus == "" {
worstStatus = metricStatus
} else {
if analysisutil.IsWorse(worstStatus, metricStatus) {
worstStatus = metricStatus
}
}
}
}
}
if !everythingCompleted || worstStatus == "" {
return v1alpha1.AnalysisPhaseRunning
}
return worstStatus
}
// assessMetricStatus assesses the status of a single metric based on:
// * current/latest measurement status
// * parameters given by the metric (failureLimit, count, etc...)
// * whether or not we are terminating (e.g. due to failing run, or termination request)
func assessMetricStatus(metric v1alpha1.Metric, result v1alpha1.MetricResult, terminating bool) v1alpha1.AnalysisPhase {
if result.Phase.Completed() {
return result.Phase
}
log := log.WithField("metric", metric.Name)
if len(result.Measurements) == 0 {
if terminating {
// we have yet to take a single measurement, but have already been instructed to stop
log.Infof("metric assessed %s: run terminated", v1alpha1.AnalysisPhaseSuccessful)
return v1alpha1.AnalysisPhaseSuccessful
}
return v1alpha1.AnalysisPhasePending
}
lastMeasurement := result.Measurements[len(result.Measurements)-1]
if !lastMeasurement.Phase.Completed() {
// we still have a in-flight measurement
return v1alpha1.AnalysisPhaseRunning
}
if result.Failed > metric.FailureLimit {
log.Infof("metric assessed %s: failed (%d) > failureLimit (%d)", v1alpha1.AnalysisPhaseFailed, result.Failed, metric.FailureLimit)
return v1alpha1.AnalysisPhaseFailed
}
if result.Inconclusive > metric.InconclusiveLimit {
log.Infof("metric assessed %s: inconclusive (%d) > inconclusiveLimit (%d)", v1alpha1.AnalysisPhaseInconclusive, result.Inconclusive, metric.InconclusiveLimit)
return v1alpha1.AnalysisPhaseInconclusive
}
consecutiveErrorLimit := DefaultConsecutiveErrorLimit
if metric.ConsecutiveErrorLimit != nil {
consecutiveErrorLimit = *metric.ConsecutiveErrorLimit
}
if result.ConsecutiveError > consecutiveErrorLimit {
log.Infof("metric assessed %s: consecutiveErrors (%d) > consecutiveErrorLimit (%d)", v1alpha1.AnalysisPhaseError, result.ConsecutiveError, consecutiveErrorLimit)
return v1alpha1.AnalysisPhaseError
}
// If a count was specified, and we reached that count, then metric is considered Successful.
// The Error, Failed, Inconclusive counters are ignored because those checks have already been
// taken into consideration above, and we do not want to fail if failures < failureLimit.
effectiveCount := metric.EffectiveCount()
if effectiveCount != nil && result.Count >= *effectiveCount {
log.Infof("metric assessed %s: count (%d) reached", v1alpha1.AnalysisPhaseSuccessful, *effectiveCount)
return v1alpha1.AnalysisPhaseSuccessful
}
// if we get here, this metric runs indefinitely
if terminating {
log.Infof("metric assessed %s: run terminated", v1alpha1.AnalysisPhaseSuccessful)
return v1alpha1.AnalysisPhaseSuccessful
}
return v1alpha1.AnalysisPhaseRunning
}
// calculateNextReconcileTime calculates the next time that this AnalysisRun should be reconciled,
// based on the earliest time of all metrics intervals, counts, and their finishedAt timestamps
func calculateNextReconcileTime(run *v1alpha1.AnalysisRun) *time.Time {
var reconcileTime *time.Time
for _, metric := range run.Spec.Metrics {
if analysisutil.MetricCompleted(run, metric.Name) {
// NOTE: this also covers the case where metric.Count is reached
continue
}
logCtx := logutil.WithAnalysisRun(run).WithField("metric", metric.Name)
lastMeasurement := analysisutil.LastMeasurement(run, metric.Name)
if lastMeasurement == nil {
if metric.InitialDelay != "" {
startTime := metav1.Now()
if run.Status.StartedAt != nil {
startTime = *run.Status.StartedAt
}
duration, err := metric.InitialDelay.Duration()
if err != nil {
logCtx.Warnf("failed to parse interval: %v", err)
continue
}
endInitialDelay := startTime.Add(duration)
if reconcileTime == nil || reconcileTime.After(endInitialDelay) {
reconcileTime = &endInitialDelay
}
continue
}
// no measurement was started . we should never get here
logCtx.Warnf("metric never started. not factored into enqueue time")
continue
}
if lastMeasurement.FinishedAt == nil {
// unfinished in-flight measurement.
if lastMeasurement.ResumeAt != nil {
if reconcileTime == nil || reconcileTime.After(lastMeasurement.ResumeAt.Time) {
reconcileTime = &lastMeasurement.ResumeAt.Time
}
}
continue
}
metricResult := analysisutil.GetResult(run, metric.Name)
effectiveCount := metric.EffectiveCount()
if effectiveCount != nil && metricResult.Count >= *effectiveCount {
// we have reached desired count
continue
}
var interval time.Duration
if metric.Interval != "" {
metricInterval, err := metric.Interval.Duration()
if err != nil {
logCtx.Warnf("failed to parse interval: %v", err)
continue
}
interval = metricInterval
} else if lastMeasurement.Phase == v1alpha1.AnalysisPhaseError {
interval = DefaultErrorRetryInterval
} else {
// if we get here, an interval was not set (meaning reoccurrence was not desired), and
// there was no error (meaning we don't need to retry). no need to requeue this metric.
// NOTE: we shouldn't ever get here since it means we are not doing proper bookkeeping
// of count.
logCtx.Warnf("skipping requeue. no interval or error (count: %d, effectiveCount: %d)", metricResult.Count, metric.EffectiveCount())
continue
}
// Take the earliest time of all metrics
metricReconcileTime := lastMeasurement.FinishedAt.Add(interval)
if reconcileTime == nil || reconcileTime.After(metricReconcileTime) {
reconcileTime = &metricReconcileTime
}
}
return reconcileTime
}
// garbageCollectMeasurements trims the measurement history to the specified limit and GCs old measurements
func (c *AnalysisController) garbageCollectMeasurements(run *v1alpha1.AnalysisRun, limit int) error {
var errors []error
metricsByName := make(map[string]v1alpha1.Metric)
for _, metric := range run.Spec.Metrics {
metricsByName[metric.Name] = metric
}
for i, result := range run.Status.MetricResults {
length := len(result.Measurements)
if length > limit {
metric, ok := metricsByName[result.Name]
if !ok {
continue
}
log := logutil.WithAnalysisRun(run).WithField("metric", metric.Name)
provider, err := c.newProvider(*log, metric)
if err != nil {
errors = append(errors, err)
continue
}
err = provider.GarbageCollect(run, metric, limit)
if err != nil {
return err
}
result.Measurements = result.Measurements[length-limit : length]
}
run.Status.MetricResults[i] = result
}
if len(errors) > 0 {
return errors[0]
}
return nil
}