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 22
/
artifact_manager.go
439 lines (377 loc) · 18.9 KB
/
artifact_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
package impl
import (
"context"
"strconv"
"time"
"github.com/flyteorg/datacatalog/pkg/errors"
"github.com/flyteorg/datacatalog/pkg/manager/impl/validators"
"github.com/flyteorg/datacatalog/pkg/manager/interfaces"
"github.com/flyteorg/datacatalog/pkg/repositories"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/datacatalog"
"github.com/flyteorg/datacatalog/pkg/repositories/models"
"github.com/flyteorg/datacatalog/pkg/repositories/transformers"
"github.com/flyteorg/datacatalog/pkg/common"
"github.com/flyteorg/flytestdlib/contextutils"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/flyteorg/flytestdlib/promutils/labeled"
"github.com/flyteorg/flytestdlib/storage"
"google.golang.org/grpc/codes"
)
type artifactMetrics struct {
scope promutils.Scope
createResponseTime labeled.StopWatch
getResponseTime labeled.StopWatch
createSuccessCounter labeled.Counter
createFailureCounter labeled.Counter
getSuccessCounter labeled.Counter
getFailureCounter labeled.Counter
listSuccessCounter labeled.Counter
listFailureCounter labeled.Counter
createDataFailureCounter labeled.Counter
createDataSuccessCounter labeled.Counter
transformerErrorCounter labeled.Counter
validationErrorCounter labeled.Counter
alreadyExistsCounter labeled.Counter
doesNotExistCounter labeled.Counter
updateResponseTime labeled.StopWatch
updateSuccessCounter labeled.Counter
updateFailureCounter labeled.Counter
updateDataSuccessCounter labeled.Counter
updateDataFailureCounter labeled.Counter
deleteDataSuccessCounter labeled.Counter
deleteDataFailureCounter labeled.Counter
}
type artifactManager struct {
repo repositories.RepositoryInterface
artifactStore ArtifactDataStore
systemMetrics artifactMetrics
}
// Create an Artifact along with the associated ArtifactData. The ArtifactData will be stored in an offloaded location.
func (m *artifactManager) CreateArtifact(ctx context.Context, request *datacatalog.CreateArtifactRequest) (*datacatalog.CreateArtifactResponse, error) {
timer := m.systemMetrics.createResponseTime.Start(ctx)
defer timer.Stop()
artifact := request.Artifact
err := validators.ValidateArtifact(artifact)
if err != nil {
logger.Warningf(ctx, "Invalid create artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
return nil, err
}
ctx = contextutils.WithProjectDomain(ctx, artifact.Dataset.Project, artifact.Dataset.Domain)
datasetKey := transformers.FromDatasetID(artifact.Dataset)
// The dataset must exist for the artifact, let's verify that first
dataset, err := m.repo.DatasetRepo().Get(ctx, datasetKey)
if err != nil {
logger.Warnf(ctx, "Failed to get dataset for artifact creation %v, err: %v", datasetKey, err)
m.systemMetrics.createFailureCounter.Inc(ctx)
return nil, err
}
// TODO: when adding a tag, need to verify one tag per partition combo
// check that the artifact's partitions are the same partition values of the dataset
datasetPartitionKeys := transformers.FromPartitionKeyModel(dataset.PartitionKeys)
err = validators.ValidatePartitions(datasetPartitionKeys, artifact.Partitions)
if err != nil {
logger.Warnf(ctx, "Invalid artifact partitions %v, err: %+v", artifact.Partitions, err)
m.systemMetrics.createFailureCounter.Inc(ctx)
return nil, err
}
// create Artifact Data offloaded storage files
artifactDataModels := make([]models.ArtifactData, len(request.Artifact.Data))
for i, artifactData := range request.Artifact.Data {
dataLocation, err := m.artifactStore.PutData(ctx, artifact, artifactData)
if err != nil {
logger.Errorf(ctx, "Failed to store artifact data err: %v", err)
m.systemMetrics.createDataFailureCounter.Inc(ctx)
return nil, err
}
artifactDataModels[i].Name = artifactData.Name
artifactDataModels[i].Location = dataLocation.String()
m.systemMetrics.createDataSuccessCounter.Inc(ctx)
}
logger.Debugf(ctx, "Stored %v data for artifact %+v", len(artifactDataModels), artifact.Id)
artifactModel, err := transformers.CreateArtifactModel(request, artifactDataModels, dataset)
if err != nil {
logger.Errorf(ctx, "Failed to transform artifact err: %v", err)
m.systemMetrics.transformerErrorCounter.Inc(ctx)
return nil, err
}
err = m.repo.ArtifactRepo().Create(ctx, artifactModel)
if err != nil {
if errors.IsAlreadyExistsError(err) {
logger.Warnf(ctx, "Artifact already exists key: %+v, err %v", artifact.Id, err)
m.systemMetrics.alreadyExistsCounter.Inc(ctx)
} else {
logger.Errorf(ctx, "Failed to create artifact %v, err: %v", artifactDataModels, err)
m.systemMetrics.createFailureCounter.Inc(ctx)
}
return nil, err
}
logger.Debugf(ctx, "Successfully created artifact id: %v", artifact.Id)
m.systemMetrics.createSuccessCounter.Inc(ctx)
return &datacatalog.CreateArtifactResponse{}, nil
}
// Get the Artifact and its associated ArtifactData. The request can query by ArtifactID or TagName.
func (m *artifactManager) GetArtifact(ctx context.Context, request *datacatalog.GetArtifactRequest) (*datacatalog.GetArtifactResponse, error) {
timer := m.systemMetrics.getResponseTime.Start(ctx)
defer timer.Stop()
err := validators.ValidateGetArtifactRequest(request)
if err != nil {
logger.Warningf(ctx, "Invalid get artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
return nil, err
}
datasetID := request.Dataset
artifactModel, err := m.findArtifact(ctx, datasetID, request)
if err != nil {
logger.Errorf(ctx, "Failed to retrieve artifact for get artifact request %v, err: %v", request, err)
m.systemMetrics.getFailureCounter.Inc(ctx)
return nil, err
}
artifact, err := transformers.FromArtifactModel(artifactModel)
if err != nil {
logger.Errorf(ctx, "Error in transforming get artifact request %+v, err %v", artifactModel, err)
m.systemMetrics.transformerErrorCounter.Inc(ctx)
return nil, err
}
artifactDataList, err := m.getArtifactDataList(ctx, artifactModel.ArtifactData)
if err != nil {
m.systemMetrics.getFailureCounter.Inc(ctx)
return nil, err
}
artifact.Data = artifactDataList
logger.Debugf(ctx, "Retrieved artifact dataset %v, id: %v", artifact.Dataset, artifact.Id)
m.systemMetrics.getSuccessCounter.Inc(ctx)
return &datacatalog.GetArtifactResponse{
Artifact: artifact,
}, nil
}
type artifactQueryHandle interface {
GetArtifactId() string
GetTagName() string
}
func (m *artifactManager) findArtifact(ctx context.Context, datasetID *datacatalog.DatasetID, queryHandle artifactQueryHandle) (models.Artifact, error) {
var artifactModel models.Artifact
key := queryHandle.GetArtifactId()
if len(key) > 0 {
logger.Debugf(ctx, "Get artifact by id %v", key)
artifactKey := transformers.ToArtifactKey(datasetID, key)
var err error
artifactModel, err = m.repo.ArtifactRepo().Get(ctx, artifactKey)
if err != nil {
if errors.IsDoesNotExistError(err) {
logger.Warnf(ctx, "Artifact does not exist id: %+v, err %v", key, err)
m.systemMetrics.doesNotExistCounter.Inc(ctx)
} else {
logger.Errorf(ctx, "Unable to retrieve artifact by id: %+v, err %v", key, err)
}
return models.Artifact{}, err
}
} else {
key = queryHandle.GetTagName()
logger.Debugf(ctx, "Get artifact by tag %v", key)
tagKey := transformers.ToTagKey(datasetID, key)
tag, err := m.repo.TagRepo().Get(ctx, tagKey)
if err != nil {
if errors.IsDoesNotExistError(err) {
logger.Infof(ctx, "Artifact does not exist tag: %+v, err %v", key, err)
m.systemMetrics.doesNotExistCounter.Inc(ctx)
} else {
logger.Errorf(ctx, "Unable to retrieve Artifact by tag %v, err: %v", key, err)
}
return models.Artifact{}, err
}
artifactModel = tag.Artifact
}
if len(artifactModel.ArtifactData) == 0 {
return models.Artifact{}, errors.NewDataCatalogErrorf(codes.Internal, "artifact [%+v] with key %v does not have artifact data associated", artifactModel, key)
}
return artifactModel, nil
}
func (m *artifactManager) getArtifactDataList(ctx context.Context, artifactDataModels []models.ArtifactData) ([]*datacatalog.ArtifactData, error) {
artifactDataList := make([]*datacatalog.ArtifactData, len(artifactDataModels))
for i, artifactData := range artifactDataModels {
value, err := m.artifactStore.GetData(ctx, artifactData)
if err != nil {
logger.Errorf(ctx, "Error in getting artifact data from datastore %+v, err %v", artifactData.Location, err)
return nil, err
}
artifactDataList[i] = &datacatalog.ArtifactData{
Name: artifactData.Name,
Value: value,
}
}
return artifactDataList, nil
}
func (m *artifactManager) ListArtifacts(ctx context.Context, request *datacatalog.ListArtifactsRequest) (*datacatalog.ListArtifactsResponse, error) {
err := validators.ValidateListArtifactRequest(request)
if err != nil {
logger.Warningf(ctx, "Invalid list artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
return nil, err
}
// Verify the dataset exists before listing artifacts
datasetKey := transformers.FromDatasetID(request.Dataset)
dataset, err := m.repo.DatasetRepo().Get(ctx, datasetKey)
if err != nil {
logger.Warnf(ctx, "Failed to get dataset for listing artifacts %v, err: %v", datasetKey, err)
m.systemMetrics.listFailureCounter.Inc(ctx)
return nil, err
}
// Get the list inputs
listInput, err := transformers.FilterToListInput(ctx, common.Artifact, request.GetFilter())
if err != nil {
logger.Warningf(ctx, "Invalid list artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
return nil, err
}
err = transformers.ApplyPagination(request.Pagination, &listInput)
if err != nil {
logger.Warningf(ctx, "Invalid pagination options in list artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
return nil, err
}
// Perform the list with the dataset and listInput filters
artifactModels, err := m.repo.ArtifactRepo().List(ctx, dataset.DatasetKey, listInput)
if err != nil {
logger.Errorf(ctx, "Unable to list Artifacts err: %v", err)
m.systemMetrics.listFailureCounter.Inc(ctx)
return nil, err
}
// convert returned models into entity list
artifactsList, err := transformers.FromArtifactModels(artifactModels)
if err != nil {
logger.Errorf(ctx, "Unable to transform Artifacts %+v err: %v", artifactModels, err)
m.systemMetrics.listFailureCounter.Inc(ctx)
return nil, err
}
// Get the artifact data for the artifacts. It retrieves the data from storage and
// unmarshals the data.
for i, artifact := range artifactsList {
artifactDataList, err := m.getArtifactDataList(ctx, artifactModels[i].ArtifactData)
if err != nil {
logger.Errorf(ctx, "Unable to transform Artifacts %+v err: %v", artifactModels, err)
m.systemMetrics.listFailureCounter.Inc(ctx)
return nil, err
}
artifact.Data = artifactDataList
}
token := strconv.Itoa(listInput.Offset + len(artifactsList))
logger.Debugf(ctx, "Listed %v matching artifacts successfully", len(artifactsList))
m.systemMetrics.listSuccessCounter.Inc(ctx)
return &datacatalog.ListArtifactsResponse{Artifacts: artifactsList, NextToken: token}, nil
}
// UpdateArtifact updates the given artifact, currently only allowing the associated ArtifactData to be replaced. All
// stored data will be overwritten in the underlying blob storage, no longer existing data (based on ArtifactData name)
// will be deleted.
func (m *artifactManager) UpdateArtifact(ctx context.Context, request *datacatalog.UpdateArtifactRequest) (*datacatalog.UpdateArtifactResponse, error) {
ctx = contextutils.WithProjectDomain(ctx, request.Dataset.Project, request.Dataset.Domain)
timer := m.systemMetrics.updateResponseTime.Start(ctx)
defer timer.Stop()
err := validators.ValidateUpdateArtifactRequest(request)
if err != nil {
logger.Warningf(ctx, "Invalid update artifact request %v, err: %v", request, err)
m.systemMetrics.validationErrorCounter.Inc(ctx)
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
// artifact must already exist, verify first
artifactModel, err := m.findArtifact(ctx, request.GetDataset(), request)
if err != nil {
logger.Errorf(ctx, "Failed to get artifact for update artifact request %v, err: %v", request, err)
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
artifact, err := transformers.FromArtifactModel(artifactModel)
if err != nil {
logger.Errorf(ctx, "Error in transforming update artifact request %+v, err %v", artifactModel, err)
m.systemMetrics.transformerErrorCounter.Inc(ctx)
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
// overwrite existing artifact data and upload new entries, building a map of artifact data names to remove
// deleted entries from the blob storage after the upload completed
artifactDataNames := make(map[string]struct{})
artifactDataModels := make([]models.ArtifactData, len(request.Data))
for i, artifactData := range request.Data {
artifactDataNames[artifactData.Name] = struct{}{}
dataLocation, err := m.artifactStore.PutData(ctx, artifact, artifactData)
if err != nil {
logger.Errorf(ctx, "Failed to store artifact data during update, err: %v", err)
m.systemMetrics.updateDataFailureCounter.Inc(ctx)
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
artifactDataModels[i].Name = artifactData.Name
artifactDataModels[i].Location = dataLocation.String()
m.systemMetrics.updateDataSuccessCounter.Inc(ctx)
}
removedArtifactData := make([]models.ArtifactData, 0)
for _, artifactData := range artifactModel.ArtifactData {
if _, ok := artifactDataNames[artifactData.Name]; !ok {
removedArtifactData = append(removedArtifactData, artifactData)
}
}
// update artifact in DB, also replaces/upserts associated artifact data
artifactModel.ArtifactData = artifactDataModels
err = m.repo.ArtifactRepo().Update(ctx, artifactModel)
if err != nil {
if errors.IsDoesNotExistError(err) {
logger.Warnf(ctx, "Artifact does not exist key: %+v, err %v", artifact.Id, err)
m.systemMetrics.doesNotExistCounter.Inc(ctx)
} else {
logger.Errorf(ctx, "Failed to update artifact %v, err: %v", artifactModel, err)
}
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
// delete all artifact data no longer present in the updated artifact from the blob storage.
// blob storage data is removed last in case the DB update fail, which would leave us with artifact data DB entries
// without underlying blob data. this might still leave orphaned data in blob storage, however we can more easily
// clean that up periodically and don't risk serving artifact data records that will fail when retrieved.
for _, artifactData := range removedArtifactData {
if err := m.artifactStore.DeleteData(ctx, artifactData); err != nil {
logger.Errorf(ctx, "Failed to delete artifact data during update, err: %v", err)
m.systemMetrics.deleteDataFailureCounter.Inc(ctx)
m.systemMetrics.updateFailureCounter.Inc(ctx)
return nil, err
}
m.systemMetrics.deleteDataSuccessCounter.Inc(ctx)
}
logger.Debugf(ctx, "Successfully updated artifact id: %v", artifact.Id)
m.systemMetrics.updateSuccessCounter.Inc(ctx)
return &datacatalog.UpdateArtifactResponse{
ArtifactId: artifact.Id,
}, nil
}
func NewArtifactManager(repo repositories.RepositoryInterface, store *storage.DataStore, storagePrefix storage.DataReference, artifactScope promutils.Scope) interfaces.ArtifactManager {
artifactMetrics := artifactMetrics{
scope: artifactScope,
createResponseTime: labeled.NewStopWatch("create_duration", "The duration of the create artifact calls.", time.Millisecond, artifactScope, labeled.EmitUnlabeledMetric),
getResponseTime: labeled.NewStopWatch("get_duration", "The duration of the get artifact calls.", time.Millisecond, artifactScope, labeled.EmitUnlabeledMetric),
createSuccessCounter: labeled.NewCounter("create_success_count", "The number of times create artifact succeeded", artifactScope, labeled.EmitUnlabeledMetric),
getSuccessCounter: labeled.NewCounter("get_success_count", "The number of times get artifact succeeded", artifactScope, labeled.EmitUnlabeledMetric),
createFailureCounter: labeled.NewCounter("create_failure_count", "The number of times create artifact failed", artifactScope, labeled.EmitUnlabeledMetric),
getFailureCounter: labeled.NewCounter("get_failure_count", "The number of times get artifact failed", artifactScope, labeled.EmitUnlabeledMetric),
createDataFailureCounter: labeled.NewCounter("create_data_failure_count", "The number of times create artifact data failed", artifactScope, labeled.EmitUnlabeledMetric),
createDataSuccessCounter: labeled.NewCounter("create_data_success_count", "The number of times create artifact data succeeded", artifactScope, labeled.EmitUnlabeledMetric),
transformerErrorCounter: labeled.NewCounter("transformer_failed_count", "The number of times transformations failed", artifactScope, labeled.EmitUnlabeledMetric),
validationErrorCounter: labeled.NewCounter("validation_failed_count", "The number of times validation failed", artifactScope, labeled.EmitUnlabeledMetric),
alreadyExistsCounter: labeled.NewCounter("already_exists_count", "The number of times an artifact already exists", artifactScope, labeled.EmitUnlabeledMetric),
doesNotExistCounter: labeled.NewCounter("does_not_exists_count", "The number of times an artifact was not found", artifactScope, labeled.EmitUnlabeledMetric),
listSuccessCounter: labeled.NewCounter("list_success_count", "The number of times list artifact succeeded", artifactScope, labeled.EmitUnlabeledMetric),
listFailureCounter: labeled.NewCounter("list_failure_count", "The number of times list artifact failed", artifactScope, labeled.EmitUnlabeledMetric),
updateResponseTime: labeled.NewStopWatch("update_duration", "The duration of the update artifact calls.", time.Millisecond, artifactScope, labeled.EmitUnlabeledMetric),
updateSuccessCounter: labeled.NewCounter("update_success_count", "The number of times update artifact succeeded", artifactScope, labeled.EmitUnlabeledMetric),
updateFailureCounter: labeled.NewCounter("update_failure_count", "The number of times update artifact failed", artifactScope, labeled.EmitUnlabeledMetric),
updateDataSuccessCounter: labeled.NewCounter("update_data_success_count", "The number of times update artifact data succeeded", artifactScope, labeled.EmitUnlabeledMetric),
updateDataFailureCounter: labeled.NewCounter("update_data_failure_count", "The number of times update artifact data failed", artifactScope, labeled.EmitUnlabeledMetric),
deleteDataSuccessCounter: labeled.NewCounter("delete_data_success_count", "The number of times delete artifact data succeeded", artifactScope, labeled.EmitUnlabeledMetric),
deleteDataFailureCounter: labeled.NewCounter("delete_data_failure_count", "The number of times delete artifact data failed", artifactScope, labeled.EmitUnlabeledMetric),
}
return &artifactManager{
repo: repo,
artifactStore: NewArtifactDataStore(store, storagePrefix),
systemMetrics: artifactMetrics,
}
}