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 60
/
handler.go
391 lines (324 loc) · 13.9 KB
/
handler.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
package dynamic
import (
"context"
"time"
"github.com/lyft/flytestdlib/promutils/labeled"
"github.com/lyft/flytepropeller/pkg/compiler"
common2 "github.com/lyft/flytepropeller/pkg/compiler/common"
"github.com/lyft/flytepropeller/pkg/controller/executors"
"github.com/lyft/flytepropeller/pkg/controller/nodes/common"
"github.com/lyft/flytestdlib/promutils"
"k8s.io/apimachinery/pkg/util/rand"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flytestdlib/logger"
"github.com/lyft/flytestdlib/storage"
"github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/lyft/flytepropeller/pkg/compiler/transformers/k8s"
"github.com/lyft/flytepropeller/pkg/controller/nodes/errors"
"github.com/lyft/flytepropeller/pkg/controller/nodes/handler"
)
type dynamicNodeHandler struct {
handler.IFace
metrics metrics
simpleResolver common.SimpleOutputsResolver
store *storage.DataStore
nodeExecutor executors.Node
enQWorkflow v1alpha1.EnqueueWorkflow
}
type metrics struct {
buildDynamicWorkflow labeled.StopWatch
retrieveDynamicJobSpec labeled.StopWatch
}
func newMetrics(scope promutils.Scope) metrics {
return metrics{
buildDynamicWorkflow: labeled.NewStopWatch("build_dynamic_workflow", "Overhead for building a dynamic workflow in memory.", time.Microsecond, scope),
retrieveDynamicJobSpec: labeled.NewStopWatch("retrieve_dynamic_spec", "Overhead of downloading and unmarshaling dynamic job spec", time.Microsecond, scope),
}
}
func (e dynamicNodeHandler) ExtractOutput(ctx context.Context, w v1alpha1.ExecutableWorkflow, n v1alpha1.ExecutableNode,
bindToVar handler.VarName) (values *core.Literal, err error) {
outputResolver, casted := e.IFace.(handler.OutputResolver)
if !casted {
return e.simpleResolver.ExtractOutput(ctx, w, n, bindToVar)
}
return outputResolver.ExtractOutput(ctx, w, n, bindToVar)
}
func (e dynamicNodeHandler) getDynamicJobSpec(ctx context.Context, node v1alpha1.ExecutableNode, nodeStatus v1alpha1.ExecutableNodeStatus) (*core.DynamicJobSpec, error) {
t := e.metrics.retrieveDynamicJobSpec.Start(ctx)
defer t.Stop()
futuresFilePath, err := e.store.ConstructReference(ctx, nodeStatus.GetDataDir(), v1alpha1.GetFutureFile())
if err != nil {
logger.Warnf(ctx, "Failed to construct data path for futures file. Error: %v", err)
return nil, err
}
// If no futures file produced, then declare success and return.
if metadata, err := e.store.Head(ctx, futuresFilePath); err != nil {
logger.Warnf(ctx, "Failed to read futures file. Error: %v", err)
return nil, errors.Wrapf(errors.CausedByError, node.GetID(), err, "Failed to do HEAD on futures file.")
} else if !metadata.Exists() {
return nil, nil
}
djSpec := &core.DynamicJobSpec{}
if err := e.store.ReadProtobuf(ctx, futuresFilePath, djSpec); err != nil {
logger.Warnf(ctx, "Failed to read futures file. Error: %v", err)
return nil, errors.Wrapf(errors.CausedByError, node.GetID(), err, "Failed to read futures protobuf file.")
}
return djSpec, nil
}
func (e dynamicNodeHandler) buildDynamicWorkflowTemplate(ctx context.Context, djSpec *core.DynamicJobSpec,
w v1alpha1.ExecutableWorkflow, node v1alpha1.ExecutableNode, nodeStatus v1alpha1.ExecutableNodeStatus) (
*core.WorkflowTemplate, error) {
iface, err := underlyingInterface(w, node)
if err != nil {
return nil, err
}
// Modify node IDs to include lineage, the entire system assumes node IDs are unique per parent WF.
// We keep track of the original node ids because that's where inputs are written to.
parentNodeID := node.GetID()
for _, n := range djSpec.Nodes {
newID, err := hierarchicalNodeID(parentNodeID, n.Id)
if err != nil {
return nil, err
}
// Instantiate a nodeStatus using the modified name but set its data directory using the original name.
subNodeStatus := nodeStatus.GetNodeExecutionStatus(newID)
originalNodePath, err := e.store.ConstructReference(ctx, nodeStatus.GetDataDir(), n.Id)
if err != nil {
return nil, err
}
subNodeStatus.SetDataDir(originalNodePath)
subNodeStatus.ResetDirty()
n.Id = newID
}
if node.GetTaskID() != nil {
// If the parent is a task, pass down data children nodes should inherit.
parentTask, err := w.GetTask(*node.GetTaskID())
if err != nil {
return nil, errors.Wrapf(errors.CausedByError, node.GetID(), err, "Failed to find task [%v].", node.GetTaskID())
}
for _, t := range djSpec.Tasks {
if t.GetContainer() != nil && parentTask.CoreTask().GetContainer() != nil {
t.GetContainer().Config = append(t.GetContainer().Config, parentTask.CoreTask().GetContainer().Config...)
}
}
}
for _, o := range djSpec.Outputs {
err = updateBindingNodeIDsWithLineage(parentNodeID, o.Binding)
if err != nil {
return nil, err
}
}
return &core.WorkflowTemplate{
Id: &core.Identifier{
Project: w.GetExecutionID().Project,
Domain: w.GetExecutionID().Domain,
Version: rand.String(10),
Name: rand.String(10),
ResourceType: core.ResourceType_WORKFLOW,
},
Nodes: djSpec.Nodes,
Outputs: djSpec.Outputs,
Interface: iface,
}, nil
}
// For any node that is not in a NEW/READY state in the recording, CheckNodeStatus will be invoked. The implementation should handle
// idempotency and return the current observed state of the node
func (e dynamicNodeHandler) CheckNodeStatus(ctx context.Context, w v1alpha1.ExecutableWorkflow, node v1alpha1.ExecutableNode,
previousNodeStatus v1alpha1.ExecutableNodeStatus) (handler.Status, error) {
var status handler.Status
var err error
switch previousNodeStatus.GetOrCreateDynamicNodeStatus().GetDynamicNodePhase() {
case v1alpha1.DynamicNodePhaseExecuting:
// If the node succeeded, check if it generated a futures.pb file to execute.
dynamicWF, nStatus, _, err := e.buildContextualDynamicWorkflow(ctx, w, node, previousNodeStatus)
if err != nil {
return handler.StatusFailed(err), nil
}
s, err := e.progressDynamicWorkflow(ctx, nStatus, dynamicWF)
if err == nil && s == handler.StatusSuccess {
// After the dynamic node completes we need to copy the outputs from its end nodes to the parent nodes status
endNode := dynamicWF.GetNodeExecutionStatus(v1alpha1.EndNodeID)
outputPath := v1alpha1.GetOutputsFile(endNode.GetDataDir())
destinationPath := v1alpha1.GetOutputsFile(previousNodeStatus.GetDataDir())
logger.Infof(ctx, "Dynamic workflow completed, copying outputs from the end-node [%s] to the parent node data dir [%s]", outputPath, destinationPath)
if err := e.store.CopyRaw(ctx, outputPath, destinationPath, storage.Options{}); err != nil {
logger.Errorf(ctx, "Failed to copy outputs from dynamic sub-wf [%s] to [%s]. Error: %s", outputPath, destinationPath, err.Error())
return handler.StatusUndefined, errors.Wrapf(errors.StorageError, node.GetID(), err, "Failed to copy outputs from dynamic sub-wf [%s] to [%s]. Error: %s", outputPath, destinationPath, err.Error())
}
if successHandler, ok := e.IFace.(handler.PostNodeSuccessHandler); ok {
return successHandler.HandleNodeSuccess(ctx, w, node)
}
logger.Warnf(ctx, "Bad configuration for dynamic node, no post node success handler found!")
}
return s, err
default:
// Invoke the underlying check node status.
status, err = e.IFace.CheckNodeStatus(ctx, w, node, previousNodeStatus)
if err != nil {
return status, err
}
if status.Phase != handler.PhaseSuccess {
return status, err
}
// If the node succeeded, check if it generated a futures.pb file to execute.
_, _, isDynamic, err := e.buildContextualDynamicWorkflow(ctx, w, node, previousNodeStatus)
if err != nil {
return handler.StatusFailed(err), nil
}
if !isDynamic {
if successHandler, ok := e.IFace.(handler.PostNodeSuccessHandler); ok {
return successHandler.HandleNodeSuccess(ctx, w, node)
}
logger.Warnf(ctx, "Bad configuration for dynamic node, no post node success handler found!")
return status, err
}
// Mark the node as a dynamic node executing its child nodes. Next time check node status is called, it'll go
// directly to progress the dynamically generated workflow.
previousNodeStatus.GetOrCreateDynamicNodeStatus().SetDynamicNodePhase(v1alpha1.DynamicNodePhaseExecuting)
return handler.StatusRunning, nil
}
}
func (e dynamicNodeHandler) buildContextualDynamicWorkflow(ctx context.Context, w v1alpha1.ExecutableWorkflow, node v1alpha1.ExecutableNode,
previousNodeStatus v1alpha1.ExecutableNodeStatus) (dynamicWf v1alpha1.ExecutableWorkflow, status v1alpha1.ExecutableNodeStatus, isDynamic bool, err error) {
t := e.metrics.buildDynamicWorkflow.Start(ctx)
defer t.Stop()
var nStatus v1alpha1.ExecutableNodeStatus
// We will only get here if the Phase is success. The downside is that this is an overhead for all nodes that are
// not dynamic. But given that we will only check once, it should be ok.
// TODO: Check for node.is_dynamic once the IDL changes are in and SDK migration has happened.
djSpec, err := e.getDynamicJobSpec(ctx, node, previousNodeStatus)
if err != nil {
return nil, nil, false, err
}
if djSpec == nil {
return nil, status, false, nil
}
rootNodeStatus := w.GetNodeExecutionStatus(node.GetID())
if node.GetTaskID() != nil {
// TODO: This is a hack to set parent task execution id, we should move to node-node relationship.
execID, err := e.getTaskExecutionIdentifier(ctx, w, node)
if err != nil {
return nil, nil, false, err
}
dynamicNode := &v1alpha1.NodeSpec{
ID: "dynamic-node",
}
nStatus = rootNodeStatus.GetNodeExecutionStatus(dynamicNode.GetID())
nStatus.SetDataDir(rootNodeStatus.GetDataDir())
nStatus.SetParentTaskID(execID)
} else {
nStatus = w.GetNodeExecutionStatus(node.GetID())
}
var closure *core.CompiledWorkflowClosure
wf, err := e.buildDynamicWorkflowTemplate(ctx, djSpec, w, node, nStatus)
if err != nil {
return nil, nil, true, err
}
compiledTasks, err := compileTasks(ctx, djSpec.Tasks)
if err != nil {
return nil, nil, true, err
}
// TODO: This will currently fail if the WF references any launch plans
closure, err = compiler.CompileWorkflow(wf, djSpec.Subworkflows, compiledTasks, []common2.InterfaceProvider{})
if err != nil {
return nil, nil, true, err
}
subwf, err := k8s.BuildFlyteWorkflow(closure, nil, nil, "")
if err != nil {
return nil, nil, true, err
}
return newContextualWorkflow(w, subwf, nStatus, subwf.Tasks, subwf.SubWorkflows), nStatus, true, nil
}
func (e dynamicNodeHandler) progressDynamicWorkflow(ctx context.Context, parentNodeStatus v1alpha1.ExecutableNodeStatus,
w v1alpha1.ExecutableWorkflow) (handler.Status, error) {
state, err := e.nodeExecutor.RecursiveNodeHandler(ctx, w, w.StartNode())
if err != nil {
return handler.StatusUndefined, err
}
if state.HasFailed() {
if w.GetOnFailureNode() != nil {
return handler.StatusFailing(state.Err), nil
}
return handler.StatusFailed(state.Err), nil
}
if state.IsComplete() {
nodeID := ""
if parentNodeStatus.GetParentNodeID() != nil {
nodeID = *parentNodeStatus.GetParentNodeID()
}
// If the WF interface has outputs, validate that the outputs file was written.
if outputBindings := w.GetOutputBindings(); len(outputBindings) > 0 {
endNodeStatus := w.GetNodeExecutionStatus(v1alpha1.EndNodeID)
if endNodeStatus == nil {
return handler.StatusFailed(errors.Errorf(errors.SubWorkflowExecutionFailed, nodeID,
"No end node found in subworkflow.")), nil
}
sourcePath := v1alpha1.GetOutputsFile(endNodeStatus.GetDataDir())
if metadata, err := e.store.Head(ctx, sourcePath); err == nil {
if !metadata.Exists() {
return handler.StatusFailed(errors.Errorf(errors.SubWorkflowExecutionFailed, nodeID,
"Subworkflow is expected to produce outputs but no outputs file was written to %v.",
sourcePath)), nil
}
} else {
return handler.StatusUndefined, err
}
destinationPath := v1alpha1.GetOutputsFile(parentNodeStatus.GetDataDir())
if err := e.store.CopyRaw(ctx, sourcePath, destinationPath, storage.Options{}); err != nil {
return handler.StatusFailed(errors.Wrapf(errors.OutputsNotFoundError, nodeID,
err, "Failed to copy subworkflow outputs from [%v] to [%v]",
sourcePath, destinationPath)), nil
}
}
return handler.StatusSuccess, nil
}
if state.PartiallyComplete() {
// Re-enqueue the workflow
e.enQWorkflow(w.GetK8sWorkflowID().String())
}
return handler.StatusRunning, nil
}
func (e dynamicNodeHandler) getTaskExecutionIdentifier(_ context.Context, w v1alpha1.ExecutableWorkflow,
node v1alpha1.ExecutableNode) (*core.TaskExecutionIdentifier, error) {
taskID := node.GetTaskID()
task, err := w.GetTask(*taskID)
if err != nil {
return nil, errors.Wrapf(errors.BadSpecificationError, node.GetID(), err, "Unable to find task for taskId: [%v]", *taskID)
}
nodeStatus := w.GetNodeExecutionStatus(node.GetID())
return &core.TaskExecutionIdentifier{
TaskId: task.CoreTask().Id,
RetryAttempt: nodeStatus.GetAttempts(),
NodeExecutionId: &core.NodeExecutionIdentifier{
NodeId: node.GetID(),
ExecutionId: w.GetExecutionID().WorkflowExecutionIdentifier,
},
}, nil
}
func (e dynamicNodeHandler) AbortNode(ctx context.Context, w v1alpha1.ExecutableWorkflow, node v1alpha1.ExecutableNode) error {
previousNodeStatus := w.GetNodeExecutionStatus(node.GetID())
switch previousNodeStatus.GetOrCreateDynamicNodeStatus().GetDynamicNodePhase() {
case v1alpha1.DynamicNodePhaseExecuting:
dynamicWF, _, isDynamic, err := e.buildContextualDynamicWorkflow(ctx, w, node, previousNodeStatus)
if err != nil {
return err
}
if !isDynamic {
return nil
}
return e.nodeExecutor.AbortHandler(ctx, dynamicWF, dynamicWF.StartNode())
default:
// Invoke the underlying abort node.
return e.IFace.AbortNode(ctx, w, node)
}
}
func New(underlying handler.IFace, nodeExecutor executors.Node, enQWorkflow v1alpha1.EnqueueWorkflow, store *storage.DataStore,
scope promutils.Scope) handler.IFace {
return dynamicNodeHandler{
IFace: underlying,
metrics: newMetrics(scope),
nodeExecutor: nodeExecutor,
enQWorkflow: enQWorkflow,
store: store,
}
}