Skip to content

Commit 09610c2

Browse files
committed
Lint
Signed-off-by: Justin Jung <[email protected]>
1 parent 1606df4 commit 09610c2

File tree

8 files changed

+76
-79
lines changed

8 files changed

+76
-79
lines changed

pkg/frontend/v1/frontend.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,15 @@ func (f *Frontend) RoundTripGRPC(ctx context.Context, req *httpgrpc.HTTPRequest,
211211
}
212212

213213
queryPriority := f.limits.QueryPriority(userID)
214-
queryPriorityChanged := !reflect.DeepEqual(f.queryPriority, queryPriority)
214+
215215
if reqParams != nil && queryPriority.Enabled {
216-
request.priority = util_query.GetPriority(reqParams, ts, &queryPriority, queryPriorityChanged)
216+
queryPriorityChanged := !reflect.DeepEqual(f.queryPriority, queryPriority)
217+
if queryPriorityChanged {
218+
f.queryPriority = queryPriority
219+
f.compiledQueryPriority = queryPriority
220+
}
221+
222+
request.priority = util_query.GetPriority(reqParams, ts, &f.compiledQueryPriority, queryPriorityChanged)
217223
}
218224

219225
if err := f.queueRequest(ctx, &request); err != nil {

pkg/frontend/v2/frontend.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"flag"
66
"fmt"
7-
"github.com/cortexproject/cortex/pkg/util/validation"
87
"math/rand"
98
"net/http"
109
"net/url"
@@ -32,6 +31,7 @@ import (
3231
util_log "github.com/cortexproject/cortex/pkg/util/log"
3332
util_query "github.com/cortexproject/cortex/pkg/util/query"
3433
"github.com/cortexproject/cortex/pkg/util/services"
34+
"github.com/cortexproject/cortex/pkg/util/validation"
3535
)
3636

3737
// Config for a Frontend.
@@ -216,9 +216,15 @@ func (f *Frontend) RoundTripGRPC(ctx context.Context, req *httpgrpc.HTTPRequest,
216216
}
217217

218218
queryPriority := f.limits.QueryPriority(userID)
219-
queryPriorityChanged := !reflect.DeepEqual(f.queryPriority, queryPriority)
219+
220220
if reqParams != nil && queryPriority.Enabled {
221-
freq.priority = util_query.GetPriority(reqParams, ts, &queryPriority, queryPriorityChanged)
221+
queryPriorityChanged := !reflect.DeepEqual(f.queryPriority, queryPriority)
222+
if queryPriorityChanged {
223+
f.queryPriority = queryPriority
224+
f.compiledQueryPriority = queryPriority
225+
}
226+
227+
freq.priority = util_query.GetPriority(reqParams, ts, &f.compiledQueryPriority, queryPriorityChanged)
222228
}
223229

224230
f.requests.put(freq)

pkg/scheduler/queue/queue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package queue
33
import (
44
"context"
55
"fmt"
6-
"github.com/cortexproject/cortex/pkg/util/validation"
76
"strconv"
87
"sync"
98
"testing"
@@ -14,6 +13,7 @@ import (
1413
"github.com/stretchr/testify/require"
1514

1615
"github.com/cortexproject/cortex/pkg/util/services"
16+
"github.com/cortexproject/cortex/pkg/util/validation"
1717
)
1818

1919
func BenchmarkGetNextRequest(b *testing.B) {

pkg/scheduler/queue/user_queues.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ func (q *queues) updateUserQueuesAttributes(uq *userQueue, userID string, maxQue
234234
}
235235

236236
if priorityEnabled && !reflect.DeepEqual(uq.priorityList, priorityList) {
237-
var reservedQueriers map[string]int64
237+
reservedQueriers := make(map[string]int64)
238238

239239
i := 0
240-
for querierID, _ := range uq.queriers {
240+
for querierID := range uq.queriers {
241241
reservedQueriers[querierID] = priorityList[i]
242242
i++
243243
if i == len(priorityList) {
@@ -394,7 +394,7 @@ type MockLimits struct {
394394
queryPriority validation.QueryPriority
395395
}
396396

397-
func (l MockLimits) MaxQueriersPerUser(user string) float64 {
397+
func (l MockLimits) MaxQueriersPerUser(_ string) float64 {
398398
return l.maxQueriersPerUser
399399
}
400400

pkg/scheduler/queue/user_request_queue_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package queue
22

33
import (
4-
"github.com/cortexproject/cortex/pkg/util"
5-
"github.com/stretchr/testify/assert"
64
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/cortexproject/cortex/pkg/util"
79
)
810

911
func TestFIFORequestQueue(t *testing.T) {
@@ -20,9 +22,9 @@ func TestFIFORequestQueue(t *testing.T) {
2022
queue.enqueueRequest(request1)
2123
queue.enqueueRequest(request2)
2224
assert.Equal(t, 2, queue.length())
23-
assert.Equal(t, request1, queue.dequeueRequest(0))
25+
assert.Equal(t, request1, queue.dequeueRequest(0, false))
2426
assert.Equal(t, 1, queue.length())
25-
assert.Equal(t, request2, queue.dequeueRequest(0))
27+
assert.Equal(t, request2, queue.dequeueRequest(0, false))
2628
assert.Equal(t, 0, queue.length())
2729
queue.closeQueue()
2830
assert.Panics(t, func() { queue.enqueueRequest(request1) })
@@ -42,16 +44,17 @@ func TestPriorityRequestQueue(t *testing.T) {
4244
queue.enqueueRequest(request1)
4345
queue.enqueueRequest(request2)
4446
assert.Equal(t, 2, queue.length())
45-
assert.Equal(t, request2, queue.dequeueRequest(0))
47+
assert.Equal(t, request2, queue.dequeueRequest(0, true))
4648
assert.Equal(t, 1, queue.length())
47-
assert.Equal(t, request1, queue.dequeueRequest(0))
49+
assert.Equal(t, request1, queue.dequeueRequest(0, true))
4850
assert.Equal(t, 0, queue.length())
4951

5052
queue.enqueueRequest(request1)
5153
queue.enqueueRequest(request2)
5254
assert.Equal(t, 2, queue.length())
53-
assert.Equal(t, request2, queue.dequeueRequest(2))
54-
55+
assert.Equal(t, request2, queue.dequeueRequest(2, true))
56+
assert.Equal(t, request1, queue.dequeueRequest(2, false))
57+
5558
queue.closeQueue()
5659
assert.Panics(t, func() { queue.enqueueRequest(request1) })
5760
}

pkg/util/priority_queue.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ func (pq *PriorityQueue) Length() int {
6464
func (pq *PriorityQueue) Close() {
6565
pq.lock.Lock()
6666
defer pq.lock.Unlock()
67-
pq.closing = true
67+
if len(pq.queue) > 0 {
68+
pq.closing = true
69+
} else {
70+
pq.closed = true
71+
}
6872
pq.cond.Broadcast()
6973
}
7074

pkg/util/query/priority_test.go

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package query
22

33
import (
4-
"github.com/cortexproject/cortex/pkg/util/validation"
5-
"github.com/stretchr/testify/assert"
64
"net/url"
75
"strconv"
86
"testing"
97
"time"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/cortexproject/cortex/pkg/util/validation"
1012
)
1113

1214
func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t *testing.T) {
@@ -23,21 +25,20 @@ func Test_GetPriorityShouldReturnDefaultPriorityIfNotEnabledOrEmptyQueryString(t
2325
},
2426
},
2527
}
28+
queryPriority := validation.QueryPriority{
29+
Priorities: priorities,
30+
}
2631

2732
assert.Equal(t, int64(0), GetPriority(url.Values{
2833
"query": []string{"sum(up)"},
2934
"time": []string{strconv.FormatInt(now.Unix(), 10)},
30-
}, now, validation.QueryPriority{
31-
Priorities: priorities,
32-
}))
35+
}, now, &queryPriority, true))
3336

37+
queryPriority.Enabled = true
3438
assert.Equal(t, int64(0), GetPriority(url.Values{
3539
"query": []string{""},
3640
"time": []string{strconv.FormatInt(now.Unix(), 10)},
37-
}, now, validation.QueryPriority{
38-
Enabled: true,
39-
Priorities: priorities,
40-
}))
41+
}, now, &queryPriority, true))
4142
}
4243

4344
func Test_GetPriorityShouldConsiderRegex(t *testing.T) {
@@ -54,72 +55,52 @@ func Test_GetPriorityShouldConsiderRegex(t *testing.T) {
5455
},
5556
},
5657
}
57-
limits, _ := validation.NewOverrides(validation.Limits{
58-
QueryPriority: validation.QueryPriority{
59-
Enabled: true,
60-
Priorities: priorities,
61-
},
62-
}, nil)
58+
queryPriority := validation.QueryPriority{
59+
Enabled: true,
60+
Priorities: priorities,
61+
}
6362

6463
assert.Equal(t, int64(1), GetPriority(url.Values{
6564
"query": []string{"sum(up)"},
6665
"time": []string{strconv.FormatInt(now.Unix(), 10)},
67-
}, now, limits.QueryPriority("")))
66+
}, now, &queryPriority, true))
6867
assert.Equal(t, int64(0), GetPriority(url.Values{
6968
"query": []string{"count(up)"},
7069
"time": []string{strconv.FormatInt(now.Unix(), 10)},
71-
}, now, limits.QueryPriority("")))
70+
}, now, &queryPriority, false))
7271

