Skip to content

Commit 899abc1

Browse files
authored
Merge pull request #8410 from thanos-io/partial_delete_marked
compact: ignore blocks with deletion mark in partial deletes
2 parents 5fc08d5 + d06dc23 commit 899abc1

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

cmd/thanos/compact.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func runCompact(
442442
return errors.Wrap(err, "syncing metas")
443443
}
444444

445-
compact.BestEffortCleanAbortedPartialUploads(ctx, logger, sy.Partial(), insBkt, compactMetrics.partialUploadDeleteAttempts, compactMetrics.blocksCleaned, compactMetrics.blockCleanupFailures)
445+
compact.BestEffortCleanAbortedPartialUploads(ctx, logger, sy.Partial(), insBkt, compactMetrics.partialUploadDeleteAttempts, compactMetrics.blocksCleaned, compactMetrics.blockCleanupFailures, ignoreDeletionMarkFilter.DeletionMarkBlocks())
446446
if err := blocksCleaner.DeleteMarkedBlocks(ctx); err != nil {
447447
return errors.Wrap(err, "cleaning marked blocks")
448448
}

cmd/thanos/tools_bucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ func registerBucketCleanup(app extkingpin.AppClause, objStoreConfig *extflag.Pat
917917

918918
level.Info(logger).Log("msg", "synced blocks done")
919919

920-
compact.BestEffortCleanAbortedPartialUploads(ctx, logger, sy.Partial(), insBkt, stubCounter, stubCounter, stubCounter)
920+
compact.BestEffortCleanAbortedPartialUploads(ctx, logger, sy.Partial(), insBkt, stubCounter, stubCounter, stubCounter, ignoreDeletionMarkFilter.DeletionMarkBlocks())
921921
if err := blocksCleaner.DeleteMarkedBlocks(ctx); err != nil {
922922
return errors.Wrap(err, "error cleaning blocks")
923923
}

pkg/compact/clean.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/thanos-io/objstore"
1818

1919
"github.com/thanos-io/thanos/pkg/block"
20+
"github.com/thanos-io/thanos/pkg/block/metadata"
2021
)
2122

2223
const (
@@ -61,10 +62,19 @@ func BestEffortCleanAbortedPartialUploads(
6162
deleteAttempts prometheus.Counter,
6263
blockCleanups prometheus.Counter,
6364
blockCleanupFailures prometheus.Counter,
65+
deletionMarkBlocks map[ulid.ULID]*metadata.DeletionMark,
6466
) {
6567
level.Info(logger).Log("msg", "started cleaning of aborted partial uploads")
6668

6769
for id := range partial {
70+
// NOTE(GiedriusS): we start to delete blocks from meta.json so at that point they are marked as partial.
71+
// If you have multiple compactor shards then they might try to delete the same block here.
72+
// We do not want to delete blocks that are marked for deletion, as they are already scheduled for deletion in the blocks cleaner.
73+
if _, ok := deletionMarkBlocks[id]; ok {
74+
level.Debug(logger).Log("msg", "ignoring block marked for deletion", "block", id)
75+
continue
76+
}
77+
6878
lastModifiedTime, err := getOldestModifiedTime(ctx, id, bkt)
6979
if err != nil {
7080
level.Warn(logger).Log("msg", "failed to get last modified time for block; falling back to block creation time", "block", id, "err", err)

pkg/compact/clean_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestBestEffortCleanAbortedPartialUploads(t *testing.T) {
7272
_, partial, err := metaFetcher.Fetch(ctx)
7373
testutil.Ok(t, err)
7474

75-
BestEffortCleanAbortedPartialUploads(ctx, logger, partial, bkt, deleteAttempts, blockCleanups, blockCleanupFailures)
75+
BestEffortCleanAbortedPartialUploads(ctx, logger, partial, bkt, deleteAttempts, blockCleanups, blockCleanupFailures, map[ulid.ULID]*metadata.DeletionMark{})
7676
testutil.Equals(t, 1.0, promtest.ToFloat64(deleteAttempts))
7777
testutil.Equals(t, 1.0, promtest.ToFloat64(blockCleanups))
7878
testutil.Equals(t, 0.0, promtest.ToFloat64(blockCleanupFailures))
@@ -88,6 +88,23 @@ func TestBestEffortCleanAbortedPartialUploads(t *testing.T) {
8888
exists, err = bkt.Exists(ctx, path.Join(shouldIgnoreID2.String(), "chunks", "000001"))
8989
testutil.Ok(t, err)
9090
testutil.Equals(t, true, exists)
91+
92+
// It has been marked for deletion, so it should not be deleted.
93+
shouldDeleteID, err = ulid.New(uint64(time.Now().Add(-PartialUploadThresholdAge-1*time.Hour).Unix()*1000), nil)
94+
testutil.Ok(t, err)
95+
96+
fakeChunk.Reset()
97+
fakeChunk.Write([]byte{0, 1, 2, 3})
98+
testutil.Ok(t, bkt.Upload(ctx, path.Join(shouldDeleteID.String(), "chunks", "000001"), &fakeChunk))
99+
testutil.Ok(t, mb.ChangeLastModified(path.Join(shouldDeleteID.String(), "chunks", "000001"), time.Now().Add(-PartialUploadThresholdAge-1*time.Hour)))
100+
101+
BestEffortCleanAbortedPartialUploads(ctx, logger, partial, bkt, deleteAttempts, blockCleanups, blockCleanupFailures, map[ulid.ULID]*metadata.DeletionMark{
102+
shouldDeleteID: {},
103+
})
104+
105+
exists, err = bkt.Exists(ctx, path.Join(shouldDeleteID.String(), "chunks", "000001"))
106+
testutil.Ok(t, err)
107+
testutil.Equals(t, true, exists)
91108
}
92109

93110
func TestGetLastModifiedTime(t *testing.T) {

0 commit comments

Comments
 (0)