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 query priority throws 5xx when 4xx #5704

Merged
merged 2 commits into from
Dec 9, 2023
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
9 changes: 8 additions & 1 deletion pkg/querier/tripperware/priority.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tripperware

import (
"errors"
"net/http"
"strings"
"time"
Expand All @@ -11,6 +12,10 @@ import (
"github.com/cortexproject/cortex/pkg/util/validation"
)

var (
errParseExpr = errors.New("failed to parse expr")
)

func GetPriority(r *http.Request, userID string, limits Limits, now time.Time, lookbackDelta time.Duration) (int64, error) {
isQuery := strings.HasSuffix(r.URL.Path, "/query")
isQueryRange := strings.HasSuffix(r.URL.Path, "/query_range")
Expand All @@ -23,7 +28,9 @@ func GetPriority(r *http.Request, userID string, limits Limits, now time.Time, l

expr, err := parser.ParseExpr(query)
if err != nil {
return 0, err
// If query fails to be parsed, we throw a simple parse error
// and fail query later on querier.
return 0, errParseExpr
}

if len(queryPriority.Priorities) == 0 {
Expand Down
19 changes: 17 additions & 2 deletions pkg/querier/tripperware/priority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/cortexproject/cortex/pkg/util/validation"
)

func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t *testing.T) {
func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrInvalidQueryString(t *testing.T) {
now := time.Now()
limits := mockLimits{queryPriority: validation.QueryPriority{
Priorities: []validation.PriorityDef{
Expand All @@ -33,6 +33,7 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t
type testCase struct {
url string
queryPriorityEnabled bool
err error
}

tests := map[string]testCase{
Expand All @@ -43,10 +44,20 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t
url: "/query?query=",
queryPriorityEnabled: true,
},
"shouldn't return error if query is invalid": {
url: "/query?query=up[4h",
queryPriorityEnabled: true,
err: errParseExpr,
},
"should miss if query string empty - range query": {
url: "/query_range?query=",
queryPriorityEnabled: true,
},
"shouldn't return error if query is invalid, range query": {
url: "/query_range?query=up[4h",
queryPriorityEnabled: true,
err: errParseExpr,
},
"should miss if neither instant nor range query": {
url: "/series",
queryPriorityEnabled: true,
Expand All @@ -58,7 +69,11 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t
limits.queryPriority.Enabled = testData.queryPriorityEnabled
req, _ := http.NewRequest(http.MethodPost, testData.url, bytes.NewReader([]byte{}))
priority, err := GetPriority(req, "", limits, now, 0)
assert.NoError(t, err)
if err != nil {
assert.Equal(t, testData.err, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, int64(0), priority)
})
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/querier/tripperware/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ func NewQueryTripperware(

if limits != nil && limits.QueryPriority(userStr).Enabled {
priority, err := GetPriority(r, userStr, limits, now, lookbackDelta)
if err != nil {
return nil, err
if err != nil && err == errParseExpr {
// If query is invalid, no need to go through tripperwares
// for further splitting.
return next.RoundTrip(r)
}
r.Header.Set(util.QueryPriorityHeaderKey, strconv.FormatInt(priority, 10))
}
Expand Down
Loading