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
245 lines (220 loc) · 8.74 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
package transformers
import (
"context"
"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/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
)
type ToNodeExecutionModelInput struct {
Request *admin.NodeExecutionEventRequest
ParentTaskExecutionID uint
ParentID *uint
DynamicWorkflowRemoteClosure string
}
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(
request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
closure *admin.NodeExecutionClosure) 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.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
}
return nil
}
func CreateNodeExecutionModel(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,
}
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(input.Request, nodeExecution, &closure)
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
return nodeExecution, nil
}
func UpdateNodeExecutionModel(
request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
targetExecution *core.WorkflowExecutionIdentifier, dynamicWorkflowRemoteClosure string) 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(request, nodeExecutionModel, &nodeExecutionClosure)
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 && request.Event.GetTaskNodeMetadata().CatalogKey != nil {
st := request.Event.GetTaskNodeMetadata().GetCacheStatus().String()
targetMetadata := &admin.NodeExecutionClosure_TaskNodeMetadata{
TaskNodeMetadata: &admin.TaskNodeMetadata{
CacheStatus: request.Event.GetTaskNodeMetadata().GetCacheStatus(),
CatalogKey: request.Event.GetTaskNodeMetadata().GetCatalogKey(),
},
}
nodeExecutionClosure.TargetMetadata = targetMetadata
nodeExecutionModel.CacheStatus = &st
}
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
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")
}
if len(nodeExecutionModel.ChildNodeExecutions) > 0 {
nodeExecutionMetadata.IsParentNode = 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 FromNodeExecutionModels(
nodeExecutionModels []models.NodeExecution) ([]*admin.NodeExecution, error) {
nodeExecutions := make([]*admin.NodeExecution, len(nodeExecutionModels))
for idx, nodeExecutionModel := range nodeExecutionModels {
nodeExecution, err := FromNodeExecutionModel(nodeExecutionModel)
if err != nil {
return nil, err
}
nodeExecutions[idx] = nodeExecution
}
return nodeExecutions, nil
}