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
/
node_execution.go
319 lines (289 loc) · 12.3 KB
/
node_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
package transformers
import (
"context"
"github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flytestdlib/storage"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/golang/protobuf/proto"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/golang/protobuf/ptypes"
"github.com/flyteorg/flyteadmin/pkg/errors"
genModel "github.com/flyteorg/flyteadmin/pkg/repositories/gen/models"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
)
type ToNodeExecutionModelInput struct {
Request *admin.NodeExecutionEventRequest
ParentTaskExecutionID *uint
ParentID *uint
DynamicWorkflowRemoteClosure string
InlineEventDataPolicy interfaces.InlineEventDataPolicy
StorageClient *storage.DataStore
}
func addNodeRunningState(request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
closure *admin.NodeExecutionClosure) error {
occurredAt, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal occurredAt with error: %v", err)
}
nodeExecutionModel.StartedAt = &occurredAt
startedAtProto, err := ptypes.TimestampProto(occurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal,
"failed to marshal occurredAt into a timestamp proto with error: %v", err)
}
closure.StartedAt = startedAtProto
return nil
}
func addTerminalState(
ctx context.Context,
request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
closure *admin.NodeExecutionClosure, inlineEventDataPolicy interfaces.InlineEventDataPolicy, storageClient *storage.DataStore) error {
if closure.StartedAt == nil {
logger.Warning(context.Background(), "node execution is missing StartedAt")
} else {
endTime, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(
codes.Internal, "Failed to parse node execution occurred at timestamp: %v", err)
}
nodeExecutionModel.Duration = endTime.Sub(*nodeExecutionModel.StartedAt)
closure.Duration = ptypes.DurationProto(nodeExecutionModel.Duration)
}
// Serialize output results (if they exist)
if request.Event.GetOutputUri() != "" {
closure.OutputResult = &admin.NodeExecutionClosure_OutputUri{
OutputUri: request.Event.GetOutputUri(),
}
} else if request.Event.GetOutputData() != nil {
switch inlineEventDataPolicy {
case interfaces.InlineEventDataPolicyStoreInline:
closure.OutputResult = &admin.NodeExecutionClosure_OutputData{
OutputData: request.Event.GetOutputData(),
}
default:
logger.Debugf(ctx, "Offloading outputs per InlineEventDataPolicy")
uri, err := common.OffloadLiteralMap(ctx, storageClient, request.Event.GetOutputData(),
request.Event.Id.ExecutionId.Project, request.Event.Id.ExecutionId.Domain, request.Event.Id.ExecutionId.Name,
request.Event.Id.NodeId, OutputsObjectSuffix)
if err != nil {
return err
}
closure.OutputResult = &admin.NodeExecutionClosure_OutputUri{
OutputUri: uri.String(),
}
}
} else if request.Event.GetError() != nil {
closure.OutputResult = &admin.NodeExecutionClosure_Error{
Error: request.Event.GetError(),
}
k := request.Event.GetError().Kind.String()
nodeExecutionModel.ErrorKind = &k
nodeExecutionModel.ErrorCode = &request.Event.GetError().Code
}
closure.DeckUri = request.Event.DeckUri
return nil
}
func CreateNodeExecutionModel(ctx context.Context, input ToNodeExecutionModelInput) (*models.NodeExecution, error) {
nodeExecution := &models.NodeExecution{
NodeExecutionKey: models.NodeExecutionKey{
NodeID: input.Request.Event.Id.NodeId,
ExecutionKey: models.ExecutionKey{
Project: input.Request.Event.Id.ExecutionId.Project,
Domain: input.Request.Event.Id.ExecutionId.Domain,
Name: input.Request.Event.Id.ExecutionId.Name,
},
},
Phase: input.Request.Event.Phase.String(),
InputURI: input.Request.Event.InputUri,
}
closure := admin.NodeExecutionClosure{
Phase: input.Request.Event.Phase,
CreatedAt: input.Request.Event.OccurredAt,
UpdatedAt: input.Request.Event.OccurredAt,
}
nodeExecutionMetadata := admin.NodeExecutionMetaData{
RetryGroup: input.Request.Event.RetryGroup,
SpecNodeId: input.Request.Event.SpecNodeId,
IsParentNode: input.Request.Event.IsParent,
IsDynamic: input.Request.Event.IsDynamic,
}
if input.Request.Event.Phase == core.NodeExecution_RUNNING {
err := addNodeRunningState(input.Request, nodeExecution, &closure)
if err != nil {
return nil, err
}
}
if common.IsNodeExecutionTerminal(input.Request.Event.Phase) {
err := addTerminalState(ctx, input.Request, nodeExecution, &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 node execution closure with error: %v", err)
}
marshaledNodeExecutionMetadata, err := proto.Marshal(&nodeExecutionMetadata)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(
codes.Internal, "failed to marshal node execution metadata with error: %v", err)
}
nodeExecution.Closure = marshaledClosure
nodeExecution.NodeExecutionMetadata = marshaledNodeExecutionMetadata
nodeExecutionCreatedAt, err := ptypes.Timestamp(input.Request.Event.OccurredAt)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to read event timestamp")
}
nodeExecution.NodeExecutionCreatedAt = &nodeExecutionCreatedAt
nodeExecution.NodeExecutionUpdatedAt = &nodeExecutionCreatedAt
if input.Request.Event.ParentTaskMetadata != nil {
nodeExecution.ParentTaskExecutionID = input.ParentTaskExecutionID
}
nodeExecution.ParentID = input.ParentID
nodeExecution.DynamicWorkflowRemoteClosureReference = input.DynamicWorkflowRemoteClosure
internalData := &genModel.NodeExecutionInternalData{
EventVersion: input.Request.Event.EventVersion,
}
internalDataBytes, err := proto.Marshal(internalData)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to marshal node execution data with err: %v", err)
}
nodeExecution.InternalData = internalDataBytes
return nodeExecution, nil
}
func UpdateNodeExecutionModel(
ctx context.Context, request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
targetExecution *core.WorkflowExecutionIdentifier, dynamicWorkflowRemoteClosure string,
inlineEventDataPolicy interfaces.InlineEventDataPolicy, storageClient *storage.DataStore) error {
var nodeExecutionClosure admin.NodeExecutionClosure
err := proto.Unmarshal(nodeExecutionModel.Closure, &nodeExecutionClosure)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal,
"failed to unmarshal node execution closure with error: %+v", err)
}
nodeExecutionModel.Phase = request.Event.Phase.String()
nodeExecutionClosure.Phase = request.Event.Phase
nodeExecutionClosure.UpdatedAt = request.Event.OccurredAt
if request.Event.Phase == core.NodeExecution_RUNNING {
err := addNodeRunningState(request, nodeExecutionModel, &nodeExecutionClosure)
if err != nil {
return err
}
}
if common.IsNodeExecutionTerminal(request.Event.Phase) {
err := addTerminalState(ctx, request, nodeExecutionModel, &nodeExecutionClosure, inlineEventDataPolicy, storageClient)
if err != nil {
return err
}
}
// If the node execution kicked off a workflow execution update the closure if it wasn't set
if targetExecution != nil && nodeExecutionClosure.GetWorkflowNodeMetadata() == nil {
nodeExecutionClosure.TargetMetadata = &admin.NodeExecutionClosure_WorkflowNodeMetadata{
WorkflowNodeMetadata: &admin.WorkflowNodeMetadata{
ExecutionId: targetExecution,
},
}
}
// Update TaskNodeMetadata, which includes caching information today.
if request.Event.GetTaskNodeMetadata() != nil {
targetMetadata := &admin.NodeExecutionClosure_TaskNodeMetadata{
TaskNodeMetadata: &admin.TaskNodeMetadata{
CheckpointUri: request.Event.GetTaskNodeMetadata().CheckpointUri,
},
}
if request.Event.GetTaskNodeMetadata().CatalogKey != nil {
st := request.Event.GetTaskNodeMetadata().GetCacheStatus().String()
targetMetadata.TaskNodeMetadata.CacheStatus = request.Event.GetTaskNodeMetadata().GetCacheStatus()
targetMetadata.TaskNodeMetadata.CatalogKey = request.Event.GetTaskNodeMetadata().GetCatalogKey()
nodeExecutionModel.CacheStatus = &st
}
nodeExecutionClosure.TargetMetadata = targetMetadata
}
marshaledClosure, err := proto.Marshal(&nodeExecutionClosure)
if err != nil {
return errors.NewFlyteAdminErrorf(
codes.Internal, "failed to marshal node execution closure with error: %v", err)
}
nodeExecutionModel.Closure = marshaledClosure
updatedAt, err := ptypes.Timestamp(request.Event.OccurredAt)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to parse updated at timestamp")
}
nodeExecutionModel.NodeExecutionUpdatedAt = &updatedAt
nodeExecutionModel.DynamicWorkflowRemoteClosureReference = dynamicWorkflowRemoteClosure
// In the case of dynamic nodes reporting DYNAMIC_RUNNING, the IsParent and IsDynamic bits will be set for this event.
// Update the node execution metadata accordingly.
if request.Event.IsParent || request.Event.IsDynamic {
var nodeExecutionMetadata admin.NodeExecutionMetaData
if len(nodeExecutionModel.NodeExecutionMetadata) > 0 {
if err := proto.Unmarshal(nodeExecutionModel.NodeExecutionMetadata, &nodeExecutionMetadata); err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal,
"failed to unmarshal node execution metadata with error: %+v", err)
}
}
// Not every event sends IsParent and IsDynamic as an artifact of how propeller handles dynamic nodes.
// Only explicitly set the fields, when they're set in the event itself.
if request.Event.IsParent {
nodeExecutionMetadata.IsParentNode = true
}
if request.Event.IsDynamic {
nodeExecutionMetadata.IsDynamic = true
}
nodeExecMetadataBytes, err := proto.Marshal(&nodeExecutionMetadata)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal,
"failed to marshal node execution metadata with error: %+v", err)
}
nodeExecutionModel.NodeExecutionMetadata = nodeExecMetadataBytes
}
return nil
}
func FromNodeExecutionModel(nodeExecutionModel models.NodeExecution) (*admin.NodeExecution, error) {
var closure admin.NodeExecutionClosure
err := proto.Unmarshal(nodeExecutionModel.Closure, &closure)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal closure")
}
var nodeExecutionMetadata admin.NodeExecutionMetaData
err = proto.Unmarshal(nodeExecutionModel.NodeExecutionMetadata, &nodeExecutionMetadata)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal nodeExecutionMetadata")
}
// TODO: delete this block and references to preloading child node executions no earlier than Q3 2022
// This is required for historical reasons because propeller did not always send IsParent or IsDynamic in events.
if !(nodeExecutionMetadata.IsParentNode || nodeExecutionMetadata.IsDynamic) {
if len(nodeExecutionModel.ChildNodeExecutions) > 0 {
nodeExecutionMetadata.IsParentNode = true
if len(nodeExecutionModel.DynamicWorkflowRemoteClosureReference) > 0 {
nodeExecutionMetadata.IsDynamic = true
}
}
}
return &admin.NodeExecution{
Id: &core.NodeExecutionIdentifier{
NodeId: nodeExecutionModel.NodeID,
ExecutionId: &core.WorkflowExecutionIdentifier{
Project: nodeExecutionModel.NodeExecutionKey.ExecutionKey.Project,
Domain: nodeExecutionModel.NodeExecutionKey.ExecutionKey.Domain,
Name: nodeExecutionModel.NodeExecutionKey.ExecutionKey.Name,
},
},
InputUri: nodeExecutionModel.InputURI,
Closure: &closure,
Metadata: &nodeExecutionMetadata,
}, nil
}
func GetNodeExecutionInternalData(internalData []byte) (*genModel.NodeExecutionInternalData, error) {
var nodeExecutionInternalData genModel.NodeExecutionInternalData
if len(internalData) > 0 {
err := proto.Unmarshal(internalData, &nodeExecutionInternalData)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal node execution data: %v", err)
}
}
return &nodeExecutionInternalData, nil
}