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
/
taskexec_context.go
300 lines (257 loc) · 10.1 KB
/
taskexec_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
package task
import (
"bytes"
"context"
"strconv"
"strings"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
pluginCatalog "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/catalog"
pluginCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/encoding"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"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/task/resourcemanager"
"github.com/flyteorg/flytepropeller/pkg/utils"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
var (
_ pluginCore.TaskExecutionContext = &taskExecutionContext{}
)
const IDMaxLength = 50
const DefaultMaxAttempts = 1
type taskExecutionID struct {
execName string
id *core.TaskExecutionIdentifier
}
func (te taskExecutionID) GetID() core.TaskExecutionIdentifier {
return *te.id
}
func (te taskExecutionID) GetGeneratedName() string {
return te.execName
}
func (te taskExecutionID) GetGeneratedNameWith(minLength, maxLength int) (string, error) {
origLength := len(te.execName)
if origLength < minLength {
return te.execName + strings.Repeat("0", minLength-origLength), nil
}
if origLength > maxLength {
return encoding.FixedLengthUniqueID(te.execName, maxLength)
}
return te.execName, nil
}
type taskExecutionMetadata struct {
handler.NodeExecutionMetadata
taskExecID taskExecutionID
o pluginCore.TaskOverrides
maxAttempts uint32
platformResources *v1.ResourceRequirements
}
func (t taskExecutionMetadata) GetTaskExecutionID() pluginCore.TaskExecutionID {
return t.taskExecID
}
func (t taskExecutionMetadata) GetOverrides() pluginCore.TaskOverrides {
return t.o
}
func (t taskExecutionMetadata) GetMaxAttempts() uint32 {
return t.maxAttempts
}
func (t taskExecutionMetadata) GetPlatformResources() *v1.ResourceRequirements {
return t.platformResources
}
type taskExecutionContext struct {
handler.NodeExecutionContext
tm taskExecutionMetadata
rm resourcemanager.TaskResourceManager
psm *pluginStateManager
tr pluginCore.TaskReader
ow *ioutils.BufferedOutputWriter
ber *bufferedEventRecorder
sm pluginCore.SecretManager
c pluginCatalog.AsyncClient
}
func (t *taskExecutionContext) TaskRefreshIndicator() pluginCore.SignalAsync {
return func(ctx context.Context) {
err := t.NodeExecutionContext.EnqueueOwnerFunc()
if err != nil {
logger.Errorf(ctx, "Failed to enqueue owner for Task [%v] and Owner [%v]. Error: %v",
t.TaskExecutionMetadata().GetTaskExecutionID(),
t.TaskExecutionMetadata().GetOwnerID(),
err)
}
}
}
func (t *taskExecutionContext) Catalog() pluginCatalog.AsyncClient {
return t.c
}
func (t taskExecutionContext) EventsRecorder() pluginCore.EventsRecorder {
return t.ber
}
func (t taskExecutionContext) ResourceManager() pluginCore.ResourceManager {
return t.rm
}
func (t taskExecutionContext) PluginStateReader() pluginCore.PluginStateReader {
return t.psm
}
func (t *taskExecutionContext) TaskReader() pluginCore.TaskReader {
return t.tr
}
func (t *taskExecutionContext) TaskExecutionMetadata() pluginCore.TaskExecutionMetadata {
return t.tm
}
func (t *taskExecutionContext) OutputWriter() io.OutputWriter {
return t.ow
}
func (t *taskExecutionContext) PluginStateWriter() pluginCore.PluginStateWriter {
return t.psm
}
func (t taskExecutionContext) SecretManager() pluginCore.SecretManager {
return t.sm
}
// Validates and assigns a single resource by examining the default requests and max limit with the static resource value
// defined by this task and node execution context.
func assignResource(resourceName v1.ResourceName, execConfigRequest, execConfigLimit resource.Quantity, requests, limits v1.ResourceList) {
maxLimit := execConfigLimit
request, ok := requests[resourceName]
if !ok {
// Requests aren't required so we glean it from the execution config value (when possible)
if !execConfigRequest.IsZero() {
request = execConfigRequest
}
} else {
if request.Cmp(maxLimit) == 1 && !maxLimit.IsZero() {
// Adjust the request downwards to not exceed the max limit if it's set.
request = maxLimit
}
}
limit, ok := limits[resourceName]
if !ok {
limit = request
} else {
if limit.Cmp(maxLimit) == 1 && !maxLimit.IsZero() {
// Adjust the limit downwards to not exceed the max limit if it's set.
limit = maxLimit
}
}
if request.Cmp(limit) == 1 {
// The limit should always be greater than or equal to the request
request = limit
}
if !request.IsZero() {
requests[resourceName] = request
}
if !limit.IsZero() {
limits[resourceName] = limit
}
}
func convertTaskResourcesToRequirements(taskResources v1alpha1.TaskResources) *v1.ResourceRequirements {
return &v1.ResourceRequirements{
Requests: v1.ResourceList{
v1.ResourceCPU: taskResources.Requests.CPU,
v1.ResourceMemory: taskResources.Requests.Memory,
v1.ResourceEphemeralStorage: taskResources.Requests.EphemeralStorage,
v1.ResourceStorage: taskResources.Requests.Storage,
utils.ResourceNvidiaGPU: taskResources.Requests.GPU,
},
Limits: v1.ResourceList{
v1.ResourceCPU: taskResources.Limits.CPU,
v1.ResourceMemory: taskResources.Limits.Memory,
v1.ResourceEphemeralStorage: taskResources.Limits.EphemeralStorage,
v1.ResourceStorage: taskResources.Limits.Storage,
utils.ResourceNvidiaGPU: taskResources.Limits.GPU,
},
}
}
// ComputeRawOutputPrefix constructs the output directory, where raw outputs of a task can be stored by the task. FlytePropeller may not have
// access to this location and can be passed in per execution.
// the function also returns the uniqueID generated
func ComputeRawOutputPrefix(ctx context.Context, length int, nCtx handler.NodeExecutionContext, currentNodeUniqueID v1alpha1.NodeID, currentAttempt uint32) (io.RawOutputPaths, string, error) {
uniqueID, err := encoding.FixedLengthUniqueIDForParts(length, []string{nCtx.NodeExecutionMetadata().GetOwnerID().Name, currentNodeUniqueID, strconv.Itoa(int(currentAttempt))})
if err != nil {
// SHOULD never really happen
return nil, uniqueID, err
}
rawOutputPrefix, err := ioutils.NewShardedRawOutputPath(ctx, nCtx.OutputShardSelector(), nCtx.RawOutputPrefix(), uniqueID, nCtx.DataStore())
if err != nil {
return nil, uniqueID, errors.Wrapf(errors.StorageError, nCtx.NodeID(), err, "failed to create output sandbox for node execution")
}
return rawOutputPrefix, uniqueID, nil
}
// ComputePreviousCheckpointPath returns the checkpoint path for the previous attempt, if this is the first attempt then returns an empty path
func ComputePreviousCheckpointPath(ctx context.Context, length int, nCtx handler.NodeExecutionContext, currentNodeUniqueID v1alpha1.NodeID, currentAttempt uint32) (storage.DataReference, error) {
if currentAttempt == 0 {
return "", nil
}
prevAttempt := currentAttempt - 1
prevRawOutputPrefix, _, err := ComputeRawOutputPrefix(ctx, length, nCtx, currentNodeUniqueID, prevAttempt)
if err != nil {
return "", err
}
return ioutils.ConstructCheckpointPath(nCtx.DataStore(), prevRawOutputPrefix.GetRawOutputPrefix()), nil
}
func (t *Handler) newTaskExecutionContext(ctx context.Context, nCtx handler.NodeExecutionContext, plugin pluginCore.Plugin) (*taskExecutionContext, error) {
id := GetTaskExecutionIdentifier(nCtx)
currentNodeUniqueID := nCtx.NodeID()
if nCtx.ExecutionContext().GetEventVersion() != v1alpha1.EventVersion0 {
var err error
currentNodeUniqueID, err = common.GenerateUniqueID(nCtx.ExecutionContext().GetParentInfo(), nCtx.NodeID())
if err != nil {
return nil, err
}
}
length := IDMaxLength
if l := plugin.GetProperties().GeneratedNameMaxLength; l != nil {
length = *l
}
rawOutputPrefix, uniqueID, err := ComputeRawOutputPrefix(ctx, length, nCtx, currentNodeUniqueID, id.RetryAttempt)
if err != nil {
return nil, err
}
prevCheckpointPath, err := ComputePreviousCheckpointPath(ctx, length, nCtx, currentNodeUniqueID, id.RetryAttempt)
if err != nil {
return nil, err
}
ow := ioutils.NewBufferedOutputWriter(ctx, ioutils.NewCheckpointRemoteFilePaths(ctx, nCtx.DataStore(), nCtx.NodeStatus().GetOutputDir(), rawOutputPrefix, prevCheckpointPath))
ts := nCtx.NodeStateReader().GetTaskNodeState()
var b *bytes.Buffer
if ts.PluginState != nil {
b = bytes.NewBuffer(ts.PluginState)
}
psm, err := newPluginStateManager(ctx, GobCodecVersion, ts.PluginStateVersion, b)
if err != nil {
return nil, errors.Wrapf(errors.RuntimeExecutionError, nCtx.NodeID(), err, "unable to initialize plugin state manager")
}
resourceNamespacePrefix := pluginCore.ResourceNamespace(t.resourceManager.GetID()).CreateSubNamespace(pluginCore.ResourceNamespace(plugin.GetID()))
maxAttempts := uint32(DefaultMaxAttempts)
if nCtx.Node().GetRetryStrategy() != nil && nCtx.Node().GetRetryStrategy().MinAttempts != nil {
maxAttempts = uint32(*nCtx.Node().GetRetryStrategy().MinAttempts)
}
taskTemplatePath, err := ioutils.GetTaskTemplatePath(ctx, nCtx.DataStore(), nCtx.NodeStatus().GetDataDir())
if err != nil {
return nil, err
}
return &taskExecutionContext{
NodeExecutionContext: nCtx,
tm: taskExecutionMetadata{
NodeExecutionMetadata: nCtx.NodeExecutionMetadata(),
taskExecID: taskExecutionID{execName: uniqueID, id: id},
o: nCtx.Node(),
maxAttempts: maxAttempts,
platformResources: convertTaskResourcesToRequirements(nCtx.ExecutionContext().GetExecutionConfig().TaskResources),
},
rm: resourcemanager.GetTaskResourceManager(
t.resourceManager, resourceNamespacePrefix, id),
psm: psm,
tr: ioutils.NewLazyUploadingTaskReader(nCtx.TaskReader(), taskTemplatePath, nCtx.DataStore()),
ow: ow,
ber: newBufferedEventRecorder(),
c: t.asyncCatalog,
sm: t.secretManager,
}, nil
}