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
403 lines (336 loc) · 15.1 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
392
393
394
395
396
397
398
399
400
401
402
403
package dynamic
import (
"context"
"fmt"
"time"
"github.com/Masterminds/semver"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/catalog"
pluginCore "github.com/lyft/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/lyft/flytestdlib/logger"
"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/task"
"github.com/lyft/flytestdlib/promutils"
"k8s.io/apimachinery/pkg/util/rand"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"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"
)
//go:generate mockery -all -case=underscore
const dynamicNodeID = "dynamic-node"
// TODO: Remove after deploying version 0.2.4
var arrayFlyteKitVersion = semver.MustParse("0.2.3")
type TaskNodeHandler interface {
handler.Node
ValidateOutputAndCacheAdd(ctx context.Context, nodeID v1alpha1.NodeID, i io.InputReader, r io.OutputReader, outputCommitter io.OutputWriter,
tr pluginCore.TaskReader, m catalog.Metadata) (*io.ExecutionError, error)
}
type metrics struct {
buildDynamicWorkflow labeled.StopWatch
retrieveDynamicJobSpec labeled.StopWatch
CacheHit labeled.StopWatch
CacheError labeled.Counter
}
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 un-marshaling dynamic job spec", time.Microsecond, scope),
CacheHit: labeled.NewStopWatch("dynamic_workflow_cache_hit", "A dynamic workflow was loaded from store.", time.Microsecond, scope),
CacheError: labeled.NewCounter("cache_err", "A dynamic workflow failed to store or load from data store.", scope),
}
}
type dynamicNodeTaskNodeHandler struct {
TaskNodeHandler
metrics metrics
nodeExecutor executors.Node
}
func (d dynamicNodeTaskNodeHandler) handleParentNode(ctx context.Context, prevState handler.DynamicNodeState, nCtx handler.NodeExecutionContext) (handler.Transition, handler.DynamicNodeState, error) {
// It seems parent node is still running, lets call handle for parent node
trns, err := d.TaskNodeHandler.Handle(ctx, nCtx)
if err != nil {
return trns, prevState, err
}
if trns.Info().GetPhase() == handler.EPhaseSuccess {
f, err := task.NewRemoteFutureFileReader(ctx, nCtx.NodeStatus().GetDataDir(), nCtx.DataStore())
if err != nil {
return handler.UnknownTransition, prevState, err
}
// If the node succeeded, check if it generated a futures.pb file to execute.
ok, err := f.Exists(ctx)
if err != nil {
return handler.UnknownTransition, prevState, err
}
if ok {
// Mark the node that parent node has completed and a dynamic node executing its child nodes. Next time check node status is called, it'll go
// directly to progress the dynamically generated workflow.
logger.Infof(ctx, "future file detected, assuming dynamic node")
// There is a futures file, so we need to continue running the node with the modified state
return trns.WithInfo(handler.PhaseInfoRunning(trns.Info().GetInfo())), handler.DynamicNodeState{Phase: v1alpha1.DynamicNodePhaseExecuting}, nil
}
}
logger.Infof(ctx, "regular node detected, (no future file found)")
return trns, prevState, nil
}
func (d dynamicNodeTaskNodeHandler) handleDynamicSubNodes(ctx context.Context, nCtx handler.NodeExecutionContext) (handler.Transition, error) {
dynamicWF, _, err := d.buildContextualDynamicWorkflow(ctx, nCtx)
if err != nil {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure("DynamicWorkflowBuildFailed", err.Error(), nil)), nil
}
trns, err := d.progressDynamicWorkflow(ctx, dynamicWF, nCtx)
if err != nil {
return handler.UnknownTransition, err
}
if trns.Info().GetPhase() == handler.EPhaseSuccess {
logger.Infof(ctx, "dynamic workflow node has succeeded, will call on success handler for parent node [%s]", nCtx.NodeID())
outputPaths := ioutils.NewRemoteFileOutputPaths(ctx, nCtx.DataStore(), nCtx.NodeStatus().GetDataDir())
execID := task.GetTaskExecutionIdentifier(nCtx)
outputReader := ioutils.NewRemoteFileOutputReader(ctx, nCtx.DataStore(), outputPaths, nCtx.MaxDatasetSizeBytes())
ee, err := d.TaskNodeHandler.ValidateOutputAndCacheAdd(ctx, nCtx.NodeID(), nCtx.InputReader(), outputReader, nil, nCtx.TaskReader(), catalog.Metadata{
TaskExecutionIdentifier: execID,
})
if err != nil {
return handler.UnknownTransition, err
}
if ee != nil {
if ee.IsRecoverable {
return trns.WithInfo(handler.PhaseInfoRetryableFailureErr(ee.ExecutionError, trns.Info().GetInfo())), nil
}
return trns.WithInfo(handler.PhaseInfoFailureErr(ee.ExecutionError, trns.Info().GetInfo())), nil
}
}
return trns, nil
}
func (d dynamicNodeTaskNodeHandler) Handle(ctx context.Context, nCtx handler.NodeExecutionContext) (handler.Transition, error) {
ds := nCtx.NodeStateReader().GetDynamicNodeState()
var err error
var trns handler.Transition
newState := ds
if ds.Phase == v1alpha1.DynamicNodePhaseExecuting {
trns, err = d.handleDynamicSubNodes(ctx, nCtx)
if err != nil {
logger.Errorf(ctx, "handling dynamic subnodes failed with error: %s", err.Error())
return trns, err
}
} else {
trns, newState, err = d.handleParentNode(ctx, ds, nCtx)
if err != nil {
logger.Errorf(ctx, "handling parent node failed with error: %s", err.Error())
return trns, err
}
}
if err := nCtx.NodeStateWriter().PutDynamicNodeState(newState); err != nil {
return handler.UnknownTransition, err
}
return trns, nil
}
func (d dynamicNodeTaskNodeHandler) Abort(ctx context.Context, nCtx handler.NodeExecutionContext, reason string) error {
ds := nCtx.NodeStateReader().GetDynamicNodeState()
switch ds.Phase {
case v1alpha1.DynamicNodePhaseExecuting:
dynamicWF, isDynamic, err := d.buildContextualDynamicWorkflow(ctx, nCtx)
if err != nil {
return err
}
if !isDynamic {
return nil
}
return d.nodeExecutor.AbortHandler(ctx, dynamicWF, dynamicWF.StartNode(), reason)
default:
// The parent node has not yet completed, so we will abort the parent node
return d.TaskNodeHandler.Abort(ctx, nCtx, reason)
}
}
// This is a weird method. We should always finalize before we set the dynamic parent node phase as complete?
// TODO we are finalizing the parent node only after sub tasks are completed
func (d dynamicNodeTaskNodeHandler) Finalize(ctx context.Context, nCtx handler.NodeExecutionContext) error {
return d.TaskNodeHandler.Finalize(ctx, nCtx)
}
func (d dynamicNodeTaskNodeHandler) buildDynamicWorkflowTemplate(ctx context.Context, djSpec *core.DynamicJobSpec, nCtx handler.NodeExecutionContext, parentNodeStatus v1alpha1.ExecutableNodeStatus) (
*core.WorkflowTemplate, error) {
iface, err := underlyingInterface(ctx, nCtx.TaskReader())
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 := nCtx.NodeID()
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 := parentNodeStatus.GetNodeExecutionStatus(newID)
originalNodePath, err := nCtx.DataStore().ConstructReference(ctx, nCtx.NodeStatus().GetDataDir(), n.Id)
if err != nil {
return nil, err
}
subNodeStatus.SetDataDir(originalNodePath)
subNodeStatus.ResetDirty()
n.Id = newID
}
if nCtx.TaskReader().GetTaskID() != nil {
// If the parent is a task, pass down data children nodes should inherit.
parentTask, err := nCtx.TaskReader().Read(ctx)
if err != nil {
return nil, errors.Wrapf(errors.CausedByError, nCtx.NodeID(), err, "Failed to find task [%v].", nCtx.TaskReader().GetTaskID())
}
for _, t := range djSpec.Tasks {
if t.GetContainer() != nil && parentTask.GetContainer() != nil {
t.GetContainer().Config = append(t.GetContainer().Config, parentTask.GetContainer().Config...)
}
// TODO: This is a hack since array tasks' interfaces are malformed. Remove after
// FlyteKit version that generates the right interfaces is deployed.
if t.Type == "container_array" {
isBelow, err := isFlyteKitVersionBelow(t.GetMetadata().Runtime, arrayFlyteKitVersion)
if err != nil {
logger.Warnf(ctx, "Failed to validate flytekit version of task [%v] will skip array"+
" interface manipulation.", t.GetId())
continue
}
if isBelow {
iface := t.GetInterface()
iface.Outputs = makeArrayInterface(iface.Outputs)
}
}
}
}
for _, o := range djSpec.Outputs {
err = updateBindingNodeIDsWithLineage(parentNodeID, o.Binding)
if err != nil {
return nil, err
}
}
return &core.WorkflowTemplate{
Id: &core.Identifier{
Project: nCtx.NodeExecutionMetadata().GetExecutionID().Project,
Domain: nCtx.NodeExecutionMetadata().GetExecutionID().Domain,
Version: rand.String(10),
Name: rand.String(10),
ResourceType: core.ResourceType_WORKFLOW,
},
Nodes: djSpec.Nodes,
Outputs: djSpec.Outputs,
Interface: iface,
}, nil
}
func (d dynamicNodeTaskNodeHandler) buildContextualDynamicWorkflow(ctx context.Context, nCtx handler.NodeExecutionContext) (dynamicWf v1alpha1.ExecutableWorkflow, isDynamic bool, err error) {
t := d.metrics.buildDynamicWorkflow.Start(ctx)
defer t.Stop()
f, err := task.NewRemoteFutureFileReader(ctx, nCtx.NodeStatus().GetDataDir(), nCtx.DataStore())
if err != nil {
return nil, false, err
}
// TODO: This is a hack to set parent task execution id, we should move to node-node relationship.
execID := task.GetTaskExecutionIdentifier(nCtx)
nStatus := nCtx.NodeStatus().GetNodeExecutionStatus(dynamicNodeID)
nStatus.SetDataDir(nCtx.NodeStatus().GetDataDir())
nStatus.SetParentTaskID(execID)
// cacheHitStopWatch := d.metrics.CacheHit.Start(ctx)
// Check if we have compiled the workflow before:
// If there is a cached compiled Workflow, load and return it.
// if ok, err := f.CacheExists(ctx); err != nil {
// logger.Warnf(ctx, "Failed to call head on compiled futures file. Error: %v", err)
// return nil, false, errors.Wrapf(errors.CausedByError, nCtx.NodeID(), err, "Failed to do HEAD on compiled futures file.")
// } else if ok {
// // It exists, load and return it
// compiledWf, err := f.RetrieveCache(ctx)
// if err != nil {
// logger.Warnf(ctx, "Failed to load cached flyte workflow , this will cause the dynamic workflow to be recompiled. Error: %v", err)
// d.metrics.CacheError.Inc(ctx)
// } else {
// cacheHitStopWatch.Stop()
// return newContextualWorkflow(nCtx.Workflow(), compiledWf, nStatus, compiledWf.Tasks, compiledWf.SubWorkflows), true, nil
// }
// }
// We know for sure that futures file was generated. Lets read it
djSpec, err := f.Read(ctx)
if err != nil {
return nil, false, errors.Wrapf(errors.RuntimeExecutionError, nCtx.NodeID(), err, "unable to read futures file, maybe corrupted")
}
var closure *core.CompiledWorkflowClosure
wf, err := d.buildDynamicWorkflowTemplate(ctx, djSpec, nCtx, nStatus)
if err != nil {
return nil, true, err
}
compiledTasks, err := compileTasks(ctx, djSpec.Tasks)
if err != nil {
return 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, true, err
}
subwf, err := k8s.BuildFlyteWorkflow(closure, &core.LiteralMap{}, nil, "")
if err != nil {
return nil, true, err
}
if err := f.Cache(ctx, subwf); err != nil {
logger.Errorf(ctx, "Failed to cache Dynamic workflow [%s]", err.Error())
}
return newContextualWorkflow(nCtx.Workflow(), subwf, nStatus, subwf.Tasks, subwf.SubWorkflows), true, nil
}
func (d dynamicNodeTaskNodeHandler) progressDynamicWorkflow(ctx context.Context, dynamicWorkflow v1alpha1.ExecutableWorkflow,
nCtx handler.NodeExecutionContext) (handler.Transition, error) {
state, err := d.nodeExecutor.RecursiveNodeHandler(ctx, dynamicWorkflow, dynamicWorkflow.StartNode())
if err != nil {
return handler.UnknownTransition, err
}
if state.HasFailed() {
if dynamicWorkflow.GetOnFailureNode() != nil {
// TODO Once we migrate to closure node we need to handle subworkflow using the subworkflow handler
logger.Errorf(ctx, "We do not support failure nodes in dynamic workflow today")
}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure("DynamicWorkflowFailure", state.Err.Error(), nil)), err
}
if state.IsComplete() {
var o *handler.OutputInfo
// If the WF interface has outputs, validate that the outputs file was written.
if outputBindings := dynamicWorkflow.GetOutputBindings(); len(outputBindings) > 0 {
endNodeStatus := dynamicWorkflow.GetNodeExecutionStatus(v1alpha1.EndNodeID)
if endNodeStatus == nil {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure("MalformedDynamicWorkflow", "no end-node found in dynamic workflow", nil)), nil
}
sourcePath := v1alpha1.GetOutputsFile(endNodeStatus.GetDataDir())
if metadata, err := nCtx.DataStore().Head(ctx, sourcePath); err == nil {
if !metadata.Exists() {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure("DynamicWorkflowOutputsNotFound", " is expected to produce outputs but no outputs file was written to %v.", nil)), nil
}
} else {
return handler.UnknownTransition, err
}
destinationPath := v1alpha1.GetOutputsFile(nCtx.NodeStatus().GetDataDir())
if err := nCtx.DataStore().CopyRaw(ctx, sourcePath, destinationPath, storage.Options{}); err != nil {
return handler.DoTransition(handler.TransitionTypeEphemeral,
handler.PhaseInfoFailure(errors.OutputsNotFoundError.String(),
fmt.Sprintf("Failed to copy subworkflow outputs from [%v] to [%v]", sourcePath, destinationPath), nil),
), nil
}
o = &handler.OutputInfo{OutputURI: destinationPath}
}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoSuccess(&handler.ExecutionInfo{
OutputInfo: o,
})), nil
}
if state.PartiallyComplete() {
if err := nCtx.EnqueueOwnerFunc()(); err != nil {
return handler.UnknownTransition, err
}
}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoRunning(nil)), nil
}
func New(underlying TaskNodeHandler, nodeExecutor executors.Node, scope promutils.Scope) handler.Node {
return &dynamicNodeTaskNodeHandler{
TaskNodeHandler: underlying,
metrics: newMetrics(scope),
nodeExecutor: nodeExecutor,
}
}