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
/
node_exec_context.go
253 lines (211 loc) · 7.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
package nodes
import (
"context"
"fmt"
"strconv"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/storage"
"k8s.io/apimachinery/pkg/types"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/flyteorg/flytepropeller/events"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/flyteorg/flytepropeller/pkg/controller/executors"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/handler"
"github.com/flyteorg/flytepropeller/pkg/utils"
)
const NodeIDLabel = "node-id"
const TaskNameLabel = "task-name"
const NodeInterruptibleLabel = "interruptible"
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 handler.TaskReader
md handler.NodeExecutionMetadata
er events.TaskEventRecorder
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() handler.TaskReader {
return e.tr
}
func (e nodeExecContext) NodeStateReader() handler.NodeStateReader {
return e.nsm
}
func (e nodeExecContext) NodeStateWriter() handler.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() events.TaskEventRecorder {
return e.er
}
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() handler.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, er events.TaskEventRecorder, tr handler.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,
er: er,
maxDatasetSizeBytes: maxDatasetSize,
tr: tr,
nsm: nsm,
enqueueOwner: enqueueOwner,
rawOutputPrefix: rawOutputPrefix,
shardSelector: outputShardSelector,
nl: nl,
ic: execContext,
}
}
func (c *nodeExecutor) newNodeExecContextDefault(ctx context.Context, currentNodeID v1alpha1.NodeID,
executionContext executors.ExecutionContext, nl executors.NodeLookup) (*nodeExecContext, 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 handler.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,
&taskEventRecorder{TaskEventRecorder: c.taskRecorder},
tr,
newNodeStateManager(ctx, s),
workflowEnqueuer,
rawOutputPrefix,
c.shardSelector,
), nil
}