Skip to content

Commit

Permalink
fix postings offset reader and npe
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 committed Jun 23, 2023
1 parent 264fa16 commit f3eb423
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/block/indexheader/binary_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ func (r *BinaryReader) postingsOffset(name string, values ...string) ([]index.Ra
}

if valueIndex != len(values) && wantedValue <= e.offsets[i+1].value {
// Increment i when wanted value is same as next offset.
if wantedValue == e.offsets[i+1].value {
i++
}
// wantedValue is smaller or same as the next offset we know about, let's iterate further to add those.
continue
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/store/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ func (b *blockSeriesClient) ExpandPostings(
return errors.Wrap(err, "expanded matching posting")
}

b.lazyPostings = ps
if len(ps.postings) == 0 {
return nil
}
Expand All @@ -995,7 +996,6 @@ func (b *blockSeriesClient) ExpandPostings(
return httpgrpc.Errorf(int(codes.ResourceExhausted), "exceeded series limit: %s", err)
}

b.lazyPostings = ps
if b.batchSize > len(ps.postings) {
b.batchSize = len(ps.postings)
}
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func (b *blockSeriesClient) nextBatch() error {
if b.lazyPostings.lazyExpanded() {
v, err := b.indexr.IndexVersion()
if err != nil {
errors.Wrap(err, "get index version")
return errors.Wrap(err, "get index version")
}
if v >= 2 {
for i := range b.expandedPostings {
Expand Down Expand Up @@ -2264,7 +2264,7 @@ func (r *bucketIndexReader) ExpandedPostings(ctx context.Context, ms []*labels.M
// Shortcut the case of `len(postingGroups) == 0`. It will only happen when no
// matchers specified, and we don't need to fetch expanded postings from cache.
if len(ms) == 0 {
return nil, nil
return emptyLazyPostings, nil
}

hit, postings, err := r.fetchExpandedPostingsFromCache(ctx, ms, bytesLimiter)
Expand Down Expand Up @@ -2297,7 +2297,7 @@ func (r *bucketIndexReader) ExpandedPostings(ctx context.Context, ms []*labels.M
// E.g. label="non-existing-value" returns empty group.
if !pg.addAll && len(pg.addKeys) == 0 {
r.storeExpandedPostingsToCache(ms, index.EmptyPostings(), 0)
return nil, nil
return emptyLazyPostings, nil
}

allRequested = allRequested || pg.addAll
Expand Down
5 changes: 5 additions & 0 deletions pkg/store/lazy_postings.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package store

import (
Expand All @@ -10,6 +13,8 @@ import (
"github.com/prometheus/prometheus/tsdb/index"
)

var emptyLazyPostings = &lazyExpandedPostings{postings: nil, matchers: nil}

type lazyExpandedPostings struct {
postings []storage.SeriesRef
matchers []*labels.Matcher
Expand Down

0 comments on commit f3eb423

Please sign in to comment.