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
/
Copy pathstate.go
328 lines (269 loc) · 9.78 KB
/
state.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package core
import (
"context"
"fmt"
"time"
"github.com/flyteorg/flytestdlib/errors"
"github.com/flyteorg/flyteplugins/go/tasks/plugins/array/arraystatus"
"github.com/flyteorg/flytestdlib/bitarray"
idlCore "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
idlPlugins "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/plugins"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyteplugins/go/tasks/pluginmachinery/utils"
"github.com/flyteorg/flytestdlib/logger"
structpb "github.com/golang/protobuf/ptypes/struct"
)
//go:generate mockery -all -case=underscore
//go:generate enumer -type=Phase
type Phase uint8
const (
PhaseStart Phase = iota
PhasePreLaunch
PhaseLaunch
PhaseWaitingForResources
PhaseCheckingSubTaskExecutions
PhaseAssembleFinalOutput
PhaseWriteToDiscovery
PhaseWriteToDiscoveryThenFail
PhaseSuccess
PhaseAssembleFinalError
PhaseRetryableFailure
PhasePermanentFailure
)
type State struct {
CurrentPhase Phase `json:"phase"`
PhaseVersion uint32 `json:"phaseVersion"`
Reason string `json:"reason"`
ExecutionErr *idlCore.ExecutionError `json:"err"`
ExecutionArraySize int `json:"arraySize"`
OriginalArraySize int64 `json:"originalArraySize"`
ArrayStatus arraystatus.ArrayStatus `json:"arrayStatus"`
OriginalMinSuccesses int64 `json:"minSuccess"`
// Which sub-tasks to cache, (using the original index, that is, the length is ArrayJob.size)
IndexesToCache *bitarray.BitSet `json:"indexesToCache"`
// Tracks the number of subtask retries using the execution index
RetryAttempts bitarray.CompactArray `json:"retryAttempts"`
// Tracks the number of system failures for each subtask using the execution index
SystemFailures bitarray.CompactArray `json:"systemFailures"`
}
func (s State) GetReason() string {
return s.Reason
}
func (s State) GetExecutionArraySize() int {
return s.ExecutionArraySize
}
func (s State) GetPhase() (phase Phase, version uint32) {
return s.CurrentPhase, s.PhaseVersion
}
func (s State) GetArrayStatus() arraystatus.ArrayStatus {
return s.ArrayStatus
}
func (s *State) GetOriginalArraySize() int64 {
return s.OriginalArraySize
}
func (s *State) GetOriginalMinSuccesses() int64 {
return s.OriginalMinSuccesses
}
func (s *State) GetIndexesToCache() *bitarray.BitSet {
return s.IndexesToCache
}
func (s *State) GetExecutionErr() *idlCore.ExecutionError {
return s.ExecutionErr
}
func (s *State) SetExecutionErr(err *idlCore.ExecutionError) *State {
s.ExecutionErr = err
return s
}
func (s *State) SetIndexesToCache(set *bitarray.BitSet) *State {
s.IndexesToCache = set
return s
}
func (s *State) SetOriginalArraySize(size int64) *State {
s.OriginalArraySize = size
return s
}
func (s *State) SetOriginalMinSuccesses(size int64) *State {
s.OriginalMinSuccesses = size
return s
}
func (s *State) SetReason(reason string) *State {
s.Reason = reason
return s
}
func (s *State) SetRetryAttempts(retryAttempts bitarray.CompactArray) *State {
s.RetryAttempts = retryAttempts
return s
}
func (s *State) SetExecutionArraySize(size int) *State {
s.ExecutionArraySize = size
return s
}
func (s *State) SetPhase(phase Phase, phaseVersion uint32) *State {
s.CurrentPhase = phase
s.PhaseVersion = phaseVersion
return s
}
func (s *State) SetArrayStatus(state arraystatus.ArrayStatus) *State {
s.ArrayStatus = state
return s
}
const (
ErrorWorkQueue errors.ErrorCode = "CATALOG_READER_QUEUE_FAILED"
ErrorInternalMismatch errors.ErrorCode = "ARRAY_MISMATCH"
ErrorK8sArrayGeneric errors.ErrorCode = "ARRAY_JOB_GENERIC_FAILURE"
)
func ToArrayJob(structObj *structpb.Struct, taskTypeVersion int32) (*idlPlugins.ArrayJob, error) {
if structObj == nil {
if taskTypeVersion == 0 {
return &idlPlugins.ArrayJob{
Parallelism: 0,
Size: 1,
SuccessCriteria: &idlPlugins.ArrayJob_MinSuccesses{
MinSuccesses: 1,
},
}, nil
}
return &idlPlugins.ArrayJob{
Parallelism: 0,
Size: 1,
SuccessCriteria: &idlPlugins.ArrayJob_MinSuccessRatio{
MinSuccessRatio: 1.0,
},
}, nil
}
arrayJob := &idlPlugins.ArrayJob{}
err := utils.UnmarshalStruct(structObj, arrayJob)
return arrayJob, err
}
// Any state of the plugin needs to map to a core.PhaseInfo (which in turn will map to Admin events) so that the rest
// of the Flyte platform can understand what's happening. That is, each possible state that our plugin state
// machine returns should map to a unique (core.Phase, core.PhaseInfo.version).
// Info fields will always be nil, because we're going to send log links individually. This simplifies our state
// handling as we don't have to keep an ever growing list of log links (our batch jobs can be 5000 sub-tasks, keeping
// all the log links takes up a lot of space).
func MapArrayStateToPluginPhase(_ context.Context, state *State, logLinks []*idlCore.TaskLog, externalResources []*core.ExternalResource) (core.PhaseInfo, error) {
phaseInfo := core.PhaseInfoUndefined
t := time.Now()
nowTaskInfo := &core.TaskInfo{
OccurredAt: &t,
Logs: logLinks,
ExternalResources: externalResources,
}
switch p, version := state.GetPhase(); p {
case PhaseStart:
phaseInfo = core.PhaseInfoInitializing(t, core.DefaultPhaseVersion, state.GetReason(), nowTaskInfo)
case PhaseWaitingForResources:
phaseInfo = core.PhaseInfoWaitingForResourcesInfo(t, version, state.GetReason(), nowTaskInfo)
case PhasePreLaunch:
fallthrough
case PhaseLaunch:
fallthrough
case PhaseCheckingSubTaskExecutions:
fallthrough
case PhaseAssembleFinalOutput:
fallthrough
case PhaseAssembleFinalError:
fallthrough
case PhaseWriteToDiscoveryThenFail:
fallthrough
case PhaseWriteToDiscovery:
// The state version is only incremented in PhaseCheckingSubTaskExecutions when subtask
// phases are updated. Therefore by adding the phase to the state version we ensure that
// (1) all phase changes will have a new phase version and (2) all subtask phase updates
// result in monotonically increasing phase version.
phaseInfo = core.PhaseInfoRunning(version+uint32(p), nowTaskInfo)
case PhaseSuccess:
phaseInfo = core.PhaseInfoSuccess(nowTaskInfo)
case PhaseRetryableFailure:
if state.GetExecutionErr() != nil {
phaseInfo = core.PhaseInfoFailed(core.PhaseRetryableFailure, state.GetExecutionErr(), nowTaskInfo)
} else {
phaseInfo = core.PhaseInfoRetryableFailure(ErrorK8sArrayGeneric, state.GetReason(), nowTaskInfo)
}
case PhasePermanentFailure:
if state.GetExecutionErr() != nil {
phaseInfo = core.PhaseInfoFailed(core.PhasePermanentFailure, state.GetExecutionErr(), nowTaskInfo)
} else {
phaseInfo = core.PhaseInfoSystemFailure(ErrorK8sArrayGeneric, state.GetReason(), nowTaskInfo)
}
default:
return phaseInfo, fmt.Errorf("failed to map custom state phase to core phase. State Phase [%v]", p)
}
return phaseInfo, nil
}
func SummaryToPhase(ctx context.Context, minSuccesses int64, summary arraystatus.ArraySummary) Phase {
totalCount := int64(0)
totalSuccesses := int64(0)
totalPermanentFailures := int64(0)
totalRetryableFailures := int64(0)
totalRunning := int64(0)
totalWaitingForResources := int64(0)
for phase, count := range summary {
totalCount += count
switch phase {
case core.PhaseSuccess:
totalSuccesses += count
case core.PhasePermanentFailure:
totalPermanentFailures += count
case core.PhaseRetryableFailure:
totalRetryableFailures += count
case core.PhaseWaitingForResources:
totalWaitingForResources += count
default:
totalRunning += count
}
}
if totalCount < minSuccesses {
logger.Infof(ctx, "Array failed because totalCount[%v] < minSuccesses[%v]", totalCount, minSuccesses)
return PhaseWriteToDiscoveryThenFail
}
// No chance to reach the required success numbers.
if totalRunning+totalSuccesses+totalWaitingForResources+totalRetryableFailures < minSuccesses {
logger.Infof(ctx, "Array failed early because total failures > minSuccesses[%v]. Snapshot totalRunning[%v] + totalSuccesses[%v] + totalWaitingForResource[%v] + totalRetryableFailures[%v]",
minSuccesses, totalRunning, totalSuccesses, totalWaitingForResources, totalRetryableFailures)
return PhaseWriteToDiscoveryThenFail
}
if totalWaitingForResources > 0 {
logger.Infof(ctx, "Array is still running and waiting for resources totalWaitingForResources[%v]", totalWaitingForResources)
return PhaseWaitingForResources
}
if totalSuccesses >= minSuccesses && totalRunning == 0 {
logger.Infof(ctx, "Array succeeded because totalSuccesses[%v] >= minSuccesses[%v]", totalSuccesses, minSuccesses)
return PhaseWriteToDiscovery
}
logger.Debugf(ctx, "Array is still running [Successes: %v, PermanentFailures: %v, RetryableFailures: %v, Total: %v, MinSuccesses: %v]",
totalSuccesses, totalPermanentFailures, totalRetryableFailures, totalCount, minSuccesses)
return PhaseCheckingSubTaskExecutions
}
func InvertBitSet(input *bitarray.BitSet, limit uint) *bitarray.BitSet {
output := bitarray.NewBitSet(limit)
for i := uint(0); i < limit; i++ {
if !input.IsSet(i) {
output.Set(i)
}
}
return output
}
func NewPhasesCompactArray(count uint) bitarray.CompactArray {
// TODO: This is fragile, we should introduce a TaskPhaseCount as the last element in the enum
a, err := bitarray.NewCompactArray(count, bitarray.Item(len(core.Phases)-1))
if err != nil {
logger.Warnf(context.Background(), "Failed to create compact array with provided parameters [count: %v]",
count)
return bitarray.CompactArray{}
}
return a
}
// CalculateOriginalIndex computes the original index of a sub-task.
func CalculateOriginalIndex(childIdx int, toCache *bitarray.BitSet) int {
var sum = 0
for i := uint(0); i < toCache.Cap(); i++ {
if toCache.IsSet(i) {
sum++
}
if childIdx+1 == sum {
return int(i)
}
}
return -1
}