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
/
launch_plan_repo.go
197 lines (170 loc) · 6 KB
/
launch_plan_repo.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
package gormimpl
import (
"context"
"errors"
"time"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/promutils"
adminErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
"github.com/flyteorg/flytestdlib/logger"
"gorm.io/gorm"
)
const launchPlanTableName = "launch_plans"
type launchPlanMetrics struct {
SetActiveDuration promutils.StopWatch
}
// Implementation of LaunchPlanRepoInterface.
type LaunchPlanRepo struct {
db *gorm.DB
errorTransformer adminErrors.ErrorTransformer
metrics gormMetrics
launchPlanMetrics launchPlanMetrics
}
func (r *LaunchPlanRepo) Create(ctx context.Context, input models.LaunchPlan) error {
timer := r.metrics.CreateDuration.Start()
tx := r.db.Omit("id").Create(&input)
timer.Stop()
if tx.Error != nil {
return r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return nil
}
func (r *LaunchPlanRepo) Update(ctx context.Context, input models.LaunchPlan) error {
timer := r.metrics.UpdateDuration.Start()
tx := r.db.Model(&input).Updates(input)
timer.Stop()
if err := tx.Error; err != nil {
return r.errorTransformer.ToFlyteAdminError(err)
}
return nil
}
func (r *LaunchPlanRepo) Get(ctx context.Context, input interfaces.Identifier) (models.LaunchPlan, error) {
var launchPlan models.LaunchPlan
timer := r.metrics.GetDuration.Start()
tx := r.db.Where(&models.LaunchPlan{
LaunchPlanKey: models.LaunchPlanKey{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
},
}).Take(&launchPlan)
timer.Stop()
if tx.Error != nil && errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return models.LaunchPlan{},
adminErrors.GetMissingEntityError(core.ResourceType_LAUNCH_PLAN.String(), &core.Identifier{
Project: input.Project,
Domain: input.Domain,
Name: input.Name,
Version: input.Version,
})
} else if tx.Error != nil {
return models.LaunchPlan{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return launchPlan, nil
}
// This operation is performed as a two-step transaction because only one launch plan version can be active at a time.
// Transactional semantics are used to guarantee that setting the desired launch plan to active also disables
// the existing launch plan version (if any).
func (r *LaunchPlanRepo) SetActive(
ctx context.Context, toEnable models.LaunchPlan, toDisable *models.LaunchPlan) error {
timer := r.launchPlanMetrics.SetActiveDuration.Start()
defer timer.Stop()
// Use a transaction to guarantee no partial updates.
tx := r.db.Begin()
// There is a launch plan to disable as part of this transaction
if toDisable != nil {
tx.Model(&toDisable).UpdateColumns(toDisable)
if err := tx.Error; err != nil {
tx.Rollback()
return r.errorTransformer.ToFlyteAdminError(err)
}
}
// And update the desired version.
tx.Model(&toEnable).UpdateColumns(toEnable)
if err := tx.Error; err != nil {
tx.Rollback()
return r.errorTransformer.ToFlyteAdminError(err)
}
if err := tx.Commit().Error; err != nil {
return r.errorTransformer.ToFlyteAdminError(err)
}
return nil
}
func (r *LaunchPlanRepo) List(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.LaunchPlanCollectionOutput, error) {
// First validate input.
if err := ValidateListInput(input); err != nil {
return interfaces.LaunchPlanCollectionOutput{}, err
}
var launchPlans []models.LaunchPlan
tx := r.db.Limit(input.Limit).Offset(input.Offset)
// Add join conditions
tx = tx.Joins("inner join workflows on launch_plans.workflow_id = workflows.id")
// Apply filters
tx, err := applyScopedFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.LaunchPlanCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
timer := r.metrics.ListDuration.Start()
tx.Find(&launchPlans)
timer.Stop()
if tx.Error != nil {
logger.Warningf(ctx,
"Failed to list launch plans by workflow with input [%+v] with err: %+v", input, tx.Error)
return interfaces.LaunchPlanCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.LaunchPlanCollectionOutput{
LaunchPlans: launchPlans,
}, nil
}
func (r *LaunchPlanRepo) ListLaunchPlanIdentifiers(ctx context.Context, input interfaces.ListResourceInput) (
interfaces.LaunchPlanCollectionOutput, error) {
// Validate input, input must have a limit
if err := ValidateListInput(input); err != nil {
return interfaces.LaunchPlanCollectionOutput{}, err
}
tx := r.db.Model(models.LaunchPlan{}).Limit(input.Limit).Offset(input.Offset)
// Apply filters
tx, err := applyFilters(tx, input.InlineFilters, input.MapFilters)
if err != nil {
return interfaces.LaunchPlanCollectionOutput{}, err
}
// Apply sort ordering.
if input.SortParameter != nil {
tx = tx.Order(input.SortParameter.GetGormOrderExpr())
}
// Scan the results into a list of launch plans
var launchPlans []models.LaunchPlan
timer := r.metrics.ListIdentifiersDuration.Start()
tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&launchPlans)
timer.Stop()
if tx.Error != nil {
return interfaces.LaunchPlanCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)
}
return interfaces.LaunchPlanCollectionOutput{
LaunchPlans: launchPlans,
}, nil
}
// Returns an instance of LaunchPlanRepoInterface
func NewLaunchPlanRepo(
db *gorm.DB, errorTransformer adminErrors.ErrorTransformer, scope promutils.Scope) interfaces.LaunchPlanRepoInterface {
metrics := newMetrics(scope)
launchPlanMetrics := launchPlanMetrics{
SetActiveDuration: scope.MustNewStopWatch(
"set_active",
"time taken to set a launch plan to active (and disable the currently active version)", time.Millisecond),
}
return &LaunchPlanRepo{
db: db,
errorTransformer: errorTransformer,
metrics: metrics,
launchPlanMetrics: launchPlanMetrics,
}
}