From ae02ffd5aeff159896c85478e89202ef0b1ed204 Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Tue, 27 Dec 2022 16:42:05 +0100 Subject: [PATCH 1/3] Add more benchmark cases Signed-off-by: Oleg Zaytsev --- tsdb/querier_bench_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tsdb/querier_bench_test.go b/tsdb/querier_bench_test.go index 1ef9ab6317..3e076aa4d8 100644 --- a/tsdb/querier_bench_test.go +++ b/tsdb/querier_bench_test.go @@ -93,6 +93,7 @@ func BenchmarkQuerier(b *testing.B) { func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { n1 := labels.MustNewMatcher(labels.MatchEqual, "n", "1"+postingsBenchSuffix) + nX := labels.MustNewMatcher(labels.MatchNotEqual, "n", "X"+postingsBenchSuffix) jFoo := labels.MustNewMatcher(labels.MatchEqual, "j", "foo") jNotFoo := labels.MustNewMatcher(labels.MatchNotEqual, "j", "foo") @@ -109,35 +110,50 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { iNot2Star := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^2.*$") iNotStar2Star := labels.MustNewMatcher(labels.MatchNotRegexp, "i", "^.*2.*$") jFooBar := labels.MustNewMatcher(labels.MatchRegexp, "j", "foo|bar") + jXXXYYY := labels.MustNewMatcher(labels.MatchRegexp, "j", "XXX|YYY") + jXplus := labels.MustNewMatcher(labels.MatchRegexp, "j", "X.+") iCharSet := labels.MustNewMatcher(labels.MatchRegexp, "i", "1[0-9]") iAlternate := labels.MustNewMatcher(labels.MatchRegexp, "i", "(1|2|3|4|5|6|20|55)") + iXYZ := labels.MustNewMatcher(labels.MatchRegexp, "i", "X|Y|Z") cases := []struct { name string matchers []*labels.Matcher }{ {`n="1"`, []*labels.Matcher{n1}}, + {`n="X"`, []*labels.Matcher{nX}}, {`n="1",j="foo"`, []*labels.Matcher{n1, jFoo}}, + {`n="X",j="foo"`, []*labels.Matcher{nX, jFoo}}, {`j="foo",n="1"`, []*labels.Matcher{jFoo, n1}}, {`n="1",j!="foo"`, []*labels.Matcher{n1, jNotFoo}}, + {`n="X",j!="foo"`, []*labels.Matcher{nX, jNotFoo}}, {`i=~"1[0-9]",j=~"foo|bar"`, []*labels.Matcher{iCharSet, jFooBar}}, {`j=~"foo|bar"`, []*labels.Matcher{jFooBar}}, + {`j=~"XXX|YYY"`, []*labels.Matcher{jXXXYYY}}, + {`j=~"X.+"`, []*labels.Matcher{jXplus}}, {`i=~"(1|2|3|4|5|6|20|55)"`, []*labels.Matcher{iAlternate}}, + {`i=~"X|Y|Z"`, []*labels.Matcher{iXYZ}}, {`i=~".*"`, []*labels.Matcher{iStar}}, {`i=~"1.*"`, []*labels.Matcher{i1Star}}, {`i=~".*1"`, []*labels.Matcher{iStar1}}, {`i=~".+"`, []*labels.Matcher{iPlus}}, + {`i=~".+",j=~"X.+"`, []*labels.Matcher{iPlus, jXplus}}, {`i=~""`, []*labels.Matcher{iEmptyRe}}, {`i!=""`, []*labels.Matcher{iNotEmpty}}, {`n="1",i=~".*",j="foo"`, []*labels.Matcher{n1, iStar, jFoo}}, + {`n="X",i=~".*",j="foo"`, []*labels.Matcher{nX, iStar, jFoo}}, {`n="1",i=~".*",i!="2",j="foo"`, []*labels.Matcher{n1, iStar, iNot2, jFoo}}, {`n="1",i!=""`, []*labels.Matcher{n1, iNotEmpty}}, {`n="1",i!="",j="foo"`, []*labels.Matcher{n1, iNotEmpty, jFoo}}, + {`n="1",i!="",j=~"X.+"`, []*labels.Matcher{n1, iNotEmpty, jXplus}}, + {`n="1",i!="",j=~"XXX|YYY"`, []*labels.Matcher{n1, iNotEmpty, jXXXYYY}}, + {`n="1",i=~"X|Y|Z",j="foo"`, []*labels.Matcher{n1, iXYZ, jFoo}}, {`n="1",i=~".+",j="foo"`, []*labels.Matcher{n1, iPlus, jFoo}}, {`n="1",i=~"1.+",j="foo"`, []*labels.Matcher{n1, i1Plus, jFoo}}, {`n="1",i=~".*1.*",j="foo"`, []*labels.Matcher{n1, iStar1Star, jFoo}}, {`n="1",i=~".+",i!="2",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2, jFoo}}, {`n="1",i=~".+",i!~"2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNot2Star, jFoo}}, {`n="1",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{n1, iPlus, iNotStar2Star, jFoo}}, + {`n="X",i=~".+",i!~".*2.*",j="foo"`, []*labels.Matcher{nX, iPlus, iNotStar2Star, jFoo}}, } for _, c := range cases { @@ -154,7 +170,10 @@ func benchmarkLabelValuesWithMatchers(b *testing.B, ir IndexReader) { i1 := labels.MustNewMatcher(labels.MatchEqual, "i", "1") iStar := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.*$") jNotFoo := labels.MustNewMatcher(labels.MatchNotEqual, "j", "foo") + jXXXYYY := labels.MustNewMatcher(labels.MatchRegexp, "j", "XXX|YYY") + jXplus := labels.MustNewMatcher(labels.MatchRegexp, "j", "X.+") n1 := labels.MustNewMatcher(labels.MatchEqual, "n", "1"+postingsBenchSuffix) + nX := labels.MustNewMatcher(labels.MatchNotEqual, "n", "X"+postingsBenchSuffix) nPlus := labels.MustNewMatcher(labels.MatchRegexp, "i", "^.+$") cases := []struct { @@ -166,6 +185,9 @@ func benchmarkLabelValuesWithMatchers(b *testing.B, ir IndexReader) { {`i with n="1"`, "i", []*labels.Matcher{n1}}, {`i with n="^.+$"`, "i", []*labels.Matcher{nPlus}}, {`i with n="1",j!="foo"`, "i", []*labels.Matcher{n1, jNotFoo}}, + {`i with n="1",j=~"X.+"`, "i", []*labels.Matcher{n1, jXplus}}, + {`i with n="1",j=~"XXX|YYY"`, "i", []*labels.Matcher{n1, jXXXYYY}}, + {`i with n="X",j!="foo"`, "i", []*labels.Matcher{nX, jNotFoo}}, {`i with n="1",i=~"^.*$",j!="foo"`, "i", []*labels.Matcher{n1, iStar, jNotFoo}}, // n has 10 values. {`n with j!="foo"`, "n", []*labels.Matcher{jNotFoo}}, From f3d001df2ed018a7cd40a1f206a8bef57e3dd98d Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Tue, 27 Dec 2022 18:13:56 +0100 Subject: [PATCH 2/3] Add shortcuts for empty postings Signed-off-by: Oleg Zaytsev --- tsdb/head_read.go | 4 +++- tsdb/index/postings.go | 7 +++++++ tsdb/querier.go | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tsdb/head_read.go b/tsdb/head_read.go index a963daa930..03e8d9ac58 100644 --- a/tsdb/head_read.go +++ b/tsdb/head_read.go @@ -113,7 +113,9 @@ func (h *headIndexReader) Postings(name string, values ...string) (index.Posting default: res := make([]index.Postings, 0, len(values)) for _, value := range values { - res = append(res, h.head.postings.Get(name, value)) + if p := h.head.postings.Get(name, value); !index.IsEmptyPostings(p) { + res = append(res, p) + } } return index.Merge(res...), nil } diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index fc78cc7a63..2e6781e117 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -428,6 +428,13 @@ func EmptyPostings() Postings { return emptyPostings } +// IsEmptyPostings returns true if the postings are an empty postings list. +// When this function returns false, it doesn't mean that the postings isn't empty +// (it could be an empty intersection of two non-empty postings, for example). +func IsEmptyPostings(p Postings) bool { + return p == emptyPostings +} + // ErrPostings returns new postings that immediately error. func ErrPostings(err error) Postings { return errPostings{err} diff --git a/tsdb/querier.go b/tsdb/querier.go index ed4c40e6b8..6df4f2c9fb 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -215,6 +215,9 @@ func PostingsForMatchers(ix IndexPostingsReader, ms ...*labels.Matcher) (index.P if err != nil { return nil, err } + if index.IsEmptyPostings(it) { + return index.EmptyPostings(), nil + } its = append(its, it) } else { // l="a" // Non-Not matcher, use normal postingsForMatcher. @@ -222,6 +225,9 @@ func PostingsForMatchers(ix IndexPostingsReader, ms ...*labels.Matcher) (index.P if err != nil { return nil, err } + if index.IsEmptyPostings(it) { + return index.EmptyPostings(), nil + } its = append(its, it) } } else { // l="" From e246ce436d69cd48334b0e85d25a8225e8110e45 Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Thu, 29 Dec 2022 13:43:42 +0100 Subject: [PATCH 3/3] s/IsEmptyPostings/IsEmptyPostingsType/ Signed-off-by: Oleg Zaytsev --- tsdb/head_read.go | 2 +- tsdb/index/postings.go | 4 ++-- tsdb/querier.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tsdb/head_read.go b/tsdb/head_read.go index 03e8d9ac58..a30507fbfb 100644 --- a/tsdb/head_read.go +++ b/tsdb/head_read.go @@ -113,7 +113,7 @@ func (h *headIndexReader) Postings(name string, values ...string) (index.Posting default: res := make([]index.Postings, 0, len(values)) for _, value := range values { - if p := h.head.postings.Get(name, value); !index.IsEmptyPostings(p) { + if p := h.head.postings.Get(name, value); !index.IsEmptyPostingsType(p) { res = append(res, p) } } diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index 2e6781e117..869ee636de 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -428,10 +428,10 @@ func EmptyPostings() Postings { return emptyPostings } -// IsEmptyPostings returns true if the postings are an empty postings list. +// IsEmptyPostingsType returns true if the postings are an empty postings list. // When this function returns false, it doesn't mean that the postings isn't empty // (it could be an empty intersection of two non-empty postings, for example). -func IsEmptyPostings(p Postings) bool { +func IsEmptyPostingsType(p Postings) bool { return p == emptyPostings } diff --git a/tsdb/querier.go b/tsdb/querier.go index 6df4f2c9fb..bafc7f46d4 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -215,7 +215,7 @@ func PostingsForMatchers(ix IndexPostingsReader, ms ...*labels.Matcher) (index.P if err != nil { return nil, err } - if index.IsEmptyPostings(it) { + if index.IsEmptyPostingsType(it) { return index.EmptyPostings(), nil } its = append(its, it) @@ -225,7 +225,7 @@ func PostingsForMatchers(ix IndexPostingsReader, ms ...*labels.Matcher) (index.P if err != nil { return nil, err } - if index.IsEmptyPostings(it) { + if index.IsEmptyPostingsType(it) { return index.EmptyPostings(), nil } its = append(its, it)