Skip to content

Commit 7da992e

Browse files
Compactor: skip parquet converter files when downloading blocks (#7972)
* Compactor: skip parquet converter files when downloading blocks The parquet converter writes its output under the same object prefix as the TSDB block, along with parquet-converter-mark.json. block.Download lists and downloads everything under that prefix and has no allowlist or filter for these files, so the compactor pulled *.parquet files and the marker onto local disk even though compaction only reads the index and chunks. block.Download is vendored Thanos code and builds its own ignore list, so the filter cannot be passed in from here. Wrap the per-tenant bucket used for compaction so that parquet files and the converter marker are hidden from Iter and IterWithAttributes. Reads, uploads and deletes are unchanged, and the blocks cleaner keeps using the unwrapped bucket, so it still sees and deletes these files with the block. Fixes #7967 Signed-off-by: Souvik Biswas <souvikbiswas.dev@gmail.com> * Compactor: also hide the no-convert marker and add a compactor-level test The parquet converter also writes parquet-no-convert-mark.json under the block prefix when it skips a block with too many label names. Hide it from listings together with the other parquet converter files. Add a compactor-level test: two real TSDB blocks plus parquet files and both markers in an in-memory bucket, run a compaction cycle, and assert that the index and chunks were read while no parquet file or marker was. Signed-off-by: Souvik Biswas <souvikbiswas.dev@gmail.com> * Compactor: declare the parquet test files inline in each test Signed-off-by: Souvik Biswas <souvikbiswas.dev@gmail.com> --------- Signed-off-by: Souvik Biswas <souvikbiswas.dev@gmail.com>
1 parent e21063a commit 7da992e

5 files changed

Lines changed: 379 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [BUGFIX] Ring: Ignore READONLY ingesters when assigning tokens so the active series remain balanced. #7946
4242
* [BUGFIX] Distributor: Fix a `panic: send on closed channel` crash during shutdown when `-distributor.num-push-workers` or `-distributor.num-query-workers` is greater than 0. #7964
4343
* [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975
44+
* [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972
4445

4546
## 1.22.0 2026-11-19
4647

‎pkg/compactor/compactor.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ func (c *Compactor) compactUserWithRetries(ctx context.Context, userID string) e
10221022
}
10231023

10241024
func (c *Compactor) compactUser(ctx context.Context, userID string) error {
1025-
bucket := bucket.NewUserBucketClient(userID, c.bucketClient, c.limits)
1025+
bucket := newParquetFilteredBucket(bucket.NewUserBucketClient(userID, c.bucketClient, c.limits))
10261026

10271027
reg := prometheus.NewRegistry()
10281028

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package compactor
2+
3+
import (
4+
"context"
5+
"io"
6+
"path"
7+
"strings"
8+
"sync"
9+
"testing"
10+
"time"
11+
12+
"github.com/oklog/ulid/v2"
13+
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
14+
"github.com/stretchr/testify/mock"
15+
"github.com/stretchr/testify/require"
16+
"github.com/thanos-io/objstore"
17+
"github.com/thanos-io/thanos/pkg/block/metadata"
18+
19+
cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet"
20+
"github.com/cortexproject/cortex/pkg/util/services"
21+
)
22+
23+
// bucketReadRecorder records the names of the objects read from a bucket.
24+
type bucketReadRecorder struct {
25+
mtx sync.Mutex
26+
reads []string
27+
}
28+
29+
func (r *bucketReadRecorder) record(name string) {
30+
r.mtx.Lock()
31+
defer r.mtx.Unlock()
32+
r.reads = append(r.reads, name)
33+
}
34+
35+
func (r *bucketReadRecorder) names() []string {
36+
r.mtx.Lock()
37+
defer r.mtx.Unlock()
38+
return append([]string(nil), r.reads...)
39+
}
40+
41+
// readRecordingBucket records every object read through Get and GetRange.
42+
type readRecordingBucket struct {
43+
objstore.Bucket
44+
rec *bucketReadRecorder
45+
}
46+
47+
func newReadRecordingBucket(b objstore.Bucket, rec *bucketReadRecorder) objstore.InstrumentedBucket {
48+
return &readRecordingBucket{Bucket: b, rec: rec}
49+
}
50+
51+
func (b *readRecordingBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
52+
b.rec.record(name)
53+
return b.Bucket.Get(ctx, name)
54+
}
55+
56+
func (b *readRecordingBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
57+
b.rec.record(name)
58+
return b.Bucket.GetRange(ctx, name, off, length)
59+
}
60+
61+
func (b *readRecordingBucket) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket {
62+
if ib, ok := b.Bucket.(objstore.InstrumentedBucket); ok {
63+
return &readRecordingBucket{Bucket: ib.WithExpectedErrs(fn), rec: b.rec}
64+
}
65+
return b
66+
}
67+
68+
func (b *readRecordingBucket) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader {
69+
return b.WithExpectedErrs(fn)
70+
}
71+
72+
func TestCompactor_ShouldNotDownloadParquetFilesWhenCompacting(t *testing.T) {
73+
rec := &bucketReadRecorder{}
74+
bucketClient := newReadRecordingBucket(objstore.WithNoopInstr(objstore.NewInMemBucket()), rec)
75+
76+
blocks := []ulid.ULID{
77+
createTSDBBlock(t, bucketClient, "user-1", 10, 20, map[string]string{"__name__": "Teste"}),
78+
createTSDBBlock(t, bucketClient, "user-1", 20, 30, map[string]string{"__name__": "Teste"}),
79+
}
80+
81+
// Put the parquet converter output next to each block.
82+
parquetFiles := []string{
83+
"0.labels.parquet",
84+
"0.chunks.parquet",
85+
"1.labels.parquet",
86+
"nested/2.chunks.parquet",
87+
cortex_parquet.ConverterMarkerFileName,
88+
cortex_parquet.NoConvertMarkerFileName,
89+
}
90+
for _, id := range blocks {
91+
for _, name := range parquetFiles {
92+
require.NoError(t, bucketClient.Upload(context.Background(), path.Join("user-1", id.String(), name), strings.NewReader("parquet")))
93+
}
94+
}
95+
96+
c, tsdbCompactor, tsdbPlanner, _, _ := prepare(t, prepareConfig(), bucketClient, nil)
97+
98+
// The TSDB compaction only runs once the blocks are on disk, so interrupt
99+
// the run from there.
100+
ctx, cancel := context.WithCancel(context.Background())
101+
tsdbCompactor.On("CompactWithBlockPopulator", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]ulid.ULID{}, context.Canceled).Run(func(args mock.Arguments) {
102+
cancel()
103+
})
104+
tsdbPlanner.On("Plan", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*metadata.Meta{
105+
{
106+
ULID: blocks[0],
107+
MinTime: 10,
108+
MaxTime: 20,
109+
},
110+
{
111+
ULID: blocks[1],
112+
MinTime: 20,
113+
MaxTime: 30,
114+
},
115+
}, nil)
116+
117+
require.NoError(t, services.StartAndAwaitRunning(ctx, c))
118+
require.Eventually(t, func() bool {
119+
return prom_testutil.ToFloat64(c.CompactionRunsInterrupted) == 1
120+
}, 20*time.Second, 50*time.Millisecond)
121+
require.NoError(t, services.StopAndAwaitTerminated(context.Background(), c))
122+
123+
tsdbCompactor.AssertCalled(t, "CompactWithBlockPopulator", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
124+
125+
reads := rec.names()
126+
127+
// The compaction inputs were really downloaded.
128+
for _, id := range blocks {
129+
prefix := path.Join("user-1", id.String()) + "/"
130+
require.Contains(t, reads, prefix+"index")
131+
132+
hasChunks := false
133+
for _, name := range reads {
134+
if strings.HasPrefix(name, prefix+"chunks/") {
135+
hasChunks = true
136+
}
137+
}
138+
require.True(t, hasChunks, "no chunk file of block %s was read", id)
139+
}
140+
141+
// None of the parquet converter files were.
142+
for _, name := range reads {
143+
require.False(t, strings.HasSuffix(name, ".parquet"), name)
144+
require.NotEqual(t, cortex_parquet.ConverterMarkerFileName, path.Base(name), name)
145+
require.NotEqual(t, cortex_parquet.NoConvertMarkerFileName, path.Base(name), name)
146+
}
147+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package compactor
2+
3+
import (
4+
"context"
5+
"path"
6+
"strings"
7+
8+
"github.com/thanos-io/objstore"
9+
10+
cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet"
11+
)
12+
13+
// parquetFileSuffix is the extension of the parquet files written by the parquet converter.
14+
const parquetFileSuffix = ".parquet"
15+
16+
// parquetFilteredBucket hides the files produced by the parquet converter from
17+
// listings. The converter writes them under the same prefix as the TSDB block,
18+
// and block.Download lists and downloads everything under that prefix, although
19+
// compaction never reads them. Only listings are filtered: reads, uploads and
20+
// deletes behave as usual.
21+
type parquetFilteredBucket struct {
22+
objstore.Bucket
23+
}
24+
25+
// newParquetFilteredBucket wraps the bucket used for compaction so that parquet
26+
// files and the parquet converter markers are not listed.
27+
func newParquetFilteredBucket(b objstore.InstrumentedBucket) objstore.InstrumentedBucket {
28+
return &parquetFilteredBucket{Bucket: b}
29+
}
30+
31+
func isParquetObject(name string) bool {
32+
if strings.HasSuffix(name, objstore.DirDelim) {
33+
return false
34+
}
35+
base := path.Base(name)
36+
return strings.HasSuffix(name, parquetFileSuffix) ||
37+
base == cortex_parquet.ConverterMarkerFileName ||
38+
base == cortex_parquet.NoConvertMarkerFileName
39+
}
40+
41+
func (b *parquetFilteredBucket) Iter(ctx context.Context, dir string, f func(name string) error, options ...objstore.IterOption) error {
42+
return b.Bucket.Iter(ctx, dir, func(name string) error {
43+
if isParquetObject(name) {
44+
return nil
45+
}
46+
return f(name)
47+
}, options...)
48+
}
49+
50+
func (b *parquetFilteredBucket) IterWithAttributes(ctx context.Context, dir string, f func(attrs objstore.IterObjectAttributes) error, options ...objstore.IterOption) error {
51+
return b.Bucket.IterWithAttributes(ctx, dir, func(attrs objstore.IterObjectAttributes) error {
52+
if isParquetObject(attrs.Name) {
53+
return nil
54+
}
55+
return f(attrs)
56+
}, options...)
57+
}
58+
59+
// WithExpectedErrs keeps the filtering when the underlying bucket is re-wrapped.
60+
func (b *parquetFilteredBucket) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket {
61+
if ib, ok := b.Bucket.(objstore.InstrumentedBucket); ok {
62+
return &parquetFilteredBucket{Bucket: ib.WithExpectedErrs(fn)}
63+
}
64+
return b
65+
}
66+
67+
// ReaderWithExpectedErrs keeps the filtering when the underlying bucket is re-wrapped.
68+
func (b *parquetFilteredBucket) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader {
69+
return b.WithExpectedErrs(fn)
70+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package compactor
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/go-kit/log"
10+
"github.com/oklog/ulid/v2"
11+
"github.com/stretchr/testify/require"
12+
"github.com/thanos-io/objstore"
13+
"github.com/thanos-io/thanos/pkg/block"
14+
"github.com/thanos-io/thanos/pkg/block/metadata"
15+
16+
cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet"
17+
)
18+
19+
func uploadBlockWithParquetFiles(t *testing.T, bkt objstore.Bucket, id ulid.ULID, parquetFiles []string) {
20+
t.Helper()
21+
22+
var meta metadata.Meta
23+
meta.ULID = id
24+
meta.MinTime = 0
25+
meta.MaxTime = 1000
26+
meta.Version = metadata.TSDBVersion1
27+
meta.Thanos = metadata.Thanos{Version: metadata.ThanosVersion1, Source: metadata.TestSource}
28+
var buf bytes.Buffer
29+
require.NoError(t, meta.Write(&buf))
30+
31+
files := map[string][]byte{
32+
block.MetaFilename: buf.Bytes(),
33+
block.IndexFilename: []byte("index"),
34+
"chunks/000001": []byte("chunk"),
35+
}
36+
for _, name := range parquetFiles {
37+
files[name] = []byte("parquet")
38+
}
39+
40+
for name, data := range files {
41+
require.NoError(t, bkt.Upload(context.Background(), id.String()+"/"+name, bytes.NewReader(data)))
42+
}
43+
}
44+
45+
func TestParquetFilteredBucket_Iter(t *testing.T) {
46+
ctx := context.Background()
47+
inmem := objstore.NewInMemBucket()
48+
id := ulid.MustNew(1, nil)
49+
parquetFiles := []string{
50+
"0.labels.parquet",
51+
"0.chunks.parquet",
52+
"1.labels.parquet",
53+
"nested/2.chunks.parquet",
54+
cortex_parquet.ConverterMarkerFileName,
55+
cortex_parquet.NoConvertMarkerFileName,
56+
}
57+
uploadBlockWithParquetFiles(t, inmem, id, parquetFiles)
58+
59+
filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem))
60+
61+
listRecursive := func(bkt objstore.BucketReader) []string {
62+
var names []string
63+
require.NoError(t, bkt.Iter(ctx, id.String(), func(name string) error {
64+
names = append(names, name)
65+
return nil
66+
}, objstore.WithRecursiveIter()))
67+
return names
68+
}
69+
70+
// Control: the unfiltered bucket lists the parquet files.
71+
unfiltered := listRecursive(inmem)
72+
for _, name := range parquetFiles {
73+
require.Contains(t, unfiltered, id.String()+"/"+name)
74+
}
75+
76+
got := listRecursive(filtered)
77+
for _, name := range parquetFiles {
78+
require.NotContains(t, got, id.String()+"/"+name)
79+
}
80+
require.Contains(t, got, id.String()+"/"+block.MetaFilename)
81+
require.Contains(t, got, id.String()+"/"+block.IndexFilename)
82+
require.Contains(t, got, id.String()+"/chunks/000001")
83+
84+
// Same behavior for IterWithAttributes.
85+
var gotAttrs []string
86+
require.NoError(t, filtered.IterWithAttributes(ctx, id.String(), func(attrs objstore.IterObjectAttributes) error {
87+
gotAttrs = append(gotAttrs, attrs.Name)
88+
return nil
89+
}, objstore.WithRecursiveIter()))
90+
for _, name := range parquetFiles {
91+
require.NotContains(t, gotAttrs, id.String()+"/"+name)
92+
}
93+
require.Contains(t, gotAttrs, id.String()+"/"+block.IndexFilename)
94+
}
95+
96+
func TestParquetFilteredBucket_ReadsAreNotFiltered(t *testing.T) {
97+
ctx := context.Background()
98+
inmem := objstore.NewInMemBucket()
99+
id := ulid.MustNew(1, nil)
100+
parquetFiles := []string{
101+
"0.labels.parquet",
102+
"0.chunks.parquet",
103+
"1.labels.parquet",
104+
"nested/2.chunks.parquet",
105+
cortex_parquet.ConverterMarkerFileName,
106+
cortex_parquet.NoConvertMarkerFileName,
107+
}
108+
uploadBlockWithParquetFiles(t, inmem, id, parquetFiles)
109+
110+
filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem))
111+
112+
for _, name := range parquetFiles {
113+
exists, err := filtered.Exists(ctx, id.String()+"/"+name)
114+
require.NoError(t, err)
115+
require.True(t, exists, name)
116+
}
117+
118+
// The filter survives re-wrapping for expected errors.
119+
rewrapped := filtered.WithExpectedErrs(filtered.IsObjNotFoundErr)
120+
var names []string
121+
require.NoError(t, rewrapped.Iter(ctx, id.String(), func(name string) error {
122+
names = append(names, name)
123+
return nil
124+
}, objstore.WithRecursiveIter()))
125+
require.NotContains(t, names, id.String()+"/0.labels.parquet")
126+
require.Contains(t, names, id.String()+"/"+block.IndexFilename)
127+
}
128+
129+
func TestParquetFilteredBucket_BlockDownloadSkipsParquetFiles(t *testing.T) {
130+
ctx := context.Background()
131+
inmem := objstore.NewInMemBucket()
132+
id := ulid.MustNew(1, nil)
133+
parquetFiles := []string{
134+
"0.labels.parquet",
135+
"0.chunks.parquet",
136+
"1.labels.parquet",
137+
"nested/2.chunks.parquet",
138+
cortex_parquet.ConverterMarkerFileName,
139+
cortex_parquet.NoConvertMarkerFileName,
140+
}
141+
uploadBlockWithParquetFiles(t, inmem, id, parquetFiles)
142+
143+
// Control: downloading from the unfiltered bucket pulls the parquet files.
144+
controlDir := t.TempDir()
145+
require.NoError(t, block.Download(ctx, log.NewNopLogger(), inmem, id, controlDir))
146+
require.FileExists(t, filepath.Join(controlDir, "0.labels.parquet"))
147+
require.FileExists(t, filepath.Join(controlDir, cortex_parquet.ConverterMarkerFileName))
148+
149+
filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem))
150+
dst := t.TempDir()
151+
require.NoError(t, block.Download(ctx, log.NewNopLogger(), filtered, id, dst))
152+
153+
require.FileExists(t, filepath.Join(dst, block.MetaFilename))
154+
require.FileExists(t, filepath.Join(dst, block.IndexFilename))
155+
require.FileExists(t, filepath.Join(dst, block.ChunksDirname, "000001"))
156+
157+
for _, name := range parquetFiles {
158+
require.NoFileExists(t, filepath.Join(dst, name))
159+
}
160+
}

0 commit comments

Comments
 (0)