2020-12-09 12:24:37 +00:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metabase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2021-04-23 10:52:40 +01:00
|
|
|
"storj.io/private/dbutil"
|
|
|
|
"storj.io/private/tagsql"
|
2020-12-09 12:24:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const deleteBatchSizeLimit = 100
|
|
|
|
|
|
|
|
// DeleteBucketObjects contains arguments for deleting a whole bucket.
|
|
|
|
type DeleteBucketObjects struct {
|
|
|
|
Bucket BucketLocation
|
|
|
|
BatchSize int
|
|
|
|
|
|
|
|
// DeletePieces is called for every batch of objects.
|
|
|
|
// Slice `segments` will be reused between calls.
|
|
|
|
DeletePieces func(ctx context.Context, segments []DeletedSegmentInfo) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucketObjects deletes all objects in the specified bucket.
|
|
|
|
func (db *DB) DeleteBucketObjects(ctx context.Context, opts DeleteBucketObjects) (deletedObjectCount int64, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
if err := opts.Bucket.Verify(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
batchSize := opts.BatchSize
|
|
|
|
if batchSize <= 0 || batchSize > deleteBatchSizeLimit {
|
|
|
|
batchSize = deleteBatchSizeLimit
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:48:33 +00:00
|
|
|
var query string
|
2021-05-11 09:49:26 +01:00
|
|
|
switch db.impl {
|
2021-02-19 15:48:33 +00:00
|
|
|
case dbutil.Cockroach:
|
|
|
|
query = `
|
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE project_id = $1 AND bucket_name = $2 LIMIT $3
|
|
|
|
RETURNING objects.stream_id
|
|
|
|
)
|
|
|
|
DELETE FROM segments
|
|
|
|
WHERE segments.stream_id in (SELECT deleted_objects.stream_id FROM deleted_objects)
|
|
|
|
RETURNING segments.stream_id, segments.root_piece_id, segments.remote_alias_pieces
|
|
|
|
`
|
|
|
|
case dbutil.Postgres:
|
|
|
|
query = `
|
|
|
|
WITH deleted_objects AS (
|
|
|
|
DELETE FROM objects
|
|
|
|
WHERE stream_id IN (
|
|
|
|
SELECT stream_id FROM objects
|
|
|
|
WHERE project_id = $1 AND bucket_name = $2
|
|
|
|
LIMIT $3
|
|
|
|
)
|
|
|
|
RETURNING objects.stream_id
|
|
|
|
)
|
|
|
|
DELETE FROM segments
|
|
|
|
WHERE segments.stream_id in (SELECT deleted_objects.stream_id FROM deleted_objects)
|
|
|
|
RETURNING segments.stream_id, segments.root_piece_id, segments.remote_alias_pieces
|
|
|
|
`
|
|
|
|
default:
|
2021-06-04 14:21:09 +01:00
|
|
|
return 0, Error.New("unhandled database: %v", db.impl)
|
2021-02-19 15:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:37 +00:00
|
|
|
// TODO: fix the count for objects without segments
|
|
|
|
var deleteSegments []DeletedSegmentInfo
|
|
|
|
for {
|
|
|
|
deleteSegments = deleteSegments[:0]
|
2021-06-04 14:21:09 +01:00
|
|
|
batchDeletedObjects := 0
|
2021-02-19 15:48:33 +00:00
|
|
|
err = withRows(db.db.Query(ctx, query,
|
2021-03-02 11:22:49 +00:00
|
|
|
opts.Bucket.ProjectID, []byte(opts.Bucket.BucketName), batchSize))(func(rows tagsql.Rows) error {
|
2020-12-09 12:24:37 +00:00
|
|
|
ids := map[uuid.UUID]struct{}{} // TODO: avoid map here
|
|
|
|
for rows.Next() {
|
|
|
|
var streamID uuid.UUID
|
|
|
|
var segment DeletedSegmentInfo
|
2021-02-08 09:33:45 +00:00
|
|
|
var aliasPieces AliasPieces
|
|
|
|
err := rows.Scan(&streamID, &segment.RootPieceID, &aliasPieces)
|
2020-12-09 12:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
2021-02-08 09:33:45 +00:00
|
|
|
segment.Pieces, err = db.aliasCache.ConvertAliasesToPieces(ctx, aliasPieces)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-12-09 12:24:37 +00:00
|
|
|
ids[streamID] = struct{}{}
|
|
|
|
deleteSegments = append(deleteSegments, segment)
|
|
|
|
}
|
2021-06-04 14:21:09 +01:00
|
|
|
batchDeletedObjects = len(ids)
|
2020-12-09 12:24:37 +00:00
|
|
|
deletedObjectCount += int64(len(ids))
|
|
|
|
return nil
|
|
|
|
})
|
2021-06-04 14:21:09 +01:00
|
|
|
|
|
|
|
mon.Meter("object_delete").Mark(batchDeletedObjects)
|
|
|
|
mon.Meter("segment_delete").Mark(len(deleteSegments))
|
|
|
|
|
2020-12-09 12:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return deletedObjectCount, nil
|
|
|
|
}
|
|
|
|
return deletedObjectCount, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
if len(deleteSegments) == 0 {
|
|
|
|
return deletedObjectCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.DeletePieces != nil {
|
|
|
|
err = opts.DeletePieces(ctx, deleteSegments)
|
|
|
|
if err != nil {
|
|
|
|
return deletedObjectCount, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|