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 2 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
25 changes: 21 additions & 4 deletions pkg/queryfrontend/labels_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,25 @@ func (c labelsCodec) MergeResponse(responses ...queryrange.Response) (queryrange
if len(responses) == 1 {
return responses[0], nil
}
seriesData := make(labelpb.ZLabelSets, 0)

i := 0
var resp queryrange.Response
for _, response := range responses {
if len(response.(*ThanosSeriesResponse).Data) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

I don't get this, why we cannot check this in loop below?

Copy link
Contributor Author

@yeya24 yeya24 Dec 3, 2020

Choose a reason for hiding this comment

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

Makes sense. As you mentioned I ll remove this change since it is unrelated and I ll fix them in next pr against master

i++
resp = response
}
if i > 1 {
break
}
}
// Fast path for the case that only one response contains series data and other responses are empty.
// We can just return that response to avoid deduplication and sorting.
if i == 1 {
return resp, nil
}

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 +304,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], queryv1.StoreMatcherParam)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -321,7 +338,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], queryv1.MatcherParam)
if err != nil {
return nil, err
}
Expand All @@ -340,7 +357,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], queryv1.StoreMatcherParam)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 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], queryv1.StoreMatcherParam)
Copy link
Member

Choose a reason for hiding this comment

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

This is bad code smell - passing same information twice. Maybe we should pass just Form and matcherKey to param function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point.

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) {
func parseMatchersParam(ss []string, matcherParam string) ([][]*labels.Matcher, error) {
matchers := make([][]*labels.Matcher, 0, len(ss))
for _, s := range ss {
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