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 63
/
task_execution.go
441 lines (396 loc) · 16.2 KB
/
task_execution.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package transformers
import (
"context"
"sort"
"strconv"
"github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flytestdlib/storage"
"google.golang.org/protobuf/encoding/protojson"
jsonpatch "github.com/evanphx/json-patch"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/event"
"github.com/flyteorg/flytestdlib/logger"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
_struct "github.com/golang/protobuf/ptypes/struct"
"google.golang.org/grpc/codes"
)
var empty _struct.Struct
var jsonEmpty, _ = protojson.Marshal(&empty)
type CreateTaskExecutionModelInput struct {
Request *admin.TaskExecutionEventRequest
InlineEventDataPolicy interfaces.InlineEventDataPolicy
StorageClient *storage.DataStore
}
func addTaskStartedState(request *admin.TaskExecutionEventRequest, taskExecutionModel *models.TaskExecution,
closure *admin.TaskExecutionClosure) error {
occurredAt, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal occurredAt with error: %v", err)
}
taskExecutionModel.StartedAt = &occurredAt
closure.StartedAt = request.Event.OccurredAt
return nil
}
func addTaskTerminalState(
ctx context.Context,
request *admin.TaskExecutionEventRequest,
taskExecutionModel *models.TaskExecution, closure *admin.TaskExecutionClosure,
inlineEventDataPolicy interfaces.InlineEventDataPolicy, storageClient *storage.DataStore) error {
if taskExecutionModel.StartedAt == nil {
logger.Warning(context.Background(), "task execution is missing StartedAt")
} else {
endTime, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to parse task execution occurredAt timestamp: %v", err)
}
closure.StartedAt, err = ptypes.TimestampProto(*taskExecutionModel.StartedAt)
if err != nil {
return errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to parse task execution startedAt timestamp: %v", err)
}
taskExecutionModel.Duration = endTime.Sub(*taskExecutionModel.StartedAt)
closure.Duration = ptypes.DurationProto(taskExecutionModel.Duration)
}
if request.Event.GetOutputUri() != "" {
closure.OutputResult = &admin.TaskExecutionClosure_OutputUri{
OutputUri: request.Event.GetOutputUri(),
}
} else if request.Event.GetOutputData() != nil {
switch inlineEventDataPolicy {
case interfaces.InlineEventDataPolicyStoreInline:
closure.OutputResult = &admin.TaskExecutionClosure_OutputData{
OutputData: request.Event.GetOutputData(),
}
default:
logger.Debugf(ctx, "Offloading outputs per InlineEventDataPolicy")
uri, err := common.OffloadLiteralMap(ctx, storageClient, request.Event.GetOutputData(),
request.Event.ParentNodeExecutionId.ExecutionId.Project, request.Event.ParentNodeExecutionId.ExecutionId.Domain,
request.Event.ParentNodeExecutionId.ExecutionId.Name, request.Event.ParentNodeExecutionId.NodeId,
request.Event.TaskId.Project, request.Event.TaskId.Domain, request.Event.TaskId.Name, request.Event.TaskId.Version,
strconv.FormatUint(uint64(request.Event.RetryAttempt), 10), OutputsObjectSuffix)
if err != nil {
return err
}
closure.OutputResult = &admin.TaskExecutionClosure_OutputUri{
OutputUri: uri.String(),
}
}
} else if request.Event.GetError() != nil {
closure.OutputResult = &admin.TaskExecutionClosure_Error{
Error: request.Event.GetError(),
}
}
return nil
}
func CreateTaskExecutionModel(ctx context.Context, input CreateTaskExecutionModelInput) (*models.TaskExecution, error) {
taskExecution := &models.TaskExecution{
TaskExecutionKey: models.TaskExecutionKey{
TaskKey: models.TaskKey{
Project: input.Request.Event.TaskId.Project,
Domain: input.Request.Event.TaskId.Domain,
Name: input.Request.Event.TaskId.Name,
Version: input.Request.Event.TaskId.Version,
},
NodeExecutionKey: models.NodeExecutionKey{
NodeID: input.Request.Event.ParentNodeExecutionId.NodeId,
ExecutionKey: models.ExecutionKey{
Project: input.Request.Event.ParentNodeExecutionId.ExecutionId.Project,
Domain: input.Request.Event.ParentNodeExecutionId.ExecutionId.Domain,
Name: input.Request.Event.ParentNodeExecutionId.ExecutionId.Name,
},
},
RetryAttempt: &input.Request.Event.RetryAttempt,
},
Phase: input.Request.Event.Phase.String(),
PhaseVersion: input.Request.Event.PhaseVersion,
InputURI: input.Request.Event.InputUri,
}
metadata := input.Request.Event.Metadata
if metadata != nil && len(metadata.ExternalResources) > 1 {
sort.Slice(metadata.ExternalResources, func(i, j int) bool {
a := metadata.ExternalResources[i]
b := metadata.ExternalResources[j]
if a.GetIndex() == b.GetIndex() {
return a.GetRetryAttempt() < b.GetRetryAttempt()
}
return a.GetIndex() < b.GetIndex()
})
}
closure := &admin.TaskExecutionClosure{
Phase: input.Request.Event.Phase,
UpdatedAt: input.Request.Event.OccurredAt,
CreatedAt: input.Request.Event.OccurredAt,
Logs: input.Request.Event.Logs,
CustomInfo: input.Request.Event.CustomInfo,
Reason: input.Request.Event.Reason,
TaskType: input.Request.Event.TaskType,
Metadata: metadata,
EventVersion: input.Request.Event.EventVersion,
}
eventPhase := input.Request.Event.Phase
// Different tasks may report different phases as their first event.
// If the first event we receive for this execution is a valid
// non-terminal phase, mark the execution start time.
if eventPhase == core.TaskExecution_RUNNING {
err := addTaskStartedState(input.Request, taskExecution, closure)
if err != nil {
return nil, err
}
}
if common.IsTaskExecutionTerminal(input.Request.Event.Phase) {
err := addTaskTerminalState(ctx, input.Request, taskExecution, closure, input.InlineEventDataPolicy, input.StorageClient)
if err != nil {
return nil, err
}
}
marshaledClosure, err := proto.Marshal(closure)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(
codes.Internal, "failed to marshal task execution closure with error: %v", err)
}
taskExecution.Closure = marshaledClosure
taskExecutionCreatedAt, err := ptypes.Timestamp(input.Request.Event.OccurredAt)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to read event timestamp")
}
taskExecution.TaskExecutionCreatedAt = &taskExecutionCreatedAt
taskExecution.TaskExecutionUpdatedAt = &taskExecutionCreatedAt
return taskExecution, nil
}
// mergeLogs returns the unique list of logs across an existing list and the latest list sent in a task execution event
// update.
// It returns all the new logs receives + any existing log that hasn't been overwritten by a new log.
// An existing logLink is said to have been overwritten if a new logLink with the same Uri or the same Name has been
// received.
func mergeLogs(existing, latest []*core.TaskLog) []*core.TaskLog {
if len(latest) == 0 {
return existing
}
if len(existing) == 0 {
return latest
}
latestSetByURI := make(map[string]*core.TaskLog, len(latest))
latestSetByName := make(map[string]*core.TaskLog, len(latest))
for _, latestLog := range latest {
latestSetByURI[latestLog.Uri] = latestLog
if len(latestLog.Name) > 0 {
latestSetByName[latestLog.Name] = latestLog
}
}
// Copy over the latest logs since names will change for existing logs as a task transitions across phases.
logs := latest
for _, existingLog := range existing {
if _, ok := latestSetByURI[existingLog.Uri]; !ok {
if _, ok = latestSetByName[existingLog.Name]; !ok {
// We haven't seen this log before: add it to the output result list.
logs = append(logs, existingLog)
}
}
}
return logs
}
func mergeCustom(existing, latest *_struct.Struct) (*_struct.Struct, error) {
if existing == nil {
return latest, nil
}
if latest == nil {
return existing, nil
}
// To merge latest into existing we first create a patch object that consists of applying changes from latest to
// an empty struct. Then we apply this patch to existing so that the values changed in latest take precedence but
// barring conflicts/overwrites the values in existing stay the same.
jsonExisting, err := protojson.Marshal(existing)
if err != nil {
return nil, err
}
jsonLatest, err := protojson.Marshal(latest)
if err != nil {
return nil, err
}
patch, err := jsonpatch.CreateMergePatch(jsonEmpty, jsonLatest)
if err != nil {
return nil, err
}
custom, err := jsonpatch.MergePatch(jsonExisting, patch)
if err != nil {
return nil, err
}
var response _struct.Struct
err = protojson.Unmarshal(custom, &response)
if err != nil {
return nil, err
}
return &response, nil
}
// mergeExternalResource combines the lastest ExternalResourceInfo proto with an existing instance
// by updating fields and merging logs.
func mergeExternalResource(existing, latest *event.ExternalResourceInfo) *event.ExternalResourceInfo {
if existing == nil {
return latest
}
if latest == nil {
return existing
}
if latest.ExternalId != "" && existing.ExternalId != latest.ExternalId {
existing.ExternalId = latest.ExternalId
}
// note we are not updating existing.Index and existing.RetryAttempt because they are the
// search key for our ExternalResource pool.
existing.Phase = latest.Phase
if latest.CacheStatus != core.CatalogCacheStatus_CACHE_DISABLED && existing.CacheStatus != latest.CacheStatus {
existing.CacheStatus = latest.CacheStatus
}
existing.Logs = mergeLogs(existing.Logs, latest.Logs)
return existing
}
// mergeExternalResources combines lists of external resources. This involves appending new
// resources and updating in-place resources attributes.
func mergeExternalResources(existing, latest []*event.ExternalResourceInfo) []*event.ExternalResourceInfo {
if len(latest) == 0 {
return existing
}
for _, externalResource := range latest {
// we use a binary search over the ExternalResource Index and RetryAttempt fields to
// determine if a new subtask is being reported or an existing is being updated. it is
// important to note that this means anytime more than one ExternalResource is reported
// they must set the Index field.
index := sort.Search(len(existing), func(i int) bool {
if existing[i].GetIndex() == externalResource.GetIndex() {
return existing[i].GetRetryAttempt() >= externalResource.GetRetryAttempt()
}
return existing[i].GetIndex() >= externalResource.GetIndex()
})
if index >= len(existing) {
existing = append(existing, externalResource)
} else if existing[index].GetIndex() == externalResource.GetIndex() && existing[index].GetRetryAttempt() == externalResource.GetRetryAttempt() {
existing[index] = mergeExternalResource(existing[index], externalResource)
} else {
existing = append(existing, &event.ExternalResourceInfo{})
copy(existing[index+1:], existing[index:])
existing[index] = externalResource
}
}
return existing
}
// mergeMetadata merges an existing TaskExecutionMetadata instance with the provided instance. This
// includes updating non-defaulted fields and merging ExternalResources.
func mergeMetadata(existing, latest *event.TaskExecutionMetadata) *event.TaskExecutionMetadata {
if existing == nil {
return latest
}
if latest == nil {
return existing
}
if latest.GeneratedName != "" && existing.GeneratedName != latest.GeneratedName {
existing.GeneratedName = latest.GeneratedName
}
existing.ExternalResources = mergeExternalResources(existing.ExternalResources, latest.ExternalResources)
existing.ResourcePoolInfo = latest.ResourcePoolInfo
if latest.PluginIdentifier != "" && existing.PluginIdentifier != latest.PluginIdentifier {
existing.PluginIdentifier = latest.PluginIdentifier
}
if latest.InstanceClass != event.TaskExecutionMetadata_DEFAULT && existing.InstanceClass != latest.InstanceClass {
existing.InstanceClass = latest.InstanceClass
}
return existing
}
func UpdateTaskExecutionModel(ctx context.Context, request *admin.TaskExecutionEventRequest, taskExecutionModel *models.TaskExecution,
inlineEventDataPolicy interfaces.InlineEventDataPolicy, storageClient *storage.DataStore) error {
var taskExecutionClosure admin.TaskExecutionClosure
err := proto.Unmarshal(taskExecutionModel.Closure, &taskExecutionClosure)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal,
"failed to unmarshal task execution closure with error: %+v", err)
}
existingTaskPhase := taskExecutionModel.Phase
taskExecutionModel.Phase = request.Event.Phase.String()
taskExecutionModel.PhaseVersion = request.Event.PhaseVersion
taskExecutionClosure.Phase = request.Event.Phase
taskExecutionClosure.UpdatedAt = request.Event.OccurredAt
taskExecutionClosure.Logs = mergeLogs(taskExecutionClosure.Logs, request.Event.Logs)
if len(request.Event.Reason) > 0 {
taskExecutionClosure.Reason = request.Event.Reason
}
if existingTaskPhase != core.TaskExecution_RUNNING.String() && taskExecutionModel.Phase == core.TaskExecution_RUNNING.String() {
err = addTaskStartedState(request, taskExecutionModel, &taskExecutionClosure)
if err != nil {
return err
}
}
if common.IsTaskExecutionTerminal(request.Event.Phase) {
err := addTaskTerminalState(ctx, request, taskExecutionModel, &taskExecutionClosure, inlineEventDataPolicy, storageClient)
if err != nil {
return err
}
}
taskExecutionClosure.CustomInfo, err = mergeCustom(taskExecutionClosure.CustomInfo, request.Event.CustomInfo)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to merge task event custom_info with error: %v", err)
}
taskExecutionClosure.Metadata = mergeMetadata(taskExecutionClosure.Metadata, request.Event.Metadata)
if request.Event.EventVersion > taskExecutionClosure.EventVersion {
taskExecutionClosure.EventVersion = request.Event.EventVersion
}
marshaledClosure, err := proto.Marshal(&taskExecutionClosure)
if err != nil {
return errors.NewFlyteAdminErrorf(
codes.Internal, "failed to marshal task execution closure with error: %v", err)
}
taskExecutionModel.Closure = marshaledClosure
updatedAt, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to parse updated at timestamp")
}
taskExecutionModel.TaskExecutionUpdatedAt = &updatedAt
return nil
}
func FromTaskExecutionModel(taskExecutionModel models.TaskExecution) (*admin.TaskExecution, error) {
var closure admin.TaskExecutionClosure
err := proto.Unmarshal(taskExecutionModel.Closure, &closure)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal closure")
}
taskExecution := &admin.TaskExecution{
Id: &core.TaskExecutionIdentifier{
TaskId: &core.Identifier{
ResourceType: core.ResourceType_TASK,
Project: taskExecutionModel.TaskExecutionKey.TaskKey.Project,
Domain: taskExecutionModel.TaskExecutionKey.TaskKey.Domain,
Name: taskExecutionModel.TaskExecutionKey.TaskKey.Name,
Version: taskExecutionModel.TaskExecutionKey.TaskKey.Version,
},
NodeExecutionId: &core.NodeExecutionIdentifier{
NodeId: taskExecutionModel.NodeExecutionKey.NodeID,
ExecutionId: &core.WorkflowExecutionIdentifier{
Project: taskExecutionModel.TaskExecutionKey.NodeExecutionKey.ExecutionKey.Project,
Domain: taskExecutionModel.TaskExecutionKey.NodeExecutionKey.ExecutionKey.Domain,
Name: taskExecutionModel.TaskExecutionKey.NodeExecutionKey.ExecutionKey.Name,
},
},
RetryAttempt: *taskExecutionModel.TaskExecutionKey.RetryAttempt,
},
InputUri: taskExecutionModel.InputURI,
Closure: &closure,
}
if len(taskExecutionModel.ChildNodeExecution) > 0 {
taskExecution.IsParent = true
}
return taskExecution, nil
}
func FromTaskExecutionModels(taskExecutionModels []models.TaskExecution) ([]*admin.TaskExecution, error) {
taskExecutions := make([]*admin.TaskExecution, len(taskExecutionModels))
for idx, taskExecutionModel := range taskExecutionModels {
taskExecution, err := FromTaskExecutionModel(taskExecutionModel)
if err != nil {
return nil, err
}
taskExecutions[idx] = taskExecution
}
return taskExecutions, nil
}