forked from flyteorg/flyteplugins
/
monitor.go
139 lines (114 loc) · 4.62 KB
/
monitor.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
package awsbatch
import (
"context"
core2 "github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/io"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/ioutils"
"github.com/lyft/flyteplugins/go/tasks/plugins/array"
"github.com/lyft/flytestdlib/storage"
"github.com/lyft/flytestdlib/logger"
arrayCore "github.com/lyft/flyteplugins/go/tasks/plugins/array/core"
"github.com/lyft/flytestdlib/bitarray"
"github.com/lyft/flyteplugins/go/tasks/plugins/array/arraystatus"
"github.com/lyft/flyteplugins/go/tasks/plugins/array/awsbatch/config"
"github.com/lyft/flyteplugins/go/tasks/plugins/array/errorcollector"
"github.com/lyft/flyteplugins/go/tasks/pluginmachinery/core"
)
func createSubJobList(count int) []*Job {
res := make([]*Job, count)
for i := range res {
res[i] = &Job{
Status: JobStatus{Phase: core.PhaseNotReady},
}
}
return res
}
func CheckSubTasksState(ctx context.Context, taskMeta core.TaskExecutionMetadata, outputPrefix storage.DataReference, jobStore *JobStore,
dataStore *storage.DataStore, cfg *config.Config, currentState *State) (newState *State, err error) {
newState = currentState
parentState := currentState.State
jobName := taskMeta.GetTaskExecutionID().GetGeneratedName()
job := jobStore.Get(jobName)
// If job isn't currently being monitored (recovering from a restart?), add it to the sync-cache and return
if job == nil {
logger.Info(ctx, "Job not found in cache, adding it. [%v]", jobName)
_, err = jobStore.GetOrCreate(jobName, &Job{
ID: *currentState.ExternalJobID,
OwnerReference: taskMeta.GetOwnerID(),
SubJobs: createSubJobList(currentState.GetExecutionArraySize()),
})
if err != nil {
return nil, err
}
return currentState, nil
}
msg := errorcollector.NewErrorMessageCollector()
newArrayStatus := arraystatus.ArrayStatus{
Summary: arraystatus.ArraySummary{},
Detailed: arrayCore.NewPhasesCompactArray(uint(currentState.GetExecutionArraySize())),
}
for childIdx, subJob := range job.SubJobs {
actualPhase := subJob.Status.Phase
originalIdx := arrayCore.CalculateOriginalIndex(childIdx, currentState.GetIndexesToCache())
if subJob.Status.Phase.IsFailure() {
if len(subJob.Status.Message) > 0 {
// If the service reported an error but there is no error.pb written, write one with the
// service-provided error message.
msg.Collect(childIdx, subJob.Status.Message)
or, err := array.ConstructOutputReader(ctx, dataStore, outputPrefix, originalIdx)
if err != nil {
return nil, err
}
if hasErr, err := or.IsError(ctx); err != nil {
return nil, err
} else if !hasErr {
// The subtask has not produced an error.pb, write one.
ow, err := array.ConstructOutputWriter(ctx, dataStore, outputPrefix, originalIdx)
if err != nil {
return nil, err
}
if err = ow.Put(ctx, ioutils.NewInMemoryOutputReader(nil, &io.ExecutionError{
ExecutionError: &core2.ExecutionError{
Code: "",
Message: subJob.Status.Message,
ErrorUri: "",
},
IsRecoverable: false,
})); err != nil {
return nil, err
}
}
} else {
msg.Collect(childIdx, "Job failed")
}
} else if subJob.Status.Phase.IsSuccess() {
actualPhase, err = array.CheckTaskOutput(ctx, dataStore, outputPrefix, childIdx, originalIdx)
if err != nil {
return nil, err
}
}
newArrayStatus.Detailed.SetItem(childIdx, bitarray.Item(actualPhase))
newArrayStatus.Summary.Inc(actualPhase)
}
parentState = parentState.SetArrayStatus(newArrayStatus)
// Based on the summary produced above, deduce the overall phase of the task.
phase := arrayCore.SummaryToPhase(ctx, currentState.GetOriginalMinSuccesses()-currentState.GetOriginalArraySize()+int64(currentState.GetExecutionArraySize()), newArrayStatus.Summary)
if phase == arrayCore.PhaseWriteToDiscoveryThenFail {
errorMsg := msg.Summary(cfg.MaxErrorStringLength)
parentState = parentState.SetReason(errorMsg)
}
if phase == arrayCore.PhaseCheckingSubTaskExecutions {
newPhaseVersion := uint32(0)
// For now, the only changes to PhaseVersion and PreviousSummary occur for running array jobs.
for phase, count := range parentState.GetArrayStatus().Summary {
newPhaseVersion += uint32(phase) * uint32(count)
}
parentState = parentState.SetPhase(phase, newPhaseVersion).SetReason("Task is still running.")
} else {
parentState = parentState.SetPhase(phase, core.DefaultPhaseVersion)
}
p, v := parentState.GetPhase()
logger.Debugf(ctx, "Current phase [phase: %v, version: %v]. Summary: %+v", p, v, newArrayStatus.Summary)
newState.State = parentState
return newState, nil
}