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 59
/
transformers.go
209 lines (189 loc) · 6.03 KB
/
transformers.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
package nodes
import (
"context"
"fmt"
"strconv"
"time"
"github.com/lyft/flytepropeller/pkg/controller/executors"
"github.com/lyft/flytepropeller/pkg/controller/nodes/common"
"github.com/golang/protobuf/ptypes"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/event"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/lyft/flytestdlib/logger"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/lyft/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/lyft/flytepropeller/pkg/controller/nodes/handler"
)
func ToNodeExecOutput(info *handler.OutputInfo) *event.NodeExecutionEvent_OutputUri {
if info == nil || info.OutputURI == "" {
return nil
}
return &event.NodeExecutionEvent_OutputUri{
OutputUri: info.OutputURI.String(),
}
}
func ToNodeExecWorkflowNodeMetadata(info *handler.WorkflowNodeInfo) *event.NodeExecutionEvent_WorkflowNodeMetadata {
if info == nil || info.LaunchedWorkflowID == nil {
return nil
}
return &event.NodeExecutionEvent_WorkflowNodeMetadata{
WorkflowNodeMetadata: &event.WorkflowNodeMetadata{
ExecutionId: info.LaunchedWorkflowID,
},
}
}
func ToNodeExecTaskNodeMetadata(info *handler.TaskNodeInfo) *event.NodeExecutionEvent_TaskNodeMetadata {
if info == nil || info.TaskNodeMetadata == nil {
return nil
}
return &event.NodeExecutionEvent_TaskNodeMetadata{
TaskNodeMetadata: info.TaskNodeMetadata,
}
}
func ToNodeExecEventPhase(p handler.EPhase) core.NodeExecution_Phase {
switch p {
case handler.EPhaseQueued:
return core.NodeExecution_QUEUED
case handler.EPhaseRunning, handler.EPhaseRetryableFailure:
return core.NodeExecution_RUNNING
case handler.EPhaseSkip:
return core.NodeExecution_SKIPPED
case handler.EPhaseSuccess:
return core.NodeExecution_SUCCEEDED
case handler.EPhaseFailed:
return core.NodeExecution_FAILED
default:
return core.NodeExecution_UNDEFINED
}
}
func ToNodeExecutionEvent(nodeExecID *core.NodeExecutionIdentifier,
info handler.PhaseInfo,
reader io.InputReader,
status v1alpha1.ExecutableNodeStatus,
eventVersion v1alpha1.EventVersion,
parentInfo executors.ImmutableParentInfo,
node v1alpha1.ExecutableNode) (*event.NodeExecutionEvent, error) {
if info.GetPhase() == handler.EPhaseNotReady {
return nil, nil
}
if info.GetPhase() == handler.EPhaseUndefined {
return nil, fmt.Errorf("illegal state, undefined phase received for node [%s]", nodeExecID.NodeId)
}
occurredTime, err := ptypes.TimestampProto(info.GetOccurredAt())
if err != nil {
return nil, err
}
nev := &event.NodeExecutionEvent{
Id: nodeExecID,
Phase: ToNodeExecEventPhase(info.GetPhase()),
InputUri: reader.GetInputPath().String(),
OccurredAt: occurredTime,
}
if eventVersion == v1alpha1.EventVersion0 && status.GetParentTaskID() != nil {
nev.ParentTaskMetadata = &event.ParentTaskExecutionMetadata{
Id: status.GetParentTaskID(),
}
}
if eventVersion != v1alpha1.EventVersion0 {
currentNodeUniqueID, err := common.GenerateUniqueID(parentInfo, nev.Id.NodeId)
if err != nil {
return nil, err
}
if parentInfo != nil {
nev.ParentNodeMetadata = &event.ParentNodeExecutionMetadata{
NodeId: parentInfo.GetUniqueID(),
}
nev.RetryGroup = strconv.Itoa(int(parentInfo.CurrentAttempt()))
}
nev.SpecNodeId = node.GetID()
nev.Id.NodeId = currentNodeUniqueID
nev.NodeName = node.GetName()
}
eInfo := info.GetInfo()
if eInfo != nil {
if eInfo.WorkflowNodeInfo != nil {
v := ToNodeExecWorkflowNodeMetadata(eInfo.WorkflowNodeInfo)
if v != nil {
nev.TargetMetadata = v
}
} else if eInfo.TaskNodeInfo != nil {
v := ToNodeExecTaskNodeMetadata(eInfo.TaskNodeInfo)
if v != nil {
nev.TargetMetadata = v
}
}
}
if eInfo != nil && eInfo.OutputInfo != nil {
nev.OutputResult = ToNodeExecOutput(eInfo.OutputInfo)
} else if info.GetErr() != nil {
nev.OutputResult = &event.NodeExecutionEvent_Error{
Error: info.GetErr(),
}
}
return nev, nil
}
func ToNodePhase(p handler.EPhase) (v1alpha1.NodePhase, error) {
switch p {
case handler.EPhaseNotReady:
return v1alpha1.NodePhaseNotYetStarted, nil
case handler.EPhaseQueued:
return v1alpha1.NodePhaseQueued, nil
case handler.EPhaseRunning:
return v1alpha1.NodePhaseRunning, nil
case handler.EPhaseRetryableFailure:
return v1alpha1.NodePhaseRetryableFailure, nil
case handler.EPhaseSkip:
return v1alpha1.NodePhaseSkipped, nil
case handler.EPhaseSuccess:
return v1alpha1.NodePhaseSucceeding, nil
case handler.EPhaseFailed:
return v1alpha1.NodePhaseFailing, nil
case handler.EPhaseTimedout:
return v1alpha1.NodePhaseTimingOut, nil
}
return v1alpha1.NodePhaseNotYetStarted, fmt.Errorf("no known conversion from handlerPhase[%d] to NodePhase", p)
}
func ToK8sTime(t time.Time) v1.Time {
return v1.Time{Time: t}
}
func UpdateNodeStatus(np v1alpha1.NodePhase, p handler.PhaseInfo, n *nodeStateManager, s v1alpha1.ExecutableNodeStatus) {
// We update the phase only if it is not already updated
if np != s.GetPhase() {
s.UpdatePhase(np, ToK8sTime(p.GetOccurredAt()), p.GetReason(), p.GetErr())
}
// Update TaskStatus
if n.t != nil {
t := s.GetOrCreateTaskStatus()
t.SetPhaseVersion(n.t.PluginPhaseVersion)
t.SetPhase(int(n.t.PluginPhase))
t.SetLastPhaseUpdatedAt(n.t.LastPhaseUpdatedAt)
t.SetPluginState(n.t.PluginState)
t.SetPluginStateVersion(n.t.PluginStateVersion)
t.SetBarrierClockTick(n.t.BarrierClockTick)
}
// Update dynamic node status
if n.d != nil {
t := s.GetOrCreateDynamicNodeStatus()
t.SetDynamicNodePhase(n.d.Phase)
t.SetDynamicNodeReason(n.d.Reason)
t.SetExecutionError(n.d.Error)
}
// Update branch node status
if n.b != nil {
t := s.GetOrCreateBranchStatus()
if n.b.Phase == v1alpha1.BranchNodeError {
t.SetBranchNodeError()
} else if n.b.FinalizedNodeID != nil {
t.SetBranchNodeSuccess(*n.b.FinalizedNodeID)
} else {
logger.Warnf(context.TODO(), "branch node status neither success nor error set")
}
}
// Update workflow node status
if n.w != nil {
t := s.GetOrCreateWorkflowStatus()
t.SetWorkflowNodePhase(n.w.Phase)
t.SetExecutionError(n.w.Error)
}
}