Skip to content

Commit 274f95e

Browse files
authored
store: label_values: fetch less postings (#7814)
* label_values: fetch less postings Signed-off-by: Vasiliy Rumyantsev <[email protected]> * CHANGELOG.md Signed-off-by: Vasiliy Rumyantsev <[email protected]> * added acceptance test Signed-off-by: Vasiliy Rumyantsev <[email protected]> * removed redundant comment Signed-off-by: Vasiliy Rumyantsev <[email protected]> * check if matcher is EQ matcher Signed-off-by: Vasiliy Rumyantsev <[email protected]> * Update CHANGELOG.md Signed-off-by: Vasiliy Rumyantsev <[email protected]> --------- Signed-off-by: Vasiliy Rumyantsev <[email protected]>
1 parent fe51bd6 commit 274f95e

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
1919
- [#7658](https://github.com/thanos-io/thanos/pull/7658) Store: Fix panic because too small buffer in pool.
2020
- [#7643](https://github.com/thanos-io/thanos/pull/7643) Receive: fix thanos_receive_write_{timeseries,samples} stats
2121
- [#7644](https://github.com/thanos-io/thanos/pull/7644) fix(ui): add null check to find overlapping blocks logic
22+
- [#7814](https://github.com/thanos-io/thanos/pull/7814) Store: label_values: if matchers contain __name__=="something", do not add <labelName> != "" to fetch less postings.
2223
- [#7679](https://github.com/thanos-io/thanos/pull/7679) Query: respect store.limit.* flags when evaluating queries
2324
- [#7821](https://github.com/thanos-io/thanos/pull/7679) Query/Receive: Fix coroutine leak introduced in https://github.com/thanos-io/thanos/pull/7796.
2425

pkg/store/acceptance_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,46 @@ func testStoreAPIsAcceptance(t *testing.T, startStore startStoreFn) {
740740
},
741741
},
742742
},
743+
{
744+
desc: "label_values(kube_pod_info{}, pod) don't fetch postings for pod!=''",
745+
appendFn: func(app storage.Appender) {
746+
_, err := app.Append(0, labels.FromStrings("__name__", "up", "pod", "pod-1"), timestamp.FromTime(now), 1)
747+
testutil.Ok(t, err)
748+
_, err = app.Append(0, labels.FromStrings("__name__", "up", "pod", "pod-2"), timestamp.FromTime(now), 1)
749+
testutil.Ok(t, err)
750+
_, err = app.Append(0, labels.FromStrings("__name__", "kube_pod_info", "pod", "pod-1"), timestamp.FromTime(now), 1)
751+
testutil.Ok(t, err)
752+
testutil.Ok(t, app.Commit())
753+
},
754+
labelNameCalls: []labelNameCallCase{
755+
{
756+
start: timestamp.FromTime(minTime),
757+
end: timestamp.FromTime(maxTime),
758+
expectedNames: []string{"__name__", "pod", "region"},
759+
matchers: []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "__name__", Value: "kube_pod_info"}},
760+
},
761+
{
762+
start: timestamp.FromTime(minTime),
763+
end: timestamp.FromTime(maxTime),
764+
expectedNames: []string{"__name__", "pod", "region"},
765+
},
766+
},
767+
labelValuesCalls: []labelValuesCallCase{
768+
{
769+
start: timestamp.FromTime(minTime),
770+
end: timestamp.FromTime(maxTime),
771+
label: "pod",
772+
expectedValues: []string{"pod-1"},
773+
matchers: []storepb.LabelMatcher{{Type: storepb.LabelMatcher_EQ, Name: "__name__", Value: "kube_pod_info"}},
774+
},
775+
{
776+
start: timestamp.FromTime(minTime),
777+
end: timestamp.FromTime(maxTime),
778+
label: "pod",
779+
expectedValues: []string{"pod-1", "pod-2"},
780+
},
781+
},
782+
},
743783
} {
744784
t.Run(tc.desc, func(t *testing.T) {
745785
appendFn := tc.appendFn

pkg/store/bucket.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,14 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
19901990

19911991
resHints := &hintspb.LabelValuesResponseHints{}
19921992

1993+
var hasMetricNameEqMatcher = false
1994+
for _, m := range reqSeriesMatchers {
1995+
if m.Name == labels.MetricName && m.Type == labels.MatchEqual {
1996+
hasMetricNameEqMatcher = true
1997+
break
1998+
}
1999+
}
2000+
19932001
g, gctx := errgroup.WithContext(ctx)
19942002

19952003
var reqBlockMatchers []*labels.Matcher
@@ -2013,6 +2021,7 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
20132021
var seriesLimiter = s.seriesLimiterFactory(s.metrics.queriesDropped.WithLabelValues("series", tenant))
20142022
var bytesLimiter = s.bytesLimiterFactory(s.metrics.queriesDropped.WithLabelValues("bytes", tenant))
20152023
var logger = s.requestLoggerFunc(ctx, s.logger)
2024+
var stats = &queryStats{}
20162025

20172026
for _, b := range s.blocks {
20182027
b := b
@@ -2031,7 +2040,8 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
20312040

20322041
// If we have series matchers and the Label is not an external one, add <labelName> != "" matcher
20332042
// to only select series that have given label name.
2034-
if len(reqSeriesMatchersNoExtLabels) > 0 && !b.extLset.Has(req.Label) {
2043+
// We don't need such matcher if matchers already contain __name__=="something" matcher.
2044+
if !hasMetricNameEqMatcher && len(reqSeriesMatchersNoExtLabels) > 0 && !b.extLset.Has(req.Label) {
20352045
m, err := labels.NewMatcher(labels.MatchNotEqual, req.Label, "")
20362046
if err != nil {
20372047
return nil, status.Error(codes.InvalidArgument, err.Error())
@@ -2099,7 +2109,12 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
20992109
s.metrics.lazyExpandedPostingSeriesOverfetchedSizeBytes,
21002110
tenant,
21012111
)
2102-
defer blockClient.Close()
2112+
defer func() {
2113+
mtx.Lock()
2114+
stats = blockClient.MergeStats(stats)
2115+
mtx.Unlock()
2116+
blockClient.Close()
2117+
}()
21032118

21042119
if err := blockClient.ExpandPostings(
21052120
sortedReqSeriesMatchersNoExtLabels,
@@ -2128,7 +2143,7 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
21282143
}
21292144

21302145
val := labelpb.ZLabelsToPromLabels(ls.GetSeries().Labels).Get(req.Label)
2131-
if val != "" { // Should never be empty since we added labelName!="" matcher to the list of matchers.
2146+
if val != "" {
21322147
values[val] = struct{}{}
21332148
}
21342149
}
@@ -2152,6 +2167,15 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
21522167

21532168
s.mtx.RUnlock()
21542169

2170+
defer func() {
2171+
if s.debugLogging {
2172+
level.Debug(logger).Log("msg", "stats query processed",
2173+
"request", req,
2174+
"tenant", tenant,
2175+
"stats", fmt.Sprintf("%+v", stats), "err", err)
2176+
}
2177+
}()
2178+
21552179
if err := g.Wait(); err != nil {
21562180
code := codes.Internal
21572181
if s, ok := status.FromError(errors.Cause(err)); ok {

0 commit comments

Comments
 (0)