diff --git a/CHANGELOG.md b/CHANGELOG.md index cb3f83a2d0..b84cfd9693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re ### Fixed +- [#6817](https://github.com/thanos-io/thanos/pull/6817) Store Gateway: fix `matchersToPostingGroups` label values variable got shadowed bug. + ### Added ### Changed diff --git a/pkg/store/bucket.go b/pkg/store/bucket.go index 8b12e13e4f..c491c8f9cf 100644 --- a/pkg/store/bucket.go +++ b/pkg/store/bucket.go @@ -2503,8 +2503,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 bd73e5d08d..55e4d830f8 100644 --- a/pkg/store/bucket_test.go +++ b/pkg/store/bucket_test.go @@ -3076,6 +3076,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) {