-
Notifications
You must be signed in to change notification settings - Fork 669
/
executor.go
229 lines (185 loc) · 7.52 KB
/
executor.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
package k8s
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/errors"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/pluginmachinery/core"
"github.com/flyteorg/flyte/flyteplugins/go/tasks/plugins/array"
arrayCore "github.com/flyteorg/flyte/flyteplugins/go/tasks/plugins/array/core"
"github.com/flyteorg/flyte/flytestdlib/logger"
"github.com/flyteorg/flyte/flytestdlib/promutils"
)
const executorName = "k8s-array"
const arrayTaskType = "container_array"
const pluginStateVersion = 0
type Executor struct {
kubeClient core.KubeClient
outputsAssembler array.OutputAssembler
errorAssembler array.OutputAssembler
}
type KubeClientObj struct {
client client.Client
}
func (k KubeClientObj) GetClient() client.Client {
return k.client
}
func (k KubeClientObj) GetCache() cache.Cache {
return nil
}
func NewKubeClientObj(c client.Client) core.KubeClient {
return &KubeClientObj{
client: c,
}
}
func NewExecutor(kubeClient core.KubeClient, cfg *Config, scope promutils.Scope) (Executor, error) {
outputAssembler, err := array.NewOutputAssembler(cfg.OutputAssembler, scope.NewSubScope("output_assembler"))
if err != nil {
return Executor{}, err
}
errorAssembler, err := array.NewErrorAssembler(cfg.MaxErrorStringLength, cfg.ErrorAssembler, scope.NewSubScope("error_assembler"))
if err != nil {
return Executor{}, err
}
return Executor{
kubeClient: kubeClient,
outputsAssembler: outputAssembler,
errorAssembler: errorAssembler,
}, nil
}
func (e Executor) GetID() string {
return executorName
}
func (Executor) GetProperties() core.PluginProperties {
return core.PluginProperties{
DisableNodeLevelCaching: true,
}
}
func (e Executor) Handle(ctx context.Context, tCtx core.TaskExecutionContext) (core.Transition, error) {
pluginConfig := GetConfig()
pluginState := &arrayCore.State{}
if _, err := tCtx.PluginStateReader().Get(pluginState); err != nil {
return core.UnknownTransition, errors.Wrapf(errors.CorruptedPluginState, err, "Failed to read unmarshal custom state")
}
var nextState *arrayCore.State
var err error
var externalResources []*core.ExternalResource
switch p, version := pluginState.GetPhase(); p {
case arrayCore.PhaseStart:
nextState, err = array.DetermineDiscoverability(ctx, tCtx, pluginConfig.MaxArrayJobSize, pluginState)
nextPhase, _ := nextState.GetPhase()
if err == nil && nextPhase != arrayCore.PhaseStart && nextPhase != arrayCore.PhasePermanentFailure {
// we wait to transition out of PhaseStart to InitializeExternalResources because then the array
// job configuration has then been validated and all of the metadata necessary to report subtask
// status (ie. cache hit / etc) is available.
externalResources, err = arrayCore.InitializeExternalResources(ctx, tCtx, nextState,
func(tCtx core.TaskExecutionContext, childIndex int) string {
subTaskExecutionID := NewSubTaskExecutionID(tCtx.TaskExecutionMetadata().GetTaskExecutionID(), childIndex, 0)
return subTaskExecutionID.GetGeneratedName()
},
)
}
case arrayCore.PhasePreLaunch:
nextState = pluginState.SetPhase(arrayCore.PhaseLaunch, version+1).SetReason("Nothing to do in PreLaunch phase.")
case arrayCore.PhaseLaunch:
// In order to maintain backwards compatibility with the state transitions
// in the aws batch plugin. Forward to PhaseCheckingSubTasksExecutions where the launching
// is actually occurring.
nextState = pluginState.SetPhase(arrayCore.PhaseCheckingSubTaskExecutions, version+1).SetReason("Nothing to do in Launch phase.")
err = nil
case arrayCore.PhaseWaitingForResources:
fallthrough
case arrayCore.PhaseCheckingSubTaskExecutions:
nextState, externalResources, err = LaunchAndCheckSubTasksState(ctx, tCtx, e.kubeClient, pluginConfig,
tCtx.DataStore(), tCtx.OutputWriter().GetOutputPrefixPath(), tCtx.OutputWriter().GetRawOutputPrefix(), pluginState)
case arrayCore.PhaseAbortSubTasks:
nextState, externalResources, err = TerminateSubTasks(ctx, tCtx, e.kubeClient, GetConfig(), abortSubtask, pluginState)
case arrayCore.PhaseAssembleFinalOutput:
nextState, err = array.AssembleFinalOutputs(ctx, e.outputsAssembler, tCtx, arrayCore.PhaseSuccess, version+1, pluginState)
case arrayCore.PhaseWriteToDiscoveryThenFail:
nextState, externalResources, err = array.WriteToDiscovery(ctx, tCtx, pluginState, arrayCore.PhaseAssembleFinalError, version+1)
case arrayCore.PhaseWriteToDiscovery:
nextState, externalResources, err = array.WriteToDiscovery(ctx, tCtx, pluginState, arrayCore.PhaseAssembleFinalOutput, version+1)
case arrayCore.PhaseAssembleFinalError:
nextState, err = array.AssembleFinalOutputs(ctx, e.errorAssembler, tCtx, arrayCore.PhasePermanentFailure, version+1, pluginState)
default:
nextState = pluginState
err = nil
}
if err != nil {
return core.UnknownTransition, err
}
if err := tCtx.PluginStateWriter().Put(pluginStateVersion, nextState); err != nil {
return core.UnknownTransition, err
}
// Determine transition information from the state
phaseInfo, err := arrayCore.MapArrayStateToPluginPhase(ctx, nextState, nil, externalResources)
if err != nil {
return core.UnknownTransition, err
}
return core.DoTransition(phaseInfo), nil
}
func (e Executor) Abort(ctx context.Context, tCtx core.TaskExecutionContext) error {
pluginState := &arrayCore.State{}
if _, err := tCtx.PluginStateReader().Get(pluginState); err != nil {
return errors.Wrapf(errors.CorruptedPluginState, err, "Failed to read unmarshal custom state")
}
return TerminateSubTasksOnAbort(ctx, tCtx, e.kubeClient, GetConfig(), abortSubtask, pluginState)
}
func (e Executor) Finalize(ctx context.Context, tCtx core.TaskExecutionContext) error {
pluginState := &arrayCore.State{}
if _, err := tCtx.PluginStateReader().Get(pluginState); err != nil {
return errors.Wrapf(errors.CorruptedPluginState, err, "Failed to read unmarshal custom state")
}
_, _, err := TerminateSubTasks(ctx, tCtx, e.kubeClient, GetConfig(), finalizeSubtask, pluginState)
return err
}
func (e Executor) Start(ctx context.Context) error {
if err := e.outputsAssembler.Start(ctx); err != nil {
return err
}
if err := e.errorAssembler.Start(ctx); err != nil {
return err
}
return nil
}
func init() {
pluginmachinery.PluginRegistry().RegisterCorePlugin(
core.PluginEntry{
ID: executorName,
RegisteredTaskTypes: []core.TaskType{arrayTaskType},
LoadPlugin: GetNewExecutorPlugin,
IsDefault: false,
})
}
func GetNewExecutorPlugin(ctx context.Context, iCtx core.SetupContext) (core.Plugin, error) {
var kubeClient core.KubeClient
remoteClusterConfig := GetConfig().RemoteClusterConfig
if remoteClusterConfig.Enabled {
client, err := GetK8sClient(remoteClusterConfig)
if err != nil {
return nil, err
}
kubeClient = NewKubeClientObj(client)
} else {
kubeClient = iCtx.KubeClient()
}
exec, err := NewExecutor(kubeClient, GetConfig(), iCtx.MetricsScope())
if err != nil {
return nil, err
}
if err = exec.Start(ctx); err != nil {
return nil, err
}
resourceConfig := GetConfig().ResourceConfig
if IsResourceConfigSet(resourceConfig) {
primaryLabel := resourceConfig.PrimaryLabel
limit := resourceConfig.Limit
if err := iCtx.ResourceRegistrar().RegisterResourceQuota(ctx, core.ResourceNamespace(primaryLabel), limit); err != nil {
logger.Errorf(ctx, "Token Resource registration for [%v] failed due to error [%v]", primaryLabel, err)
return nil, err
}
}
return exec, nil
}