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 53
/
subtask_exec_context.go
279 lines (236 loc) · 10.3 KB
/
subtask_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
package k8s
import (
"context"
"fmt"
"regexp"
"strconv"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
pluginsCore "github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/tasklog"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/utils"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/utils/secrets"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array"
podPlugin "github.com/flyteorg/flyteplugins/go/tasks/plugins/k8s/pod"
"github.com/flyteorg/flytestdlib/storage"
)
// SubTaskExecutionContext wraps the core TaskExecutionContext so that the k8s array task context
// can be used within the pod plugin
type SubTaskExecutionContext struct {
pluginsCore.TaskExecutionContext
arrayInputReader io.InputReader
metadataOverride pluginsCore.TaskExecutionMetadata
originalIndex int
outputWriter io.OutputWriter
subtaskReader SubTaskReader
}
// InputReader overrides the base TaskExecutionContext to return a custom InputReader
func (s SubTaskExecutionContext) InputReader() io.InputReader {
return s.arrayInputReader
}
// OutputWriter overrides the base TaskExecutionContext to return a custom OutputWriter
func (s SubTaskExecutionContext) OutputWriter() io.OutputWriter {
return s.outputWriter
}
// TaskExecutionMetadata overrides the base TaskExecutionContext to return custom
// TaskExecutionMetadata
func (s SubTaskExecutionContext) TaskExecutionMetadata() pluginsCore.TaskExecutionMetadata {
return s.metadataOverride
}
// TaskReader overrides the base TaskExecutionContext to return a custom TaskReader
func (s SubTaskExecutionContext) TaskReader() pluginsCore.TaskReader {
return s.subtaskReader
}
// pluginStateReader overrides the default PluginStateReader because the maptask does not persist
// state between task evaluations.
type pluginStateReader struct{}
func (p pluginStateReader) GetStateVersion() uint8 {
return 0
}
func (p pluginStateReader) Get(t interface{}) (stateVersion uint8, err error) {
return 0, nil
}
// PluginStateReader overrides the default behavior to return a custom pluginStateReader.
func (s SubTaskExecutionContext) PluginStateReader() pluginsCore.PluginStateReader {
return pluginStateReader{}
}
// NewSubtaskExecutionContext constructs a SubTaskExecutionContext using the provided parameters
func NewSubTaskExecutionContext(ctx context.Context, tCtx pluginsCore.TaskExecutionContext, taskTemplate *core.TaskTemplate,
executionIndex, originalIndex int, retryAttempt uint64, systemFailures uint64) (SubTaskExecutionContext, error) {
subTaskExecutionMetadata, err := NewSubTaskExecutionMetadata(tCtx.TaskExecutionMetadata(), taskTemplate, executionIndex, retryAttempt, systemFailures)
if err != nil {
return SubTaskExecutionContext{}, err
}
// construct TaskTemplate
subtaskTemplate := &core.TaskTemplate{}
*subtaskTemplate = *taskTemplate
subtaskTemplate.TaskTypeVersion = 2
if subtaskTemplate.GetContainer() != nil {
subtaskTemplate.Type = podPlugin.ContainerTaskType
} else if taskTemplate.GetK8SPod() != nil {
subtaskTemplate.Type = podPlugin.SidecarTaskType
}
arrayInputReader := array.GetInputReader(tCtx, taskTemplate)
subtaskReader := SubTaskReader{tCtx.TaskReader(), subtaskTemplate}
// construct OutputWriter
dataStore := tCtx.DataStore()
checkpointPrefix, err := dataStore.ConstructReference(ctx, tCtx.OutputWriter().GetRawOutputPrefix(), strconv.Itoa(originalIndex))
if err != nil {
return SubTaskExecutionContext{}, err
}
checkpoint, err := dataStore.ConstructReference(ctx, checkpointPrefix, strconv.FormatUint(retryAttempt, 10))
if err != nil {
return SubTaskExecutionContext{}, err
}
checkpointPath := ioutils.NewRawOutputPaths(ctx, checkpoint)
var prevCheckpoint storage.DataReference
if retryAttempt == 0 {
prevCheckpoint = ""
} else {
prevCheckpoint, err = dataStore.ConstructReference(ctx, checkpointPrefix, strconv.FormatUint(retryAttempt-1, 10))
if err != nil {
return SubTaskExecutionContext{}, err
}
}
prevCheckpointPath := ioutils.ConstructCheckpointPath(dataStore, prevCheckpoint)
// note that we must not append the originalIndex to the original OutputPrefixPath because
// flytekit is already doing this
p := ioutils.NewCheckpointRemoteFilePaths(ctx, dataStore, tCtx.OutputWriter().GetOutputPrefixPath(), checkpointPath, prevCheckpointPath)
outputWriter := ioutils.NewRemoteFileOutputWriter(ctx, dataStore, p)
return SubTaskExecutionContext{
TaskExecutionContext: tCtx,
arrayInputReader: arrayInputReader,
metadataOverride: subTaskExecutionMetadata,
originalIndex: originalIndex,
outputWriter: outputWriter,
subtaskReader: subtaskReader,
}, nil
}
// SubTaskReader wraps the core TaskReader to customize the task template task type and version
type SubTaskReader struct {
pluginsCore.TaskReader
subtaskTemplate *core.TaskTemplate
}
// Read overrides the base TaskReader to return a custom TaskTemplate
func (s SubTaskReader) Read(ctx context.Context) (*core.TaskTemplate, error) {
return s.subtaskTemplate, nil
}
// SubTaskExecutionID wraps the core TaskExecutionID to customize the generated pod name
type SubTaskExecutionID struct {
pluginsCore.TaskExecutionID
executionIndex int
parentName string
subtaskRetryAttempt uint64
taskRetryAttempt uint32
}
// GetGeneratedName overrides the base TaskExecutionID to append the subtask index and retryAttempt
func (s SubTaskExecutionID) GetGeneratedName() string {
indexStr := strconv.Itoa(s.executionIndex)
// If the retryAttempt is 0 we do not include it in the pod name. The gives us backwards
// compatibility in the ability to dynamically transition running map tasks to use subtask retries.
if s.subtaskRetryAttempt == 0 {
return utils.ConvertToDNS1123SubdomainCompatibleString(fmt.Sprintf("%v-%v", s.parentName, indexStr))
}
retryAttemptStr := strconv.FormatUint(s.subtaskRetryAttempt, 10)
return utils.ConvertToDNS1123SubdomainCompatibleString(fmt.Sprintf("%v-%v-%v", s.parentName, indexStr, retryAttemptStr))
}
// GetLogSuffix returns the suffix which should be appended to subtask log names
func (s SubTaskExecutionID) GetLogSuffix() string {
// Append the retry attempt and executionIndex so that log names coincide with pod names per
// https://github.com/flyteorg/flyteplugins/pull/186#discussion_r666569825. To maintain
// backwards compatibility we append the subtaskRetryAttempt if it is not 0.
if s.subtaskRetryAttempt == 0 {
return fmt.Sprintf(" #%d-%d", s.taskRetryAttempt, s.executionIndex)
}
return fmt.Sprintf(" #%d-%d-%d", s.taskRetryAttempt, s.executionIndex, s.subtaskRetryAttempt)
}
var logTemplateRegexes = struct {
ExecutionIndex *regexp.Regexp
ParentName *regexp.Regexp
RetryAttempt *regexp.Regexp
ParentRetryAttempt *regexp.Regexp
}{
tasklog.MustCreateRegex("subtaskExecutionIndex"),
tasklog.MustCreateRegex("subtaskParentName"),
tasklog.MustCreateRegex("subtaskRetryAttempt"),
tasklog.MustCreateRegex("subtaskParentRetryAttempt"),
}
func (s SubTaskExecutionID) TemplateVarsByScheme() *tasklog.TemplateVarsByScheme {
return &tasklog.TemplateVarsByScheme{
TaskExecution: tasklog.TemplateVars{
{Regex: logTemplateRegexes.ParentName, Value: s.parentName},
{
Regex: logTemplateRegexes.ExecutionIndex,
Value: strconv.FormatUint(uint64(s.executionIndex), 10),
},
{
Regex: logTemplateRegexes.RetryAttempt,
Value: strconv.FormatUint(s.subtaskRetryAttempt, 10),
},
{
Regex: logTemplateRegexes.ParentRetryAttempt,
Value: strconv.FormatUint(uint64(s.taskRetryAttempt), 10),
},
},
}
}
// NewSubtaskExecutionID constructs a SubTaskExecutionID using the provided parameters
func NewSubTaskExecutionID(taskExecutionID pluginsCore.TaskExecutionID, executionIndex int, retryAttempt uint64) SubTaskExecutionID {
return SubTaskExecutionID{
taskExecutionID,
executionIndex,
taskExecutionID.GetGeneratedName(),
retryAttempt,
taskExecutionID.GetID().RetryAttempt,
}
}
// SubTaskExecutionMetadata wraps the core TaskExecutionMetadata to customize the TaskExecutionID
type SubTaskExecutionMetadata struct {
pluginsCore.TaskExecutionMetadata
annotations map[string]string
labels map[string]string
interruptible bool
subtaskExecutionID SubTaskExecutionID
}
// GetAnnotations overrides the base TaskExecutionMetadata to return a custom map
func (s SubTaskExecutionMetadata) GetAnnotations() map[string]string {
return s.annotations
}
// GetLabels overrides the base TaskExecutionMetadata to return a custom map
func (s SubTaskExecutionMetadata) GetLabels() map[string]string {
return s.labels
}
// GetTaskExecutionID overrides the base TaskExecutionMetadata to return a custom TaskExecutionID
func (s SubTaskExecutionMetadata) GetTaskExecutionID() pluginsCore.TaskExecutionID {
return s.subtaskExecutionID
}
// IsInterruptbile overrides the base NodeExecutionMetadata to return a subtask specific identifier
func (s SubTaskExecutionMetadata) IsInterruptible() bool {
return s.interruptible
}
// NewSubtaskExecutionMetadata constructs a SubTaskExecutionMetadata using the provided parameters
func NewSubTaskExecutionMetadata(taskExecutionMetadata pluginsCore.TaskExecutionMetadata, taskTemplate *core.TaskTemplate,
executionIndex int, retryAttempt uint64, systemFailures uint64) (SubTaskExecutionMetadata, error) {
var err error
secretsMap := make(map[string]string)
injectSecretsLabel := make(map[string]string)
if taskTemplate.SecurityContext != nil && len(taskTemplate.SecurityContext.Secrets) > 0 {
secretsMap, err = secrets.MarshalSecretsToMapStrings(taskTemplate.SecurityContext.Secrets)
if err != nil {
return SubTaskExecutionMetadata{}, err
}
injectSecretsLabel = map[string]string{
secrets.PodLabel: secrets.PodLabelValue,
}
}
subTaskExecutionID := NewSubTaskExecutionID(taskExecutionMetadata.GetTaskExecutionID(), executionIndex, retryAttempt)
interruptible := taskExecutionMetadata.IsInterruptible() && uint32(systemFailures) < taskExecutionMetadata.GetInterruptibleFailureThreshold()
return SubTaskExecutionMetadata{
taskExecutionMetadata,
utils.UnionMaps(taskExecutionMetadata.GetAnnotations(), secretsMap),
utils.UnionMaps(taskExecutionMetadata.GetLabels(), injectSecretsLabel),
interruptible,
subTaskExecutionID,
}, nil
}