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
/
node_exec_context.go
324 lines (273 loc) · 10.3 KB
/
node_exec_context.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
package nodes
import (
"context"
"fmt"
"strconv"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
"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"
nodeerrors "github.com/flyteorg/flytepropeller/pkg/controller/nodes/errors"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/interfaces"
"github.com/flyteorg/flytepropeller/pkg/utils"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
)
const NodeIDLabel = "node-id"
const TaskNameLabel = "task-name"
const NodeInterruptibleLabel = "interruptible"
type eventRecorder struct {
taskEventRecorder events.TaskEventRecorder
nodeEventRecorder events.NodeEventRecorder
}
func (e eventRecorder) RecordTaskEvent(ctx context.Context, ev *event.TaskExecutionEvent, eventConfig *config.EventConfig) error {
if err := e.taskEventRecorder.RecordTaskEvent(ctx, ev, eventConfig); err != nil {
if eventsErr.IsAlreadyExists(err) {
logger.Warningf(ctx, "Failed to record taskEvent, error [%s]. Trying to record state: %s. Ignoring this error!", err.Error(), ev.Phase)
return nil
} else if eventsErr.IsEventAlreadyInTerminalStateError(err) {
if IsTerminalTaskPhase(ev.Phase) {
// Event is terminal and the stored value in flyteadmin is already terminal. This implies aborted case. So ignoring
logger.Warningf(ctx, "Failed to record taskEvent, error [%s]. Trying to record state: %s. Ignoring this error!", err.Error(), ev.Phase)
return nil
}
logger.Warningf(ctx, "Failed to record taskEvent in state: %s, error: %s", ev.Phase, err)
return errors.Wrapf(err, "failed to record task event, as it already exists in terminal state. Event state: %s", ev.Phase)
}
return err
}
return nil
}
func (e eventRecorder) RecordNodeEvent(ctx context.Context, nodeEvent *event.NodeExecutionEvent, eventConfig *config.EventConfig) 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 := e.nodeEventRecorder.RecordNodeEvent(ctx, nodeEvent, 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 nodeerrors.Wrapf(nodeerrors.IllegalStateError, nodeEvent.Id.NodeId, err, "phase mis-match mismatch between propeller and control plane; Trying to record Node p: %s", nodeEvent.Phase)
}
}
return err
}
type nodeExecMetadata struct {
v1alpha1.Meta
nodeExecID *core.NodeExecutionIdentifier
interruptible bool
interruptibleFailureThreshold uint32
nodeLabels map[string]string
}
func (e nodeExecMetadata) GetNodeExecutionID() *core.NodeExecutionIdentifier {
return e.nodeExecID
}
func (e nodeExecMetadata) GetK8sServiceAccount() string {
return e.Meta.GetServiceAccountName()
}
func (e nodeExecMetadata) GetOwnerID() types.NamespacedName {
return types.NamespacedName{Name: e.GetName(), Namespace: e.GetNamespace()}
}
func (e nodeExecMetadata) IsInterruptible() bool {
return e.interruptible
}
func (e nodeExecMetadata) GetInterruptibleFailureThreshold() uint32 {
return e.interruptibleFailureThreshold
}
func (e nodeExecMetadata) GetLabels() map[string]string {
return e.nodeLabels
}
type nodeExecContext struct {
store *storage.DataStore
tr interfaces.TaskReader
md interfaces.NodeExecutionMetadata
eventRecorder interfaces.EventRecorder
inputs io.InputReader
node v1alpha1.ExecutableNode
nodeStatus v1alpha1.ExecutableNodeStatus
maxDatasetSizeBytes int64
nsm *nodeStateManager
enqueueOwner func() error
rawOutputPrefix storage.DataReference
shardSelector ioutils.ShardSelector
nl executors.NodeLookup
ic executors.ExecutionContext
}
func (e nodeExecContext) ExecutionContext() executors.ExecutionContext {
return e.ic
}
func (e nodeExecContext) ContextualNodeLookup() executors.NodeLookup {
return e.nl
}
func (e nodeExecContext) OutputShardSelector() ioutils.ShardSelector {
return e.shardSelector
}
func (e nodeExecContext) RawOutputPrefix() storage.DataReference {
return e.rawOutputPrefix
}
func (e nodeExecContext) EnqueueOwnerFunc() func() error {
return e.enqueueOwner
}
func (e nodeExecContext) TaskReader() interfaces.TaskReader {
return e.tr
}
func (e nodeExecContext) NodeStateReader() interfaces.NodeStateReader {
return e.nsm
}
func (e nodeExecContext) NodeStateWriter() interfaces.NodeStateWriter {
return e.nsm
}
func (e nodeExecContext) DataStore() *storage.DataStore {
return e.store
}
func (e nodeExecContext) InputReader() io.InputReader {
return e.inputs
}
func (e nodeExecContext) EventsRecorder() interfaces.EventRecorder {
return e.eventRecorder
}
func (e nodeExecContext) NodeID() v1alpha1.NodeID {
return e.node.GetID()
}
func (e nodeExecContext) Node() v1alpha1.ExecutableNode {
return e.node
}
func (e nodeExecContext) CurrentAttempt() uint32 {
return e.nodeStatus.GetAttempts()
}
func (e nodeExecContext) NodeStatus() v1alpha1.ExecutableNodeStatus {
return e.nodeStatus
}
func (e nodeExecContext) NodeExecutionMetadata() interfaces.NodeExecutionMetadata {
return e.md
}
func (e nodeExecContext) MaxDatasetSizeBytes() int64 {
return e.maxDatasetSizeBytes
}
func newNodeExecContext(_ context.Context, store *storage.DataStore, execContext executors.ExecutionContext, nl executors.NodeLookup,
node v1alpha1.ExecutableNode, nodeStatus v1alpha1.ExecutableNodeStatus, inputs io.InputReader, interruptible bool, interruptibleFailureThreshold uint32,
maxDatasetSize int64, taskEventRecorder events.TaskEventRecorder, nodeEventRecorder events.NodeEventRecorder, tr interfaces.TaskReader, nsm *nodeStateManager,
enqueueOwner func() error, rawOutputPrefix storage.DataReference, outputShardSelector ioutils.ShardSelector) *nodeExecContext {
md := nodeExecMetadata{
Meta: execContext,
nodeExecID: &core.NodeExecutionIdentifier{
NodeId: node.GetID(),
ExecutionId: execContext.GetExecutionID().WorkflowExecutionIdentifier,
},
interruptible: interruptible,
interruptibleFailureThreshold: interruptibleFailureThreshold,
}
// Copy the wf labels before adding node specific labels.
nodeLabels := make(map[string]string)
for k, v := range execContext.GetLabels() {
nodeLabels[k] = v
}
nodeLabels[NodeIDLabel] = utils.SanitizeLabelValue(node.GetID())
if tr != nil && tr.GetTaskID() != nil {
nodeLabels[TaskNameLabel] = utils.SanitizeLabelValue(tr.GetTaskID().Name)
}
nodeLabels[NodeInterruptibleLabel] = strconv.FormatBool(interruptible)
md.nodeLabels = nodeLabels
return &nodeExecContext{
md: md,
store: store,
node: node,
nodeStatus: nodeStatus,
inputs: inputs,
eventRecorder: &eventRecorder{
taskEventRecorder: taskEventRecorder,
nodeEventRecorder: nodeEventRecorder,
},
maxDatasetSizeBytes: maxDatasetSize,
tr: tr,
nsm: nsm,
enqueueOwner: enqueueOwner,
rawOutputPrefix: rawOutputPrefix,
shardSelector: outputShardSelector,
nl: nl,
ic: execContext,
}
}
func (c *nodeExecutor) BuildNodeExecutionContext(ctx context.Context, executionContext executors.ExecutionContext,
nl executors.NodeLookup, currentNodeID v1alpha1.NodeID) (interfaces.NodeExecutionContext, error) {
n, ok := nl.GetNode(currentNodeID)
if !ok {
return nil, fmt.Errorf("failed to find node with ID [%s] in execution [%s]", currentNodeID, executionContext.GetID())
}
var tr interfaces.TaskReader
if n.GetKind() == v1alpha1.NodeKindTask {
if n.GetTaskID() == nil {
return nil, fmt.Errorf("bad state, no task-id defined for node [%s]", n.GetID())
}
tk, err := executionContext.GetTask(*n.GetTaskID())
if err != nil {
return nil, err
}
tr = taskReader{TaskTemplate: tk.CoreTask()}
}
workflowEnqueuer := func() error {
c.enqueueWorkflow(executionContext.GetID())
return nil
}
interruptible := executionContext.IsInterruptible()
if n.IsInterruptible() != nil {
interruptible = *n.IsInterruptible()
}
s := nl.GetNodeExecutionStatus(ctx, currentNodeID)
// a node is not considered interruptible if the system failures have exceeded the configured threshold
if interruptible && s.GetSystemFailures() >= c.interruptibleFailureThreshold {
interruptible = false
c.metrics.InterruptedThresholdHit.Inc(ctx)
}
rawOutputPrefix := c.defaultDataSandbox
if executionContext.GetRawOutputDataConfig().RawOutputDataConfig != nil && len(executionContext.GetRawOutputDataConfig().OutputLocationPrefix) > 0 {
rawOutputPrefix = storage.DataReference(executionContext.GetRawOutputDataConfig().OutputLocationPrefix)
}
return newNodeExecContext(ctx, c.store, executionContext, nl, n, s,
ioutils.NewCachedInputReader(
ctx,
ioutils.NewRemoteFileInputReader(
ctx,
c.store,
ioutils.NewInputFilePaths(
ctx,
c.store,
s.GetDataDir(),
),
),
),
interruptible,
c.interruptibleFailureThreshold,
c.maxDatasetSizeBytes,
c.taskRecorder,
c.nodeRecorder,
tr,
newNodeStateManager(ctx, s),
workflowEnqueuer,
rawOutputPrefix,
c.shardSelector,
), nil
}