From 1eaffc7eec149557e41ccbf8b52897b446cb5f41 Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Tue, 23 Apr 2024 11:34:30 -0700 Subject: [PATCH] Optimize empty posting check in lazy posting (#7298) * change lazy postings empty posting check to use cardinality Signed-off-by: Ben Ye * update lazy posting test Signed-off-by: Ben Ye --------- Signed-off-by: Ben Ye --- pkg/store/lazy_postings.go | 14 +++++--------- pkg/store/lazy_postings_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pkg/store/lazy_postings.go b/pkg/store/lazy_postings.go index 0f4038bc61d..9469be9b472 100644 --- a/pkg/store/lazy_postings.go +++ b/pkg/store/lazy_postings.go @@ -54,15 +54,6 @@ func optimizePostingsFetchByDownloadedBytes(r *bucketIndexReader, postingGroups return nil, false, errors.Wrapf(err, "postings offsets for %s", pg.name) } - // If the posting group adds keys, no posting ranges found means empty posting. - if len(pg.addKeys) > 0 && len(rngs) == 0 { - return nil, true, nil - } - // If the posting group removes keys, no posting ranges found is fine. It means - // that the posting group is a noop. {job != "some_non_existent_value"} - if len(pg.removeKeys) > 0 && len(rngs) == 0 { - continue - } for _, r := range rngs { if r == indexheader.NotFoundRange { continue @@ -72,6 +63,11 @@ func optimizePostingsFetchByDownloadedBytes(r *bucketIndexReader, postingGroups // https://github.com/prometheus/prometheus/blob/v2.46.0/tsdb/docs/format/index.md. pg.cardinality += (r.End - r.Start - 4) / 4 } + // If the posting group adds keys, 0 cardinality means the posting doesn't exist. + // If the posting group removes keys, no posting ranges found is fine as it is a noop. + if len(pg.addKeys) > 0 && pg.cardinality == 0 { + return nil, true, nil + } } slices.SortFunc(postingGroups, func(a, b *postingGroup) int { if a.cardinality == b.cardinality { diff --git a/pkg/store/lazy_postings_test.go b/pkg/store/lazy_postings_test.go index 53f96502344..08e30b6d5ba 100644 --- a/pkg/store/lazy_postings_test.go +++ b/pkg/store/lazy_postings_test.go @@ -352,6 +352,21 @@ func TestOptimizePostingsFetchByDownloadedBytes(t *testing.T) { {name: "foo", addKeys: []string{"bar"}, cardinality: 1}, }, }, + { + name: "posting group label with add keys exist but no matching value, expect empty posting", + inputPostings: map[string]map[string]index.Range{ + "foo": {"bar": index.Range{End: 8}}, + "bar": {"baz": index.Range{Start: 8, End: 16}}, + }, + seriesMaxSize: 1000, + seriesMatchRatio: 0.5, + postingGroups: []*postingGroup{ + {name: "foo", addKeys: []string{"bar"}}, + {name: "bar", addKeys: []string{"foo"}}, + }, + expectedPostingGroups: nil, + expectedEmptyPosting: true, + }, { name: "posting group label with remove keys exist but no matching value, noop", inputPostings: map[string]map[string]index.Range{