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
|
|
|
)
|
|
|
|
|
2021-06-25 09:19:32 +01:00
|
|
|
const (
|
|
|
|
deleteBatchSizeLimit = intLimitRange(100)
|
|
|
|
deletePieceBatchLimit = intLimitRange(1000)
|
|
|
|
)
|
2021-06-24 16:14:58 +01:00
|
|
|
|
2020-12-09 12:24:37 +00:00
|
|
|
// DeleteBucketObjects contains arguments for deleting a whole bucket.
|
|
|
|
type DeleteBucketObjects struct {
|
|
|
|
Bucket BucketLocation
|
|
|
|
BatchSize int
|
|
|
|
|
2021-06-24 16:14:58 +01:00
|
|
|
// DeletePiecesBatchSize maximum number of DeletedSegmentInfo entries
|
|
|
|
// passed to DeletePieces function at once.
|
|
|
|
DeletePiecesBatchSize int
|
2020-12-09 12:24:37 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:19:32 +01:00
|
|
|
deleteBatchSizeLimit.Ensure(&opts.BatchSize)
|
|
|
|
deletePieceBatchLimit.Ensure(&opts.DeletePiecesBatchSize)
|
2021-06-24 16:14:58 +01:00
|
|
|
|
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
|
2021-06-25 09:19:32 +01:00
|
|
|
deletedSegmentsBatch := make([]DeletedSegmentInfo, 0, opts.DeletePiecesBatchSize)
|
2020-12-09 12:24:37 +00:00
|
|
|
for {
|
2021-06-24 16:14:58 +01:00
|
|
|
deletedSegmentsBatch = deletedSegmentsBatch[:0]
|
2021-06-04 14:21:09 +01:00
|
|
|
batchDeletedObjects := 0
|
2021-06-24 16:14:58 +01:00
|
|
|
deletedSegments := 0
|
2021-07-28 14:44:22 +01:00
|
|
|
err = withRows(db.db.QueryContext(ctx, query,
|
2021-06-25 09:19:32 +01:00
|
|
|
opts.Bucket.ProjectID, []byte(opts.Bucket.BucketName), opts.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{}{}
|
2021-06-24 16:14:58 +01:00
|
|
|
deletedSegmentsBatch = append(deletedSegmentsBatch, segment)
|
|
|
|
|
2021-06-25 09:19:32 +01:00
|
|
|
if len(deletedSegmentsBatch) >= opts.DeletePiecesBatchSize {
|
2021-06-24 16:14:58 +01:00
|
|
|
if opts.DeletePieces != nil {
|
|
|
|
err = opts.DeletePieces(ctx, deletedSegmentsBatch)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
deletedSegmentsBatch = deletedSegmentsBatch[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deletedSegments++
|
2020-12-09 12:24:37 +00:00
|
|
|
}
|
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)
|
2021-06-24 16:14:58 +01:00
|
|
|
mon.Meter("segment_delete").Mark(deletedSegments)
|
2021-06-04 14:21:09 +01:00
|
|
|
|
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)
|
|
|
|
}
|
2021-07-28 16:16:01 +01:00
|
|
|
if deletedSegments == 0 {
|
2020-12-09 12:24:37 +00:00
|
|
|
return deletedObjectCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.DeletePieces != nil {
|
2021-06-24 16:14:58 +01:00
|
|
|
err = opts.DeletePieces(ctx, deletedSegmentsBatch)
|
2020-12-09 12:24:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return deletedObjectCount, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|