Merge pull request #534 from grafana/label-values-with-matchers-is-bypassing-cache

`LabelValues()` with matchers should use cache
This commit is contained in:
Marco Pracucci 2023-09-28 10:47:45 +02:00 committed by GitHub
commit d2f7ecbaba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View file

@ -380,7 +380,7 @@ func inversePostingsForMatcher(ctx context.Context, ix IndexPostingsReader, m *l
const maxExpandedPostingsFactor = 100 // Division factor for maximum number of matched series.
func labelValuesWithMatchers(ctx context.Context, r IndexReader, name string, matchers ...*labels.Matcher) ([]string, error) {
p, err := PostingsForMatchers(ctx, r, matchers...)
p, err := r.PostingsForMatchers(ctx, false, matchers...)
if err != nil {
return nil, errors.Wrap(err, "fetching postings for matchers")
}

View file

@ -2914,9 +2914,25 @@ func TestLabelsValuesWithMatchersOptimization(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
values, err := labelValuesWithMatchers(ctx, ir, c.labelName, c.matchers...)
cir := &indexReaderCountingPostingsForMatchersCalls{IndexReader: ir}
values, err := labelValuesWithMatchers(ctx, cir, c.labelName, c.matchers...)
require.NoError(t, err)
require.ElementsMatch(t, c.expectedResults, values)
require.Equal(t, 1, cir.postingsForMatchersCalls,
"expected PostingsForMatchers to be called once. "+
"labelValuesWithMatchers should call the IndexReader.PostingsForMatchers instead of calling the package function PostingsForMatchers "+
"because IndexReader may use the cached version of the PostingsForMatchers",
)
})
}
}
type indexReaderCountingPostingsForMatchersCalls struct {
IndexReader
postingsForMatchersCalls int
}
func (f *indexReaderCountingPostingsForMatchersCalls) PostingsForMatchers(ctx context.Context, concurrent bool, ms ...*labels.Matcher) (index.Postings, error) {
f.postingsForMatchersCalls++
return f.IndexReader.PostingsForMatchers(ctx, concurrent, ms...)
}