73-
priorities[0].QueryAttributes[0].Regex = "(^sum$|c(.+)t)"
74-
limits, _ = validation.NewOverrides(validation.Limits{
75-
QueryPriority: validation.QueryPriority{
76-
Enabled: true,
77-
Priorities: priorities,
78-
},
79-
}, nil)
72+
queryPriority.Priorities[0].QueryAttributes[0].Regex = "(^sum$|c(.+)t)"
8073

8174
assert.Equal(t, int64(0), GetPriority(url.Values{
8275
"query": []string{"sum(up)"},
8376
"time": []string{strconv.FormatInt(now.Unix(), 10)},
84-
}, now, limits.QueryPriority("")))
77+
}, now, &queryPriority, true))
8578
assert.Equal(t, int64(1), GetPriority(url.Values{
8679
"query": []string{"count(up)"},
8780
"time": []string{strconv.FormatInt(now.Unix(), 10)},
88-
}, now, limits.QueryPriority("")))
81+
}, now, &queryPriority, false))
8982

90-
priorities[0].QueryAttributes[0].Regex = ".*"
91-
limits, _ = validation.NewOverrides(validation.Limits{
92-
QueryPriority: validation.QueryPriority{
93-
Enabled: true,
94-
Priorities: priorities,
95-
},
96-
}, nil)
83+
queryPriority.Priorities[0].QueryAttributes[0].Regex = ".*"
9784

