-
Notifications
You must be signed in to change notification settings - Fork 402
/
delete_objects.go
561 lines (500 loc) · 18.3 KB
/
delete_objects.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package metabase
import (
"context"
"encoding/hex"
"fmt"
"time"
"cloud.google.com/go/spanner"
"github.com/jackc/pgx/v5"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/shared/dbutil/pgxutil"
"storj.io/storj/shared/dbutil/spannerutil"
"storj.io/storj/shared/tagsql"
)
const (
deleteBatchsizeLimit = intLimitRange(1000)
)
// DeleteExpiredObjects contains all the information necessary to delete expired objects and segments.
type DeleteExpiredObjects struct {
ExpiredBefore time.Time
AsOfSystemInterval time.Duration
BatchSize int
}
// DeleteExpiredObjects deletes all objects that expired before expiredBefore.
func (db *DB) DeleteExpiredObjects(ctx context.Context, opts DeleteExpiredObjects) (err error) {
defer mon.Task()(&ctx)(&err)
for _, a := range db.adapters {
err = db.deleteObjectsAndSegmentsBatch(ctx, opts.BatchSize, func(startAfter ObjectStream, batchsize int) (last ObjectStream, err error) {
expiredObjects, err := a.FindExpiredObjects(ctx, opts, startAfter, batchsize)
if err != nil {
return ObjectStream{}, Error.New("unable to select expired objects for deletion: %w", err)
}
if len(expiredObjects) == 0 {
return ObjectStream{}, nil
}
objectsDeleted, segmentsDeleted, err := a.DeleteObjectsAndSegments(ctx, expiredObjects)
mon.Meter("object_delete").Mark64(objectsDeleted)
mon.Meter("segment_delete").Mark64(segmentsDeleted)
return expiredObjects[len(expiredObjects)-1], err
})
if err != nil {
db.log.Error("failed to delete expired objects from DB", zap.Error(err), zap.String("adapter", fmt.Sprintf("%T", a)))
}
}
return nil
}
// FindExpiredObjects finds up to batchSize objects that expired before opts.ExpiredBefore.
func (p *PostgresAdapter) FindExpiredObjects(ctx context.Context, opts DeleteExpiredObjects, startAfter ObjectStream, batchSize int) (expiredObjects []ObjectStream, err error) {
query := `
SELECT
project_id, bucket_name, object_key, version, stream_id,
expires_at
FROM objects
` + p.impl.AsOfSystemInterval(opts.AsOfSystemInterval) + `
WHERE
(project_id, bucket_name, object_key, version) > ($1, $2, $3, $4)
AND expires_at < $5
ORDER BY project_id, bucket_name, object_key, version
LIMIT $6;
`
expiredObjects = make([]ObjectStream, 0, batchSize)
err = withRows(p.db.QueryContext(ctx, query,
startAfter.ProjectID, startAfter.BucketName, []byte(startAfter.ObjectKey), startAfter.Version,
opts.ExpiredBefore,
batchSize),
)(func(rows tagsql.Rows) error {
var last ObjectStream
for rows.Next() {
var expiresAt time.Time
err = rows.Scan(
&last.ProjectID, &last.BucketName, &last.ObjectKey, &last.Version, &last.StreamID,
&expiresAt)
if err != nil {
return Error.Wrap(err)
}
p.log.Info("Deleting expired object",
zap.Stringer("Project", last.ProjectID),
zap.Stringer("Bucket", last.BucketName),
zap.String("Object Key", string(last.ObjectKey)),
zap.Int64("Version", int64(last.Version)),
zap.String("StreamID", hex.EncodeToString(last.StreamID[:])),
zap.Time("Expired At", expiresAt),
)
expiredObjects = append(expiredObjects, last)
}
return nil
})
if err != nil {
return nil, Error.Wrap(err)
}
return expiredObjects, nil
}
// FindExpiredObjects finds up to batchSize objects that expired before opts.ExpiredBefore.
func (s *SpannerAdapter) FindExpiredObjects(ctx context.Context, opts DeleteExpiredObjects, startAfter ObjectStream, batchSize int) (expiredObjects []ObjectStream, err error) {
// TODO(spanner): check whether this query is executed efficiently
expiredObjects, err = spannerutil.CollectRows(s.client.Single().Query(ctx, spanner.Statement{
SQL: `
SELECT
project_id, bucket_name, object_key, version, stream_id,
expires_at
FROM objects
WHERE
expires_at < @expires_at
AND (
project_id > @project_id
OR (project_id = @project_id AND bucket_name > @bucket_name)
OR (project_id = @project_id AND bucket_name = @bucket_name AND object_key > @object_key)
OR (project_id = @project_id AND bucket_name = @bucket_name AND object_key = @object_key AND version > @version)
)
ORDER BY project_id, bucket_name, object_key, version
LIMIT @batch_size;
`, Params: map[string]interface{}{
"project_id": startAfter.ProjectID,
"bucket_name": startAfter.BucketName,
"object_key": startAfter.ObjectKey,
"version": startAfter.Version,
"expires_at": opts.ExpiredBefore,
"batch_size": batchSize,
},
}), func(row *spanner.Row, object *ObjectStream) error {
var expiresAt time.Time
err := row.Columns(
&object.ProjectID, &object.BucketName, &object.ObjectKey, &object.Version, &object.StreamID,
&expiresAt)
if err != nil {
return Error.Wrap(err)
}
s.log.Info("Deleting expired object",
zap.Stringer("Project", object.ProjectID),
zap.Stringer("Bucket", object.BucketName),
zap.String("Object Key", string(object.ObjectKey)),
zap.Int64("Version", int64(object.Version)),
zap.String("StreamID", hex.EncodeToString(object.StreamID[:])),
zap.Time("Expired At", expiresAt),
)
return nil
})
return expiredObjects, Error.Wrap(err)
}
// DeleteZombieObjects contains all the information necessary to delete zombie objects and segments.
type DeleteZombieObjects struct {
DeadlineBefore time.Time
InactiveDeadline time.Time
AsOfSystemInterval time.Duration
BatchSize int
}
// DeleteZombieObjects deletes all objects that zombie deletion deadline passed.
// TODO will be removed when objects table will be free from pending objects.
func (db *DB) DeleteZombieObjects(ctx context.Context, opts DeleteZombieObjects) (err error) {
defer mon.Task()(&ctx)(&err)
for _, a := range db.adapters {
err = db.deleteObjectsAndSegmentsBatch(ctx, opts.BatchSize, func(startAfter ObjectStream, batchsize int) (last ObjectStream, err error) {
objects, err := a.FindZombieObjects(ctx, opts, startAfter, batchsize)
if err != nil {
return ObjectStream{}, Error.Wrap(err)
}
if len(objects) == 0 {
return ObjectStream{}, nil
}
objectsDeleted, segmentsDeleted, err := a.DeleteInactiveObjectsAndSegments(ctx, objects, opts)
if err != nil {
return ObjectStream{}, Error.Wrap(err)
}
mon.Meter("zombie_object_delete").Mark64(objectsDeleted)
mon.Meter("object_delete").Mark64(objectsDeleted)
mon.Meter("zombie_segment_delete").Mark64(segmentsDeleted)
mon.Meter("segment_delete").Mark64(segmentsDeleted)
return objects[len(objects)-1], nil
})
if err != nil {
db.log.Warn("delete from DB zombie objects", zap.Error(err))
}
}
return nil
}
// FindZombieObjects locates up to batchSize zombie objects that need deletion.
func (p *PostgresAdapter) FindZombieObjects(ctx context.Context, opts DeleteZombieObjects, startAfter ObjectStream, batchSize int) (objects []ObjectStream, err error) {
// pending objects migrated to metabase didn't have zombie_deletion_deadline column set, because
// of that we need to get into account also object with zombie_deletion_deadline set to NULL
query := `
SELECT
project_id, bucket_name, object_key, version, stream_id
FROM objects
` + p.impl.AsOfSystemInterval(opts.AsOfSystemInterval) + `
WHERE
(project_id, bucket_name, object_key, version) > ($1, $2, $3, $4)
AND status = ` + statusPending + `
AND (zombie_deletion_deadline IS NULL OR zombie_deletion_deadline < $5)
ORDER BY project_id, bucket_name, object_key, version
LIMIT $6;`
objects = make([]ObjectStream, 0, batchSize)
err = withRows(p.db.QueryContext(ctx, query,
startAfter.ProjectID, startAfter.BucketName, []byte(startAfter.ObjectKey), startAfter.Version,
opts.DeadlineBefore,
batchSize),
)(func(rows tagsql.Rows) error {
var last ObjectStream
for rows.Next() {
err = rows.Scan(&last.ProjectID, &last.BucketName, &last.ObjectKey, &last.Version, &last.StreamID)
if err != nil {
return Error.Wrap(err)
}
p.log.Debug("selected zombie object for deleting it",
zap.Stringer("Project", last.ProjectID),
zap.Stringer("Bucket", last.BucketName),
zap.String("Object Key", string(last.ObjectKey)),
zap.Int64("Version", int64(last.Version)),
zap.String("StreamID", hex.EncodeToString(last.StreamID[:])),
)
objects = append(objects, last)
}
return nil
})
if err != nil {
return nil, Error.Wrap(err)
}
return objects, nil
}
// FindZombieObjects locates up to batchSize zombie objects that need deletion.
func (s *SpannerAdapter) FindZombieObjects(ctx context.Context, opts DeleteZombieObjects, startAfter ObjectStream, batchSize int) (objects []ObjectStream, err error) {
// pending objects migrated to metabase didn't have zombie_deletion_deadline column set, because
// of that we need to get into account also object with zombie_deletion_deadline set to NULL
objects, err = spannerutil.CollectRows(s.client.Single().Query(ctx, spanner.Statement{
SQL: `
SELECT
project_id, bucket_name, object_key, version, stream_id
FROM objects
WHERE
status = ` + statusPending + `
AND (zombie_deletion_deadline IS NULL OR zombie_deletion_deadline < @deadline)
AND ` +
TupleGreaterThanSQL(
[]string{"project_id", "bucket_name", "object_key", "version"},
[]string{"@project_id", "@bucket_name", "@object_key", "@version"}, false) + `
ORDER BY project_id, bucket_name, object_key, version
LIMIT @batch_size
`,
Params: map[string]interface{}{
"project_id": startAfter.ProjectID,
"bucket_name": startAfter.BucketName,
"object_key": startAfter.ObjectKey,
"version": startAfter.Version,
"deadline": opts.DeadlineBefore,
"batch_size": batchSize,
},
}), func(row *spanner.Row, object *ObjectStream) error {
err := row.Columns(&object.ProjectID, &object.BucketName, &object.ObjectKey, &object.Version, &object.StreamID)
if err != nil {
return Error.Wrap(err)
}
s.log.Debug("selected zombie object for deleting it",
zap.Stringer("Project", object.ProjectID),
zap.Stringer("Bucket", object.BucketName),
zap.String("Object Key", string(object.ObjectKey)),
zap.Int64("Version", int64(object.Version)),
zap.String("StreamID", hex.EncodeToString(object.StreamID[:])),
)
return nil
})
if err != nil {
return nil, Error.Wrap(err)
}
return objects, nil
}
func (db *DB) deleteObjectsAndSegmentsBatch(ctx context.Context, batchsize int, deleteBatch func(startAfter ObjectStream, batchsize int) (last ObjectStream, err error)) (err error) {
defer mon.Task()(&ctx)(&err)
deleteBatchsizeLimit.Ensure(&batchsize)
var startAfter ObjectStream
for {
lastDeleted, err := deleteBatch(startAfter, batchsize)
if err != nil {
return err
}
if lastDeleted.StreamID.IsZero() {
return nil
}
startAfter = lastDeleted
}
}
// DeleteObjectsAndSegments deletes expired objects and associated segments.
func (p *PostgresAdapter) DeleteObjectsAndSegments(ctx context.Context, objects []ObjectStream) (objectsDeleted, segmentsDeleted int64, err error) {
defer mon.Task()(&ctx)(&err)
if len(objects) == 0 {
return 0, 0, nil
}
err = pgxutil.Conn(ctx, p.db, func(conn *pgx.Conn) error {
var batch pgx.Batch
for _, obj := range objects {
obj := obj
batch.Queue(`
WITH deleted_objects AS (
DELETE FROM objects
WHERE (project_id, bucket_name, object_key, version, stream_id) = ($1::BYTEA, $2, $3, $4, $5::BYTEA)
RETURNING stream_id
)
DELETE FROM segments
WHERE segments.stream_id = $5::BYTEA
`, obj.ProjectID, obj.BucketName, []byte(obj.ObjectKey), obj.Version, obj.StreamID)
}
results := conn.SendBatch(ctx, &batch)
defer func() { err = errs.Combine(err, results.Close()) }()
var errlist errs.Group
for i := 0; i < batch.Len(); i++ {
result, err := results.Exec()
errlist.Add(err)
if affectedSegmentCount := result.RowsAffected(); affectedSegmentCount > 0 {
// Note, this slightly miscounts objects without any segments
// there doesn't seem to be a simple work around for this.
// Luckily, this is used only for metrics, where it's not a
// significant problem to slightly miscount.
objectsDeleted++
segmentsDeleted += affectedSegmentCount
}
}
return errlist.Err()
})
if err != nil {
return 0, 0, Error.New("unable to delete expired objects: %w", err)
}
return objectsDeleted, segmentsDeleted, nil
}
// DeleteObjectsAndSegments deletes expired objects and associated segments.
func (s *SpannerAdapter) DeleteObjectsAndSegments(ctx context.Context, objects []ObjectStream) (objectsDeleted, segmentsDeleted int64, err error) {
defer mon.Task()(&ctx)(&err)
if len(objects) == 0 {
return 0, 0, nil
}
_, err = s.client.ReadWriteTransaction(ctx, func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
// can't use Mutations here, since we only want to delete objects by the specified keys
// if and only if the stream_id matches.
var statements []spanner.Statement
for _, obj := range objects {
obj := obj
statements = append(statements, spanner.Statement{
SQL: `
DELETE FROM objects
WHERE (project_id, bucket_name, object_key, version, stream_id) = (@project_id, @bucket_name, @object_key, @version, @stream_id)
`,
Params: map[string]interface{}{
"project_id": obj.ProjectID,
"bucket_name": obj.BucketName,
"object_key": obj.ObjectKey,
"version": obj.Version,
"stream_id": obj.StreamID,
},
})
}
numDeleteds, err := tx.BatchUpdate(ctx, statements)
if err != nil {
return Error.Wrap(err)
}
for _, numDeleted := range numDeleteds {
objectsDeleted += numDeleted
}
streamIDs := make([][]byte, 0, len(objects))
for _, obj := range objects {
streamIDs = append(streamIDs, obj.StreamID.Bytes())
}
numSegments, err := tx.Update(ctx, spanner.Statement{
SQL: `
DELETE FROM segments
WHERE ARRAY_INCLUDES(@stream_ids, stream_id)
`,
Params: map[string]interface{}{
"stream_ids": streamIDs,
},
})
if err != nil {
return Error.Wrap(err)
}
segmentsDeleted += numSegments
return nil
})
if err != nil {
return 0, 0, Error.New("unable to delete expired objects: %w", err)
}
return objectsDeleted, segmentsDeleted, nil
}
// DeleteInactiveObjectsAndSegments deletes inactive objects and associated segments.
func (p *PostgresAdapter) DeleteInactiveObjectsAndSegments(ctx context.Context, objects []ObjectStream, opts DeleteZombieObjects) (objectsDeleted, segmentsDeleted int64, err error) {
defer mon.Task()(&ctx)(&err)
if len(objects) == 0 {
return 0, 0, nil
}
err = pgxutil.Conn(ctx, p.db, func(conn *pgx.Conn) error {
var batch pgx.Batch
for _, obj := range objects {
batch.Queue(`
WITH check_segments AS (
SELECT 1 FROM segments
WHERE stream_id = $5::BYTEA AND created_at > $6
), deleted_objects AS (
DELETE FROM objects
WHERE
(project_id, bucket_name, object_key, version) = ($1::BYTEA, $2::BYTEA, $3::BYTEA, $4) AND
NOT EXISTS (SELECT 1 FROM check_segments)
RETURNING stream_id
)
DELETE FROM segments
WHERE segments.stream_id IN (SELECT stream_id FROM deleted_objects)
`, obj.ProjectID, obj.BucketName, []byte(obj.ObjectKey), obj.Version, obj.StreamID, opts.InactiveDeadline)
}
results := conn.SendBatch(ctx, &batch)
defer func() { err = errs.Combine(err, results.Close()) }()
// TODO calculate deleted objects
var errList errs.Group
for i := 0; i < batch.Len(); i++ {
result, err := results.Exec()
errList.Add(err)
if err == nil {
segmentsDeleted += result.RowsAffected()
}
}
return errList.Err()
})
if err != nil {
return objectsDeleted, segmentsDeleted, Error.New("unable to delete zombie objects: %w", err)
}
return objectsDeleted, segmentsDeleted, nil
}
// DeleteInactiveObjectsAndSegments deletes inactive objects and associated segments.
func (s *SpannerAdapter) DeleteInactiveObjectsAndSegments(ctx context.Context, objects []ObjectStream, opts DeleteZombieObjects) (objectsDeleted, segmentsDeleted int64, err error) {
defer mon.Task()(&ctx)(&err)
if len(objects) == 0 {
return 0, 0, nil
}
_, err = s.client.ReadWriteTransaction(ctx, func(ctx context.Context, tx *spanner.ReadWriteTransaction) error {
// can't use Mutations here, since we only want to delete objects by the specified keys
// if and only if the stream_id matches and no associated segments were uploaded after
// opts.InactiveDeadline.
var statements []spanner.Statement
for _, obj := range objects {
obj := obj
statements = append(statements, spanner.Statement{
SQL: `
DELETE FROM objects
WHERE
(project_id, bucket_name, object_key, version, stream_id) = (@project_id, @bucket_name, @object_key, @version, @stream_id)
AND NOT EXISTS (
SELECT 1 FROM segments
WHERE
segments.stream_id = objects.stream_id
AND segments.created_at > @inactive_deadline
)
`,
Params: map[string]interface{}{
"project_id": obj.ProjectID,
"bucket_name": obj.BucketName,
"object_key": obj.ObjectKey,
"version": obj.Version,
"stream_id": obj.StreamID,
"inactive_deadline": opts.InactiveDeadline,
},
})
}
numDeleteds, err := tx.BatchUpdate(ctx, statements)
if err != nil {
return Error.Wrap(err)
}
for _, numDeleted := range numDeleteds {
objectsDeleted += numDeleted
}
streamIDs := make([][]byte, 0, len(objects))
for _, obj := range objects {
streamIDs = append(streamIDs, obj.StreamID.Bytes())
}
// TODO(spanner): this is not quite correct, instead of assuming how the previous check went,
// we should delete the segments based on what we actually deleted.
//
// Alternatively, we might be able to do a single statement per object to delete both segments
// and object itself at the same time.
numSegments, err := tx.Update(ctx, spanner.Statement{
SQL: `
DELETE FROM segments
WHERE ARRAY_INCLUDES(@stream_ids, stream_id)
AND NOT EXISTS (
SELECT 1 FROM segments s2
WHERE
s2.stream_id = segments.stream_id
AND s2.created_at > @inactive_deadline
)
`,
Params: map[string]interface{}{
"stream_ids": streamIDs,
"inactive_deadline": opts.InactiveDeadline,
},
})
if err != nil {
return Error.Wrap(err)
}
segmentsDeleted += numSegments
return nil
})
if err != nil {
return objectsDeleted, segmentsDeleted, Error.New("unable to delete zombie objects: %w", err)
}
return objectsDeleted, segmentsDeleted, nil
}