forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag.go
512 lines (467 loc) · 16.1 KB
/
dag.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
505
506
507
508
509
510
511
512
package controller
import (
"encoding/json"
"fmt"
"strings"
"github.com/argoproj/argo/errors"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo/workflow/common"
"github.com/valyala/fasttemplate"
)
// dagContext holds context information about this context's DAG
type dagContext struct {
// boundaryName is the node name of the boundary node to this DAG.
// This is used to incorporate into each of the task's node names.
boundaryName string
boundaryID string
// tasks are all the tasks in the template
tasks []wfv1.DAGTask
// visited keeps track of tasks we have already visited during an invocation of executeDAG
// in order to avoid duplicating work
visited map[string]bool
// tmpl is the template spec. it is needed to resolve hard-wired artifacts
tmpl *wfv1.Template
// wf is stored to formulate nodeIDs
wf *wfv1.Workflow
}
func (d *dagContext) getTask(taskName string) *wfv1.DAGTask {
for _, task := range d.tasks {
if task.Name == taskName {
return &task
}
}
panic("target " + taskName + " does not exist")
}
// taskNodeName formulates the nodeName for a dag task
func (d *dagContext) taskNodeName(taskName string) string {
return fmt.Sprintf("%s.%s", d.boundaryName, taskName)
}
// taskNodeID formulates the node ID for a dag task
func (d *dagContext) taskNodeID(taskName string) string {
nodeName := d.taskNodeName(taskName)
return d.wf.NodeID(nodeName)
}
func (d *dagContext) getTaskNode(taskName string) *wfv1.NodeStatus {
nodeID := d.taskNodeID(taskName)
node, ok := d.wf.Status.Nodes[nodeID]
if !ok {
return nil
}
return &node
}
// assessDAGPhase assesses the overall DAG status
func (d *dagContext) assessDAGPhase(targetTasks []string, nodes map[string]wfv1.NodeStatus) wfv1.NodePhase {
// First check all our nodes to see if anything is still running. If so, then the DAG is
// considered still running (even if there are failures). Remember any failures and if retry
// nodes have been exhausted.
var unsuccessfulPhase wfv1.NodePhase
retriesExhausted := true
for _, node := range nodes {
if node.BoundaryID != d.boundaryID {
continue
}
if !node.Completed() {
return wfv1.NodeRunning
}
if node.Successful() {
continue
}
// failed retry attempts should not factor into the overall unsuccessful phase of the dag
// because the subsequent attempt may have succeeded
if unsuccessfulPhase == "" && !isRetryAttempt(node, nodes) {
unsuccessfulPhase = node.Phase
}
if node.Type == wfv1.NodeTypeRetry && hasMoreRetries(&node, d.wf) {
retriesExhausted = false
}
}
if unsuccessfulPhase != "" {
// If failFast set to false, we should return Running to continue this workflow for other DAG branch
if d.wf.Spec.FailFast != nil && !*d.wf.Spec.FailFast {
tmpOverAllFinished := true
// If all the nodes have finished, we should mark the failed node to finish overall workflow
// So we should check all the targetTasks have finished
for _, tmpDepName := range targetTasks {
tmpDepNode := d.getTaskNode(tmpDepName)
if tmpDepNode == nil {
tmpOverAllFinished = false
break
}
if tmpDepNode.Type == wfv1.NodeTypeRetry && hasMoreRetries(tmpDepNode, d.wf) {
tmpOverAllFinished = false
break
}
}
if !tmpOverAllFinished {
return wfv1.NodeRunning
}
}
// if we were unsuccessful, we can return *only* if all retry nodes have ben exhausted.
if retriesExhausted {
return unsuccessfulPhase
}
}
// There are no currently running tasks. Now check if our dependencies were met
for _, depName := range targetTasks {
depNode := d.getTaskNode(depName)
if depNode == nil {
return wfv1.NodeRunning
}
if !depNode.Successful() {
// we should theoretically never get here since it would have been caught in first loop
return depNode.Phase
}
}
// If we get here, all our dependencies were completed and successful
return wfv1.NodeSucceeded
}
// isRetryAttempt detects if a node is part of a retry
func isRetryAttempt(node wfv1.NodeStatus, nodes map[string]wfv1.NodeStatus) bool {
for _, potentialParent := range nodes {
if potentialParent.Type == wfv1.NodeTypeRetry {
for _, child := range potentialParent.Children {
if child == node.ID {
return true
}
}
}
}
return false
}
func hasMoreRetries(node *wfv1.NodeStatus, wf *wfv1.Workflow) bool {
if node.Phase == wfv1.NodeSucceeded {
return false
}
if len(node.Children) == 0 {
return true
}
// pick the first child to determine it's template type
childNode := wf.Status.Nodes[node.Children[0]]
tmpl := wf.GetTemplate(childNode.TemplateName)
if tmpl.RetryStrategy.Limit != nil && int32(len(node.Children)) > *tmpl.RetryStrategy.Limit {
return false
}
return true
}
func (woc *wfOperationCtx) executeDAG(nodeName string, tmpl *wfv1.Template, boundaryID string) *wfv1.NodeStatus {
node := woc.getNodeByName(nodeName)
if node != nil && node.Completed() {
return node
}
defer func() {
if node != nil && woc.wf.Status.Nodes[node.ID].Completed() {
_ = woc.killDaemonedChildren(node.ID)
}
}()
dagCtx := &dagContext{
boundaryName: nodeName,
boundaryID: woc.wf.NodeID(nodeName),
tasks: tmpl.DAG.Tasks,
visited: make(map[string]bool),
tmpl: tmpl,
wf: woc.wf,
}
// Identify our target tasks. If user did not specify any, then we choose all tasks which have
// no dependants.
var targetTasks []string
if tmpl.DAG.Target == "" {
targetTasks = findLeafTaskNames(tmpl.DAG.Tasks)
} else {
targetTasks = strings.Split(tmpl.DAG.Target, " ")
}
if node == nil {
node = woc.initializeNode(nodeName, wfv1.NodeTypeDAG, tmpl.Name, boundaryID, wfv1.NodeRunning)
}
// kick off execution of each target task asynchronously
for _, taskNames := range targetTasks {
woc.executeDAGTask(dagCtx, taskNames)
}
// check if we are still running any tasks in this dag and return early if we do
dagPhase := dagCtx.assessDAGPhase(targetTasks, woc.wf.Status.Nodes)
switch dagPhase {
case wfv1.NodeRunning:
return woc.getNodeByName(nodeName)
case wfv1.NodeError, wfv1.NodeFailed:
return woc.markNodePhase(nodeName, dagPhase)
}
// set outputs from tasks in order for DAG templates to support outputs
scope := wfScope{
tmpl: tmpl,
scope: make(map[string]interface{}),
}
for _, task := range tmpl.DAG.Tasks {
taskNode := dagCtx.getTaskNode(task.Name)
if taskNode == nil {
// Can happen when dag.target was specified
continue
}
woc.processNodeOutputs(&scope, fmt.Sprintf("tasks.%s", task.Name), taskNode)
}
outputs, err := getTemplateOutputsFromScope(tmpl, &scope)
if err != nil {
return woc.markNodeError(nodeName, err)
}
if outputs != nil {
node = woc.getNodeByName(nodeName)
node.Outputs = outputs
woc.wf.Status.Nodes[node.ID] = *node
}
// set the outbound nodes from the target tasks
node = woc.getNodeByName(nodeName)
outbound := make([]string, 0)
for _, depName := range targetTasks {
depNode := dagCtx.getTaskNode(depName)
if depNode == nil {
woc.log.Println(depName)
}
outboundNodeIDs := woc.getOutboundNodes(depNode.ID)
outbound = append(outbound, outboundNodeIDs...)
}
woc.log.Infof("Outbound nodes of %s set to %s", node.ID, outbound)
node.OutboundNodes = outbound
woc.wf.Status.Nodes[node.ID] = *node
return woc.markNodePhase(nodeName, wfv1.NodeSucceeded)
}
// executeDAGTask traverses and executes the upward chain of dependencies of a task
func (woc *wfOperationCtx) executeDAGTask(dagCtx *dagContext, taskName string) {
if _, ok := dagCtx.visited[taskName]; ok {
return
}
dagCtx.visited[taskName] = true
node := dagCtx.getTaskNode(taskName)
if node != nil && node.Completed() {
return
}
// Check if our dependencies completed. If not, recurse our parents executing them if necessary
task := dagCtx.getTask(taskName)
dependenciesCompleted := true
dependenciesSuccessful := true
nodeName := dagCtx.taskNodeName(taskName)
for _, depName := range task.Dependencies {
depNode := dagCtx.getTaskNode(depName)
if depNode != nil {
if depNode.Completed() {
if !depNode.Successful() && !dagCtx.getTask(depName).ContinuesOn(depNode.Phase) {
dependenciesSuccessful = false
}
continue
}
}
dependenciesCompleted = false
dependenciesSuccessful = false
// recurse our dependency
woc.executeDAGTask(dagCtx, depName)
}
if !dependenciesCompleted {
return
}
if !dependenciesSuccessful {
// TODO: in the future we may support some more sophisticated syntax for deciding on how
// to proceed if at least one dependency succeeded, analogous to airflow's trigger rules,
// (e.g. one_success, all_done, one_failed, etc...). This decision would be made here.
return
}
// All our dependencies were satisfied and successful. It's our turn to run
taskGroupNode := woc.getNodeByName(nodeName)
if taskGroupNode != nil && taskGroupNode.Type != wfv1.NodeTypeTaskGroup {
taskGroupNode = nil
}
// connectDependencies is a helper to connect our dependencies to current task as children
connectDependencies := func(taskNodeName string) {
if len(task.Dependencies) == 0 || taskGroupNode != nil {
// if we had no dependencies, then we are a root task, and we should connect the
// boundary node as our parent
if taskGroupNode == nil {
woc.addChildNode(dagCtx.boundaryName, taskNodeName)
} else {
woc.addChildNode(taskGroupNode.Name, taskNodeName)
}
} else {
// Otherwise, add all outbound nodes of our dependencies as parents to this node
for _, depName := range task.Dependencies {
depNode := dagCtx.getTaskNode(depName)
outboundNodeIDs := woc.getOutboundNodes(depNode.ID)
woc.log.Infof("DAG outbound nodes of %s are %s", depNode, outboundNodeIDs)
for _, outNodeID := range outboundNodeIDs {
woc.addChildNode(woc.wf.Status.Nodes[outNodeID].Name, taskNodeName)
}
}
}
}
// First resolve/substitute params/artifacts from our dependencies
newTask, err := woc.resolveDependencyReferences(dagCtx, task)
if err != nil {
woc.initializeNode(nodeName, wfv1.NodeTypeSkipped, task.Template, dagCtx.boundaryID, wfv1.NodeError, err.Error())
connectDependencies(nodeName)
return
}
// Next, expand the DAG's withItems/withParams/withSequence (if any). If there was none, then
// expandedTasks will be a single element list of the same task
expandedTasks, err := woc.expandTask(*newTask)
if err != nil {
woc.initializeNode(nodeName, wfv1.NodeTypeSkipped, task.Template, dagCtx.boundaryID, wfv1.NodeError, err.Error())
connectDependencies(nodeName)
return
}
// If DAG task has withParam of with withSequence then we need to create virtual node of type TaskGroup.
// For example, if we had task A with withItems of ['foo', 'bar'] which expanded to ['A(0:foo)', 'A(1:bar)'], we still
// need to create a node for A.
if len(task.WithItems) > 0 || task.WithParam != "" || task.WithSequence != nil {
if taskGroupNode == nil {
connectDependencies(nodeName)
taskGroupNode = woc.initializeNode(nodeName, wfv1.NodeTypeTaskGroup, task.Template, dagCtx.boundaryID, wfv1.NodeRunning, "")
}
}
for _, t := range expandedTasks {
node = dagCtx.getTaskNode(t.Name)
taskNodeName := dagCtx.taskNodeName(t.Name)
if node == nil {
woc.log.Infof("All of node %s dependencies %s completed", taskNodeName, task.Dependencies)
// Add the child relationship from our dependency's outbound nodes to this node.
connectDependencies(taskNodeName)
// Check the task's when clause to decide if it should execute
proceed, err := shouldExecute(t.When)
if err != nil {
woc.initializeNode(taskNodeName, wfv1.NodeTypeSkipped, task.Template, dagCtx.boundaryID, wfv1.NodeError, err.Error())
continue
}
if !proceed {
skipReason := fmt.Sprintf("when '%s' evaluated false", t.When)
woc.initializeNode(taskNodeName, wfv1.NodeTypeSkipped, task.Template, dagCtx.boundaryID, wfv1.NodeSkipped, skipReason)
continue
}
}
// Finally execute the template
_, _ = woc.executeTemplate(t.Template, t.Arguments, taskNodeName, dagCtx.boundaryID)
}
if taskGroupNode != nil {
groupPhase := wfv1.NodeSucceeded
for _, t := range expandedTasks {
// Add the child relationship from our dependency's outbound nodes to this node.
node := dagCtx.getTaskNode(t.Name)
if node == nil || !node.Completed() {
return
}
if !node.Successful() {
groupPhase = node.Phase
}
}
woc.markNodePhase(taskGroupNode.Name, groupPhase)
}
}
// resolveDependencyReferences replaces any references to outputs of task dependencies, or artifacts in the inputs
// NOTE: by now, input parameters should have been substituted throughout the template
func (woc *wfOperationCtx) resolveDependencyReferences(dagCtx *dagContext, task *wfv1.DAGTask) (*wfv1.DAGTask, error) {
// build up the scope
scope := wfScope{
tmpl: dagCtx.tmpl,
scope: make(map[string]interface{}),
}
woc.addOutputsToScope("workflow", woc.wf.Status.Outputs, &scope)
ancestors := common.GetTaskAncestry(task.Name, dagCtx.tasks)
for _, ancestor := range ancestors {
ancestorNode := dagCtx.getTaskNode(ancestor)
prefix := fmt.Sprintf("tasks.%s", ancestor)
if ancestorNode.Type == wfv1.NodeTypeTaskGroup {
var ancestorNodes []wfv1.NodeStatus
for _, node := range woc.wf.Status.Nodes {
if node.BoundaryID == dagCtx.boundaryID && strings.HasPrefix(node.Name, ancestorNode.Name+"(") {
ancestorNodes = append(ancestorNodes, node)
}
}
woc.processAggregateNodeOutputs(ancestorNode.TemplateName, &scope, prefix, ancestorNodes)
} else {
woc.processNodeOutputs(&scope, prefix, ancestorNode)
}
}
// Perform replacement
// Replace woc.volumes
err := woc.substituteParamsInVolumes(scope.replaceMap())
if err != nil {
return nil, err
}
// Replace task's parameters
taskBytes, err := json.Marshal(task)
if err != nil {
return nil, errors.InternalWrapError(err)
}
fstTmpl := fasttemplate.New(string(taskBytes), "{{", "}}")
newTaskStr, err := common.Replace(fstTmpl, scope.replaceMap(), true)
if err != nil {
return nil, err
}
var newTask wfv1.DAGTask
err = json.Unmarshal([]byte(newTaskStr), &newTask)
if err != nil {
return nil, errors.InternalWrapError(err)
}
// replace all artifact references
for j, art := range newTask.Arguments.Artifacts {
if art.From == "" {
continue
}
resolvedArt, err := scope.resolveArtifact(art.From)
if err != nil {
return nil, err
}
resolvedArt.Name = art.Name
newTask.Arguments.Artifacts[j] = *resolvedArt
}
return &newTask, nil
}
// findLeafTaskNames finds the names of all tasks whom no other nodes depend on.
// This list of tasks is used as the the default list of targets when dag.targets is omitted.
func findLeafTaskNames(tasks []wfv1.DAGTask) []string {
taskIsLeaf := make(map[string]bool)
for _, task := range tasks {
if _, ok := taskIsLeaf[task.Name]; !ok {
taskIsLeaf[task.Name] = true
}
for _, dependency := range task.Dependencies {
taskIsLeaf[dependency] = false
}
}
leafTaskNames := make([]string, 0)
for taskName, isLeaf := range taskIsLeaf {
if isLeaf {
leafTaskNames = append(leafTaskNames, taskName)
}
}
return leafTaskNames
}
// expandTask expands a single DAG task containing withItems, withParams, withSequence into multiple parallel tasks
func (woc *wfOperationCtx) expandTask(task wfv1.DAGTask) ([]wfv1.DAGTask, error) {
taskBytes, err := json.Marshal(task)
if err != nil {
return nil, errors.InternalWrapError(err)
}
var items []wfv1.Item
if len(task.WithItems) > 0 {
items = task.WithItems
} else if task.WithParam != "" {
err = json.Unmarshal([]byte(task.WithParam), &items)
if err != nil {
return nil, errors.Errorf(errors.CodeBadRequest, "withParam value could not be parsed as a JSON list: %s", strings.TrimSpace(task.WithParam))
}
} else if task.WithSequence != nil {
items, err = expandSequence(task.WithSequence)
if err != nil {
return nil, err
}
} else {
return []wfv1.DAGTask{task}, nil
}
fstTmpl := fasttemplate.New(string(taskBytes), "{{", "}}")
expandedTasks := make([]wfv1.DAGTask, 0)
for i, item := range items {
var newTask wfv1.DAGTask
newTaskName, err := processItem(fstTmpl, task.Name, i, item, &newTask)
if err != nil {
return nil, err
}
newTask.Name = newTaskName
newTask.Template = task.Template
expandedTasks = append(expandedTasks, newTask)
}
return expandedTasks, nil
}