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
/
Copy pathnode_execution.go
200 lines (181 loc) · 6.82 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
package transformers
import (
"context"
"github.com/lyft/flyteadmin/pkg/common"
"github.com/lyft/flytestdlib/logger"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flyteadmin/pkg/errors"
"github.com/lyft/flyteadmin/pkg/repositories/models"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"google.golang.org/grpc/codes"
)
type ToNodeExecutionModelInput struct {
Request *admin.NodeExecutionEventRequest
ParentTaskExecutionID uint
}
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(),
}
}
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,
}
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)
}
nodeExecution.Closure = marshaledClosure
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
}
return nodeExecution, nil
}
func UpdateNodeExecutionModel(
request *admin.NodeExecutionEventRequest, nodeExecutionModel *models.NodeExecution,
targetExecution *core.WorkflowExecutionIdentifier) 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,
},
}
}
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
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")
}
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,
}, 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
}