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 60
/
handler.go
231 lines (196 loc) · 8.49 KB
/
handler.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
package gate
import (
"context"
"fmt"
"time"
"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/service"
"github.com/flyteorg/flytepropeller/pkg/apis/flyteworkflow/v1alpha1"
"github.com/flyteorg/flytepropeller/pkg/controller/config"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/errors"
"github.com/flyteorg/flytepropeller/pkg/controller/nodes/handler"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flytestdlib/storage"
)
//go:generate mockery -all -case=underscore
// SignalServiceClient is a SignalServiceClient wrapper interface used specifically for generating
// mocks for testing
type SignalServiceClient interface {
service.SignalServiceClient
}
// gateNodeHandler is a handle implementation for processing gate nodes
type gateNodeHandler struct {
signalClient SignalServiceClient
metrics metrics
}
// metrics encapsulates the prometheus metrics for this handler
type metrics struct {
scope promutils.Scope
}
// newMetrics initializes a new metrics struct
func newMetrics(scope promutils.Scope) metrics {
return metrics{
scope: scope,
}
}
// Abort stops the gate node defined in the NodeExecutionContext
func (g *gateNodeHandler) Abort(ctx context.Context, nCtx handler.NodeExecutionContext, reason string) error {
return nil
}
// Finalize completes the gate node defined in the NodeExecutionContext
func (g *gateNodeHandler) Finalize(ctx context.Context, _ handler.NodeExecutionContext) error {
return nil
}
// FinalizeRequired defines whether or not this handler requires finalize to be called on
// node completion
func (g *gateNodeHandler) FinalizeRequired() bool {
return false
}
// Handle is responsible for transitioning and reporting node state to complete the node defined
// by the NodeExecutionContext
func (g *gateNodeHandler) Handle(ctx context.Context, nCtx handler.NodeExecutionContext) (handler.Transition, error) {
gateNode := nCtx.Node().GetGateNode()
gateNodeState := nCtx.NodeStateReader().GetGateNodeState()
if gateNodeState.Phase == v1alpha1.GateNodePhaseUndefined {
gateNodeState.Phase = v1alpha1.GateNodePhaseExecuting
}
switch gateNode.GetKind() {
case v1alpha1.ConditionKindApprove:
// retrieve approve condition
approveCondition := gateNode.GetApprove()
if approveCondition == nil {
errMsg := "gateNode approve condition is nil"
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM,
errors.BadSpecificationError, errMsg, nil)), nil
}
// use admin client to query for signal
request := &admin.SignalGetOrCreateRequest{
Id: &core.SignalIdentifier{
ExecutionId: nCtx.ExecutionContext().GetExecutionID().WorkflowExecutionIdentifier,
SignalId: approveCondition.SignalId,
},
Type: &core.LiteralType{
Type: &core.LiteralType_Simple{
Simple: core.SimpleType_BOOLEAN,
},
},
}
signal, err := g.signalClient.GetOrCreateSignal(ctx, request)
if err != nil {
return handler.UnknownTransition, err
}
// if signal has value then check for approval
if signal.Value != nil && signal.Value.Value != nil {
approved, ok := getBoolean(signal.Value)
if !ok {
errMsg := fmt.Sprintf("received a non-boolean approve signal value [%v]", signal.Value)
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_UNKNOWN,
errors.RuntimeExecutionError, errMsg, nil)), nil
}
if !approved {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_USER,
"ReceivedRejectSignal", "received a reject signal to disapprove the node input values", nil)), nil
}
// copy input values to outputs
inputs, err := nCtx.InputReader().Get(ctx)
if err != nil {
errMsg := fmt.Sprintf("failed to read input with error [%s]", err)
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM, errors.RuntimeExecutionError, errMsg, nil)), nil
}
outputFile := v1alpha1.GetOutputsFile(nCtx.NodeStatus().GetOutputDir())
if err := nCtx.DataStore().WriteProtobuf(ctx, outputFile, storage.Options{}, inputs); err != nil {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM, "WriteOutputsFailed",
fmt.Sprintf("failed to write signal value to [%v] with error [%s]", outputFile, err.Error()), nil)), nil
}
o := &handler.OutputInfo{OutputURI: outputFile}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoSuccess(&handler.ExecutionInfo{
OutputInfo: o,
})), nil
}
case v1alpha1.ConditionKindSignal:
// retrieve signal condition
signalCondition := gateNode.GetSignal()
if signalCondition == nil {
errMsg := "gateNode signal condition is nil"
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM,
errors.BadSpecificationError, errMsg, nil)), nil
}
// use admin client to query for signal
request := &admin.SignalGetOrCreateRequest{
Id: &core.SignalIdentifier{
ExecutionId: nCtx.ExecutionContext().GetExecutionID().WorkflowExecutionIdentifier,
SignalId: signalCondition.SignalId,
},
Type: signalCondition.Type,
}
signal, err := g.signalClient.GetOrCreateSignal(ctx, request)
if err != nil {
return handler.UnknownTransition, err
}
// if signal has value then write to output and transition to success
if signal.Value != nil && signal.Value.Value != nil {
outputs := &core.LiteralMap{
Literals: map[string]*core.Literal{
signalCondition.OutputVariableName: signal.Value,
},
}
outputFile := v1alpha1.GetOutputsFile(nCtx.NodeStatus().GetOutputDir())
if err := nCtx.DataStore().WriteProtobuf(ctx, outputFile, storage.Options{}, outputs); err != nil {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM, "WriteOutputsFailed",
fmt.Sprintf("failed to write signal value to [%v] with error: %s", outputFile, err.Error()), nil)), nil
}
o := &handler.OutputInfo{OutputURI: outputFile}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoSuccess(&handler.ExecutionInfo{
OutputInfo: o,
})), nil
}
case v1alpha1.ConditionKindSleep:
// retrieve sleep duration
sleepCondition := gateNode.GetSleep()
if sleepCondition == nil {
errMsg := "gateNode sleep condition is nil"
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM,
errors.BadSpecificationError, errMsg, nil)), nil
}
sleepDuration := sleepCondition.GetDuration().AsDuration()
// check duration of node sleep
lastAttemptStartedAt := nCtx.NodeStatus().GetLastAttemptStartedAt()
if lastAttemptStartedAt != nil && sleepDuration <= time.Since(lastAttemptStartedAt.Time) {
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoSuccess(&handler.ExecutionInfo{})), nil
}
default:
errMsg := "gateNode does not have a supported condition reference"
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoFailure(core.ExecutionError_SYSTEM,
errors.BadSpecificationError, errMsg, nil)), nil
}
// update gate node status
if err := nCtx.NodeStateWriter().PutGateNodeState(gateNodeState); err != nil {
logger.Errorf(ctx, "failed to store TaskNode state with err [%s]", err.Error())
return handler.UnknownTransition, err
}
return handler.DoTransition(handler.TransitionTypeEphemeral, handler.PhaseInfoRunning(&handler.ExecutionInfo{})), nil
}
// Setup handles any initialization requirements for this handler
func (g *gateNodeHandler) Setup(_ context.Context, _ handler.SetupContext) error {
return nil
}
// New initializes a new gateNodeHandler
func New(eventConfig *config.EventConfig, signalClient service.SignalServiceClient, scope promutils.Scope) handler.Node {
gateScope := scope.NewSubScope("gate")
return &gateNodeHandler{
signalClient: signalClient,
metrics: newMetrics(gateScope),
}
}
func getBoolean(literal *core.Literal) (bool, bool) {
if scalarValue, ok := literal.Value.(*core.Literal_Scalar); ok {
if primitiveValue, ok := scalarValue.Scalar.Value.(*core.Scalar_Primitive); ok {
if booleanValue, ok := primitiveValue.Primitive.Value.(*core.Primitive_Boolean); ok {
return booleanValue.Boolean, true
}
}
}
return false, false
}