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 53
/
phase.go
272 lines (230 loc) · 8.87 KB
/
phase.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
package core
import (
"fmt"
"time"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
structpb "github.com/golang/protobuf/ptypes/struct"
)
const DefaultPhaseVersion = uint32(0)
const SystemErrorCode = "SystemError"
//go:generate enumer -type=Phase
type Phase int8
const (
// Does not mean an error, but simply states that we dont know the state in this round, try again later. But can be used to signal a system error too
PhaseUndefined Phase = iota
PhaseNotReady
// Indicates plugin is not ready to submit the request as it is waiting for resources
PhaseWaitingForResources
// Indicates plugin has submitted the execution, but it has not started executing yet
PhaseQueued
// The system has started the pre-execution process, like container download, cluster startup etc
PhaseInitializing
// Indicates that the task has started executing
PhaseRunning
// Indicates that the task has completed successfully
PhaseSuccess
// Indicates that the Failure is recoverable, by re-executing the task if retries permit
PhaseRetryableFailure
// Indicate that the failure is non recoverable even if retries exist
PhasePermanentFailure
// Indicates the task is waiting for the cache to be populated so it can reuse results
PhaseWaitingForCache
)
var Phases = []Phase{
PhaseUndefined,
PhaseNotReady,
PhaseWaitingForResources,
PhaseQueued,
PhaseInitializing,
PhaseRunning,
PhaseSuccess,
PhaseRetryableFailure,
PhasePermanentFailure,
PhaseWaitingForCache,
}
// Returns true if the given phase is failure, retryable failure or success
func (p Phase) IsTerminal() bool {
return p.IsFailure() || p.IsSuccess()
}
func (p Phase) IsFailure() bool {
return p == PhasePermanentFailure || p == PhaseRetryableFailure
}
func (p Phase) IsSuccess() bool {
return p == PhaseSuccess
}
func (p Phase) IsWaitingForResources() bool {
return p == PhaseWaitingForResources
}
type ExternalResource struct {
// A unique identifier for the external resource
ExternalID string
// Captures the status of caching for this external resource
CacheStatus core.CatalogCacheStatus
// A unique index for the external resource. Although the ID may change, this will remain the same
// throughout task event reports and retries.
Index uint32
// Log information for the external resource
Logs []*core.TaskLog
// The number of times this external resource has been attempted
RetryAttempt uint32
// Phase (if exists) associated with the external resource
Phase Phase
}
type TaskInfo struct {
// log information for the task execution
Logs []*core.TaskLog
// This value represents the time the status occurred at. If not provided, it will be defaulted to the time Flyte
// checked the task status.
OccurredAt *time.Time
// This value represents the time the status was reported at. If not provided, will be defaulted to the current time
// when Flyte published the event.
ReportedAt *time.Time
// Custom Event information that the plugin would like to expose to the front-end
CustomInfo *structpb.Struct
// A collection of information about external resources launched by this task
ExternalResources []*ExternalResource
}
func (t *TaskInfo) String() string {
return fmt.Sprintf("Info<@%s>", t.OccurredAt.String())
}
// Additional info that should be sent to the front end. The Information is sent to the front-end if it meets certain
// criterion, for example currently, it is sent only if an event was not already sent for
type PhaseInfo struct {
// Observed Phase of the launched Task execution
phase Phase
// Phase version. by default this can be left as empty => 0. This can be used if there is some additional information
// to be provided to the Control plane. Phase information is immutable in control plane for a given Phase, unless
// a new version is provided.
version uint32
// In case info needs to be provided
info *TaskInfo
// If only an error is observed. It is complementary to info
err *core.ExecutionError
// reason why the current phase exists.
reason string
// cleanupOnFailure indicates that this task should be cleaned up even though the phase indicates a failure. This
// applies to situations where a task is marked a failure but is still running, for example an ImagePullBackoff in
// a k8s Pod where the image does not exist will continually reattempt the pull even though it will never succeed.
cleanupOnFailure bool
}
func (p PhaseInfo) Phase() Phase {
return p.phase
}
func (p PhaseInfo) Version() uint32 {
return p.version
}
func (p PhaseInfo) Reason() string {
return p.reason
}
func (p PhaseInfo) Info() *TaskInfo {
return p.info
}
func (p PhaseInfo) Err() *core.ExecutionError {
return p.err
}
func (p PhaseInfo) CleanupOnFailure() bool {
return p.cleanupOnFailure
}
func (p PhaseInfo) WithVersion(version uint32) PhaseInfo {
return PhaseInfo{
phase: p.phase,
version: version,
info: p.info,
err: p.err,
reason: p.reason,
}
}
func (p PhaseInfo) String() string {
if p.err != nil {
return fmt.Sprintf("Phase<%s:%d Error:%s>", p.phase, p.version, p.err)
}
return fmt.Sprintf("Phase<%s:%d %s Reason:%s>", p.phase, p.version, p.info, p.reason)
}
// PhaseInfoUndefined should be used when the Phase is unknown usually associated with an error
var PhaseInfoUndefined = PhaseInfo{phase: PhaseUndefined}
func phaseInfo(p Phase, v uint32, err *core.ExecutionError, info *TaskInfo, cleanupOnFailure bool) PhaseInfo {
if info == nil {
info = &TaskInfo{}
}
if info.OccurredAt == nil {
t := time.Now()
info.OccurredAt = &t
}
return PhaseInfo{
phase: p,
version: v,
info: info,
err: err,
cleanupOnFailure: cleanupOnFailure,
}
}
// Return in the case the plugin is not ready to start
func PhaseInfoNotReady(t time.Time, version uint32, reason string) PhaseInfo {
pi := phaseInfo(PhaseNotReady, version, nil, &TaskInfo{OccurredAt: &t}, false)
pi.reason = reason
return pi
}
// Deprecated: Please use PhaseInfoWaitingForResourcesInfo instead
func PhaseInfoWaitingForResources(t time.Time, version uint32, reason string) PhaseInfo {
pi := phaseInfo(PhaseWaitingForResources, version, nil, &TaskInfo{OccurredAt: &t}, false)
pi.reason = reason
return pi
}
// Return in the case the plugin is not ready to start
func PhaseInfoWaitingForResourcesInfo(t time.Time, version uint32, reason string, info *TaskInfo) PhaseInfo {
pi := phaseInfo(PhaseWaitingForResources, version, nil, info, false)
pi.reason = reason
return pi
}
func PhaseInfoQueued(t time.Time, version uint32, reason string) PhaseInfo {
pi := phaseInfo(PhaseQueued, version, nil, &TaskInfo{OccurredAt: &t}, false)
pi.reason = reason
return pi
}
func PhaseInfoQueuedWithTaskInfo(version uint32, reason string, info *TaskInfo) PhaseInfo {
pi := phaseInfo(PhaseQueued, version, nil, info, false)
pi.reason = reason
return pi
}
func PhaseInfoInitializing(t time.Time, version uint32, reason string, info *TaskInfo) PhaseInfo {
pi := phaseInfo(PhaseInitializing, version, nil, info, false)
pi.reason = reason
return pi
}
func phaseInfoFailed(p Phase, err *core.ExecutionError, info *TaskInfo, cleanupOnFailure bool) PhaseInfo {
if err == nil {
err = &core.ExecutionError{
Code: "Unknown",
Message: "Unknown error message",
}
}
return phaseInfo(p, DefaultPhaseVersion, err, info, cleanupOnFailure)
}
func PhaseInfoFailed(p Phase, err *core.ExecutionError, info *TaskInfo) PhaseInfo {
return phaseInfo(p, DefaultPhaseVersion, err, info, false)
}
func PhaseInfoRunning(version uint32, info *TaskInfo) PhaseInfo {
return phaseInfo(PhaseRunning, version, nil, info, false)
}
func PhaseInfoSuccess(info *TaskInfo) PhaseInfo {
return phaseInfo(PhaseSuccess, DefaultPhaseVersion, nil, info, false)
}
func PhaseInfoSystemFailure(code, reason string, info *TaskInfo) PhaseInfo {
return PhaseInfoFailed(PhasePermanentFailure, &core.ExecutionError{Code: code, Message: reason, Kind: core.ExecutionError_SYSTEM}, info)
}
func PhaseInfoFailure(code, reason string, info *TaskInfo) PhaseInfo {
return PhaseInfoFailed(PhasePermanentFailure, &core.ExecutionError{Code: code, Message: reason, Kind: core.ExecutionError_USER}, info)
}
func PhaseInfoRetryableFailure(code, reason string, info *TaskInfo) PhaseInfo {
return PhaseInfoFailed(PhaseRetryableFailure, &core.ExecutionError{Code: code, Message: reason, Kind: core.ExecutionError_USER}, info)
}
func PhaseInfoRetryableFailureWithCleanup(code, reason string, info *TaskInfo) PhaseInfo {
return phaseInfoFailed(PhaseRetryableFailure, &core.ExecutionError{Code: code, Message: reason, Kind: core.ExecutionError_USER}, info, true)
}
func PhaseInfoSystemRetryableFailure(code, reason string, info *TaskInfo) PhaseInfo {
return PhaseInfoFailed(PhaseRetryableFailure, &core.ExecutionError{Code: code, Message: reason, Kind: core.ExecutionError_SYSTEM}, info)
}
// Creates a new PhaseInfo with phase set to PhaseWaitingForCache
func PhaseInfoWaitingForCache(version uint32, info *TaskInfo) PhaseInfo {
return phaseInfo(PhaseWaitingForCache, version, nil, info, false)
}