From 17963226417effda5b6363928cd2c83323f75062 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Sat, 27 Jan 2024 17:08:03 +0100 Subject: [PATCH] Stores: convert tests to not rely on slice labels Signed-off-by: Michael Hoffmann --- pkg/store/acceptance_test.go | 2 +- pkg/store/bucket_test.go | 36 ++++++++++++++++++------------------ pkg/store/proxy_heap_test.go | 8 ++++---- pkg/store/proxy_test.go | 10 +++++----- pkg/store/tsdb_test.go | 6 +++--- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pkg/store/acceptance_test.go b/pkg/store/acceptance_test.go index 113a4a87f5..be1a1179f1 100644 --- a/pkg/store/acceptance_test.go +++ b/pkg/store/acceptance_test.go @@ -1011,7 +1011,7 @@ func TestProxyStore_Acceptance(t *testing.T) { storetestutil.TestClient{StoreClient: storepb.ServerAsClient(p2, 0)}, } - return NewProxyStore(nil, nil, func() []Client { return clients }, component.Query, nil, 0*time.Second, RetrievalStrategy(EagerRetrieval)) + return NewProxyStore(nil, nil, func() []Client { return clients }, component.Query, labels.EmptyLabels(), 0*time.Second, RetrievalStrategy(EagerRetrieval)) } testStoreAPIsAcceptance(t, startStore) diff --git a/pkg/store/bucket_test.go b/pkg/store/bucket_test.go index b8306fa110..d38e14587d 100644 --- a/pkg/store/bucket_test.go +++ b/pkg/store/bucket_test.go @@ -709,19 +709,19 @@ func TestBucketStore_Sharding(t *testing.T) { bkt := objstore.NewInMemBucket() series := []labels.Labels{labels.FromStrings("a", "1", "b", "1")} - id1, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.Labels{{Name: "cluster", Value: "a"}, {Name: "region", Value: "r1"}}, 0, metadata.NoneFunc) + id1, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.FromStrings("cluster", "a", "region", "r1"), 0, metadata.NoneFunc) testutil.Ok(t, err) testutil.Ok(t, block.Upload(ctx, logger, bkt, filepath.Join(dir, id1.String()), metadata.NoneFunc)) - id2, err := e2eutil.CreateBlock(ctx, dir, series, 10, 1000, 2000, labels.Labels{{Name: "cluster", Value: "a"}, {Name: "region", Value: "r1"}}, 0, metadata.NoneFunc) + id2, err := e2eutil.CreateBlock(ctx, dir, series, 10, 1000, 2000, labels.FromStrings("cluster", "a", "region", "r1"), 0, metadata.NoneFunc) testutil.Ok(t, err) testutil.Ok(t, block.Upload(ctx, logger, bkt, filepath.Join(dir, id2.String()), metadata.NoneFunc)) - id3, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.Labels{{Name: "cluster", Value: "b"}, {Name: "region", Value: "r1"}}, 0, metadata.NoneFunc) + id3, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.FromStrings("cluster", "b", "region", "r1"), 0, metadata.NoneFunc) testutil.Ok(t, err) testutil.Ok(t, block.Upload(ctx, logger, bkt, filepath.Join(dir, id3.String()), metadata.NoneFunc)) - id4, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.Labels{{Name: "cluster", Value: "a"}, {Name: "region", Value: "r2"}}, 0, metadata.NoneFunc) + id4, err := e2eutil.CreateBlock(ctx, dir, series, 10, 0, 1000, labels.FromStrings("cluster", "a", "region", "r2"), 0, metadata.NoneFunc) testutil.Ok(t, err) testutil.Ok(t, block.Upload(ctx, logger, bkt, filepath.Join(dir, id4.String()), metadata.NoneFunc)) @@ -1116,7 +1116,7 @@ func uploadTestBlock(t testing.TB, tmpDir string, bkt objstore.Bucket, series in testutil.Ok(t, err) _, err = metadata.InjectThanos(log.NewNopLogger(), filepath.Join(tmpDir, "tmp", id.String()), metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, IndexStats: metadata.IndexStats{SeriesMaxSize: stats.SeriesMaxSize, ChunkMaxSize: stats.ChunkMaxSize}, @@ -1383,7 +1383,7 @@ func benchBucketSeries(t testutil.TB, sampleType chunkenc.ValueType, skipChunk, random = rand.New(rand.NewSource(120)) ) - extLset := labels.Labels{{Name: "ext1", Value: "1"}} + extLset := labels.FromStrings("ext1", "1") blockDir := filepath.Join(tmpDir, "tmp") samplesPerSeriesPerBlock := samplesPerSeries / numOfBlocks @@ -1582,7 +1582,7 @@ func TestBucketSeries_OneBlock_InMemIndexCacheSegfault(t *testing.T) { logger := log.NewLogfmtLogger(os.Stderr) thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -1696,7 +1696,7 @@ func TestBucketSeries_OneBlock_InMemIndexCacheSegfault(t *testing.T) { indexReaderPool: indexheader.NewReaderPool(log.NewNopLogger(), false, 0, indexheader.NewReaderPoolMetrics(nil), indexheader.AlwaysEagerDownloadIndexHeader), metrics: newBucketStoreMetrics(nil), blockSets: map[uint64]*bucketBlockSet{ - labels.Labels{{Name: "ext1", Value: "1"}}.Hash(): {blocks: [][]*bucketBlock{{b1, b2}}}, + labels.FromStrings("ext1", "1").Hash(): {blocks: [][]*bucketBlock{{b1, b2}}}, }, blocks: map[ulid.ULID]*bucketBlock{ b1.meta.ULID: b1, @@ -1964,7 +1964,7 @@ func TestSeries_BlockWithMultipleChunks(t *testing.T) { blk := createBlockFromHead(t, headOpts.ChunkDirRoot, h) thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -2215,7 +2215,7 @@ func uploadSeriesToBucket(t *testing.T, bkt *filesystem.Bucket, replica string, blk := storetestutil.CreateBlockFromHead(t, headOpts.ChunkDirRoot, h) thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: replica}}.Map(), + Labels: labels.FromStrings("ext1", replica).Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -2269,7 +2269,7 @@ func createBlockWithOneSeriesWithStep(t testutil.TB, dir string, lbls labels.Lab ref, err := app.Append(0, lbls, ts, random.Float64()) testutil.Ok(t, err) for i := 1; i < totalSamples; i++ { - _, err := app.Append(ref, nil, ts+step*int64(i), random.Float64()) + _, err := app.Append(ref, labels.EmptyLabels(), ts+step*int64(i), random.Float64()) testutil.Ok(t, err) } testutil.Ok(t, app.Commit()) @@ -2295,7 +2295,7 @@ func setupStoreForHintsTest(t *testing.T) (testutil.TB, *BucketStore, []*storepb random = rand.New(rand.NewSource(120)) ) - extLset := labels.Labels{{Name: "ext1", Value: "1"}} + extLset := labels.FromStrings("ext1", "1") // Inject the Thanos meta to each block in the storage. thanosMeta := metadata.Thanos{ Labels: extLset.Map(), @@ -2527,7 +2527,7 @@ func TestSeries_ChunksHaveHashRepresentation(t *testing.T) { blk := createBlockFromHead(t, headOpts.ChunkDirRoot, h) thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -2657,7 +2657,7 @@ func BenchmarkBucketBlock_readChunkRange(b *testing.B) { // Upload the block to the bucket. thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -2719,7 +2719,7 @@ func prepareBucket(b *testing.B, resolutionLevel compact.ResolutionLevel) (*buck SamplesPerSeries: 86400 / 15, // Simulate 1 day block with 15s scrape interval. ScrapeInterval: 15 * time.Second, Series: 1000, - PrependLabels: nil, + PrependLabels: labels.EmptyLabels(), Random: rand.New(rand.NewSource(120)), SkipChunks: true, }) @@ -2727,7 +2727,7 @@ func prepareBucket(b *testing.B, resolutionLevel compact.ResolutionLevel) (*buck // Upload the block to the bucket. thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: "1"}}.Map(), + Labels: labels.FromStrings("ext1", "1").Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } @@ -3363,7 +3363,7 @@ func TestExpandedPostingsRace(t *testing.T) { SamplesPerSeries: 10, ScrapeInterval: 15 * time.Second, Series: 1000, - PrependLabels: nil, + PrependLabels: labels.EmptyLabels(), Random: rand.New(rand.NewSource(120)), SkipChunks: true, }) @@ -3376,7 +3376,7 @@ func TestExpandedPostingsRace(t *testing.T) { // Upload the block to the bucket. thanosMeta := metadata.Thanos{ - Labels: labels.Labels{{Name: "ext1", Value: fmt.Sprintf("%d", i)}}.Map(), + Labels: labels.FromStrings("ext1", fmt.Sprintf("%d", i)).Map(), Downsample: metadata.ThanosDownsample{Resolution: 0}, Source: metadata.TestSource, } diff --git a/pkg/store/proxy_heap_test.go b/pkg/store/proxy_heap_test.go index 50fe2d46be..edcd37fcfa 100644 --- a/pkg/store/proxy_heap_test.go +++ b/pkg/store/proxy_heap_test.go @@ -314,12 +314,12 @@ func labelsFromStrings(ss ...string) labels.Labels { if len(ss)%2 != 0 { panic("invalid number of strings") } - res := make(labels.Labels, 0, len(ss)/2) + + b := labels.NewScratchBuilder(len(ss) / 2) for i := 0; i < len(ss); i += 2 { - res = append(res, labels.Label{Name: ss[i], Value: ss[i+1]}) + b.Add(ss[i], ss[i+1]) } - - return res + return b.Labels() } func BenchmarkSortWithoutLabels(b *testing.B) { diff --git a/pkg/store/proxy_test.go b/pkg/store/proxy_test.go index 25f3e84102..ad1f65f988 100644 --- a/pkg/store/proxy_test.go +++ b/pkg/store/proxy_test.go @@ -67,7 +67,7 @@ func TestProxyStore_Info(t *testing.T) { nil, func() []Client { return nil }, component.Query, - nil, 0*time.Second, RetrievalStrategy(EagerRetrieval), + labels.EmptyLabels(), 0*time.Second, RetrievalStrategy(EagerRetrieval), ) resp, err := q.Info(ctx, &storepb.InfoRequest{}) @@ -96,7 +96,7 @@ func TestProxyStore_TSDBInfos(t *testing.T) { } q := NewProxyStore(nil, nil, func() []Client { return stores }, - component.Query, nil, 0*time.Second, EagerRetrieval, + component.Query, labels.EmptyLabels(), 0*time.Second, EagerRetrieval, ) expected := []infopb.TSDBInfo{ @@ -1227,7 +1227,7 @@ func TestProxyStore_Series_RequestParamsProxied(t *testing.T) { nil, func() []Client { return cls }, component.Query, - nil, + labels.EmptyLabels(), 1*time.Second, EagerRetrieval, ) @@ -1335,7 +1335,7 @@ func TestProxyStore_LabelValues(t *testing.T) { nil, func() []Client { return cls }, component.Query, - nil, + labels.EmptyLabels(), 0*time.Second, EagerRetrieval, ) @@ -1535,7 +1535,7 @@ func TestProxyStore_LabelNames(t *testing.T) { nil, func() []Client { return tc.storeAPIs }, component.Query, - nil, + labels.EmptyLabels(), 5*time.Second, EagerRetrieval, ) diff --git a/pkg/store/tsdb_test.go b/pkg/store/tsdb_test.go index a7066dca3b..61b4875bfc 100644 --- a/pkg/store/tsdb_test.go +++ b/pkg/store/tsdb_test.go @@ -597,7 +597,7 @@ func benchTSDBStoreSeries(t testutil.TB, totalSamples, totalSeries int) { // Add external labels & frame it. s := r.GetSeries() bytesLeftForChunks := store.maxBytesPerFrame - lbls := make([]labelpb.ZLabel, 0, len(s.Labels)+len(extLabels)) + lbls := make([]labelpb.ZLabel, 0, len(s.Labels)+extLabels.Len()) for _, l := range s.Labels { lbls = append(lbls, labelpb.ZLabel{ Name: l.Name, @@ -605,13 +605,13 @@ func benchTSDBStoreSeries(t testutil.TB, totalSamples, totalSeries int) { }) bytesLeftForChunks -= lbls[len(lbls)-1].Size() } - for _, l := range extLabels { + extLabels.Range(func(l labels.Label) { lbls = append(lbls, labelpb.ZLabel{ Name: l.Name, Value: l.Value, }) bytesLeftForChunks -= lbls[len(lbls)-1].Size() - } + }) sort.Slice(lbls, func(i, j int) bool { return lbls[i].Name < lbls[j].Name })