9885
assert.Equal(t, int64(1), GetPriority(url.Values{
9986
"query": []string{"sum(up)"},
10087
"time": []string{strconv.FormatInt(now.Unix(), 10)},
101-
}, now, limits.QueryPriority("")))
88+
}, now, &queryPriority, true))
10289
assert.Equal(t, int64(1), GetPriority(url.Values{
10390
"query": []string{"count(up)"},
10491
"time": []string{strconv.FormatInt(now.Unix(), 10)},
105-
}, now, limits.QueryPriority("")))
92+
}, now, &queryPriority, false))
10693

107-
priorities[0].QueryAttributes[0].Regex = ""
108-
limits, _ = validation.NewOverrides(validation.Limits{
109-
QueryPriority: validation.QueryPriority{
110-
Enabled: true,
111-
Priorities: priorities,
112-
},
113-
}, nil)
94+
queryPriority.Priorities[0].QueryAttributes[0].Regex = ""
11495

11596
assert.Equal(t, int64(1), GetPriority(url.Values{
11697
"query": []string{"sum(up)"},
11798
"time": []string{strconv.FormatInt(now.Unix(), 10)},
118-
}, now, limits.QueryPriority("")))
99+
}, now, &queryPriority, true))
119100
assert.Equal(t, int64(1), GetPriority(url.Values{
120101
"query": []string{"count(up)"},
121102
"time": []string{strconv.FormatInt(now.Unix(), 10)},
122-
}, now, limits.QueryPriority("")))
103+
}, now, &queryPriority, false))
123104
}
124105

125106
func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
@@ -135,57 +116,55 @@ func Test_GetPriorityShouldConsiderStartAndEndTime(t *testing.T) {
135116
},
136117
},
137118
}
138-
limits, _ := validation.NewOverrides(validation.Limits{
139-
QueryPriority: validation.QueryPriority{
140-
Enabled: true,
141-
Priorities: priorities,
142-
},
143-
}, nil)
119+
queryPriority := validation.QueryPriority{
120+
Enabled: true,
121+
Priorities: priorities,
122+
}
144123

