Skip to content

Commit

Permalink
Optimize empty posting check in lazy posting (thanos-io#7298)
Browse files Browse the repository at this point in the history
* change lazy postings empty posting check to use cardinality

Signed-off-by: Ben Ye <[email protected]>

* update lazy posting test

Signed-off-by: Ben Ye <[email protected]>

---------

Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 authored and jnyi committed Jun 1, 2024
1 parent 2d3dcf6 commit 1eaffc7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
14 changes: 5 additions & 9 deletions pkg/store/lazy_postings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions pkg/store/lazy_postings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 1eaffc7

Please sign in to comment.