From e4b579fa9062f24b00d915dd972f1d8f1ea415f6 Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Tue, 17 Oct 2023 20:33:54 -0700 Subject: [PATCH] Fix matchersToPostingGroups vals variable shadow bug (#6817) * fix matchersToPostingGroups vals variable shadow bug Signed-off-by: Ben Ye * update changelog Signed-off-by: Ben Ye --------- Signed-off-by: Ben Ye --- CHANGELOG.md | 1 + pkg/store/bucket.go | 3 ++- pkg/store/bucket_test.go | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ba214b606..eb6e8569c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Fixed - [#6802](https://github.com/thanos-io/thanos/pull/6802) Receive: head series limiter should not run if no head series limit is set. +- [#6817](https://github.com/thanos-io/thanos/pull/6817) Store Gateway: fix `matchersToPostingGroups` label values variable got shadowed bug. ### Added diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index 006a4b0ded..7b9676e915 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -2664,8 +2664,9 @@ func matchersToPostingGroups(ctx context.Context, lvalsFn func(name string) ([]s } // Cache label values because label name is the same. if !valuesCached && vals != nil { + lvals := vals lvalsFunc = func(_ string) ([]string, error) { - return vals, nil + return lvals, nil } valuesCached = true } diff --git a/pkg/store/bucket_test.go b/pkg/store/bucket_test.go index 4dc3c59643..6e2949ac02 100644 --- a/pkg/store/bucket_test.go +++ b/pkg/store/bucket_test.go @@ -3132,6 +3132,29 @@ func TestMatchersToPostingGroup(t *testing.T) { }, }, }, + { + name: "Reproduce values shadow bug", + matchers: []*labels.Matcher{ + labels.MustNewMatcher(labels.MatchRegexp, "name", "test.*"), + labels.MustNewMatcher(labels.MatchNotRegexp, "name", "testfoo"), + labels.MustNewMatcher(labels.MatchNotEqual, "name", ""), + }, + labelValues: map[string][]string{ + "name": {"testbar", "testfoo"}, + }, + expected: []*postingGroup{ + { + name: "name", + addAll: false, + addKeys: []string{"testbar"}, + matchers: []*labels.Matcher{ + labels.MustNewMatcher(labels.MatchNotEqual, "name", ""), + labels.MustNewMatcher(labels.MatchRegexp, "name", "test.*"), + labels.MustNewMatcher(labels.MatchNotRegexp, "name", "testfoo"), + }, + }, + }, + }, } { t.Run(tc.name, func(t *testing.T) { actual, err := matchersToPostingGroups(ctx, func(name string) ([]string, error) {