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
/
management.go
372 lines (310 loc) · 14 KB
/
management.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
package k8s
import (
"context"
"fmt"
"time"
idlCore "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyteplugins/go/tasks/logs"
"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/plugins/array"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array/arraystatus"
arrayCore "github.com/flyteorg/flyteplugins/go/tasks/plugins/array/core"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array/errorcollector"
"github.com/flyteorg/flytestdlib/bitarray"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/storage"
)
// allocateResource attempts to allot resources for the specified parameter with the
// TaskExecutionContexts ResourceManager.
func allocateResource(ctx context.Context, tCtx core.TaskExecutionContext, config *Config, podName string) (core.AllocationStatus, error) {
if !IsResourceConfigSet(config.ResourceConfig) {
return core.AllocationStatusGranted, nil
}
resourceNamespace := core.ResourceNamespace(config.ResourceConfig.PrimaryLabel)
resourceConstraintSpec := core.ResourceConstraintsSpec{
ProjectScopeResourceConstraint: nil,
NamespaceScopeResourceConstraint: nil,
}
allocationStatus, err := tCtx.ResourceManager().AllocateResource(ctx, resourceNamespace, podName, resourceConstraintSpec)
if err != nil {
return core.AllocationUndefined, err
}
return allocationStatus, nil
}
// deallocateResource attempts to release resources for the specified parameter with the
// TaskExecutionContexts ResourceManager.
func deallocateResource(ctx context.Context, tCtx core.TaskExecutionContext, config *Config, podName string) error {
if !IsResourceConfigSet(config.ResourceConfig) {
return nil
}
resourceNamespace := core.ResourceNamespace(config.ResourceConfig.PrimaryLabel)
err := tCtx.ResourceManager().ReleaseResource(ctx, resourceNamespace, podName)
if err != nil {
logger.Errorf(ctx, "Error releasing token [%s]. error %s", podName, err)
return err
}
return nil
}
// LaunchAndCheckSubTasksState iterates over each subtask performing operations to transition them
// to a terminal state. This may include creating new k8s resources, monitoring existing k8s
// resources, retrying failed attempts, or declaring a permanent failure among others.
func LaunchAndCheckSubTasksState(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient,
config *Config, dataStore *storage.DataStore, outputPrefix, baseOutputDataSandbox storage.DataReference, currentState *arrayCore.State) (
newState *arrayCore.State, externalResources []*core.ExternalResource, err error) {
newState = currentState
messageCollector := errorcollector.NewErrorMessageCollector()
newArrayStatus := &arraystatus.ArrayStatus{
Summary: arraystatus.ArraySummary{},
Detailed: arrayCore.NewPhasesCompactArray(uint(currentState.GetExecutionArraySize())),
}
externalResources = make([]*core.ExternalResource, 0, len(currentState.GetArrayStatus().Detailed.GetItems()))
// If we have arrived at this state for the first time then currentState has not been
// initialized with number of sub tasks.
if len(currentState.GetArrayStatus().Detailed.GetItems()) == 0 {
currentState.ArrayStatus = *newArrayStatus
}
// If the current State is newly minted then we must initialize RetryAttempts to track how many
// times each subtask is executed.
if len(currentState.RetryAttempts.GetItems()) == 0 {
count := uint(currentState.GetExecutionArraySize())
maxValue := bitarray.Item(tCtx.TaskExecutionMetadata().GetMaxAttempts())
retryAttemptsArray, err := bitarray.NewCompactArray(count, maxValue)
if err != nil {
logger.Errorf(ctx, "Failed to create attempts compact array with [count: %v, maxValue: %v]", count, maxValue)
return currentState, externalResources, nil
}
// Initialize subtask retryAttempts to 0 so that, in tandem with the podName logic, we
// maintain backwards compatibility.
for i := 0; i < currentState.GetExecutionArraySize(); i++ {
retryAttemptsArray.SetItem(i, 0)
}
currentState.RetryAttempts = retryAttemptsArray
}
// If the current State is newly minted then we must initialize SystemFailures to track how many
// times the subtask failed due to system issues, this is necessary to correctly evaluate
// interruptible subtasks.
if len(currentState.SystemFailures.GetItems()) == 0 {
count := uint(currentState.GetExecutionArraySize())
maxValue := bitarray.Item(tCtx.TaskExecutionMetadata().GetMaxAttempts())
systemFailuresArray, err := bitarray.NewCompactArray(count, maxValue)
if err != nil {
logger.Errorf(ctx, "Failed to create system failures array with [count: %v, maxValue: %v]", count, maxValue)
return currentState, externalResources, err
}
for i := 0; i < currentState.GetExecutionArraySize(); i++ {
systemFailuresArray.SetItem(i, 0)
}
currentState.SystemFailures = systemFailuresArray
}
// initialize log plugin
logPlugin, err := logs.InitializeLogPlugins(&config.LogConfig.Config)
if err != nil {
return currentState, externalResources, err
}
// identify max parallelism
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return currentState, externalResources, err
} else if taskTemplate == nil {
return currentState, externalResources, errors.Errorf(errors.BadTaskSpecification, "Required value not set, taskTemplate is nil")
}
arrayJob, err := arrayCore.ToArrayJob(taskTemplate.GetCustom(), taskTemplate.TaskTypeVersion)
if err != nil {
return currentState, externalResources, err
}
currentParallelism := 0
maxParallelism := int(arrayJob.Parallelism)
currentSubTaskPhaseHash, err := currentState.GetArrayStatus().HashCode()
if err != nil {
return currentState, externalResources, err
}
for childIdx, existingPhaseIdx := range currentState.GetArrayStatus().Detailed.GetItems() {
existingPhase := core.Phases[existingPhaseIdx]
retryAttempt := currentState.RetryAttempts.GetItem(childIdx)
if existingPhase == core.PhaseRetryableFailure {
retryAttempt++
newState.RetryAttempts.SetItem(childIdx, retryAttempt)
} else if existingPhase.IsTerminal() {
newArrayStatus.Detailed.SetItem(childIdx, bitarray.Item(existingPhase))
continue
}
originalIdx := arrayCore.CalculateOriginalIndex(childIdx, newState.GetIndexesToCache())
systemFailures := currentState.SystemFailures.GetItem(childIdx)
stCtx, err := NewSubTaskExecutionContext(ctx, tCtx, taskTemplate, childIdx, originalIdx, retryAttempt, systemFailures)
if err != nil {
return currentState, externalResources, err
}
podName := stCtx.TaskExecutionMetadata().GetTaskExecutionID().GetGeneratedName()
// depending on the existing subtask phase we either a launch new k8s resource or monitor
// an existing instance
var phaseInfo core.PhaseInfo
var perr error
if existingPhase == core.PhaseUndefined || existingPhase == core.PhaseWaitingForResources || existingPhase == core.PhaseRetryableFailure {
// attempt to allocateResource
allocationStatus, err := allocateResource(ctx, stCtx, config, podName)
if err != nil {
logger.Errorf(ctx, "Resource manager failed for TaskExecId [%s] token [%s]. error %s",
stCtx.TaskExecutionMetadata().GetTaskExecutionID().GetID(), podName, err)
return currentState, externalResources, err
}
logger.Infof(ctx, "Allocation result for [%s] is [%s]", podName, allocationStatus)
if allocationStatus != core.AllocationStatusGranted {
phaseInfo = core.PhaseInfoWaitingForResourcesInfo(time.Now(), core.DefaultPhaseVersion, "Exceeded ResourceManager quota", nil)
} else {
phaseInfo, perr = launchSubtask(ctx, stCtx, config, kubeClient)
// if launchSubtask fails we attempt to deallocate the (previously allocated)
// resource to mitigate leaks
if perr != nil {
perr = deallocateResource(ctx, stCtx, config, podName)
if perr != nil {
logger.Errorf(ctx, "Error releasing allocation token [%s] in Finalize [%s]", podName, err)
}
}
}
} else {
phaseInfo, perr = getSubtaskPhaseInfo(ctx, stCtx, config, kubeClient, logPlugin)
}
if perr != nil {
return currentState, externalResources, perr
}
if phaseInfo.Err() != nil {
messageCollector.Collect(childIdx, phaseInfo.Err().String())
// If the service reported an error but there is no error.pb written, write one with the
// service-provided error message.
or, err := array.ConstructOutputReader(ctx, dataStore, outputPrefix, baseOutputDataSandbox, originalIdx)
if err != nil {
return currentState, externalResources, err
}
if hasErr, err := or.IsError(ctx); err != nil {
return currentState, externalResources, err
} else if !hasErr {
// The subtask has not produced an error.pb, write one.
ow, err := array.ConstructOutputWriter(ctx, dataStore, outputPrefix, baseOutputDataSandbox, originalIdx)
if err != nil {
return currentState, externalResources, err
}
if err = ow.Put(ctx, ioutils.NewInMemoryOutputReader(nil, nil, &io.ExecutionError{
ExecutionError: phaseInfo.Err(),
IsRecoverable: phaseInfo.Phase() != core.PhasePermanentFailure,
})); err != nil {
return currentState, externalResources, err
}
}
}
if phaseInfo.Err() != nil && phaseInfo.Err().GetKind() == idlCore.ExecutionError_SYSTEM {
newState.SystemFailures.SetItem(childIdx, systemFailures+1)
} else {
newState.SystemFailures.SetItem(childIdx, systemFailures)
}
// process subtask phase
actualPhase := phaseInfo.Phase()
if actualPhase.IsSuccess() {
actualPhase, err = array.CheckTaskOutput(ctx, dataStore, outputPrefix, baseOutputDataSandbox, childIdx, originalIdx)
if err != nil {
return currentState, externalResources, err
}
}
if actualPhase == core.PhaseRetryableFailure && uint32(retryAttempt+1) >= stCtx.TaskExecutionMetadata().GetMaxAttempts() {
// If we see a retryable failure we must check if the number of retries exceeds the maximum
// attempts. If so, transition to a permanent failure so that is not attempted again.
actualPhase = core.PhasePermanentFailure
}
newArrayStatus.Detailed.SetItem(childIdx, bitarray.Item(actualPhase))
if actualPhase.IsTerminal() {
err = deallocateResource(ctx, stCtx, config, podName)
if err != nil {
logger.Errorf(ctx, "Error releasing allocation token [%s] in Finalize [%s]", podName, err)
return currentState, externalResources, err
}
err = finalizeSubtask(ctx, stCtx, config, kubeClient)
if err != nil {
logger.Errorf(ctx, "Error finalizing resource [%s] in Finalize [%s]", podName, err)
return currentState, externalResources, err
}
}
// process phaseInfo
var logLinks []*idlCore.TaskLog
if phaseInfo.Info() != nil {
logLinks = phaseInfo.Info().Logs
}
externalResources = append(externalResources, &core.ExternalResource{
ExternalID: podName,
Index: uint32(originalIdx),
Logs: logLinks,
RetryAttempt: uint32(retryAttempt),
Phase: actualPhase,
})
// validate parallelism
if !actualPhase.IsTerminal() || actualPhase == core.PhaseRetryableFailure {
currentParallelism++
}
if maxParallelism != 0 && currentParallelism >= maxParallelism {
break
}
}
// compute task phase from array status summary
for _, phaseIdx := range newArrayStatus.Detailed.GetItems() {
newArrayStatus.Summary.Inc(core.Phases[phaseIdx])
}
phase := arrayCore.SummaryToPhase(ctx, currentState.GetOriginalMinSuccesses()-currentState.GetOriginalArraySize()+int64(currentState.GetExecutionArraySize()), newArrayStatus.Summary)
// process new state
newState = newState.SetArrayStatus(*newArrayStatus)
if phase == arrayCore.PhaseWriteToDiscoveryThenFail {
errorMsg := messageCollector.Summary(GetConfig().MaxErrorStringLength)
newState = newState.SetReason(errorMsg)
}
_, version := currentState.GetPhase()
if phase == arrayCore.PhaseCheckingSubTaskExecutions {
newSubTaskPhaseHash, err := newState.GetArrayStatus().HashCode()
if err != nil {
return currentState, externalResources, err
}
if newSubTaskPhaseHash != currentSubTaskPhaseHash {
version++
}
newState = newState.SetPhase(phase, version).SetReason("Task is still running")
} else {
newState = newState.SetPhase(phase, version+1)
}
return newState, externalResources, nil
}
// TerminateSubTasks performs operations to gracefully terminate all subtasks. This may include
// aborting and finalizing active k8s resources.
func TerminateSubTasks(ctx context.Context, tCtx core.TaskExecutionContext, kubeClient core.KubeClient, config *Config,
terminateFunction func(context.Context, SubTaskExecutionContext, *Config, core.KubeClient) error, currentState *arrayCore.State) error {
taskTemplate, err := tCtx.TaskReader().Read(ctx)
if err != nil {
return err
} else if taskTemplate == nil {
return errors.Errorf(errors.BadTaskSpecification, "Required value not set, taskTemplate is nil")
}
messageCollector := errorcollector.NewErrorMessageCollector()
for childIdx, existingPhaseIdx := range currentState.GetArrayStatus().Detailed.GetItems() {
existingPhase := core.Phases[existingPhaseIdx]
retryAttempt := uint64(0)
if childIdx < len(currentState.RetryAttempts.GetItems()) {
// we can use RetryAttempts if it has been initialized, otherwise stay with default 0
retryAttempt = currentState.RetryAttempts.GetItem(childIdx)
}
// return immediately if subtask has completed or not yet started
if existingPhase.IsTerminal() || existingPhase == core.PhaseUndefined {
continue
}
originalIdx := arrayCore.CalculateOriginalIndex(childIdx, currentState.GetIndexesToCache())
stCtx, err := NewSubTaskExecutionContext(ctx, tCtx, taskTemplate, childIdx, originalIdx, retryAttempt, 0)
if err != nil {
return err
}
err = terminateFunction(ctx, stCtx, config, kubeClient)
if err != nil {
messageCollector.Collect(childIdx, err.Error())
}
}
if messageCollector.Length() > 0 {
return fmt.Errorf(messageCollector.Summary(config.MaxErrorStringLength))
}
return nil
}