Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix index out of bound bug when comparing ZLabelSets #3520

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/queryfrontend/labels_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (c labelsCodec) MergeResponse(responses ...queryrange.Response) (queryrange
if len(responses) == 1 {
return responses[0], nil
}
seriesData := make(labelpb.ZLabelSets, 0)

seriesData := make(labelpb.ZLabelSets, 0)
uniqueSeries := make(map[string]struct{})
for _, res := range responses {
for _, series := range res.(*ThanosSeriesResponse).Data {
Expand Down Expand Up @@ -287,7 +287,7 @@ func (c labelsCodec) parseLabelsRequest(r *http.Request, op string) (queryrange.
return nil, err
}

result.StoreMatchers, err = parseMatchersParam(r.Form[queryv1.StoreMatcherParam])
result.StoreMatchers, err = parseMatchersParam(r.Form, queryv1.StoreMatcherParam)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -321,7 +321,7 @@ func (c labelsCodec) parseSeriesRequest(r *http.Request) (queryrange.Request, er
return nil, err
}

result.Matchers, err = parseMatchersParam(r.Form[queryv1.MatcherParam])
result.Matchers, err = parseMatchersParam(r.Form, queryv1.MatcherParam)
if err != nil {
return nil, err
}
Expand All @@ -340,7 +340,7 @@ func (c labelsCodec) parseSeriesRequest(r *http.Request) (queryrange.Request, er
result.ReplicaLabels = r.Form[queryv1.ReplicaLabelsParam]
}

result.StoreMatchers, err = parseMatchersParam(r.Form[queryv1.StoreMatcherParam])
result.StoreMatchers, err = parseMatchersParam(r.Form, queryv1.StoreMatcherParam)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/queryfrontend/queryrange_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c queryRangeCodec) DecodeRequest(_ context.Context, r *http.Request) (quer
result.ReplicaLabels = r.Form[queryv1.ReplicaLabelsParam]
}

result.StoreMatchers, err = parseMatchersParam(r.Form[queryv1.StoreMatcherParam])
result.StoreMatchers, err = parseMatchersParam(r.Form, queryv1.StoreMatcherParam)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -221,12 +221,12 @@ func parsePartialResponseParam(s string, defaultEnablePartialResponse bool) (boo
return defaultEnablePartialResponse, nil
}

func parseMatchersParam(ss []string) ([][]*labels.Matcher, error) {
matchers := make([][]*labels.Matcher, 0, len(ss))
for _, s := range ss {
func parseMatchersParam(ss url.Values, matcherParam string) ([][]*labels.Matcher, error) {
matchers := make([][]*labels.Matcher, 0, len(ss[matcherParam]))
for _, s := range ss[matcherParam] {
ms, err := parser.ParseMetricSelector(s)
if err != nil {
return nil, httpgrpc.Errorf(http.StatusBadRequest, errCannotParse, queryv1.StoreMatcherParam)
return nil, httpgrpc.Errorf(http.StatusBadRequest, errCannotParse, matcherParam)
}
matchers = append(matchers, ms)
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/store/labelpb/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ func (z ZLabelSets) Less(i, j int) bool {
l := 0
r := 0
var result int
for l < z[i].Size() && r < z[j].Size() {
lenI, lenJ := len(z[i].Labels), len(z[j].Labels)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 (:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

for l < lenI && r < lenJ {
result = z[i].Labels[l].Compare(z[j].Labels[r])
if result == 0 {
l++
Expand All @@ -317,5 +318,5 @@ func (z ZLabelSets) Less(i, j int) bool {
return result < 0
}

return l == z[i].Size()
return l == lenI
}
57 changes: 57 additions & 0 deletions pkg/store/labelpb/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ func TestSortZLabelSets(t *testing.T) {
}),
),
},
{
Labels: ZLabelsFromPromLabels(
labels.FromMap(map[string]string{
"__name__": "grpc_client_handled_total",
"cluster": "test",
"grpc_code": "OK",
"aa": "1",
"bb": "2",
"cc": "3",
"dd": "4",
"ee": "5",
}),
),
},
{
Labels: ZLabelsFromPromLabels(
labels.FromMap(map[string]string{
"__name__": "grpc_client_handled_total",
"cluster": "test",
"grpc_code": "OK",
"aa": "1",
"bb": "2",
"cc": "3",
"dd": "4",
"ee": "5",
}),
),
},
{
Labels: ZLabelsFromPromLabels(
labels.FromMap(map[string]string{
Expand Down Expand Up @@ -188,6 +216,35 @@ func TestSortZLabelSets(t *testing.T) {
}),
),
},
{
Labels: ZLabelsFromPromLabels(
labels.FromMap(map[string]string{
"__name__": "grpc_client_handled_total",
"cluster": "test",
"grpc_code": "OK",
"aa": "1",
"bb": "2",
"cc": "3",
"dd": "4",
"ee": "5",
}),
),
},
// This label set is the same as the previous one, which should correctly return 0 in Less() function.
{
Labels: ZLabelsFromPromLabels(
labels.FromMap(map[string]string{
"cluster": "test",
"__name__": "grpc_client_handled_total",
"grpc_code": "OK",
"aa": "1",
"bb": "2",
"cc": "3",
"dd": "4",
"ee": "5",
}),
),
},
}

sort.Sort(list)
Expand Down