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 62
/
launch_plan_manager.go
582 lines (531 loc) · 21.9 KB
/
launch_plan_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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package impl
import (
"bytes"
"context"
"strconv"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flyteadmin/pkg/async/schedule/aws"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/prometheus/client_golang/prometheus"
scheduleInterfaces "github.com/flyteorg/flyteadmin/pkg/async/schedule/interfaces"
"github.com/flyteorg/flytestdlib/logger"
"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"
"github.com/flyteorg/flyteadmin/pkg/repositories"
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"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
)
type launchPlanMetrics struct {
Scope promutils.Scope
FailedScheduleUpdates prometheus.Counter
SpecSizeBytes prometheus.Summary
ClosureSizeBytes prometheus.Summary
}
type LaunchPlanManager struct {
db repositories.RepositoryInterface
config runtimeInterfaces.Configuration
scheduler scheduleInterfaces.EventScheduler
metrics launchPlanMetrics
}
func getLaunchPlanContext(ctx context.Context, identifier *core.Identifier) context.Context {
ctx = contextutils.WithProjectDomain(ctx, identifier.Project, identifier.Domain)
return contextutils.WithLaunchPlanID(ctx, identifier.Name)
}
func (m *LaunchPlanManager) getNamedEntityContext(ctx context.Context, identifier *admin.NamedEntityIdentifier) context.Context {
ctx = contextutils.WithProjectDomain(ctx, identifier.Project, identifier.Domain)
return contextutils.WithLaunchPlanID(ctx, identifier.Name)
}
func (m *LaunchPlanManager) CreateLaunchPlan(
ctx context.Context,
request admin.LaunchPlanCreateRequest) (*admin.LaunchPlanCreateResponse, error) {
if err := validation.ValidateIdentifier(request.GetSpec().GetWorkflowId(), common.Workflow); err != nil {
logger.Debugf(ctx, "Failed to validate provided workflow ID for CreateLaunchPlan with err: %v", err)
return nil, err
}
workflowModel, err := util.GetWorkflowModel(ctx, m.db, *request.Spec.WorkflowId)
if err != nil {
logger.Debugf(ctx, "Failed to get workflow with id [%+v] for CreateLaunchPlan with id [%+v] with err %v",
*request.Spec.WorkflowId, request.Id)
return nil, err
}
var workflowInterface core.TypedInterface
if workflowModel.TypedInterface != nil && len(workflowModel.TypedInterface) > 0 {
err = proto.Unmarshal(workflowModel.TypedInterface, &workflowInterface)
if err != nil {
logger.Errorf(ctx,
"Failed to unmarshal TypedInterface for workflow [%+v] with err: %v",
*request.Spec.WorkflowId, err)
return nil, errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal workflow inputs")
}
}
if err := validation.ValidateLaunchPlan(ctx, request, m.db, m.config.ApplicationConfiguration(), &workflowInterface); err != nil {
logger.Debugf(ctx, "could not create launch plan: %+v, request failed validation with err: %v", request.Id, err)
return nil, err
}
ctx = getLaunchPlanContext(ctx, request.Id)
launchPlan := transformers.CreateLaunchPlan(request, workflowInterface.Outputs)
launchPlanDigest, err := util.GetLaunchPlanDigest(ctx, &launchPlan)
if err != nil {
logger.Errorf(ctx, "failed to compute launch plan digest for [%+v] with err: %v", launchPlan.Id, err)
return nil, err
}
existingLaunchPlanModel, err := util.GetLaunchPlanModel(ctx, m.db, *request.Id)
if err == nil {
if bytes.Equal(existingLaunchPlanModel.Digest, launchPlanDigest) {
return nil, errors.NewFlyteAdminErrorf(codes.AlreadyExists,
"identical launch plan already exists with id %s", request.Id)
}
return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"launch plan with different structure already exists with id %v", request.Id)
}
launchPlanModel, err :=
transformers.CreateLaunchPlanModel(launchPlan, workflowModel.ID, launchPlanDigest, admin.LaunchPlanState_INACTIVE)
if err != nil {
logger.Errorf(ctx,
"Failed to transform launch plan model [%+v], and workflow outputs [%+v] with err: %v",
request, workflowInterface.Outputs, err)
return nil, err
}
err = m.db.LaunchPlanRepo().Create(ctx, launchPlanModel)
if err != nil {
logger.Errorf(ctx, "Failed to save launch plan model %+v with err: %v", request.Id, err)
return nil, err
}
m.metrics.SpecSizeBytes.Observe(float64(len(launchPlanModel.Spec)))
m.metrics.ClosureSizeBytes.Observe(float64(len(launchPlanModel.Closure)))
return &admin.LaunchPlanCreateResponse{}, nil
}
func (m *LaunchPlanManager) updateLaunchPlanModelState(launchPlan *models.LaunchPlan, state admin.LaunchPlanState) error {
var launchPlanClosure admin.LaunchPlanClosure
err := proto.Unmarshal(launchPlan.Closure, &launchPlanClosure)
if err != nil {
logger.Errorf(context.Background(), "failed to unmarshal launch plan closure: %v", err)
return errors.NewFlyteAdminErrorf(codes.Internal, "Failed to unmarshal launch plan closure: %v", err)
}
// Don't write the state in the closure - we store it only in the model column "State" and fill in the closure
// value when transforming from a model to an admin.LaunchPlan object
marshalledClosure, err := proto.Marshal(&launchPlanClosure)
if err != nil {
logger.Errorf(context.Background(), "Failed to marshal launch plan closure: %v", err)
return errors.NewFlyteAdminErrorf(codes.Internal, "Failed to marshal launch plan closure: %v", err)
}
launchPlan.Closure = marshalledClosure
stateInt := int32(state)
launchPlan.State = &stateInt
return nil
}
func isScheduleEmpty(launchPlanSpec admin.LaunchPlanSpec) bool {
schedule := launchPlanSpec.GetEntityMetadata().GetSchedule()
if schedule == nil {
return true
}
if len(schedule.GetKickoffTimeInputArg()) != 0 {
return false
}
if len(schedule.GetCronExpression()) != 0 {
return false
}
if schedule.GetRate().GetValue() != 0 {
return false
}
return true
}
func (m *LaunchPlanManager) enableSchedule(ctx context.Context, launchPlanIdentifier admin.NamedEntityIdentifier,
launchPlanSpec admin.LaunchPlanSpec) error {
payload, err := aws.SerializeScheduleWorkflowPayload(
launchPlanSpec.EntityMetadata.Schedule.GetKickoffTimeInputArg(),
launchPlanIdentifier)
if err != nil {
logger.Errorf(ctx, "failed to serialize schedule workflow payload for launch plan: %v with err: %v",
launchPlanIdentifier, err)
return err
}
addScheduleInput := scheduleInterfaces.AddScheduleInput{
Identifier: launchPlanIdentifier,
ScheduleExpression: *launchPlanSpec.EntityMetadata.Schedule,
Payload: payload,
ScheduleNamePrefix: m.config.ApplicationConfiguration().GetSchedulerConfig().EventSchedulerConfig.ScheduleNamePrefix,
}
return m.scheduler.AddSchedule(ctx, addScheduleInput)
}
func (m *LaunchPlanManager) disableSchedule(
ctx context.Context, launchPlanIdentifier admin.NamedEntityIdentifier) error {
return m.scheduler.RemoveSchedule(ctx, scheduleInterfaces.RemoveScheduleInput{
Identifier: launchPlanIdentifier,
ScheduleNamePrefix: m.config.ApplicationConfiguration().GetSchedulerConfig().EventSchedulerConfig.ScheduleNamePrefix,
})
}
func (m *LaunchPlanManager) updateSchedules(
ctx context.Context, newlyActiveLaunchPlan models.LaunchPlan, formerlyActiveLaunchPlan *models.LaunchPlan) error {
var newlyActiveLaunchPlanSpec admin.LaunchPlanSpec
err := proto.Unmarshal(newlyActiveLaunchPlan.Spec, &newlyActiveLaunchPlanSpec)
if err != nil {
logger.Errorf(ctx, "failed to unmarshal newly enabled launch plan spec")
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal newly enabled launch plan spec")
}
launchPlanIdentifier := admin.NamedEntityIdentifier{
Project: newlyActiveLaunchPlan.Project,
Domain: newlyActiveLaunchPlan.Domain,
Name: newlyActiveLaunchPlan.Name,
}
var formerlyActiveLaunchPlanSpec admin.LaunchPlanSpec
if formerlyActiveLaunchPlan != nil {
err = proto.Unmarshal(formerlyActiveLaunchPlan.Spec, &formerlyActiveLaunchPlanSpec)
if err != nil {
return errors.NewFlyteAdminErrorf(codes.Internal, "failed to unmarshal formerly enabled launch plan spec")
}
}
if proto.Equal(formerlyActiveLaunchPlanSpec.GetEntityMetadata().GetSchedule(),
newlyActiveLaunchPlanSpec.GetEntityMetadata().GetSchedule()) {
// Nothing to change/update.
logger.Infof(ctx, "activating launch plan [%+v] with identical schedule to previous version. "+
"Not updating any schedules", launchPlanIdentifier)
return nil
}
if !isScheduleEmpty(formerlyActiveLaunchPlanSpec) {
// Disable previous schedule
if err = m.disableSchedule(ctx, launchPlanIdentifier); err != nil {
return err
}
logger.Infof(ctx, "Disabled schedules for deactivated launch plan [%+v]", launchPlanIdentifier)
}
if !isScheduleEmpty(newlyActiveLaunchPlanSpec) {
// Enable new schedule
if err = m.enableSchedule(ctx, launchPlanIdentifier, newlyActiveLaunchPlanSpec); err != nil {
return err
}
logger.Infof(ctx, "Enabled schedules for activated launch plan [%+v]", launchPlanIdentifier)
}
return nil
}
func (m *LaunchPlanManager) disableLaunchPlan(ctx context.Context, request admin.LaunchPlanUpdateRequest) (
*admin.LaunchPlanUpdateResponse, error) {
if err := validation.ValidateIdentifier(request.Id, common.LaunchPlan); err != nil {
logger.Debugf(ctx, "can't disable launch plan [%+v] with invalid identifier: %v", request.Id, err)
return nil, err
}
launchPlanModel, err := util.GetLaunchPlanModel(ctx, m.db, *request.Id)
if err != nil {
logger.Debugf(ctx, "couldn't find launch plan [%+v] to disable with err: %v", request.Id, err)
return nil, err
}
if launchPlanModel.State == nil || *launchPlanModel.State == int32(admin.LaunchPlanState_INACTIVE) {
// Nothing to do.
logger.Debugf(ctx, "disable launch plan called on already inactive launch plan [%+v] nothing to do", request.Id)
return &admin.LaunchPlanUpdateResponse{}, nil
}
err = m.updateLaunchPlanModelState(&launchPlanModel, admin.LaunchPlanState_INACTIVE)
if err != nil {
logger.Debugf(ctx, "failed to disable launch plan [%+v] with err: %v", request.Id, err)
return nil, err
}
var launchPlanSpec admin.LaunchPlanSpec
err = proto.Unmarshal(launchPlanModel.Spec, &launchPlanSpec)
if err != nil {
logger.Errorf(ctx, "failed to unmarshal launch plan spec when disabling schedule for %+v", request.Id)
return nil, errors.NewFlyteAdminErrorf(codes.Internal,
"failed to unmarshal launch plan spec when disabling schedule for %+v", request.Id)
}
if launchPlanSpec.EntityMetadata != nil && launchPlanSpec.EntityMetadata.Schedule != nil {
err = m.disableSchedule(ctx, admin.NamedEntityIdentifier{
Project: launchPlanModel.Project,
Domain: launchPlanModel.Domain,
Name: launchPlanModel.Name,
})
if err != nil {
return nil, err
}
}
err = m.db.LaunchPlanRepo().Update(ctx, launchPlanModel)
if err != nil {
logger.Debugf(ctx, "Failed to update launchPlanModel with ID [%+v] with err %v", request.Id, err)
return nil, err
}
logger.Debugf(ctx, "disabled launch plan: [%+v]", request.Id)
return &admin.LaunchPlanUpdateResponse{}, nil
}
func (m *LaunchPlanManager) enableLaunchPlan(ctx context.Context, request admin.LaunchPlanUpdateRequest) (
*admin.LaunchPlanUpdateResponse, error) {
newlyActiveLaunchPlanModel, err := m.db.LaunchPlanRepo().Get(ctx, repoInterfaces.Identifier{
Project: request.Id.Project,
Domain: request.Id.Domain,
Name: request.Id.Name,
Version: request.Id.Version,
})
if err != nil {
logger.Debugf(ctx, "Failed to find launch plan to enable with id [%+v] and err %v", request.Id, err)
return nil, err
}
// Set desired launch plan version to active:
err = m.updateLaunchPlanModelState(&newlyActiveLaunchPlanModel, admin.LaunchPlanState_ACTIVE)
if err != nil {
return nil, err
}
// Find currently active version, if it exists.
filters, err := util.GetActiveLaunchPlanVersionFilters(newlyActiveLaunchPlanModel.Project, newlyActiveLaunchPlanModel.Domain, newlyActiveLaunchPlanModel.Name)
if err != nil {
return nil, err
}
formerlyActiveLaunchPlanModelOutput, err := m.db.LaunchPlanRepo().List(ctx, repoInterfaces.ListResourceInput{
InlineFilters: filters,
Limit: 1,
})
var formerlyActiveLaunchPlanModel *models.LaunchPlan
if err != nil {
// Not found is fine, there isn't always a guaranteed active launch plan model.
if err.(errors.FlyteAdminError).Code() != codes.NotFound {
logger.Infof(ctx, "Failed to search for an active launch plan model with project: %s, domain: %s, name: %s and err %v",
request.Id.Project, request.Id.Domain, request.Id.Name, err)
return nil, err
}
logger.Debugf(ctx, "No active launch plan model found to disable with project: %s, domain: %s, name: %s",
request.Id.Project, request.Id.Domain, request.Id.Name)
} else if formerlyActiveLaunchPlanModelOutput.LaunchPlans != nil &&
len(formerlyActiveLaunchPlanModelOutput.LaunchPlans) > 0 {
formerlyActiveLaunchPlanModel = &formerlyActiveLaunchPlanModelOutput.LaunchPlans[0]
err = m.updateLaunchPlanModelState(formerlyActiveLaunchPlanModel, admin.LaunchPlanState_INACTIVE)
if err != nil {
return nil, err
}
}
err = m.updateSchedules(ctx, newlyActiveLaunchPlanModel, formerlyActiveLaunchPlanModel)
if err != nil {
m.metrics.FailedScheduleUpdates.Inc()
return nil, err
}
// This operation is takes in the (formerly) active launch plan version as only one version can be active at a time.
// Setting the desired launch plan to active also requires disabling the existing active launch plan version.
err = m.db.LaunchPlanRepo().SetActive(ctx, newlyActiveLaunchPlanModel, formerlyActiveLaunchPlanModel)
if err != nil {
logger.Debugf(ctx,
"Failed to set launchPlanModel with ID [%+v] to active with err %v", request.Id, err)
return nil, err
}
return &admin.LaunchPlanUpdateResponse{}, nil
}
func (m *LaunchPlanManager) UpdateLaunchPlan(ctx context.Context, request admin.LaunchPlanUpdateRequest) (
*admin.LaunchPlanUpdateResponse, error) {
if err := validation.ValidateIdentifier(request.Id, common.LaunchPlan); err != nil {
logger.Debugf(ctx, "can't update launch plan [%+v] state, invalid identifier: %v", request.Id, err)
}
ctx = getLaunchPlanContext(ctx, request.Id)
switch request.State {
case admin.LaunchPlanState_INACTIVE:
return m.disableLaunchPlan(ctx, request)
case admin.LaunchPlanState_ACTIVE:
return m.enableLaunchPlan(ctx, request)
default:
return nil, errors.NewFlyteAdminErrorf(
codes.InvalidArgument, "Unrecognized launch plan state %v for update for launch plan [%+v]",
request.State, request.Id)
}
}
func (m *LaunchPlanManager) GetLaunchPlan(ctx context.Context, request admin.ObjectGetRequest) (
*admin.LaunchPlan, error) {
if err := validation.ValidateIdentifier(request.Id, common.LaunchPlan); err != nil {
logger.Debugf(ctx, "can't get launch plan [%+v] with invalid identifier: %v", request.Id, err)
return nil, err
}
ctx = getLaunchPlanContext(ctx, request.Id)
return util.GetLaunchPlan(ctx, m.db, *request.Id)
}
func (m *LaunchPlanManager) GetActiveLaunchPlan(ctx context.Context, request admin.ActiveLaunchPlanRequest) (
*admin.LaunchPlan, error) {
if err := validation.ValidateActiveLaunchPlanRequest(request); err != nil {
logger.Debugf(ctx, "can't get active launch plan [%+v] with invalid request: %v", request.Id, err)
return nil, err
}
ctx = m.getNamedEntityContext(ctx, request.Id)
filters, err := util.GetActiveLaunchPlanVersionFilters(request.Id.Project, request.Id.Domain, request.Id.Name)
if err != nil {
return nil, err
}
listLaunchPlansInput := repoInterfaces.ListResourceInput{
Limit: 1,
InlineFilters: filters,
}
output, err := m.db.LaunchPlanRepo().List(ctx, listLaunchPlansInput)
if err != nil {
logger.Debugf(ctx, "Failed to list active launch plan id for request [%+v] with err %v", request, err)
return nil, err
}
if len(output.LaunchPlans) != 1 {
return nil, errors.NewFlyteAdminErrorf(codes.NotFound, "No active launch plan could be found: %s:%s:%s", request.Id.Project, request.Id.Domain, request.Id.Name)
}
return transformers.FromLaunchPlanModel(output.LaunchPlans[0])
}
func (m *LaunchPlanManager) ListLaunchPlans(ctx context.Context, request admin.ResourceListRequest) (
*admin.LaunchPlanList, error) {
// Check required fields
if err := validation.ValidateResourceListRequest(request); err != nil {
logger.Debugf(ctx, "")
return nil, err
}
ctx = m.getNamedEntityContext(ctx, request.Id)
filters, err := util.GetDbFilters(util.FilterSpec{
Project: request.Id.Project,
Domain: request.Id.Domain,
Name: request.Id.Name,
RequestFilters: request.Filters,
}, common.LaunchPlan)
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 ListLaunchPlans", request.Token)
}
listLaunchPlansInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := m.db.LaunchPlanRepo().List(ctx, listLaunchPlansInput)
if err != nil {
logger.Debugf(ctx, "Failed to list launch plans for request [%+v] with err %v", request, err)
return nil, err
}
launchPlanList, err := transformers.FromLaunchPlanModels(output.LaunchPlans)
if err != nil {
logger.Errorf(ctx,
"Failed to transform launch plan models [%+v] with err: %v", output.LaunchPlans, err)
return nil, err
}
var token string
if len(output.LaunchPlans) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.LaunchPlans))
}
return &admin.LaunchPlanList{
LaunchPlans: launchPlanList,
Token: token,
}, nil
}
func (m *LaunchPlanManager) ListActiveLaunchPlans(ctx context.Context, request admin.ActiveLaunchPlanListRequest) (
*admin.LaunchPlanList, error) {
// Check required fields
if err := validation.ValidateActiveLaunchPlanListRequest(request); err != nil {
logger.Debugf(ctx, "")
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, request.Project, request.Domain)
filters, err := util.ListActiveLaunchPlanVersionsFilters(request.Project, request.Domain)
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 ListActiveLaunchPlans", request.Token)
}
listLaunchPlansInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := m.db.LaunchPlanRepo().List(ctx, listLaunchPlansInput)
if err != nil {
logger.Debugf(ctx, "Failed to list active launch plans for request [%+v] with err %v", request, err)
return nil, err
}
launchPlanList, err := transformers.FromLaunchPlanModels(output.LaunchPlans)
if err != nil {
logger.Errorf(ctx,
"Failed to transform active launch plan models [%+v] with err: %v", output.LaunchPlans, err)
return nil, err
}
var token string
if len(output.LaunchPlans) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.LaunchPlans))
}
return &admin.LaunchPlanList{
LaunchPlans: launchPlanList,
Token: token,
}, nil
}
// At least project name and domain must be specified along with limit.
func (m *LaunchPlanManager) ListLaunchPlanIds(ctx context.Context, request admin.NamedEntityIdentifierListRequest) (
*admin.NamedEntityIdentifierList, error) {
ctx = contextutils.WithProjectDomain(ctx, request.Project, request.Domain)
filters, err := util.GetDbFilters(util.FilterSpec{
Project: request.Project,
Domain: request.Domain,
}, common.LaunchPlan)
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", request.Token)
}
listLaunchPlansInput := repoInterfaces.ListResourceInput{
Limit: int(request.Limit),
Offset: offset,
InlineFilters: filters,
SortParameter: sortParameter,
}
output, err := m.db.LaunchPlanRepo().ListLaunchPlanIdentifiers(ctx, listLaunchPlansInput)
if err != nil {
logger.Debugf(ctx, "Failed to list launch plan ids for request [%+v] with err %v", request, err)
return nil, err
}
var token string
if len(output.LaunchPlans) == int(request.Limit) {
token = strconv.Itoa(offset + len(output.LaunchPlans))
}
return &admin.NamedEntityIdentifierList{
Entities: transformers.FromLaunchPlanModelsToIdentifiers(output.LaunchPlans),
Token: token,
}, nil
}
func NewLaunchPlanManager(
db repositories.RepositoryInterface,
config runtimeInterfaces.Configuration,
scheduler scheduleInterfaces.EventScheduler,
scope promutils.Scope) interfaces.LaunchPlanInterface {
metrics := launchPlanMetrics{
Scope: scope,
FailedScheduleUpdates: scope.MustNewCounter("failed_schedule_updates",
"count of unsuccessful attempts to update the schedules when updating launch plan version"),
SpecSizeBytes: scope.MustNewSummary("spec_size_bytes", "size in bytes of serialized launch plan spec"),
ClosureSizeBytes: scope.MustNewSummary("closure_size_bytes", "size in bytes of serialized launch plan closure"),
}
return &LaunchPlanManager{
db: db,
config: config,
scheduler: scheduler,
metrics: metrics,
}
}