/
api_checkpoint.go
467 lines (404 loc) · 14.1 KB
/
api_checkpoint.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
package internal
import (
"context"
"fmt"
"sort"
"strings"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/uptrace/bun"
"golang.org/x/exp/maps"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"github.com/determined-ai/determined/master/internal/api"
"github.com/determined-ai/determined/master/internal/authz"
"github.com/determined-ai/determined/master/internal/db"
expauth "github.com/determined-ai/determined/master/internal/experiment"
"github.com/determined-ai/determined/master/internal/grpcutil"
modelauth "github.com/determined-ai/determined/master/internal/model"
"github.com/determined-ai/determined/master/internal/trials"
"github.com/determined-ai/determined/master/internal/user"
"github.com/determined-ai/determined/master/internal/workspace"
"github.com/determined-ai/determined/master/pkg/model"
"github.com/determined-ai/determined/master/pkg/protoutils/protoconverter"
"github.com/determined-ai/determined/proto/pkg/apiv1"
"github.com/determined-ai/determined/proto/pkg/checkpointv1"
"github.com/determined-ai/determined/proto/pkg/modelv1"
)
func errCheckpointsNotFound(ids []string) error {
tmp := make([]string, len(ids))
for i, id := range ids {
tmp[i] = id
}
sort.Strings(tmp)
return api.NotFoundErrs("checkpoints", strings.Join(tmp, ", "), true)
}
func (m *Master) canDoActionOnCheckpoint(
ctx context.Context,
curUser model.User,
id string,
action func(context.Context, model.User, *model.Experiment) error,
) error {
uuid, err := uuid.Parse(id)
if err != nil {
return err
}
checkpoint, err := m.db.CheckpointByUUID(uuid)
if err != nil {
return err
} else if checkpoint == nil {
return api.NotFoundErrs("checkpoint", id, true)
}
if checkpoint.CheckpointTrainingMetadata.ExperimentID == 0 {
return nil // TODO(nick) add authz for other task types.
}
exp, err := db.ExperimentByID(ctx, checkpoint.CheckpointTrainingMetadata.ExperimentID)
if err != nil {
return err
}
if err := expauth.AuthZProvider.Get().CanGetExperiment(ctx, curUser, exp); err != nil {
return authz.SubIfUnauthorized(err, api.NotFoundErrs("checkpoint", id, true))
}
if err := action(ctx, curUser, exp); err != nil {
return status.Error(codes.PermissionDenied, err.Error())
}
return nil
}
func (m *Master) canDoActionOnCheckpointThroughModel(
ctx context.Context, curUser model.User, ckptID string,
) error {
ckptUUID, err := uuid.Parse(ckptID)
if err != nil {
return err
}
modelIDs, err := db.GetModelIDsAssociatedWithCheckpoint(ctx, ckptUUID)
if err != nil {
return err
}
if len(modelIDs) == 0 {
// if length of model ids is zero then permission denied
// so return checkpoint not found.
return api.NotFoundErrs("checkpoint", ckptID, true)
}
var errCanGetModel error
for _, modelID := range modelIDs {
model := &modelv1.Model{}
err = m.db.QueryProto("get_model_by_id", model, modelID)
if err != nil {
return err
}
if errCanGetModel = modelauth.AuthZProvider.Get().CanGetModel(
ctx, curUser, model, model.WorkspaceId); errCanGetModel == nil {
return nil
}
}
// we get to this return when there are no models belonging
// to a workspace where user has permissions.
return authz.SubIfUnauthorized(errCanGetModel, api.NotFoundErrs("checkpoint", ckptID, true))
}
func (a *apiServer) GetCheckpoint(
ctx context.Context, req *apiv1.GetCheckpointRequest,
) (*apiv1.GetCheckpointResponse, error) {
curUser, _, err := grpcutil.GetUser(ctx)
if err != nil {
return nil, err
}
errE := a.m.canDoActionOnCheckpoint(ctx, *curUser, req.CheckpointUuid,
expauth.AuthZProvider.Get().CanGetExperimentArtifacts)
if errE != nil {
errM := a.m.canDoActionOnCheckpointThroughModel(ctx, *curUser, req.CheckpointUuid)
if errM != nil {
return nil, errE
}
}
resp := &apiv1.GetCheckpointResponse{}
resp.Checkpoint = &checkpointv1.Checkpoint{}
if err := a.m.db.QueryProto(
"get_checkpoint", resp.Checkpoint, req.CheckpointUuid); err != nil {
return resp,
errors.Wrapf(err, "error fetching checkpoint %s from database", req.CheckpointUuid)
}
return resp, nil
}
func (a *apiServer) checkpointsRBACEditCheck(
ctx context.Context, uuids []uuid.UUID,
) ([]*model.Experiment, []*db.ExperimentCheckpointGrouping, error) {
curUser, _, err := grpcutil.GetUser(ctx)
if err != nil {
return nil, nil, err
}
groupCUUIDsByEIDs, err := a.m.db.GroupCheckpointUUIDsByExperimentID(uuids)
if err != nil {
return nil, nil, err
}
// Get checkpoints IDs not associated to any experiments.
checkpointsRequested := make(map[string]bool)
for _, c := range uuids {
checkpointsRequested[c.String()] = false
}
for _, expIDcUUIDs := range groupCUUIDsByEIDs {
for _, c := range strings.Split(expIDcUUIDs.CheckpointUUIDSStr, ",") {
checkpointsRequested[c] = true
}
}
var notFoundCheckpoints []string
for c, found := range checkpointsRequested {
if !found {
notFoundCheckpoints = append(notFoundCheckpoints, c)
}
}
// Get experiments for all checkpoints and validate
// that the user has permission to view and edit.
exps := make([]*model.Experiment, len(groupCUUIDsByEIDs))
for i, expIDcUUIDs := range groupCUUIDsByEIDs {
exp, err := db.ExperimentByID(ctx, expIDcUUIDs.ExperimentID)
if err != nil {
return nil, nil, err
}
err = expauth.AuthZProvider.Get().CanGetExperiment(ctx, *curUser, exp)
if authz.IsPermissionDenied(err) {
notFoundCheckpoints = append(notFoundCheckpoints,
strings.Split(expIDcUUIDs.CheckpointUUIDSStr, ",")...)
continue
} else if err != nil {
return nil, nil, err
}
if err = expauth.AuthZProvider.Get().CanEditExperiment(ctx, *curUser, exp); err != nil {
return nil, nil, status.Error(codes.PermissionDenied, err.Error())
}
exps[i] = exp
}
if len(notFoundCheckpoints) > 0 {
return nil, nil, errCheckpointsNotFound(notFoundCheckpoints)
}
return exps, groupCUUIDsByEIDs, nil
}
func (a *apiServer) PatchCheckpoints(
ctx context.Context,
req *apiv1.PatchCheckpointsRequest,
) (*apiv1.PatchCheckpointsResponse, error) {
var uuidStrings []string
for _, c := range req.Checkpoints {
uuidStrings = append(uuidStrings, c.Uuid)
}
conv := &protoconverter.ProtoConverter{}
uuids := conv.ToUUIDList(uuidStrings)
if cErr := conv.Error(); cErr != nil {
return nil, status.Errorf(codes.InvalidArgument, "converting checkpoint: %s", cErr)
}
if _, _, err := a.checkpointsRBACEditCheck(ctx, uuids); err != nil {
return nil, err
}
registeredCheckpointUUIDs, err := a.m.db.GetRegisteredCheckpoints(uuids)
if err != nil {
return nil, err
}
if len(registeredCheckpointUUIDs) > 0 {
return nil, status.Errorf(codes.InvalidArgument,
"this subset of checkpoints provided are in the model registry and cannot be deleted: %v.",
registeredCheckpointUUIDs)
}
err = db.Bun().RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
var updatedCheckpointSizes []uuid.UUID
for i, c := range req.Checkpoints {
if c.Resources != nil {
size := int64(0)
for _, v := range c.Resources.Resources {
size += v
}
v2Update := tx.NewUpdate().Model(&model.CheckpointV2{}).
Where("uuid = ?", c.Uuid)
if len(c.Resources.Resources) == 0 { // Full delete case.
v2Update = v2Update.Set("state = ?", model.DeletedState)
} else { // Partial delete case.
v2Update = v2Update.
Set("resources = ?", c.Resources.Resources).
Set("size = ?", size)
oldResources := struct {
bun.BaseModel `bun:"table:checkpoints_view"`
Resources map[string]int64
}{}
if err := tx.NewSelect().Model(&oldResources).
Where("uuid = ?", c.Uuid).
Scan(ctx); err != nil {
return err
}
// Add metadata.json to oldResources if it is missing for backwards compatibility.
_, alreadyHasMetadata := oldResources.Resources["metadata.json"]
metadataValue, provided := c.Resources.Resources["metadata.json"]
if !alreadyHasMetadata && provided {
oldResources.Resources["metadata.json"] = metadataValue
}
// Only set state to partially deleted if files changed.
if !maps.Equal(oldResources.Resources, c.Resources.Resources) {
v2Update = v2Update.Set("state = ?", model.PartiallyDeletedState)
}
}
if _, err := v2Update.Exec(ctx); err != nil {
return fmt.Errorf("deleting checkpoints from checkpoints_v2: %w", err)
}
updatedCheckpointSizes = append(updatedCheckpointSizes, uuids[i])
}
}
if len(updatedCheckpointSizes) > 0 {
if err := db.UpdateCheckpointSizeTx(ctx, tx, updatedCheckpointSizes); err != nil {
return fmt.Errorf("updating checkpoint size: %w", err)
}
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error patching checkpoints: %w", err)
}
return &apiv1.PatchCheckpointsResponse{}, nil
}
func (a *apiServer) CheckpointsRemoveFiles(
ctx context.Context,
req *apiv1.CheckpointsRemoveFilesRequest,
) (*apiv1.CheckpointsRemoveFilesResponse, error) {
curUser, _, err := grpcutil.GetUser(ctx)
if err != nil {
return nil, err
}
conv := &protoconverter.ProtoConverter{}
checkpointsToDelete := conv.ToUUIDList(req.CheckpointUuids)
if cErr := conv.Error(); cErr != nil {
return nil, status.Errorf(codes.InvalidArgument, "converting checkpoint: %s", cErr)
}
for _, g := range req.CheckpointGlobs {
if len(g) == 0 {
// Avoid weirdness where someone passes in "" then we concat {uuid}/{glob}
// and we unexpectedly delete the whole checkpoint folder.
return nil, status.Errorf(codes.InvalidArgument, "cannot have empty string glob")
}
if strings.Contains(g, "..") {
return nil, status.Errorf(codes.InvalidArgument, "glob '%s' cannot contain '..'", g)
}
}
exps, groupCUUIDsByEIDs, err := a.checkpointsRBACEditCheck(ctx, checkpointsToDelete)
if err != nil {
return nil, err
}
registeredCheckpointUUIDs, err := a.m.db.GetRegisteredCheckpoints(checkpointsToDelete)
if err != nil {
return nil, err
}
if len(registeredCheckpointUUIDs) > 0 {
return nil, status.Errorf(codes.InvalidArgument,
"this subset of checkpoints provided are in the model registry and cannot be deleted: %v.",
registeredCheckpointUUIDs)
}
taskSpec := *a.m.taskSpec
var expIDs []int
for _, g := range groupCUUIDsByEIDs {
expIDs = append(expIDs, g.ExperimentID)
}
workspaceIDs, err := workspace.WorkspacesIDsByExperimentIDs(ctx, expIDs)
if err != nil {
return nil, err
}
jobID := model.NewJobID()
if err = a.m.db.AddJob(&model.Job{
JobID: jobID,
JobType: model.JobTypeCheckpointGC,
OwnerID: &curUser.ID,
}); err != nil {
return nil, fmt.Errorf("persisting new job: %w", err)
}
// Submit checkpoint GC tasks for all checkpoints.
for i, expIDcUUIDs := range groupCUUIDsByEIDs {
i := i
agentUserGroup, err := user.GetAgentUserGroup(ctx, curUser.ID, workspaceIDs[i])
if err != nil {
return nil, err
}
jobSubmissionTime := time.Now().UTC().Truncate(time.Millisecond)
taskID := model.NewTaskID()
conv := &protoconverter.ProtoConverter{}
checkpointUUIDs := conv.ToUUIDList(strings.Split(expIDcUUIDs.CheckpointUUIDSStr, ","))
go func() {
err = runCheckpointGCTask(
a.m.rm, a.m.db, taskID, jobID, jobSubmissionTime, taskSpec, exps[i].ID,
exps[i].Config, checkpointUUIDs, req.CheckpointGlobs, false, agentUserGroup, curUser,
nil,
)
if err != nil {
log.WithError(err).Error("failed to start checkpoint GC task")
}
}()
}
return &apiv1.CheckpointsRemoveFilesResponse{}, nil
}
func (a *apiServer) DeleteCheckpoints(
ctx context.Context,
req *apiv1.DeleteCheckpointsRequest,
) (*apiv1.DeleteCheckpointsResponse, error) {
if _, err := a.CheckpointsRemoveFiles(ctx, &apiv1.CheckpointsRemoveFilesRequest{
CheckpointUuids: req.CheckpointUuids,
CheckpointGlobs: []string{fullDeleteGlob},
}); err != nil {
return nil, err
}
return &apiv1.DeleteCheckpointsResponse{}, nil
}
func (a *apiServer) PostCheckpointMetadata(
ctx context.Context, req *apiv1.PostCheckpointMetadataRequest,
) (*apiv1.PostCheckpointMetadataResponse, error) {
curUser, _, err := grpcutil.GetUser(ctx)
if err != nil {
return nil, err
}
if err := a.m.canDoActionOnCheckpoint(ctx, *curUser, req.Checkpoint.Uuid,
expauth.AuthZProvider.Get().CanEditExperiment); err != nil {
return nil, err
}
currCheckpoint := &checkpointv1.Checkpoint{}
if err := a.m.db.QueryProto("get_checkpoint", currCheckpoint, req.Checkpoint.Uuid); err != nil {
return nil,
errors.Wrapf(err, "error fetching checkpoint %s from database", req.Checkpoint.Uuid)
}
currMeta, err := protojson.Marshal(currCheckpoint.Metadata)
if err != nil {
return nil, errors.Wrap(err, "error marshaling database checkpoint metadata")
}
newMeta, err := protojson.Marshal(req.Checkpoint.Metadata)
if err != nil {
return nil, errors.Wrap(err, "error marshaling request checkpoint metadata")
}
currCheckpoint.Metadata = req.Checkpoint.Metadata
log.Infof("checkpoint (%s) metadata changing from %s to %s",
req.Checkpoint.Uuid, currMeta, newMeta)
err = a.m.db.QueryProto("update_checkpoint_metadata",
&checkpointv1.Checkpoint{}, req.Checkpoint.Uuid, newMeta)
return &apiv1.PostCheckpointMetadataResponse{Checkpoint: currCheckpoint},
errors.Wrapf(err, "error updating checkpoint %s in database", req.Checkpoint.Uuid)
}
// Query for all trials that use a given checkpoint and return their metrics.
func (a *apiServer) GetTrialMetricsByCheckpoint(
ctx context.Context, req *apiv1.GetTrialMetricsByCheckpointRequest,
) (*apiv1.GetTrialMetricsByCheckpointResponse, error) {
curUser, _, err := grpcutil.GetUser(ctx)
if err != nil {
return nil, err
}
err = a.m.canDoActionOnCheckpoint(ctx, *curUser, req.CheckpointUuid,
expauth.AuthZProvider.Get().CanGetExperimentArtifacts)
if err != nil {
return nil, err
}
resp := &apiv1.GetTrialMetricsByCheckpointResponse{}
trialIDsQuery := db.Bun().NewSelect().Table("trial_source_infos").
Where("checkpoint_uuid = ?", req.CheckpointUuid)
if req.TrialSourceInfoType != nil {
trialIDsQuery.Where("trial_source_info_type = ?", req.TrialSourceInfoType.String())
}
metrics, err := trials.GetMetricsForTrialSourceInfoQuery(ctx, trialIDsQuery, req.MetricGroup)
if err != nil {
return nil, fmt.Errorf("failed to get trial source info %w", err)
}
resp.Metrics = append(resp.Metrics, metrics...)
return resp, nil
}