From a7bedd16750176df8856ae22bc8e4bb1b89a466c Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Thu, 21 Mar 2024 09:47:51 +0000 Subject: [PATCH] fix lazy expanded postings cache and bug of non equal matcher with non existent values Signed-off-by: Ben Ye --- pkg/store/bucket.go | 10 ++++++---- pkg/store/lazy_postings.go | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index 20cf10e657d..4d5529c8ce7 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -1196,13 +1196,10 @@ OUTER: if err := b.ctx.Err(); err != nil { return err } - ok, err := b.indexr.LoadSeriesForTime(postingsBatch[i], &b.symbolizedLset, &b.chkMetas, b.skipChunks, b.mint, b.maxt) + hasMatchedChunks, err := b.indexr.LoadSeriesForTime(postingsBatch[i], &b.symbolizedLset, &b.chkMetas, b.skipChunks, b.mint, b.maxt) if err != nil { return errors.Wrap(err, "read series") } - if !ok { - continue - } if err := b.indexr.LookupLabelsSymbols(b.ctx, b.symbolizedLset, b.b); err != nil { return errors.Wrap(err, "Lookup labels symbols") @@ -1221,6 +1218,11 @@ OUTER: if b.lazyPostings.lazyExpanded() { b.expandedPostings = append(b.expandedPostings, postingsBatch[i]) } + // Even though there is no chunks found in the requested time range, we need to continue + // for loop after checking lazy posting matchers because of the expanded postings cache. + if !hasMatchedChunks { + continue + } completeLabelset := labelpb.ExtendSortedLabels(b.lset, b.extLset) if b.extLsetToRemove != nil { diff --git a/pkg/store/lazy_postings.go b/pkg/store/lazy_postings.go index cb245180cf2..0f4038bc61d 100644 --- a/pkg/store/lazy_postings.go +++ b/pkg/store/lazy_postings.go @@ -54,10 +54,15 @@ func optimizePostingsFetchByDownloadedBytes(r *bucketIndexReader, postingGroups return nil, false, errors.Wrapf(err, "postings offsets for %s", pg.name) } - // No posting ranges found means empty posting. - if len(rngs) == 0 { + // 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