Skip to content

Commit 91a6547

Browse files
committed
Change GetPriority to not throw error
1 parent 7835190 commit 91a6547

File tree

3 files changed

+11
-40
lines changed

3 files changed

+11
-40
lines changed

pkg/querier/tripperware/priority.go

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
11
package tripperware
22

33
import (
4-
"errors"
54
"time"
65

76
"github.com/cortexproject/cortex/pkg/util/validation"
87
)
98

10-
var (
11-
errQueryPriorityDisabled = errors.New("query priority disabled")
12-
errEmptyQueryString = errors.New("empty query string")
13-
)
14-
15-
func GetPriority(query string, minTime, maxTime int64, now time.Time, queryPriority validation.QueryPriority) (int64, error) {
16-
if !queryPriority.Enabled {
17-
return 0, errQueryPriorityDisabled
18-
}
19-
20-
if query == "" {
21-
return 0, errEmptyQueryString
22-
}
23-
24-
if len(queryPriority.Priorities) == 0 {
25-
return queryPriority.DefaultPriority, nil
9+
func GetPriority(query string, minTime, maxTime int64, now time.Time, queryPriority validation.QueryPriority) int64 {
10+
if !queryPriority.Enabled || query == "" || len(queryPriority.Priorities) == 0 {
11+
return queryPriority.DefaultPriority
2612
}
2713

2814
for _, priority := range queryPriority.Priorities {
@@ -34,12 +20,12 @@ func GetPriority(query string, minTime, maxTime int64, now time.Time, queryPrior
3420
}
3521

3622
if isWithinTimeAttributes(attribute.TimeWindow, now, minTime, maxTime) {
37-
return priority.Priority, nil
23+
return priority.Priority
3824
}
3925
}
4026
}
4127

42-
return queryPriority.DefaultPriority, nil
28+
return queryPriority.DefaultPriority
4329
}
4430

4531
func isWithinTimeAttributes(timeWindow validation.TimeWindow, now time.Time, startTime, endTime int64) bool {

pkg/querier/tripperware/priority_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,23 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrInvalidQueryString
2929

3030
type testCase struct {
3131
query string
32-
err error
3332
queryPriorityEnabled bool
3433
}
3534

3635
tests := map[string]testCase{
3736
"should miss if query priority not enabled": {
3837
query: "up",
39-
err: errQueryPriorityDisabled,
4038
},
4139
"should miss if query string empty": {
4240
query: "",
43-
err: errEmptyQueryString,
4441
queryPriorityEnabled: true,
4542
},
4643
}
4744

4845
for testName, testData := range tests {
4946
t.Run(testName, func(t *testing.T) {
5047
limits.queryPriority.Enabled = testData.queryPriorityEnabled
51-
priority, err := GetPriority(testData.query, 0, 0, now, limits.queryPriority)
52-
if err != nil {
53-
assert.Equal(t, testData.err, err)
54-
} else {
55-
assert.NoError(t, err)
56-
}
48+
priority := GetPriority(testData.query, 0, 0, now, limits.queryPriority)
5749
assert.Equal(t, int64(0), priority)
5850
})
5951
}
@@ -111,8 +103,7 @@ func Test_GetPriorityShouldConsiderRegex(t *testing.T) {
111103
t.Run(testName, func(t *testing.T) {
112104
limits.queryPriority.Priorities[0].QueryAttributes[0].Regex = testData.regex
113105
limits.queryPriority.Priorities[0].QueryAttributes[0].CompiledRegex = regexp.MustCompile(testData.regex)
114-
priority, err := GetPriority(testData.query, 0, 0, now, limits.queryPriority)
115-
assert.NoError(t, err)
106+
priority := GetPriority(testData.query, 0, 0, now, limits.queryPriority)
116107
assert.Equal(t, int64(testData.expectedPriority), priority)
117108
})
118109
}
@@ -180,8 +171,7 @@ func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
180171

181172
for testName, testData := range tests {
182173
t.Run(testName, func(t *testing.T) {
183-
priority, err := GetPriority("sum(up)", testData.start.UnixMilli(), testData.end.UnixMilli(), now, limits.queryPriority)
184-
assert.NoError(t, err)
174+
priority := GetPriority("sum(up)", testData.start.UnixMilli(), testData.end.UnixMilli(), now, limits.queryPriority)
185175
assert.Equal(t, int64(testData.expectedPriority), priority)
186176
})
187177
}
@@ -225,8 +215,7 @@ func Test_GetPriorityShouldNotConsiderStartAndEndTimeIfEmpty(t *testing.T) {
225215

226216
for testName, testData := range tests {
227217
t.Run(testName, func(t *testing.T) {
228-
priority, err := GetPriority("sum(up)", testData.start.Unix(), testData.end.Unix(), now, limits.queryPriority)
229-
assert.NoError(t, err)
218+
priority := GetPriority("sum(up)", testData.start.Unix(), testData.end.Unix(), now, limits.queryPriority)
230219
assert.Equal(t, int64(1), priority)
231220
})
232221
}

pkg/querier/tripperware/roundtrip.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,8 @@ func NewQueryTripperware(
170170
*r = *r.WithContext(context.WithValue(r.Context(), DataFetchedMaxTimeCtxKey, maxTime))
171171

172172
if limits != nil && limits.QueryPriority(userStr).Enabled {
173-
priority, err := GetPriority(query, minTime, maxTime, now, limits.QueryPriority(userStr))
174-
if err != nil {
175-
level.Debug(log).Log("msg", "failed to get query priority for user", "user", userStr, "err", err.Error())
176-
} else {
177-
*r = *r.WithContext(context.WithValue(r.Context(), QueryPriorityCtxKey, priority))
178-
}
173+
priority := GetPriority(query, minTime, maxTime, now, limits.QueryPriority(userStr))
174+
*r = *r.WithContext(context.WithValue(r.Context(), QueryPriorityCtxKey, priority))
179175
}
180176
}
181177

0 commit comments

Comments
 (0)