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 63
/
workflow_manager.go
369 lines (343 loc) · 13.9 KB
/
workflow_manager.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package impl
import (
"bytes"
"context"
"strconv"
"time"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/util"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"
"github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
repoInterfaces "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flyteadmin/pkg/repositories/transformers"
runtimeInterfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
workflowengine "github.com/flyteorg/flyteadmin/pkg/workflowengine/impl"
workflowengineInterfaces "github.com/flyteorg/flyteadmin/pkg/workflowengine/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
compiler "github.com/flyteorg/flytepropeller/pkg/compiler/common"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flytestdlib/storage"
"github.com/golang/protobuf/ptypes"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/codes"
)
var defaultStorageOptions = storage.Options{}
type workflowMetrics struct {
Scope promutils.Scope
CompilationFailures prometheus.Counter
TypedInterfaceSizeBytes prometheus.Summary
}
type WorkflowManager struct {
db repoInterfaces.Repository
config runtimeInterfaces.Configuration
compiler workflowengineInterfaces.Compiler
storageClient *storage.DataStore
storagePrefix []string
metrics workflowMetrics
}
func getWorkflowContext(ctx context.Context, identifier *core.Identifier) context.Context {
ctx = contextutils.WithProjectDomain(ctx, identifier.Project, identifier.Domain)
return contextutils.WithWorkflowID(ctx, identifier.Name)
}
func (w *WorkflowManager) setDefaults(request admin.WorkflowCreateRequest) (admin.WorkflowCreateRequest, error) {
// TODO: Also add environment and configuration defaults once those have been determined.
if request.Id == nil {
return request, errors.NewFlyteAdminError(codes.InvalidArgument, "missing identifier for WorkflowCreateRequest")
}
request.Spec.Template.Id = request.Id
return request, nil
}
func (w *WorkflowManager) getCompiledWorkflow(
ctx context.Context, request admin.WorkflowCreateRequest) (admin.WorkflowClosure, error) {
reqs, err := w.compiler.GetRequirements(request.Spec.Template, request.Spec.SubWorkflows)
if err != nil {
w.metrics.CompilationFailures.Inc()
logger.Errorf(ctx, "Failed to get workflow requirements for template [%+v] with err %v",
request.Spec.Template, err)
return admin.WorkflowClosure{}, err
}
var tasks = make([]*core.CompiledTask, len(reqs.GetRequiredTaskIds()))
for idx, taskID := range reqs.GetRequiredTaskIds() {
task, err := util.GetTask(ctx, w.db, taskID)
if err != nil {
logger.Debugf(ctx, "Failed to get task with id [%+v] when compiling workflow with id [%+v] with err %v",
taskID, request.Id, err)
return admin.WorkflowClosure{}, err
}
tasks[idx] = task.Closure.CompiledTask
}
var launchPlans = make([]compiler.InterfaceProvider, len(reqs.GetRequiredLaunchPlanIds()))
for idx, launchPlanID := range reqs.GetRequiredLaunchPlanIds() {
var launchPlanModel models.LaunchPlan
launchPlanModel, err = util.GetLaunchPlanModel(ctx, w.db, launchPlanID)
if err != nil {
logger.Debugf(ctx, "Failed to get launch plan with id [%+v] when compiling workflow with id [%+v] with err %v",
launchPlanID, request.Id, err)
return admin.WorkflowClosure{}, err
}
var launchPlanInterfaceProvider workflowengine.InterfaceProvider
launchPlanInterfaceProvider, err = workflowengine.NewLaunchPlanInterfaceProvider(launchPlanModel, launchPlanID)
if err != nil {
logger.Debugf(ctx, "Failed to create LaunchPlanInterfaceProvider for launch plan [%+v] with err %v",
launchPlanModel, err)
return admin.WorkflowClosure{}, err
}
launchPlans[idx] = launchPlanInterfaceProvider
}
closure, err := w.compiler.CompileWorkflow(request.Spec.Template, request.Spec.SubWorkflows, tasks, launchPlans)
if err != nil {
w.metrics.CompilationFailures.Inc()
logger.Debugf(ctx, "Failed to compile workflow with id [%+v] with err %v", request.Id, err)
return admin.WorkflowClosure{}, err
}
createdAt, err := ptypes.TimestampProto(time.Now())
if err != nil {
return admin.WorkflowClosure{}, errors.NewFlyteAdminErrorf(codes.Internal,
"Failed to serialize CreatedAt: %v when saving compiled workflow %+v", err, request.Id)
}
return admin.WorkflowClosure{
CompiledWorkflow: closure,
CreatedAt: createdAt,
}, nil
}
func (w *WorkflowManager) createDataReference(
ctx context.Context, identifier *core.Identifier) (storage.DataReference, error) {
nestedSubKeys := []string{
identifier.Project,
identifier.Domain,
identifier.Name,
identifier.Version,
}
nestedKeys := append(w.storagePrefix, nestedSubKeys...)
return w.storageClient.ConstructReference(ctx, w.storageClient.GetBaseContainerFQN(ctx), nestedKeys...)
}
func (w *WorkflowManager) CreateWorkflow(
ctx context.Context,
request admin.WorkflowCreateRequest) (*admin.WorkflowCreateResponse, error) {
if err := validation.ValidateWorkflow(ctx, request, w.db, w.config.ApplicationConfiguration()); err != nil {
return nil, err
}
ctx = getWorkflowContext(ctx, request.Id)
finalizedRequest, err := w.setDefaults(request)
if err != nil {
logger.Debugf(ctx, "Failed to set defaults for workflow with id [%+v] with err %v", request.Id, err)
return nil, err
}
// Validate that the workflow compiles.
workflowClosure, err := w.getCompiledWorkflow(ctx, finalizedRequest)
if err != nil {
logger.Errorf(ctx, "Failed to compile workflow with err: %v", err)
return nil, errors.NewFlyteAdminErrorf(codes.Internal,
"failed to compile workflow for [%+v] with err %v", request.Id, err)
}
err = validation.ValidateCompiledWorkflow(
*request.Id, workflowClosure, w.config.RegistrationValidationConfiguration())
if err != nil {
return nil, err
}
workflowDigest, err := util.GetWorkflowDigest(ctx, workflowClosure.CompiledWorkflow)
if err != nil {
logger.Errorf(ctx, "failed to compute workflow digest with err %v", err)
return nil, err
}
// Assert that a matching workflow doesn't already exist before uploading the workflow closure.
existingWorkflowModel, err := util.GetWorkflowModel(ctx, w.db, *request.Id)
// Check that no identical or conflicting workflows exist.
if err == nil {
// A workflow's structure is uniquely defined by its collection of nodes.
if bytes.Equal(workflowDigest, existingWorkflowModel.Digest) {
return nil, errors.NewWorkflowExistsIdenticalStructureError(ctx, &request)
}
// A workflow exists with different structure
return nil, errors.NewWorkflowExistsDifferentStructureError(ctx, &request)
} else if flyteAdminError, ok := err.(errors.FlyteAdminError); !ok || flyteAdminError.Code() != codes.NotFound {
logger.Debugf(ctx, "Failed to get workflow for comparison in CreateWorkflow with ID [%+v] with err %v",
request.Id, err)
return nil, err
}
remoteClosureDataRef, err := w.createDataReference(ctx, request.Spec.Template.Id)
if err != nil {
logger.Infof(ctx, "failed to construct data reference for workflow closure with id [%+v] with err %v",
request.Id, err)
return nil, errors.NewFlyteAdminErrorf(codes.Internal,
"failed to construct data reference for workflow closure with id [%+v] and err %v", request.Id, err)
}
err = w.storageClient.WriteProtobuf(ctx, remoteClosureDataRef, defaultStorageOptions, &workflowClosure)
if err != nil {
logger.Infof(ctx,
"failed to write marshaled workflow with id [%+v] to storage %s with err %v and base container: %s",
request.Id, remoteClosureDataRef.String(), err, w.storageClient.GetBaseContainerFQN(ctx))
return nil, errors.NewFlyteAdminErrorf(codes.Internal,
"failed to write marshaled workflow [%+v] to storage %s with err %v and base container: %s",
request.Id, remoteClosureDataRef.String(), err, w.storageClient.GetBaseContainerFQN(ctx))
}
// Save the workflow & its reference to the offloaded, compiled workflow in the database.
workflowModel, err := transformers.CreateWorkflowModel(
finalizedRequest, remoteClosureDataRef.String(), workflowDigest)
if err != nil {
logger.Errorf(ctx,
"Failed to transform workflow model for request [%+v] and remoteClosureIdentifier [%s] with err: %v",
finalizedRequest, remoteClosureDataRef.String(), err)
return nil, err
}
descriptionModel, err := transformers.CreateDescriptionEntityModel(request.Spec.Description, *request.Id)
if err != nil {
logger.Errorf(ctx,
"Failed to transform description model [%+v] with err: %v", request.Spec.Description, err)
return nil, err
}
if descriptionModel != nil {
workflowModel.ShortDescription = descriptionModel.ShortDescription
}
if err = w.db.WorkflowRepo().Create(ctx, workflowModel, descriptionModel); err != nil {
logger.Infof(ctx, "Failed to create workflow model [%+v] with err %v", request.Id, err)
return nil, err
}
w.metrics.TypedInterfaceSizeBytes.Observe(float64(len(workflowModel.TypedInterface)))
return &admin.WorkflowCreateResponse{}, nil
}
func (w *WorkflowManager) GetWorkflow(ctx context.Context, request admin.ObjectGetRequest) (*admin.Workflow, error) {
if err := validation.ValidateIdentifier(request.Id, common.Workflow); err != nil {
logger.Debugf(ctx, "invalid identifier [%+v]: %v", request.Id, err)
return nil, err
}
ctx = getWorkflowContext(ctx, request.Id)
workflow, err := util.GetWorkflow(ctx, w.db, w.storageClient, *request.Id)
if err != nil {
logger.Infof(ctx, "Failed to get workflow with id [%+v] with err %v", request.Id, err)
return nil, err
}
return workflow, nil
}
// Returns workflows *without* a populated workflow closure.
func (w *WorkflowManager) ListWorkflows(
ctx context.Context, request admin.ResourceListRequest) (*admin.WorkflowList, error) {
// Check required fields
if err := validation.ValidateResourceListRequest(request); err != nil {
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, request.Id.Project, request.Id.Domain)
ctx = contextutils.WithWorkflowID(ctx, request.Id.Name)
filters, err := util.GetDbFilters(util.FilterSpec{
Project: request.Id.Project,
Domain: request.Id.Domain,
Name: request.Id.Name,
RequestFilters: request.Filters,
}, common.Workflow)
if err != nil {
return nil, err
}
var sortParameter common.SortParameter
if request.SortBy != nil {
sortParameter, err = common.NewSortParameter(*request.SortBy)
if err != nil {
return nil, err
}
}
offset, err := validation.ValidateToken(request.Token)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid pagination token %s for ListWorkflows", request.Token)
}
listWorkflowsInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := w.db.WorkflowRepo().List(ctx, listWorkflowsInput)
if err != nil {
logger.Debugf(ctx, "Failed to list workflows with [%+v] with err %v", request.Id, err)
return nil, err
}
workflowList, err := transformers.FromWorkflowModels(output.Workflows)
if err != nil {
logger.Errorf(ctx,
"Failed to transform workflow models [%+v] with err: %v", output.Workflows, err)
return nil, err
}
var token string
if len(output.Workflows) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.Workflows))
}
return &admin.WorkflowList{
Workflows: workflowList,
Token: token,
}, nil
}
func (w *WorkflowManager) ListWorkflowIdentifiers(ctx context.Context, request admin.NamedEntityIdentifierListRequest) (
*admin.NamedEntityIdentifierList, error) {
if err := validation.ValidateNamedEntityIdentifierListRequest(request); err != nil {
logger.Debugf(ctx, "invalid request [%+v]: %v", request, err)
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, request.Project, request.Domain)
filters, err := util.GetDbFilters(util.FilterSpec{
Project: request.Project,
Domain: request.Domain,
}, common.Workflow)
if err != nil {
return nil, err
}
var sortParameter common.SortParameter
if request.SortBy != nil {
sortParameter, err = common.NewSortParameter(*request.SortBy)
if err != nil {
return nil, err
}
}
offset, err := validation.ValidateToken(request.Token)
if err != nil {
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"invalid pagination token %s for ListWorkflowIdentifiers", request.Token)
}
listWorkflowsInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := w.db.WorkflowRepo().ListIdentifiers(ctx, listWorkflowsInput)
if err != nil {
logger.Debugf(ctx, "Failed to list workflow ids with project: %s and domain: %s with err %v",
request.Project, request.Domain, err)
return nil, err
}
var token string
if len(output.Workflows) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.Workflows))
}
entities := transformers.FromWorkflowModelsToIdentifiers(output.Workflows)
return &admin.NamedEntityIdentifierList{
Entities: entities,
Token: token,
}, nil
}
func NewWorkflowManager(
db repoInterfaces.Repository,
config runtimeInterfaces.Configuration,
compiler workflowengineInterfaces.Compiler,
storageClient *storage.DataStore,
storagePrefix []string,
scope promutils.Scope) interfaces.WorkflowInterface {
metrics := workflowMetrics{
Scope: scope,
CompilationFailures: scope.MustNewCounter(
"compilation_failures", "any observed failures when compiling a workflow"),
TypedInterfaceSizeBytes: scope.MustNewSummary("typed_interface_size_bytes",
"size in bytes of serialized workflow TypedInterface"),
}
return &WorkflowManager{
db: db,
config: config,
compiler: compiler,
storageClient: storageClient,
storagePrefix: storagePrefix,
metrics: metrics,
}
}