diff --git a/CHANGELOG.md b/CHANGELOG.md index afad9d19f4..4cebf7f928 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ * [BUGFIX] Ring: Ignore READONLY ingesters when assigning tokens so the active series remain balanced. #7946 * [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 * [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 +* [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 ## 1.22.0 2026-11-19 diff --git a/pkg/compactor/compactor.go b/pkg/compactor/compactor.go index 123327911d..33f7c1ebce 100644 --- a/pkg/compactor/compactor.go +++ b/pkg/compactor/compactor.go @@ -1022,7 +1022,7 @@ func (c *Compactor) compactUserWithRetries(ctx context.Context, userID string) e } func (c *Compactor) compactUser(ctx context.Context, userID string) error { - bucket := bucket.NewUserBucketClient(userID, c.bucketClient, c.limits) + bucket := newParquetFilteredBucket(bucket.NewUserBucketClient(userID, c.bucketClient, c.limits)) reg := prometheus.NewRegistry() diff --git a/pkg/compactor/compactor_parquet_files_test.go b/pkg/compactor/compactor_parquet_files_test.go new file mode 100644 index 0000000000..eecd27e03b --- /dev/null +++ b/pkg/compactor/compactor_parquet_files_test.go @@ -0,0 +1,147 @@ +package compactor + +import ( + "context" + "io" + "path" + "strings" + "sync" + "testing" + "time" + + "github.com/oklog/ulid/v2" + prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "github.com/thanos-io/objstore" + "github.com/thanos-io/thanos/pkg/block/metadata" + + cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet" + "github.com/cortexproject/cortex/pkg/util/services" +) + +// bucketReadRecorder records the names of the objects read from a bucket. +type bucketReadRecorder struct { + mtx sync.Mutex + reads []string +} + +func (r *bucketReadRecorder) record(name string) { + r.mtx.Lock() + defer r.mtx.Unlock() + r.reads = append(r.reads, name) +} + +func (r *bucketReadRecorder) names() []string { + r.mtx.Lock() + defer r.mtx.Unlock() + return append([]string(nil), r.reads...) +} + +// readRecordingBucket records every object read through Get and GetRange. +type readRecordingBucket struct { + objstore.Bucket + rec *bucketReadRecorder +} + +func newReadRecordingBucket(b objstore.Bucket, rec *bucketReadRecorder) objstore.InstrumentedBucket { + return &readRecordingBucket{Bucket: b, rec: rec} +} + +func (b *readRecordingBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) { + b.rec.record(name) + return b.Bucket.Get(ctx, name) +} + +func (b *readRecordingBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) { + b.rec.record(name) + return b.Bucket.GetRange(ctx, name, off, length) +} + +func (b *readRecordingBucket) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket { + if ib, ok := b.Bucket.(objstore.InstrumentedBucket); ok { + return &readRecordingBucket{Bucket: ib.WithExpectedErrs(fn), rec: b.rec} + } + return b +} + +func (b *readRecordingBucket) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader { + return b.WithExpectedErrs(fn) +} + +func TestCompactor_ShouldNotDownloadParquetFilesWhenCompacting(t *testing.T) { + rec := &bucketReadRecorder{} + bucketClient := newReadRecordingBucket(objstore.WithNoopInstr(objstore.NewInMemBucket()), rec) + + blocks := []ulid.ULID{ + createTSDBBlock(t, bucketClient, "user-1", 10, 20, map[string]string{"__name__": "Teste"}), + createTSDBBlock(t, bucketClient, "user-1", 20, 30, map[string]string{"__name__": "Teste"}), + } + + // Put the parquet converter output next to each block. + parquetFiles := []string{ + "0.labels.parquet", + "0.chunks.parquet", + "1.labels.parquet", + "nested/2.chunks.parquet", + cortex_parquet.ConverterMarkerFileName, + cortex_parquet.NoConvertMarkerFileName, + } + for _, id := range blocks { + for _, name := range parquetFiles { + require.NoError(t, bucketClient.Upload(context.Background(), path.Join("user-1", id.String(), name), strings.NewReader("parquet"))) + } + } + + c, tsdbCompactor, tsdbPlanner, _, _ := prepare(t, prepareConfig(), bucketClient, nil) + + // The TSDB compaction only runs once the blocks are on disk, so interrupt + // the run from there. + ctx, cancel := context.WithCancel(context.Background()) + tsdbCompactor.On("CompactWithBlockPopulator", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]ulid.ULID{}, context.Canceled).Run(func(args mock.Arguments) { + cancel() + }) + tsdbPlanner.On("Plan", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*metadata.Meta{ + { + ULID: blocks[0], + MinTime: 10, + MaxTime: 20, + }, + { + ULID: blocks[1], + MinTime: 20, + MaxTime: 30, + }, + }, nil) + + require.NoError(t, services.StartAndAwaitRunning(ctx, c)) + require.Eventually(t, func() bool { + return prom_testutil.ToFloat64(c.CompactionRunsInterrupted) == 1 + }, 20*time.Second, 50*time.Millisecond) + require.NoError(t, services.StopAndAwaitTerminated(context.Background(), c)) + + tsdbCompactor.AssertCalled(t, "CompactWithBlockPopulator", mock.Anything, mock.Anything, mock.Anything, mock.Anything) + + reads := rec.names() + + // The compaction inputs were really downloaded. + for _, id := range blocks { + prefix := path.Join("user-1", id.String()) + "/" + require.Contains(t, reads, prefix+"index") + + hasChunks := false + for _, name := range reads { + if strings.HasPrefix(name, prefix+"chunks/") { + hasChunks = true + } + } + require.True(t, hasChunks, "no chunk file of block %s was read", id) + } + + // None of the parquet converter files were. + for _, name := range reads { + require.False(t, strings.HasSuffix(name, ".parquet"), name) + require.NotEqual(t, cortex_parquet.ConverterMarkerFileName, path.Base(name), name) + require.NotEqual(t, cortex_parquet.NoConvertMarkerFileName, path.Base(name), name) + } +} diff --git a/pkg/compactor/parquet_filtered_bucket.go b/pkg/compactor/parquet_filtered_bucket.go new file mode 100644 index 0000000000..6655e7bdae --- /dev/null +++ b/pkg/compactor/parquet_filtered_bucket.go @@ -0,0 +1,70 @@ +package compactor + +import ( + "context" + "path" + "strings" + + "github.com/thanos-io/objstore" + + cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet" +) + +// parquetFileSuffix is the extension of the parquet files written by the parquet converter. +const parquetFileSuffix = ".parquet" + +// parquetFilteredBucket hides the files produced by the parquet converter from +// listings. The converter writes them under the same prefix as the TSDB block, +// and block.Download lists and downloads everything under that prefix, although +// compaction never reads them. Only listings are filtered: reads, uploads and +// deletes behave as usual. +type parquetFilteredBucket struct { + objstore.Bucket +} + +// newParquetFilteredBucket wraps the bucket used for compaction so that parquet +// files and the parquet converter markers are not listed. +func newParquetFilteredBucket(b objstore.InstrumentedBucket) objstore.InstrumentedBucket { + return &parquetFilteredBucket{Bucket: b} +} + +func isParquetObject(name string) bool { + if strings.HasSuffix(name, objstore.DirDelim) { + return false + } + base := path.Base(name) + return strings.HasSuffix(name, parquetFileSuffix) || + base == cortex_parquet.ConverterMarkerFileName || + base == cortex_parquet.NoConvertMarkerFileName +} + +func (b *parquetFilteredBucket) Iter(ctx context.Context, dir string, f func(name string) error, options ...objstore.IterOption) error { + return b.Bucket.Iter(ctx, dir, func(name string) error { + if isParquetObject(name) { + return nil + } + return f(name) + }, options...) +} + +func (b *parquetFilteredBucket) IterWithAttributes(ctx context.Context, dir string, f func(attrs objstore.IterObjectAttributes) error, options ...objstore.IterOption) error { + return b.Bucket.IterWithAttributes(ctx, dir, func(attrs objstore.IterObjectAttributes) error { + if isParquetObject(attrs.Name) { + return nil + } + return f(attrs) + }, options...) +} + +// WithExpectedErrs keeps the filtering when the underlying bucket is re-wrapped. +func (b *parquetFilteredBucket) WithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.Bucket { + if ib, ok := b.Bucket.(objstore.InstrumentedBucket); ok { + return &parquetFilteredBucket{Bucket: ib.WithExpectedErrs(fn)} + } + return b +} + +// ReaderWithExpectedErrs keeps the filtering when the underlying bucket is re-wrapped. +func (b *parquetFilteredBucket) ReaderWithExpectedErrs(fn objstore.IsOpFailureExpectedFunc) objstore.BucketReader { + return b.WithExpectedErrs(fn) +} diff --git a/pkg/compactor/parquet_filtered_bucket_test.go b/pkg/compactor/parquet_filtered_bucket_test.go new file mode 100644 index 0000000000..5771d28524 --- /dev/null +++ b/pkg/compactor/parquet_filtered_bucket_test.go @@ -0,0 +1,160 @@ +package compactor + +import ( + "bytes" + "context" + "path/filepath" + "testing" + + "github.com/go-kit/log" + "github.com/oklog/ulid/v2" + "github.com/stretchr/testify/require" + "github.com/thanos-io/objstore" + "github.com/thanos-io/thanos/pkg/block" + "github.com/thanos-io/thanos/pkg/block/metadata" + + cortex_parquet "github.com/cortexproject/cortex/pkg/storage/parquet" +) + +func uploadBlockWithParquetFiles(t *testing.T, bkt objstore.Bucket, id ulid.ULID, parquetFiles []string) { + t.Helper() + + var meta metadata.Meta + meta.ULID = id + meta.MinTime = 0 + meta.MaxTime = 1000 + meta.Version = metadata.TSDBVersion1 + meta.Thanos = metadata.Thanos{Version: metadata.ThanosVersion1, Source: metadata.TestSource} + var buf bytes.Buffer + require.NoError(t, meta.Write(&buf)) + + files := map[string][]byte{ + block.MetaFilename: buf.Bytes(), + block.IndexFilename: []byte("index"), + "chunks/000001": []byte("chunk"), + } + for _, name := range parquetFiles { + files[name] = []byte("parquet") + } + + for name, data := range files { + require.NoError(t, bkt.Upload(context.Background(), id.String()+"/"+name, bytes.NewReader(data))) + } +} + +func TestParquetFilteredBucket_Iter(t *testing.T) { + ctx := context.Background() + inmem := objstore.NewInMemBucket() + id := ulid.MustNew(1, nil) + parquetFiles := []string{ + "0.labels.parquet", + "0.chunks.parquet", + "1.labels.parquet", + "nested/2.chunks.parquet", + cortex_parquet.ConverterMarkerFileName, + cortex_parquet.NoConvertMarkerFileName, + } + uploadBlockWithParquetFiles(t, inmem, id, parquetFiles) + + filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem)) + + listRecursive := func(bkt objstore.BucketReader) []string { + var names []string + require.NoError(t, bkt.Iter(ctx, id.String(), func(name string) error { + names = append(names, name) + return nil + }, objstore.WithRecursiveIter())) + return names + } + + // Control: the unfiltered bucket lists the parquet files. + unfiltered := listRecursive(inmem) + for _, name := range parquetFiles { + require.Contains(t, unfiltered, id.String()+"/"+name) + } + + got := listRecursive(filtered) + for _, name := range parquetFiles { + require.NotContains(t, got, id.String()+"/"+name) + } + require.Contains(t, got, id.String()+"/"+block.MetaFilename) + require.Contains(t, got, id.String()+"/"+block.IndexFilename) + require.Contains(t, got, id.String()+"/chunks/000001") + + // Same behavior for IterWithAttributes. + var gotAttrs []string + require.NoError(t, filtered.IterWithAttributes(ctx, id.String(), func(attrs objstore.IterObjectAttributes) error { + gotAttrs = append(gotAttrs, attrs.Name) + return nil + }, objstore.WithRecursiveIter())) + for _, name := range parquetFiles { + require.NotContains(t, gotAttrs, id.String()+"/"+name) + } + require.Contains(t, gotAttrs, id.String()+"/"+block.IndexFilename) +} + +func TestParquetFilteredBucket_ReadsAreNotFiltered(t *testing.T) { + ctx := context.Background() + inmem := objstore.NewInMemBucket() + id := ulid.MustNew(1, nil) + parquetFiles := []string{ + "0.labels.parquet", + "0.chunks.parquet", + "1.labels.parquet", + "nested/2.chunks.parquet", + cortex_parquet.ConverterMarkerFileName, + cortex_parquet.NoConvertMarkerFileName, + } + uploadBlockWithParquetFiles(t, inmem, id, parquetFiles) + + filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem)) + + for _, name := range parquetFiles { + exists, err := filtered.Exists(ctx, id.String()+"/"+name) + require.NoError(t, err) + require.True(t, exists, name) + } + + // The filter survives re-wrapping for expected errors. + rewrapped := filtered.WithExpectedErrs(filtered.IsObjNotFoundErr) + var names []string + require.NoError(t, rewrapped.Iter(ctx, id.String(), func(name string) error { + names = append(names, name) + return nil + }, objstore.WithRecursiveIter())) + require.NotContains(t, names, id.String()+"/0.labels.parquet") + require.Contains(t, names, id.String()+"/"+block.IndexFilename) +} + +func TestParquetFilteredBucket_BlockDownloadSkipsParquetFiles(t *testing.T) { + ctx := context.Background() + inmem := objstore.NewInMemBucket() + id := ulid.MustNew(1, nil) + parquetFiles := []string{ + "0.labels.parquet", + "0.chunks.parquet", + "1.labels.parquet", + "nested/2.chunks.parquet", + cortex_parquet.ConverterMarkerFileName, + cortex_parquet.NoConvertMarkerFileName, + } + uploadBlockWithParquetFiles(t, inmem, id, parquetFiles) + + // Control: downloading from the unfiltered bucket pulls the parquet files. + controlDir := t.TempDir() + require.NoError(t, block.Download(ctx, log.NewNopLogger(), inmem, id, controlDir)) + require.FileExists(t, filepath.Join(controlDir, "0.labels.parquet")) + require.FileExists(t, filepath.Join(controlDir, cortex_parquet.ConverterMarkerFileName)) + + filtered := newParquetFilteredBucket(objstore.WithNoopInstr(inmem)) + dst := t.TempDir() + require.NoError(t, block.Download(ctx, log.NewNopLogger(), filtered, id, dst)) + + require.FileExists(t, filepath.Join(dst, block.MetaFilename)) + require.FileExists(t, filepath.Join(dst, block.IndexFilename)) + require.FileExists(t, filepath.Join(dst, block.ChunksDirname, "000001")) + + for _, name := range parquetFiles { + require.NoFileExists(t, filepath.Join(dst, name)) + } +}