This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
executor.go
1258 lines (1108 loc) · 57.6 KB
/
executor.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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package nodes contains the Core Nodes Executor implementation and a subpackage for every node kind
// This module implements the core Nodes executor.
// This executor is the starting point for executing any node in the workflow. Since Nodes in a workflow are composable,
// i.e., one node may contain other nodes, the Node Handler is recursive in nature.
// This executor handles the core logic for all nodes, but specific logic for handling different kinds of nodes is delegated
// to the respective node handlers
//
// Available node handlers are
// - Task: Arguably the most important handler as it handles all tasks. These include all plugins. The goal of the workflow is
// is to run tasks, thus every workflow will contain atleast one TaskNode (except for the case, where the workflow
// is purely a meta-workflow and can run other workflows
// - SubWorkflow: This is one of the most important handlers. It can execute Workflows that are nested inside a workflow
// - DynamicTask Handler: This is just a decorator on the Task Handler. It handles cases, in which the Task returns a futures
// file. Every Task is actually executed through the DynamicTaskHandler
// - Branch Handler: This handler is used to execute branches
// - Start & End Node handler: these are nominal handlers for the start and end node and do no really carry a lot of logic
package nodes
import (
"context"
"fmt"
"time"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service"
"github.com/flyteorg/flytepropeller/events"
eventsErr "github.com/flyteorg/flytepropeller/events/errors"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/flyteorg/flytepropeller/pkg/controller/config"
"github.com/flyteorg/flytepropeller/pkg/controller/executors"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/common"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/errors"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/handler"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/recovery"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/subworkflow/launchplan"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/task"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/catalog"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/flyteorg/flytestdlib/contextutils"
errors2 "github.com/flyteorg/flytestdlib/errors"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flytestdlib/promutils/labeled"
"github.com/flyteorg/flytestdlib/storage"
"github.com/golang/protobuf/ptypes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type nodeMetrics struct {
Scope promutils.Scope
FailureDuration labeled.StopWatch
SuccessDuration labeled.StopWatch
RecoveryDuration labeled.StopWatch
UserErrorDuration labeled.StopWatch
SystemErrorDuration labeled.StopWatch
UnknownErrorDuration labeled.StopWatch
PermanentUserErrorDuration labeled.StopWatch
PermanentSystemErrorDuration labeled.StopWatch
PermanentUnknownErrorDuration labeled.StopWatch
ResolutionFailure labeled.Counter
InputsWriteFailure labeled.Counter
TimedOutFailure labeled.Counter
InterruptedThresholdHit labeled.Counter
InterruptibleNodesRunning labeled.Counter
InterruptibleNodesTerminated labeled.Counter
// Measures the latency between the last parent node stoppedAt time and current node's queued time.
TransitionLatency labeled.StopWatch
// Measures the latency between the time a node's been queued to the time the handler reported the executable moved
// to running state
QueuingLatency labeled.StopWatch
NodeExecutionTime labeled.StopWatch
NodeInputGatherLatency labeled.StopWatch
}
// Implements the executors.Node interface
type nodeExecutor struct {
nodeHandlerFactory HandlerFactory
enqueueWorkflow v1alpha1.EnqueueWorkflow
store *storage.DataStore
nodeRecorder events.NodeEventRecorder
taskRecorder events.TaskEventRecorder
metrics *nodeMetrics
maxDatasetSizeBytes int64
outputResolver OutputResolver
defaultExecutionDeadline time.Duration
defaultActiveDeadline time.Duration
maxNodeRetriesForSystemFailures uint32
interruptibleFailureThreshold uint32
defaultDataSandbox storage.DataReference
shardSelector ioutils.ShardSelector
recoveryClient recovery.Client
eventConfig *config.EventConfig
clusterID string
}
func (c *nodeExecutor) RecordTransitionLatency(ctx context.Context, dag executors.DAGStructure, nl executors.NodeLookup, node v1alpha1.ExecutableNode, nodeStatus v1alpha1.ExecutableNodeStatus) {
if nodeStatus.GetPhase() == v1alpha1.NodePhaseNotYetStarted || nodeStatus.GetPhase() == v1alpha1.NodePhaseQueued {
// Log transition latency (The most recently finished parent node endAt time to this node's queuedAt time -now-)
t, err := GetParentNodeMaxEndTime(ctx, dag, nl, node)
if err != nil {
logger.Warnf(ctx, "Failed to record transition latency for node. Error: %s", err.Error())
return
}
if !t.IsZero() {
c.metrics.TransitionLatency.Observe(ctx, t.Time, time.Now())
}
} else if nodeStatus.GetPhase() == v1alpha1.NodePhaseRetryableFailure && nodeStatus.GetLastUpdatedAt() != nil {
c.metrics.TransitionLatency.Observe(ctx, nodeStatus.GetLastUpdatedAt().Time, time.Now())
}
}
func (c *nodeExecutor) IdempotentRecordEvent(ctx context.Context, nodeEvent *event.NodeExecutionEvent) error {
if nodeEvent == nil {
return fmt.Errorf("event recording attempt of Nil Node execution event")
}
if nodeEvent.Id == nil {
return fmt.Errorf("event recording attempt of with nil node Event ID")
}
logger.Infof(ctx, "Recording NodeEvent [%s] phase[%s]", nodeEvent.GetId().String(), nodeEvent.Phase.String())
err := c.nodeRecorder.RecordNodeEvent(ctx, nodeEvent, c.eventConfig)
if err != nil {
if nodeEvent.GetId().NodeId == v1alpha1.EndNodeID {
return nil
}
if eventsErr.IsAlreadyExists(err) {
logger.Infof(ctx, "Node event phase: %s, nodeId %s already exist",
nodeEvent.Phase.String(), nodeEvent.GetId().NodeId)
return nil
} else if eventsErr.IsEventAlreadyInTerminalStateError(err) {
if IsTerminalNodePhase(nodeEvent.Phase) {
// Event was trying to record a different terminal phase for an already terminal event. ignoring.
logger.Infof(ctx, "Node event phase: %s, nodeId %s already in terminal phase. err: %s",
nodeEvent.Phase.String(), nodeEvent.GetId().NodeId, err.Error())
return nil
}
logger.Warningf(ctx, "Failed to record nodeEvent, error [%s]", err.Error())
return errors.Wrapf(errors.IllegalStateError, nodeEvent.Id.NodeId, err, "phase mis-match mismatch between propeller and control plane; Trying to record Node p: %s", nodeEvent.Phase)
}
}
return err
}
func (c *nodeExecutor) recoverInputs(ctx context.Context, nCtx handler.NodeExecutionContext,
recovered *admin.NodeExecution, recoveredData *admin.NodeExecutionGetDataResponse) (*core.LiteralMap, error) {
nodeInputs := recoveredData.FullInputs
if nodeInputs != nil {
if err := c.store.WriteProtobuf(ctx, nCtx.InputReader().GetInputPath(), storage.Options{}, nodeInputs); err != nil {
c.metrics.InputsWriteFailure.Inc(ctx)
logger.Errorf(ctx, "Failed to move recovered inputs for Node. Error [%v]. InputsFile [%s]", err, nCtx.InputReader().GetInputPath())
return nil, errors.Wrapf(errors.StorageError, nCtx.NodeID(), err, "Failed to store inputs for Node. InputsFile [%s]", nCtx.InputReader().GetInputPath())
}
} else if len(recovered.InputUri) > 0 {
// If the inputs are too large they won't be returned inline in the RecoverData call. We must fetch them before copying them.
nodeInputs = &core.LiteralMap{}
if recoveredData.FullInputs == nil {
if err := c.store.ReadProtobuf(ctx, storage.DataReference(recovered.InputUri), nodeInputs); err != nil {
return nil, errors.Wrapf(errors.InputsNotFoundError, nCtx.NodeID(), err, "failed to read data from dataDir [%v].", recovered.InputUri)
}
}
if err := c.store.WriteProtobuf(ctx, nCtx.InputReader().GetInputPath(), storage.Options{}, nodeInputs); err != nil {
c.metrics.InputsWriteFailure.Inc(ctx)
logger.Errorf(ctx, "Failed to move recovered inputs for Node. Error [%v]. InputsFile [%s]", err, nCtx.InputReader().GetInputPath())
return nil, errors.Wrapf(errors.StorageError, nCtx.NodeID(), err, "Failed to store inputs for Node. InputsFile [%s]", nCtx.InputReader().GetInputPath())
}
}
return nodeInputs, nil
}
func (c *nodeExecutor) attemptRecovery(ctx context.Context, nCtx handler.NodeExecutionContext) (handler.PhaseInfo, error) {
fullyQualifiedNodeID := nCtx.NodeExecutionMetadata().GetNodeExecutionID().NodeId
if nCtx.ExecutionContext().GetEventVersion() != v1alpha1.EventVersion0 {
// compute fully qualified node id (prefixed with parent id and retry attempt) to ensure uniqueness
var err error
fullyQualifiedNodeID, err = common.GenerateUniqueID(nCtx.ExecutionContext().GetParentInfo(), nCtx.NodeExecutionMetadata().GetNodeExecutionID().NodeId)
if err != nil {
return handler.PhaseInfoUndefined, err
}
}
recovered, err := c.recoveryClient.RecoverNodeExecution(ctx, nCtx.ExecutionContext().GetExecutionConfig().RecoveryExecution.WorkflowExecutionIdentifier, fullyQualifiedNodeID)
if err != nil {
st, ok := status.FromError(err)
if !ok || st.Code() != codes.NotFound {
logger.Warnf(ctx, "Failed to recover node [%+v] with err [%+v]", nCtx.NodeExecutionMetadata().GetNodeExecutionID(), err)
}
// The node is not recoverable when it's not found in the parent execution
return handler.PhaseInfoUndefined, nil
}
if recovered == nil {
logger.Warnf(ctx, "call to recover node [%+v] returned no error but also no node", nCtx.NodeExecutionMetadata().GetNodeExecutionID())
return handler.PhaseInfoUndefined, nil
}
if recovered.Closure == nil {
logger.Warnf(ctx, "Fetched node execution [%+v] data but was missing closure. Will not attempt to recover",
nCtx.NodeExecutionMetadata().GetNodeExecutionID())
return handler.PhaseInfoUndefined, nil
}
// A recoverable node execution should always be in a terminal phase
switch recovered.Closure.Phase {
case core.NodeExecution_SKIPPED:
return handler.PhaseInfoSkip(nil, "node execution recovery indicated original node was skipped"), nil
case core.NodeExecution_SUCCEEDED:
fallthrough
case core.NodeExecution_RECOVERED:
logger.Debugf(ctx, "Node [%+v] can be recovered. Proceeding to copy inputs and outputs", nCtx.NodeExecutionMetadata().GetNodeExecutionID())
default:
// The node execution may be partially recoverable through intra task checkpointing. Save the checkpoint
// uri in the task node state to pass to the task handler later on.
if metadata, ok := recovered.Closure.TargetMetadata.(*admin.NodeExecutionClosure_TaskNodeMetadata); ok {
state := nCtx.NodeStateReader().GetTaskNodeState()
state.PreviousNodeExecutionCheckpointURI = storage.DataReference(metadata.TaskNodeMetadata.CheckpointUri)
err = nCtx.NodeStateWriter().PutTaskNodeState(state)
if err != nil {
logger.Warn(ctx, "failed to save recovered checkpoint uri for [%+v]: [%+v]",
nCtx.NodeExecutionMetadata().GetNodeExecutionID(), err)
}
}
// if this node is a dynamic task we attempt to recover the compiled workflow from instances where the parent
// task succeeded but the dynamic task did not complete. this is important to ensure correctness since node ids
// within the compiled closure may not be generated deterministically.
if recovered.Metadata != nil && recovered.Metadata.IsDynamic && len(recovered.Closure.DynamicJobSpecUri) > 0 {
// recover node inputs
recoveredData, err := c.recoveryClient.RecoverNodeExecutionData(ctx,
nCtx.ExecutionContext().GetExecutionConfig().RecoveryExecution.WorkflowExecutionIdentifier, fullyQualifiedNodeID)
if err != nil || recoveredData == nil {
return handler.PhaseInfoUndefined, nil
}
if _, err := c.recoverInputs(ctx, nCtx, recovered, recoveredData); err != nil {
return handler.PhaseInfoUndefined, err
}
// copy previous DynamicJobSpec file
f, err := task.NewRemoteFutureFileReader(ctx, nCtx.NodeStatus().GetOutputDir(), nCtx.DataStore())
if err != nil {
return handler.PhaseInfoUndefined, err
}
dynamicJobSpecReference := storage.DataReference(recovered.Closure.DynamicJobSpecUri)
if err := nCtx.DataStore().CopyRaw(ctx, dynamicJobSpecReference, f.GetLoc(), storage.Options{}); err != nil {
return handler.PhaseInfoUndefined, errors.Wrapf(errors.StorageError, nCtx.NodeID(), err,
"failed to store dynamic job spec for node. source file [%s] destination file [%s]", dynamicJobSpecReference, f.GetLoc())
}
// transition node phase to 'Running' and dynamic task phase to 'DynamicNodePhaseParentFinalized'
state := nCtx.NodeStateReader().GetDynamicNodeState()
state.Phase = v1alpha1.DynamicNodePhaseParentFinalized
if err := nCtx.NodeStateWriter().PutDynamicNodeState(state); err != nil {
return handler.PhaseInfoUndefined, errors.Wrapf(errors.UnknownError, nCtx.NodeID(), err, "failed to store dynamic node state")
}
return handler.PhaseInfoRunning(&handler.ExecutionInfo{}), nil
}
logger.Debugf(ctx, "Node [%+v] phase [%v] is not recoverable", nCtx.NodeExecutionMetadata().GetNodeExecutionID(), recovered.Closure.Phase)
return handler.PhaseInfoUndefined, nil
}
recoveredData, err := c.recoveryClient.RecoverNodeExecutionData(ctx, nCtx.ExecutionContext().GetExecutionConfig().RecoveryExecution.WorkflowExecutionIdentifier, fullyQualifiedNodeID)
if err != nil {
st, ok := status.FromError(err)
if !ok || st.Code() != codes.NotFound {
logger.Warnf(ctx, "Failed to attemptRecovery node execution data for [%+v] although back-end indicated node was recoverable with err [%+v]",
nCtx.NodeExecutionMetadata().GetNodeExecutionID(), err)
}
return handler.PhaseInfoUndefined, nil
}
if recoveredData == nil {
logger.Warnf(ctx, "call to attemptRecovery node [%+v] data returned no error but also no data", nCtx.NodeExecutionMetadata().GetNodeExecutionID())
return handler.PhaseInfoUndefined, nil
}
// Copy inputs to this node's expected location
nodeInputs, err := c.recoverInputs(ctx, nCtx, recovered, recoveredData)
if err != nil {
return handler.PhaseInfoUndefined, err
}
// Similarly, copy outputs' reference
so := storage.Options{}
var outputs = &core.LiteralMap{}
if recoveredData.FullOutputs != nil {
outputs = recoveredData.FullOutputs
} else if recovered.Closure.GetOutputData() != nil {
outputs = recovered.Closure.GetOutputData()
} else if len(recovered.Closure.GetOutputUri()) > 0 {
if err := c.store.ReadProtobuf(ctx, storage.DataReference(recovered.Closure.GetOutputUri()), outputs); err != nil {
return handler.PhaseInfoUndefined, errors.Wrapf(errors.InputsNotFoundError, nCtx.NodeID(), err, "failed to read output data [%v].", recovered.Closure.GetOutputUri())
}
} else {
logger.Debugf(ctx, "No outputs found for recovered node [%+v]", nCtx.NodeExecutionMetadata().GetNodeExecutionID())
}
outputFile := v1alpha1.GetOutputsFile(nCtx.NodeStatus().GetOutputDir())
oi := &handler.OutputInfo{
OutputURI: outputFile,
}
deckFile := storage.DataReference(recovered.Closure.GetDeckUri())
if len(deckFile) > 0 {
metadata, err := nCtx.DataStore().Head(ctx, deckFile)
if err != nil {
logger.Errorf(ctx, "Failed to check the existence of deck file. Error: %v", err)
return handler.PhaseInfoUndefined, errors.Wrapf(errors.CausedByError, nCtx.NodeID(), err, "Failed to check the existence of deck file.")
}
if metadata.Exists() {
oi.DeckURI = &deckFile
}
}
if err := c.store.WriteProtobuf(ctx, outputFile, so, outputs); err != nil {
logger.Errorf(ctx, "Failed to write protobuf (metadata). Error [%v]", err)
return handler.PhaseInfoUndefined, errors.Wrapf(errors.CausedByError, nCtx.NodeID(), err, "Failed to store recovered node execution outputs")
}
info := &handler.ExecutionInfo{
Inputs: nodeInputs,
OutputInfo: oi,
}
if recovered.Closure.GetTaskNodeMetadata() != nil {
taskNodeInfo := &handler.TaskNodeInfo{
TaskNodeMetadata: &event.TaskNodeMetadata{
CatalogKey: recovered.Closure.GetTaskNodeMetadata().CatalogKey,
CacheStatus: recovered.Closure.GetTaskNodeMetadata().CacheStatus,
},
}
if recoveredData.DynamicWorkflow != nil {
taskNodeInfo.TaskNodeMetadata.DynamicWorkflow = &event.DynamicWorkflowNodeMetadata{
Id: recoveredData.DynamicWorkflow.Id,
CompiledWorkflow: recoveredData.DynamicWorkflow.CompiledWorkflow,
}
}
info.TaskNodeInfo = taskNodeInfo
} else if recovered.Closure.GetWorkflowNodeMetadata() != nil {
logger.Warnf(ctx, "Attempted to recover node")
info.WorkflowNodeInfo = &handler.WorkflowNodeInfo{
LaunchedWorkflowID: recovered.Closure.GetWorkflowNodeMetadata().ExecutionId,
}
}
return handler.PhaseInfoRecovered(info), nil
}
// In this method we check if the queue is ready to be processed and if so, we prime it in Admin as queued
// Before we start the node execution, we need to transition this Node status to Queued.
// This is because a node execution has to exist before task/wf executions can start.
func (c *nodeExecutor) preExecute(ctx context.Context, dag executors.DAGStructure, nCtx handler.NodeExecutionContext) (
handler.PhaseInfo, error) {
logger.Debugf(ctx, "Node not yet started")
// Query the nodes information to figure out if it can be executed.
predicatePhase, err := CanExecute(ctx, dag, nCtx.ContextualNodeLookup(), nCtx.Node())
if err != nil {
logger.Debugf(ctx, "Node failed in CanExecute. Error [%s]", err)
return handler.PhaseInfoUndefined, err
}
if predicatePhase == PredicatePhaseReady {
// TODO: Performance problem, we maybe in a retry loop and do not need to resolve the inputs again.
// For now we will do this
node := nCtx.Node()
var nodeInputs *core.LiteralMap
if !node.IsStartNode() {
if nCtx.ExecutionContext().GetExecutionConfig().RecoveryExecution.WorkflowExecutionIdentifier != nil {
phaseInfo, err := c.attemptRecovery(ctx, nCtx)
if err != nil || phaseInfo.GetPhase() != handler.EPhaseUndefined {
return phaseInfo, err
}
}
nodeStatus := nCtx.NodeStatus()
dataDir := nodeStatus.GetDataDir()
t := c.metrics.NodeInputGatherLatency.Start(ctx)
defer t.Stop()
// Can execute
var err error
nodeInputs, err = Resolve(ctx, c.outputResolver, nCtx.ContextualNodeLookup(), node.GetID(), node.GetInputBindings())
// TODO we need to handle retryable, network errors here!!
if err != nil {
c.metrics.ResolutionFailure.Inc(ctx)
logger.Warningf(ctx, "Failed to resolve inputs for Node. Error [%v]", err)
return handler.PhaseInfoFailure(core.ExecutionError_SYSTEM, "BindingResolutionFailure", err.Error(), nil), nil
}
if nodeInputs != nil {
inputsFile := v1alpha1.GetInputsFile(dataDir)
if err := c.store.WriteProtobuf(ctx, inputsFile, storage.Options{}, nodeInputs); err != nil {
c.metrics.InputsWriteFailure.Inc(ctx)
logger.Errorf(ctx, "Failed to store inputs for Node. Error [%v]. InputsFile [%s]", err, inputsFile)
return handler.PhaseInfoUndefined, errors.Wrapf(
errors.StorageError, node.GetID(), err, "Failed to store inputs for Node. InputsFile [%s]", inputsFile)
}
}
logger.Debugf(ctx, "Node Data Directory [%s].", nodeStatus.GetDataDir())
}
return handler.PhaseInfoQueued("node queued", nodeInputs), nil
}
// Now that we have resolved the inputs, we can record as a transition latency. This is because we have completed
// all the overhead that we have to compute. Any failures after this will incur this penalty, but it could be due
// to various external reasons - like queuing, overuse of quota, plugin overhead etc.
logger.Debugf(ctx, "preExecute completed in phase [%s]", predicatePhase.String())
if predicatePhase == PredicatePhaseSkip {
return handler.PhaseInfoSkip(nil, "Node Skipped as parent node was skipped"), nil
}
return handler.PhaseInfoNotReady("predecessor node not yet complete"), nil
}
func isTimeoutExpired(queuedAt *metav1.Time, timeout time.Duration) bool {
if !queuedAt.IsZero() && timeout != 0 {
deadline := queuedAt.Add(timeout)
if deadline.Before(time.Now()) {
return true
}
}
return false
}
func (c *nodeExecutor) isEligibleForRetry(nCtx *nodeExecContext, nodeStatus v1alpha1.ExecutableNodeStatus, err *core.ExecutionError) (currentAttempt, maxAttempts uint32, isEligible bool) {
if err.Kind == core.ExecutionError_SYSTEM {
currentAttempt = nodeStatus.GetSystemFailures()
maxAttempts = c.maxNodeRetriesForSystemFailures
isEligible = currentAttempt < c.maxNodeRetriesForSystemFailures
return
}
currentAttempt = (nodeStatus.GetAttempts() + 1) - nodeStatus.GetSystemFailures()
if nCtx.Node().GetRetryStrategy() != nil && nCtx.Node().GetRetryStrategy().MinAttempts != nil {
maxAttempts = uint32(*nCtx.Node().GetRetryStrategy().MinAttempts)
}
isEligible = currentAttempt < maxAttempts
return
}
func (c *nodeExecutor) execute(ctx context.Context, h handler.Node, nCtx *nodeExecContext, nodeStatus v1alpha1.ExecutableNodeStatus) (handler.PhaseInfo, error) {
logger.Debugf(ctx, "Executing node")
defer logger.Debugf(ctx, "Node execution round complete")
t, err := h.Handle(ctx, nCtx)
if err != nil {
return handler.PhaseInfoUndefined, err
}
phase := t.Info()
// check for timeout for non-terminal phases
if !phase.GetPhase().IsTerminal() {
activeDeadline := c.defaultActiveDeadline
if nCtx.Node().GetActiveDeadline() != nil && *nCtx.Node().GetActiveDeadline() > 0 {
activeDeadline = *nCtx.Node().GetActiveDeadline()
}
if isTimeoutExpired(nodeStatus.GetQueuedAt(), activeDeadline) {
logger.Infof(ctx, "Node has timed out; timeout configured: %v", activeDeadline)
return handler.PhaseInfoTimedOut(nil, fmt.Sprintf("task active timeout [%s] expired", activeDeadline.String())), nil
}
// Execution timeout is a retry-able error
executionDeadline := c.defaultExecutionDeadline
if nCtx.Node().GetExecutionDeadline() != nil && *nCtx.Node().GetExecutionDeadline() > 0 {
executionDeadline = *nCtx.Node().GetExecutionDeadline()
}
if isTimeoutExpired(nodeStatus.GetLastAttemptStartedAt(), executionDeadline) {
logger.Infof(ctx, "Current execution for the node timed out; timeout configured: %v", executionDeadline)
executionErr := &core.ExecutionError{Code: "TimeoutExpired", Message: fmt.Sprintf("task execution timeout [%s] expired", executionDeadline.String()), Kind: core.ExecutionError_USER}
phase = handler.PhaseInfoRetryableFailureErr(executionErr, nil)
}
}
if phase.GetPhase() == handler.EPhaseRetryableFailure {
currentAttempt, maxAttempts, isEligible := c.isEligibleForRetry(nCtx, nodeStatus, phase.GetErr())
if !isEligible {
return handler.PhaseInfoFailure(
core.ExecutionError_USER,
fmt.Sprintf("RetriesExhausted|%s", phase.GetErr().Code),
fmt.Sprintf("[%d/%d] currentAttempt done. Last Error: %s::%s", currentAttempt, maxAttempts, phase.GetErr().Kind.String(), phase.GetErr().Message),
phase.GetInfo(),
), nil
}
// Retrying to clearing all status
nCtx.nsm.clearNodeStatus()
}
return phase, nil
}
func (c *nodeExecutor) abort(ctx context.Context, h handler.Node, nCtx handler.NodeExecutionContext, reason string) error {
logger.Debugf(ctx, "Calling aborting & finalize")
if err := h.Abort(ctx, nCtx, reason); err != nil {
finalizeErr := h.Finalize(ctx, nCtx)
if finalizeErr != nil {
return errors.ErrorCollection{Errors: []error{err, finalizeErr}}
}
return err
}
return h.Finalize(ctx, nCtx)
}
func (c *nodeExecutor) finalize(ctx context.Context, h handler.Node, nCtx handler.NodeExecutionContext) error {
return h.Finalize(ctx, nCtx)
}
func (c *nodeExecutor) handleNotYetStartedNode(ctx context.Context, dag executors.DAGStructure, nCtx *nodeExecContext, _ handler.Node) (executors.NodeStatus, error) {
logger.Debugf(ctx, "Node not yet started, running pre-execute")
defer logger.Debugf(ctx, "Node pre-execute completed")
p, err := c.preExecute(ctx, dag, nCtx)
if err != nil {
logger.Errorf(ctx, "failed preExecute for node. Error: %s", err.Error())
return executors.NodeStatusUndefined, err
}
if p.GetPhase() == handler.EPhaseUndefined {
return executors.NodeStatusUndefined, errors.Errorf(errors.IllegalStateError, nCtx.NodeID(), "received undefined phase.")
}
if p.GetPhase() == handler.EPhaseNotReady {
return executors.NodeStatusPending, nil
}
np, err := ToNodePhase(p.GetPhase())
if err != nil {
return executors.NodeStatusUndefined, errors.Wrapf(errors.IllegalStateError, nCtx.NodeID(), err, "failed to move from queued")
}
nodeStatus := nCtx.NodeStatus()
if np != nodeStatus.GetPhase() {
// assert np == Queued!
logger.Infof(ctx, "Change in node state detected from [%s] -> [%s]", nodeStatus.GetPhase().String(), np.String())
nev, err := ToNodeExecutionEvent(nCtx.NodeExecutionMetadata().GetNodeExecutionID(),
p, nCtx.InputReader().GetInputPath().String(), nodeStatus, nCtx.ExecutionContext().GetEventVersion(),
nCtx.ExecutionContext().GetParentInfo(), nCtx.node, c.clusterID, nCtx.NodeStateReader().GetDynamicNodeState().Phase,
c.eventConfig)
if err != nil {
return executors.NodeStatusUndefined, errors.Wrapf(errors.IllegalStateError, nCtx.NodeID(), err, "could not convert phase info to event")
}
err = c.IdempotentRecordEvent(ctx, nev)
if err != nil {
logger.Warningf(ctx, "Failed to record nodeEvent, error [%s]", err.Error())
return executors.NodeStatusUndefined, errors.Wrapf(errors.EventRecordingFailed, nCtx.NodeID(), err, "failed to record node event")
}
UpdateNodeStatus(np, p, nCtx.nsm, nodeStatus)
c.RecordTransitionLatency(ctx, dag, nCtx.ContextualNodeLookup(), nCtx.Node(), nodeStatus)
}
if np == v1alpha1.NodePhaseQueued {
if nCtx.md.IsInterruptible() {
c.metrics.InterruptibleNodesRunning.Inc(ctx)
}
return executors.NodeStatusQueued, nil
} else if np == v1alpha1.NodePhaseSkipped {
return executors.NodeStatusSuccess, nil
}
return executors.NodeStatusPending, nil
}
func (c *nodeExecutor) handleQueuedOrRunningNode(ctx context.Context, nCtx *nodeExecContext, h handler.Node) (executors.NodeStatus, error) {
nodeStatus := nCtx.NodeStatus()
currentPhase := nodeStatus.GetPhase()
// case v1alpha1.NodePhaseQueued, v1alpha1.NodePhaseRunning:
logger.Debugf(ctx, "node executing, current phase [%s]", currentPhase)
defer logger.Debugf(ctx, "node execution completed")
// Since we reset node status inside execute for retryable failure, we use lastAttemptStartTime to carry that information
// across execute which is used to emit metrics
lastAttemptStartTime := nodeStatus.GetLastAttemptStartedAt()
p, err := c.execute(ctx, h, nCtx, nodeStatus)
if err != nil {
logger.Errorf(ctx, "failed Execute for node. Error: %s", err.Error())
return executors.NodeStatusUndefined, err
}
if p.GetPhase() == handler.EPhaseUndefined {
return executors.NodeStatusUndefined, errors.Errorf(errors.IllegalStateError, nCtx.NodeID(), "received undefined phase.")
}
np, err := ToNodePhase(p.GetPhase())
if err != nil {
return executors.NodeStatusUndefined, errors.Wrapf(errors.IllegalStateError, nCtx.NodeID(), err, "failed to move from queued")
}
// execErr in phase-info 'p' is only available if node has failed to execute, and the current phase at that time
// will be v1alpha1.NodePhaseRunning
execErr := p.GetErr()
if execErr != nil && (currentPhase == v1alpha1.NodePhaseRunning || currentPhase == v1alpha1.NodePhaseQueued ||
currentPhase == v1alpha1.NodePhaseDynamicRunning) {
endTime := time.Now()
startTime := endTime
if lastAttemptStartTime != nil {
startTime = lastAttemptStartTime.Time
}
if execErr.GetKind() == core.ExecutionError_SYSTEM {
nodeStatus.IncrementSystemFailures()
c.metrics.SystemErrorDuration.Observe(ctx, startTime, endTime)
} else if execErr.GetKind() == core.ExecutionError_USER {
c.metrics.UserErrorDuration.Observe(ctx, startTime, endTime)
} else {
c.metrics.UnknownErrorDuration.Observe(ctx, startTime, endTime)
}
// When a node fails, we fail the workflow. Independent of number of nodes succeeding/failing, whenever a first node fails,
// the entire workflow is failed.
if np == v1alpha1.NodePhaseFailing {
if execErr.GetKind() == core.ExecutionError_SYSTEM {
nodeStatus.IncrementSystemFailures()
c.metrics.PermanentSystemErrorDuration.Observe(ctx, startTime, endTime)
} else if execErr.GetKind() == core.ExecutionError_USER {
c.metrics.PermanentUserErrorDuration.Observe(ctx, startTime, endTime)
} else {
c.metrics.PermanentUnknownErrorDuration.Observe(ctx, startTime, endTime)
}
}
}
finalStatus := executors.NodeStatusRunning
if np == v1alpha1.NodePhaseFailing && !h.FinalizeRequired() {
logger.Infof(ctx, "Finalize not required, moving node to Failed")
np = v1alpha1.NodePhaseFailed
finalStatus = executors.NodeStatusFailed(p.GetErr())
}
if np == v1alpha1.NodePhaseTimingOut && !h.FinalizeRequired() {
logger.Infof(ctx, "Finalize not required, moving node to TimedOut")
np = v1alpha1.NodePhaseTimedOut
finalStatus = executors.NodeStatusTimedOut
}
if np == v1alpha1.NodePhaseSucceeding && !h.FinalizeRequired() {
logger.Infof(ctx, "Finalize not required, moving node to Succeeded")
np = v1alpha1.NodePhaseSucceeded
finalStatus = executors.NodeStatusSuccess
}
if np == v1alpha1.NodePhaseRecovered {
logger.Infof(ctx, "Finalize not required, moving node to Recovered")
finalStatus = executors.NodeStatusRecovered
}
// If it is retryable failure, we do no want to send any events, as the node is essentially still running
// Similarly if the phase has not changed from the last time, events do not need to be sent
if np != nodeStatus.GetPhase() && np != v1alpha1.NodePhaseRetryableFailure {
// assert np == skipped, succeeding, failing or recovered
logger.Infof(ctx, "Change in node state detected from [%s] -> [%s], (handler phase [%s])", nodeStatus.GetPhase().String(), np.String(), p.GetPhase().String())
nev, err := ToNodeExecutionEvent(nCtx.NodeExecutionMetadata().GetNodeExecutionID(),
p, nCtx.InputReader().GetInputPath().String(), nCtx.NodeStatus(), nCtx.ExecutionContext().GetEventVersion(),
nCtx.ExecutionContext().GetParentInfo(), nCtx.node, c.clusterID, nCtx.NodeStateReader().GetDynamicNodeState().Phase,
c.eventConfig)
if err != nil {
return executors.NodeStatusUndefined, errors.Wrapf(errors.IllegalStateError, nCtx.NodeID(), err, "could not convert phase info to event")
}
err = c.IdempotentRecordEvent(ctx, nev)
if err != nil {
if eventsErr.IsTooLarge(err) {
// With large enough dynamic task fanouts the reported node event, which contains the compiled
// workflow closure, can exceed the gRPC message size limit. In this case we immediately
// transition the node to failing to abort the workflow.
np = v1alpha1.NodePhaseFailing
p = handler.PhaseInfoFailure(core.ExecutionError_USER, "NodeFailed", err.Error(), p.GetInfo())
err = c.IdempotentRecordEvent(ctx, &event.NodeExecutionEvent{
Id: nCtx.NodeExecutionMetadata().GetNodeExecutionID(),
Phase: core.NodeExecution_FAILED,
OccurredAt: ptypes.TimestampNow(),
OutputResult: &event.NodeExecutionEvent_Error{
Error: &core.ExecutionError{
Code: "NodeFailed",
Message: err.Error(),
},
},
})
if err != nil {
return executors.NodeStatusUndefined, errors.Wrapf(errors.EventRecordingFailed, nCtx.NodeID(), err, "failed to record node event")
}
} else {
logger.Warningf(ctx, "Failed to record nodeEvent, error [%s]", err.Error())
return executors.NodeStatusUndefined, errors.Wrapf(errors.EventRecordingFailed, nCtx.NodeID(), err, "failed to record node event")
}
}
// We reach here only when transitioning from Queued to Running. In this case, the startedAt is not set.
if np == v1alpha1.NodePhaseRunning {
if nodeStatus.GetQueuedAt() != nil {
c.metrics.QueuingLatency.Observe(ctx, nodeStatus.GetQueuedAt().Time, time.Now())
}
}
}
UpdateNodeStatus(np, p, nCtx.nsm, nodeStatus)
return finalStatus, nil
}
func (c *nodeExecutor) handleRetryableFailure(ctx context.Context, nCtx *nodeExecContext, h handler.Node) (executors.NodeStatus, error) {
nodeStatus := nCtx.NodeStatus()
logger.Debugf(ctx, "node failed with retryable failure, aborting and finalizing, message: %s", nodeStatus.GetMessage())
if err := c.abort(ctx, h, nCtx, nodeStatus.GetMessage()); err != nil {
return executors.NodeStatusUndefined, err
}
// NOTE: It is important to increment attempts only after abort has been called. Increment attempt mutates the state
// Attempt is used throughout the system to determine the idempotent resource version.
nodeStatus.IncrementAttempts()
nodeStatus.UpdatePhase(v1alpha1.NodePhaseRunning, metav1.Now(), "retrying", nil)
// We are going to retry in the next round, so we should clear all current state
nodeStatus.ClearSubNodeStatus()
nodeStatus.ClearTaskStatus()
nodeStatus.ClearWorkflowStatus()
nodeStatus.ClearDynamicNodeStatus()
return executors.NodeStatusPending, nil
}
func (c *nodeExecutor) handleNode(ctx context.Context, dag executors.DAGStructure, nCtx *nodeExecContext, h handler.Node) (executors.NodeStatus, error) {
logger.Debugf(ctx, "Handling Node [%s]", nCtx.NodeID())
defer logger.Debugf(ctx, "Completed node [%s]", nCtx.NodeID())
nodeStatus := nCtx.NodeStatus()
currentPhase := nodeStatus.GetPhase()
// Optimization!
// If it is start node we directly move it to Queued without needing to run preExecute
if currentPhase == v1alpha1.NodePhaseNotYetStarted && !nCtx.Node().IsStartNode() {
p, err := c.handleNotYetStartedNode(ctx, dag, nCtx, h)
if err != nil {
return p, err
}
if p.NodePhase == executors.NodePhaseQueued {
logger.Infof(ctx, "Node was queued, parallelism is now [%d]", nCtx.ExecutionContext().IncrementParallelism())
}
return p, err
}
if currentPhase == v1alpha1.NodePhaseFailing {
logger.Debugf(ctx, "node failing")
if err := c.abort(ctx, h, nCtx, "node failing"); err != nil {
return executors.NodeStatusUndefined, err
}
nodeStatus.UpdatePhase(v1alpha1.NodePhaseFailed, metav1.Now(), nodeStatus.GetMessage(), nodeStatus.GetExecutionError())
c.metrics.FailureDuration.Observe(ctx, nodeStatus.GetStartedAt().Time, nodeStatus.GetStoppedAt().Time)
if nCtx.md.IsInterruptible() {
c.metrics.InterruptibleNodesTerminated.Inc(ctx)
}
return executors.NodeStatusFailed(nodeStatus.GetExecutionError()), nil
}
if currentPhase == v1alpha1.NodePhaseTimingOut {
logger.Debugf(ctx, "node timing out")
if err := c.abort(ctx, h, nCtx, "node timed out"); err != nil {
return executors.NodeStatusUndefined, err
}
nodeStatus.ClearSubNodeStatus()
nodeStatus.UpdatePhase(v1alpha1.NodePhaseTimedOut, metav1.Now(), nodeStatus.GetMessage(), nodeStatus.GetExecutionError())
c.metrics.TimedOutFailure.Inc(ctx)
if nCtx.md.IsInterruptible() {
c.metrics.InterruptibleNodesTerminated.Inc(ctx)
}
return executors.NodeStatusTimedOut, nil
}
if currentPhase == v1alpha1.NodePhaseSucceeding {
logger.Debugf(ctx, "node succeeding")
if err := c.finalize(ctx, h, nCtx); err != nil {
return executors.NodeStatusUndefined, err
}
t := metav1.Now()
started := nodeStatus.GetStartedAt()
if started == nil {
started = &t
}
stopped := nodeStatus.GetStoppedAt()
if stopped == nil {
stopped = &t
}
c.metrics.SuccessDuration.Observe(ctx, started.Time, stopped.Time)
nodeStatus.ClearSubNodeStatus()
nodeStatus.UpdatePhase(v1alpha1.NodePhaseSucceeded, t, "completed successfully", nil)
if nCtx.md.IsInterruptible() {
c.metrics.InterruptibleNodesTerminated.Inc(ctx)
}
return executors.NodeStatusSuccess, nil
}
if currentPhase == v1alpha1.NodePhaseRetryableFailure {
return c.handleRetryableFailure(ctx, nCtx, h)
}
if currentPhase == v1alpha1.NodePhaseFailed {
// This should never happen
return executors.NodeStatusFailed(nodeStatus.GetExecutionError()), nil
}
return c.handleQueuedOrRunningNode(ctx, nCtx, h)
}
// The space search for the next node to execute is implemented like a DFS algorithm. handleDownstream visits all the nodes downstream from
// the currentNode. Visit a node is the RecursiveNodeHandler. A visit may be partial, complete or may result in a failure.
func (c *nodeExecutor) handleDownstream(ctx context.Context, execContext executors.ExecutionContext, dag executors.DAGStructure, nl executors.NodeLookup, currentNode v1alpha1.ExecutableNode) (executors.NodeStatus, error) {
logger.Debugf(ctx, "Handling downstream Nodes")
// This node is success. Handle all downstream nodes
downstreamNodes, err := dag.FromNode(currentNode.GetID())
if err != nil {
logger.Debugf(ctx, "Error when retrieving downstream nodes, [%s]", err)
return executors.NodeStatusFailed(&core.ExecutionError{
Code: errors.BadSpecificationError,
Message: fmt.Sprintf("failed to retrieve downstream nodes for [%s]", currentNode.GetID()),
Kind: core.ExecutionError_SYSTEM,
}), nil
}
if len(downstreamNodes) == 0 {
logger.Debugf(ctx, "No downstream nodes found. Complete.")
return executors.NodeStatusComplete, nil
}
// If any downstream node is failed, fail, all
// Else if all are success then success
// Else if any one is running then Downstream is still running
allCompleted := true
partialNodeCompletion := false
onFailurePolicy := execContext.GetOnFailurePolicy()
stateOnComplete := executors.NodeStatusComplete
for _, downstreamNodeName := range downstreamNodes {
downstreamNode, ok := nl.GetNode(downstreamNodeName)
if !ok {
return executors.NodeStatusFailed(&core.ExecutionError{
Code: errors.BadSpecificationError,
Message: fmt.Sprintf("failed to retrieve downstream node [%s] for [%s]", downstreamNodeName, currentNode.GetID()),
Kind: core.ExecutionError_SYSTEM,
}), nil
}
state, err := c.RecursiveNodeHandler(ctx, execContext, dag, nl, downstreamNode)
if err != nil {
return executors.NodeStatusUndefined, err
}
if state.HasFailed() || state.HasTimedOut() {
logger.Debugf(ctx, "Some downstream node has failed. Failed: [%v]. TimedOut: [%v]. Error: [%s]", state.HasFailed(), state.HasTimedOut(), state.Err)
if onFailurePolicy == v1alpha1.WorkflowOnFailurePolicy(core.WorkflowMetadata_FAIL_AFTER_EXECUTABLE_NODES_COMPLETE) {
// If the failure policy allows other nodes to continue running, do not exit the loop,
// Keep track of the last failed state in the loop since it'll be the one to return.
// TODO: If multiple nodes fail (which this mode allows), consolidate/summarize failure states in one.
stateOnComplete = state
} else {
return state, nil
}
} else if !state.IsComplete() {
// A Failed/Timedout node is implicitly considered "complete" this means none of the downstream nodes from
// that node will ever be allowed to run.
// This else block, therefore, deals with all other states. IsComplete will return true if and only if this
// node as well as all of its downstream nodes have finished executing with success statuses. Otherwise we
// mark this node's state as not completed to ensure we will visit it again later.
allCompleted = false
}
if state.PartiallyComplete() {
// This implies that one of the downstream nodes has just succeeded and workflow is ready for propagation
// We do not propagate in current cycle to make it possible to store the state between transitions
partialNodeCompletion = true
}
}
if allCompleted {
logger.Debugf(ctx, "All downstream nodes completed")
return stateOnComplete, nil
}
if partialNodeCompletion {
return executors.NodeStatusSuccess, nil
}
return executors.NodeStatusPending, nil
}
func (c *nodeExecutor) SetInputsForStartNode(ctx context.Context, execContext executors.ExecutionContext, dag executors.DAGStructureWithStartNode, nl executors.NodeLookup, inputs *core.LiteralMap) (executors.NodeStatus, error) {
startNode := dag.StartNode()
ctx = contextutils.WithNodeID(ctx, startNode.GetID())
if inputs == nil {
logger.Infof(ctx, "No inputs for the workflow. Skipping storing inputs")
return executors.NodeStatusComplete, nil
}
// StartNode is special. It does not have any processing step. It just takes the workflow (or subworkflow) inputs and converts to its own outputs
nodeStatus := nl.GetNodeExecutionStatus(ctx, startNode.GetID())
if len(nodeStatus.GetDataDir()) == 0 {
return executors.NodeStatusUndefined, errors.Errorf(errors.IllegalStateError, startNode.GetID(), "no data-dir set, cannot store inputs")
}
outputFile := v1alpha1.GetOutputsFile(nodeStatus.GetOutputDir())
so := storage.Options{}
if err := c.store.WriteProtobuf(ctx, outputFile, so, inputs); err != nil {
logger.Errorf(ctx, "Failed to write protobuf (metadata). Error [%v]", err)
return executors.NodeStatusUndefined, errors.Wrapf(errors.CausedByError, startNode.GetID(), err, "Failed to store workflow inputs (as start node)")
}
return executors.NodeStatusComplete, nil
}
func canHandleNode(phase v1alpha1.NodePhase) bool {
return phase == v1alpha1.NodePhaseNotYetStarted ||
phase == v1alpha1.NodePhaseQueued ||
phase == v1alpha1.NodePhaseRunning ||
phase == v1alpha1.NodePhaseFailing ||
phase == v1alpha1.NodePhaseTimingOut ||
phase == v1alpha1.NodePhaseRetryableFailure ||
phase == v1alpha1.NodePhaseSucceeding ||
phase == v1alpha1.NodePhaseDynamicRunning
}
// IsMaxParallelismAchieved checks if we have already achieved max parallelism. It returns true, if the desired max parallelism
// value is achieved, false otherwise
// MaxParallelism is defined as the maximum number of TaskNodes and LaunchPlans (together) that can be executed concurrently
// by one workflow execution. A setting of `0` indicates that it is disabled.
func IsMaxParallelismAchieved(ctx context.Context, currentNode v1alpha1.ExecutableNode, currentPhase v1alpha1.NodePhase,
execContext executors.ExecutionContext) bool {
maxParallelism := execContext.GetExecutionConfig().MaxParallelism
if maxParallelism == 0 {
logger.Debugf(ctx, "Parallelism control disabled")
return false
}
if currentNode.GetKind() == v1alpha1.NodeKindTask ||
(currentNode.GetKind() == v1alpha1.NodeKindWorkflow && currentNode.GetWorkflowNode() != nil && currentNode.GetWorkflowNode().GetLaunchPlanRefID() != nil) {
// If we are queued, let us see if we can proceed within the node parallelism bounds
if execContext.CurrentParallelism() >= maxParallelism {
logger.Infof(ctx, "Maximum Parallelism for task/launch-plan nodes achieved [%d] >= Max [%d], Round will be short-circuited.", execContext.CurrentParallelism(), maxParallelism)
return true
}
// We know that Propeller goes through each workflow in a single thread, thus every node is really processed
// sequentially. So, we can continue - now that we know we are under the parallelism limits and increment the
// parallelism if the node, enters a running state
logger.Debugf(ctx, "Parallelism criteria not met, Current [%d], Max [%d]", execContext.CurrentParallelism(), maxParallelism)
} else {
logger.Debugf(ctx, "NodeKind: %s in status [%s]. Parallelism control is not applicable. Current Parallelism [%d]",
currentNode.GetKind().String(), currentPhase.String(), execContext.CurrentParallelism())
}
return false
}
// RecursiveNodeHandler This is the entrypoint of executing a node in a workflow. A workflow consists of nodes, that are
// nested within other nodes. The system follows an actor model, where the parent nodes control the execution of nested nodes
// The recursive node-handler uses a modified depth-first type of algorithm to execute non-blocked nodes.
func (c *nodeExecutor) RecursiveNodeHandler(ctx context.Context, execContext executors.ExecutionContext,
dag executors.DAGStructure, nl executors.NodeLookup, currentNode v1alpha1.ExecutableNode) (
executors.NodeStatus, error) {
currentNodeCtx := contextutils.WithNodeID(ctx, currentNode.GetID())
nodeStatus := nl.GetNodeExecutionStatus(ctx, currentNode.GetID())
nodePhase := nodeStatus.GetPhase()
if canHandleNode(nodePhase) {
// TODO Follow up Pull Request,
// 1. Rename this method to DAGTraversalHandleNode (accepts a DAGStructure along-with) the remaining arguments
// 2. Create a new method called HandleNode (part of the interface) (remaining all args as the previous method, but no DAGStructure
// 3. Additional both methods will receive inputs reader
// 4. The Downstream nodes handler will Resolve the Inputs
// 5. the method will delegate all other node handling to HandleNode.
// 6. Thus we can get rid of SetInputs for StartNode as well
logger.Debugf(currentNodeCtx, "Handling node Status [%v]", nodeStatus.GetPhase().String())
t := c.metrics.NodeExecutionTime.Start(ctx)
defer t.Stop()
// This is an optimization to avoid creating the nodeContext object in case the node has already been looked at.
// If the overhead was zero, we would just do the isDirtyCheck after the nodeContext is created
if nodeStatus.IsDirty() {
return executors.NodeStatusRunning, nil
}
if IsMaxParallelismAchieved(ctx, currentNode, nodePhase, execContext) {
return executors.NodeStatusRunning, nil
}
nCtx, err := c.newNodeExecContextDefault(ctx, currentNode.GetID(), execContext, nl)
if err != nil {
// NodeExecution creation failure is a permanent fail / system error.