145124
assert.Equal(t, int64(0), GetPriority(url.Values{
146125
"query": []string{"sum(up)"},
147126
"time": []string{strconv.FormatInt(now.Unix(), 10)},
148-
}, now, limits.QueryPriority("")))
127+
}, now, &queryPriority, true))
149128
assert.Equal(t, int64(1), GetPriority(url.Values{
150129
"query": []string{"sum(up)"},
151130
"time": []string{strconv.FormatInt(now.Add(-30*time.Minute).Unix(), 10)},
152-
}, now, limits.QueryPriority("")))
131+
}, now, &queryPriority, false))
153132
assert.Equal(t, int64(0), GetPriority(url.Values{
154133
"query": []string{"sum(up)"},
155134
"time": []string{strconv.FormatInt(now.Add(-60*time.Minute).Unix(), 10)},
156-
}, now, limits.QueryPriority("")))
135+
}, now, &queryPriority, false))
157136
assert.Equal(t, int64(1), GetPriority(url.Values{
158137
"query": []string{"sum(up)"},
159138
"time": []string{strconv.FormatInt(now.Add(-45*time.Minute).Unix(), 10)},
160-
}, now, limits.QueryPriority("")))
139+
}, now, &queryPriority, false))
161140
assert.Equal(t, int64(1), GetPriority(url.Values{
162141
"query": []string{"sum(up)"},
163142
"time": []string{strconv.FormatInt(now.Add(-15*time.Minute).Unix(), 10)},
164-
}, now, limits.QueryPriority("")))
143+
}, now, &queryPriority, false))
165144

166145
assert.Equal(t, int64(1), GetPriority(url.Values{
167146
"query": []string{"sum(up)"},
168147
"start": []string{strconv.FormatInt(now.Add(-45*time.Minute).Unix(), 10)},
169148
"end": []string{strconv.FormatInt(now.Add(-15*time.Minute).Unix(), 10)},
170-
}, now, limits.QueryPriority("")))
149+
}, now, &queryPriority, false))
171150
assert.Equal(t, int64(0), GetPriority(url.Values{
172151
"query": []string{"sum(up)"},
173152
"start": []string{strconv.FormatInt(now.Add(-50*time.Minute).Unix(), 10)},
174153
"end": []string{strconv.FormatInt(now.Add(-15*time.Minute).Unix(), 10)},
175-
}, now, limits.QueryPriority("")))
154+
}, now, &queryPriority, false))
176155
assert.Equal(t, int64(0), GetPriority(url.Values{
177156
"query": []string{"sum(up)"},
178157
"start": []string{strconv.FormatInt(now.Add(-45*time.Minute).Unix(), 10)},
179158
"end": []string{strconv.FormatInt(now.Add(-10*time.Minute).Unix(), 10)},
180-
}, now, limits.QueryPriority("")))
159+
}, now, &queryPriority, false))
181160
assert.Equal(t, int64(0), GetPriority(url.Values{
182161
"query": []string{"sum(up)"},
183162
"start": []string{strconv.FormatInt(now.Add(-60*time.Minute).Unix(), 10)},
184163
"end": []string{strconv.FormatInt(now.Add(-45*time.Minute).Unix(), 10)},
185-
}, now, limits.QueryPriority("")))
164+
}, now, &queryPriority, false))
186165
assert.Equal(t, int64(0), GetPriority(url.Values{
187166
"query": []string{"sum(up)"},
188167
"start": []string{strconv.FormatInt(now.Add(-15*time.Minute).Unix(), 10)},
189168
"end": []string{strconv.FormatInt(now.Add(-1*time.Minute).Unix(), 10)},
190-
}, now, limits.QueryPriority("")))
169+
}, now, &queryPriority, false))
191170
}

pkg/util/validation/limits_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ func TestQueryPriority(t *testing.T) {
194194
} else {
195195
assert.NotNil(t, queryPriority.Priorities[0].QueryAttributes[0].CompiledRegex)
196196
}
197-
assert.True(t, queryPriority.RegexCompiled)
198197
}
199198
}
200199

0 commit comments

Comments
 (0)