/
flow.go
380 lines (324 loc) · 9.01 KB
/
flow.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
// Copyright 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 flow provides utilities to construct a directed acyclic computational graph
// that is then executed and monitored with maximum parallelism.
package flow
import (
"context"
"fmt"
"time"
"github.com/go-logr/logr"
"github.com/hashicorp/go-multierror"
logf "sigs.k8s.io/controller-runtime/pkg/log"
errorsutils "github.com/gardener/gardener/pkg/utils/errors"
)
const (
logKeyFlow = "flow"
logKeyTask = "task"
)
// ErrorCleaner is called when a task which errored during the previous reconciliation phase completes with success
type ErrorCleaner func(context.Context, string)
type nodes map[TaskID]*node
func (ns nodes) rootIDs() TaskIDs {
roots := NewTaskIDs()
for taskID, node := range ns {
if node.required == 0 {
roots.Insert(taskID)
}
}
return roots
}
func (ns nodes) getOrCreate(id TaskID) *node {
n, ok := ns[id]
if !ok {
n = &node{}
ns[id] = n
}
return n
}
// Flow is a validated executable Graph.
type Flow struct {
name string
nodes nodes
}
// Name retrieves the name of a flow.
func (f *Flow) Name() string {
return f.name
}
// Len retrieves the amount of tasks in a Flow.
func (f *Flow) Len() int {
return len(f.nodes)
}
// node is a compiled Task that contains the triggered Tasks, the
// number of triggers the node itself requires and its payload function.
type node struct {
targetIDs TaskIDs
required int
fn TaskFn
}
func (n *node) String() string {
return fmt.Sprintf("node{targets=%s, required=%d}", n.targetIDs.List(), n.required)
}
// addTargets adds the given TaskIDs as targets to the node.
func (n *node) addTargets(taskIDs ...TaskID) {
if n.targetIDs == nil {
n.targetIDs = NewTaskIDs(TaskIDSlice(taskIDs))
return
}
n.targetIDs.Insert(TaskIDSlice(taskIDs))
}
// Opts are options for a Flow execution. If they are not set, they
// are left blank and don't affect the Flow.
type Opts struct {
// Log is used to log any output during flow execution.
Log logr.Logger
// ProgressReporter is used to report the progress during flow execution.
ProgressReporter ProgressReporter
// ErrorCleaner is used to clean up a previously failed task.
ErrorCleaner func(ctx context.Context, taskID string)
// ErrorContext is used to store any error related context.
ErrorContext *errorsutils.ErrorContext
}
// Run starts an execution of a Flow.
// It blocks until the Flow has finished and returns the error, if any.
func (f *Flow) Run(ctx context.Context, opts Opts) error {
return newExecution(f, opts).run(ctx)
}
type nodeResult struct {
TaskID TaskID
Error error
}
// Stats are the statistics of a Flow execution.
type Stats struct {
FlowName string
All TaskIDs
Succeeded TaskIDs
Failed TaskIDs
Running TaskIDs
Pending TaskIDs
}
// ProgressPercent retrieves the progress of a Flow execution in percent.
func (s *Stats) ProgressPercent() int32 {
progress := (100 * s.Succeeded.Len()) / s.All.Len()
return int32(progress)
}
// Copy deeply copies a Stats object.
func (s *Stats) Copy() *Stats {
return &Stats{
s.FlowName,
s.All.Copy(),
s.Succeeded.Copy(),
s.Failed.Copy(),
s.Running.Copy(),
s.Pending.Copy(),
}
}
// InitialStats creates a new Stats object with the given set of initial TaskIDs.
// The initial TaskIDs are added to all TaskIDs as well as to the pending ones.
func InitialStats(flowName string, all TaskIDs) *Stats {
return &Stats{
flowName,
all,
NewTaskIDs(),
NewTaskIDs(),
NewTaskIDs(),
all.Copy(),
}
}
func newExecution(flow *Flow, opts Opts) *execution {
all := NewTaskIDs()
for name := range flow.nodes {
all.Insert(name)
}
log := logf.Log.WithName("flow").WithValues(logKeyFlow, flow.name)
if opts.Log.GetSink() != nil {
log = opts.Log.WithValues(logKeyFlow, flow.name)
}
return &execution{
flow,
InitialStats(flow.name, all),
nil,
log,
opts.ProgressReporter,
opts.ErrorCleaner,
opts.ErrorContext,
make(chan *nodeResult),
make(map[TaskID]int),
}
}
type execution struct {
flow *Flow
stats *Stats
taskErrors []error
log logr.Logger
progressReporter ProgressReporter
errorCleaner ErrorCleaner
errorContext *errorsutils.ErrorContext
done chan *nodeResult
triggerCounts map[TaskID]int
}
func (e *execution) runNode(ctx context.Context, id TaskID) {
if e.errorContext != nil {
e.errorContext.AddErrorID(string(id))
}
e.stats.Pending.Delete(id)
e.stats.Running.Insert(id)
go func() {
start := time.Now().UTC()
e.log.WithValues(logKeyTask, id).V(1).Info("Started")
err := e.flow.nodes[id].fn(ctx)
end := time.Now().UTC()
e.log.WithValues(logKeyTask, id).V(1).Info("Finished", "duration", end.Sub(start))
if err != nil {
e.log.WithValues(logKeyTask, id).Error(err, "Error")
err = fmt.Errorf("task %q failed: %w", id, err)
} else {
e.log.WithValues(logKeyTask, id).Info("Succeeded")
}
e.done <- &nodeResult{TaskID: id, Error: err}
}()
}
func (e *execution) updateSuccess(id TaskID) {
e.stats.Running.Delete(id)
e.stats.Succeeded.Insert(id)
}
func (e *execution) updateFailure(id TaskID) {
e.stats.Running.Delete(id)
e.stats.Failed.Insert(id)
}
func (e *execution) processTriggers(ctx context.Context, id TaskID) {
node := e.flow.nodes[id]
for target := range node.targetIDs {
e.triggerCounts[target]++
if e.triggerCounts[target] == e.flow.nodes[target].required {
e.runNode(ctx, target)
}
}
}
func (e *execution) cleanErrors(ctx context.Context, taskID TaskID) {
if e.errorCleaner != nil {
e.errorCleaner(ctx, string(taskID))
}
}
func (e *execution) reportProgress(ctx context.Context) {
if e.progressReporter != nil {
e.progressReporter.Report(ctx, e.stats.Copy())
}
}
func (e *execution) run(ctx context.Context) error {
defer close(e.done)
if e.progressReporter != nil {
if err := e.progressReporter.Start(ctx); err != nil {
return err
}
defer e.progressReporter.Stop()
}
e.log.Info("Starting")
e.reportProgress(ctx)
var (
cancelErr error
roots = e.flow.nodes.rootIDs()
)
for name := range roots {
if cancelErr = ctx.Err(); cancelErr == nil {
e.runNode(ctx, name)
}
}
e.reportProgress(ctx)
for e.stats.Running.Len() > 0 {
result := <-e.done
if result.Error != nil {
e.taskErrors = append(e.taskErrors, errorsutils.WithID(string(result.TaskID), result.Error))
e.updateFailure(result.TaskID)
} else {
e.updateSuccess(result.TaskID)
if e.errorContext != nil && e.errorContext.HasLastErrorWithID(string(result.TaskID)) {
e.cleanErrors(ctx, result.TaskID)
}
if cancelErr = ctx.Err(); cancelErr == nil {
e.processTriggers(ctx, result.TaskID)
}
}
e.reportProgress(ctx)
}
e.log.Info("Finished")
return e.result(cancelErr)
}
func (e *execution) result(cancelErr error) error {
if cancelErr != nil {
return &flowCanceled{
name: e.flow.name,
taskErrors: e.taskErrors,
cause: cancelErr,
}
}
if len(e.taskErrors) > 0 {
return &flowFailed{
name: e.flow.name,
taskErrors: e.taskErrors,
}
}
return nil
}
type flowCanceled struct {
name string
taskErrors []error
cause error
}
type flowFailed struct {
name string
taskErrors []error
}
func (f *flowCanceled) Error() string {
if len(f.taskErrors) == 0 {
return fmt.Sprintf("flow %q was canceled: %v", f.name, f.cause)
}
return fmt.Sprintf("flow %q was canceled: %v. Encountered task errors: %v",
f.name, f.cause, f.taskErrors)
}
func (f *flowCanceled) Unwrap() error {
return f.cause
}
func (f *flowFailed) Error() string {
return fmt.Sprintf("flow %q encountered task errors: %v", f.name, f.taskErrors)
}
func (f *flowFailed) Unwrap() error {
return &multierror.Error{Errors: f.taskErrors}
}
// Errors reports all wrapped Task errors of the given Flow error.
func Errors(err error) *multierror.Error {
switch e := err.(type) {
case *flowCanceled:
return &multierror.Error{Errors: e.taskErrors}
case *flowFailed:
return &multierror.Error{Errors: e.taskErrors}
}
return nil
}
// Causes reports the causes of all Task errors of the given Flow error.
func Causes(err error) *multierror.Error {
var (
errs = Errors(err).Errors
causes = make([]error, 0, len(errs))
)
for _, err := range errs {
causes = append(causes, errorsutils.Unwrap(err))
}
return &multierror.Error{Errors: causes}
}
// WasCanceled determines whether the given flow error was caused by cancellation.
func WasCanceled(err error) bool {
_, ok := err.(*flowCanceled)
return ok
}