Repository navigation
Expand file tree
/
Copy pathqueue_manager.go
More file actions
356 lines (326 loc) · 10.9 KB
/
Copy pathqueue_manager.go
File metadata and controls
356 lines (326 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package asynq
import (
"context"
"fmt"
"math/rand/v2"
"regexp"
"slices"
"sort"
"strings"
"sync"
"time"
"unicode/utf8"
"github.com/hibiken/asynq/internal/base"
"github.com/hibiken/asynq/internal/log"
)
type queueManager struct {
logger *log.Logger
broker base.Broker
matcher *queuePriorityMatcher
// channel to communicate back to the long running "queueManager" goroutine.
done chan struct{}
// params for static queues
queuesConfig map[string]int // queue priorities from config
queueNames []string // unordered list of queue names from config
orderedQueues []string // ordered list of queue names for dequeueing tasks.
normalizedQueues map[string]int // ordered list of queue names for dequeueing tasks.
strictPriority bool
dynamicQueues bool
// function to get dynamic queues from the broker
dynamicQeueueGetter func(ctx context.Context) (map[string]struct{}, error)
// params for dynamic queues
interval time.Duration // interval between dynamic queue updates.
rw sync.RWMutex //guards dynamic queues and priorities
dynamicQueueIdx map[string]int // index priority by queue name
dynamicPriorityIdx map[int][]string // reverse index of queue names by priority
dynamicPrioritiesOrder []int // maintains the order of priorities (highest to lowest)
}
// GetQueueConfig returns the current queue configuration.
func (qm *queueManager) GetQueueConfig() (strict, dynamic bool, queues map[string]int) {
return qm.strictPriority, qm.dynamicQueues, qm.queuesConfig
}
// GetUnorderedQueueNames returns unordered set of queues from the broker.
func (qm *queueManager) GetUnorderedQueueNames() []string {
if !qm.dynamicQueues {
return qm.queueNames
}
qm.rw.RLock()
defer qm.rw.RUnlock()
queues := make([]string, 0, len(qm.dynamicQueueIdx))
for queue := range qm.dynamicQueueIdx {
queues = append(queues, queue)
}
return queues
}
func (qm *queueManager) GetQueuesInDequeueOrder() []string {
if !qm.dynamicQueues {
// use old implementation for static queues
return qm.dequeueStatic()
}
return qm.dequeueDynamic()
}
// moved from processor.go
func (qm *queueManager) dequeueStatic() []string {
// skip the overhead of generating a list of queue names
// if we are processing one queue.
if len(qm.queuesConfig) == 1 {
return qm.queueNames
}
if qm.orderedQueues != nil {
return qm.orderedQueues
}
var names []string
for qname, priority := range qm.normalizedQueues {
for i := 0; i < priority; i++ {
names = append(names, qname)
}
}
rand.Shuffle(len(names), func(i, j int) { names[i], names[j] = names[j], names[i] })
return uniq(names, len(qm.normalizedQueues))
}
func (qm *queueManager) dequeueDynamic() []string {
qm.rw.RLock()
defer qm.rw.RUnlock()
queues := make([]string, 0, len(qm.dynamicQueueIdx))
// we go over all queues from the highest priority to the lowest
// if we have multiple queues with the same priority, we shuffle them
for _, priority := range qm.dynamicPrioritiesOrder {
queueNames := qm.dynamicPriorityIdx[priority]
if len(queueNames) == 1 {
// if there is only one queue with this priority, add it directly
queues = append(queues, queueNames[0])
continue
}
oldLen := len(queues)
// first copy values to queues and then shuffle them in the copied sub-slice
queues = append(queues, queueNames...)
// shuffle sub-slice of the queues with the same priority
rand.Shuffle(len(queueNames), func(i, j int) { queues[oldLen+i], queues[oldLen+j] = queues[oldLen+j], queues[oldLen+i] })
}
return queues
}
type queueManagerParams struct {
logger *log.Logger
broker base.Broker
interval time.Duration
queues map[string]int
strictPriority bool
dynamicQueues bool
}
func newQueueManager(params queueManagerParams) *queueManager {
qm := &queueManager{
logger: params.logger,
broker: params.broker,
done: make(chan struct{}),
interval: params.interval,
matcher: nil,
queuesConfig: params.queues,
queueNames: make([]string, 0, len(params.queues)),
strictPriority: params.strictPriority,
dynamicQueues: params.dynamicQueues,
}
// init params for static queues (copied from processor.go)
for queueName := range params.queues {
qm.queueNames = append(qm.queueNames, queueName)
}
qm.normalizedQueues = normalizeQueues(params.queues)
if params.strictPriority {
qm.orderedQueues = sortByPriority(qm.normalizedQueues)
}
// init params for dynamic queues
if params.dynamicQueues {
if !params.strictPriority {
panic("invalid configuration: dynamic queues must be used with strict priority enabled")
}
if len(params.queues) == 0 {
panic("invalid configuration: dynamic queues must have at least one queue defined")
}
matcher, err := newQueuePriorityMatcher(params.queues)
if err != nil {
panic(fmt.Sprintf("failed to create queue priority matcher: %v", err))
}
qm.matcher = matcher
qm.dynamicQueueIdx = map[string]int{}
qm.dynamicPriorityIdx = make(map[int][]string, len(params.queues))
qm.dynamicQeueueGetter = params.broker.SetOfQueues
}
return qm
}
func (qm *queueManager) shutdown() {
if !qm.dynamicQueues {
return
}
qm.logger.Debug("QueueManager shutting down...")
// Signal the queue manager goroutine to stop.
qm.done <- struct{}{}
}
func (qm *queueManager) start(wg *sync.WaitGroup) {
if !qm.dynamicQueues {
return
}
wg.Add(1)
go func() {
defer wg.Done()
timer := time.NewTimer(qm.interval)
for {
qm.updateDynamicQueues()
select {
case <-qm.done:
qm.logger.Debug("QueueManager done")
timer.Stop()
return
case <-timer.C:
timer.Reset(qm.interval)
}
}
}()
}
func (qm *queueManager) updateDynamicQueues() {
// get the current set of queues from the broker
queues, err := qm.dynamicQeueueGetter(context.Background())
if err != nil {
qm.logger.Errorf("Failed to get queues: %v", err)
return
}
qm.rw.Lock()
defer qm.rw.Unlock()
// remove queues that are no longer present in the broker
toRemove := make(map[string]int)
for queue, priority := range qm.dynamicQueueIdx {
if _, ok := queues[queue]; !ok {
toRemove[queue] = priority
}
}
for queue, priority := range toRemove {
// delete from queue index
delete(qm.dynamicQueueIdx, queue)
// delete from priority index
priorities := qm.dynamicPriorityIdx[priority]
qm.dynamicPriorityIdx[priority] = slices.DeleteFunc(priorities, func(q string) bool {
return q == queue
})
}
// add new queues from the broker
updatePriorityOrder := false
for queue := range queues {
if _, ok := qm.dynamicQueueIdx[queue]; ok {
// queue already exists, skip
continue
}
// calculate priority for the new queue and add it to the queue index
priority, ok := qm.matcher.GetPriority(queue)
if !ok {
continue
}
qm.dynamicQueueIdx[queue] = priority
_, priorityExists := qm.dynamicPriorityIdx[priority]
if !priorityExists {
// add new priority value to priorities order array
qm.dynamicPrioritiesOrder = append(qm.dynamicPrioritiesOrder, priority)
// set flag to sort array later
updatePriorityOrder = true
}
// add queue to priority index
qm.dynamicPriorityIdx[priority] = append(qm.dynamicPriorityIdx[priority], queue)
}
// sort priorities in descending order
if updatePriorityOrder {
sort.Slice(qm.dynamicPrioritiesOrder, func(i, j int) bool {
return qm.dynamicPrioritiesOrder[i] > qm.dynamicPrioritiesOrder[j]
})
qm.dynamicPrioritiesOrder = slices.Compact(qm.dynamicPrioritiesOrder)
}
}
// newQueuePriorityMatcher creates a new queue priority matcher based on the provided dynamic queues.
// Example:
//
// Queues: map[string]int{
// "my-queue-name": 7, // exact queue name
// "other-queue-name": 7, // exact queue name. Same priority as "my-queue-name"
// "my-queue-*": 6, // matches any queue name that starts with "my-queue-"
// "my-queue-low-*": 5, // matches any queue name that starts with "my-queue-low-"
// // longest common prefix will be used.
// "*-my-queue": 4, // matches any queue name that ends with "-my-queue"
// "*-my-queue-*": 3, // matches any queue name that contains "-my-queue-" in the middle
// "my-*-queue": 2, // matches any queue name starting with "my-" and ending with "-queue"
// "*": 1, // default priority. Will match any queue name if set.
// // if default priority is not set, not matching queue will be ignored.
// },
func newQueuePriorityMatcher(dynamicQueues map[string]int) (*queuePriorityMatcher, error) {
if len(dynamicQueues) == 0 {
return nil, fmt.Errorf("dynamic queues cannot be empty")
}
q := &queuePriorityMatcher{
exact: make(map[string]int), // exact queue names without wildcards
wildcard: nil, // wildcard queue names patterns
}
// init queue matcher
for queueName, priority := range dynamicQueues {
hasWildcard := strings.Contains(queueName, "*")
if !hasWildcard {
// queueName is an exact match
q.exact[queueName] = priority
continue
}
// queueName contains wildcards (*)
wildcard, err := newWildcardMatcher(queueName, priority)
if err != nil {
return nil, err
}
q.wildcard = append(q.wildcard, wildcard)
}
return q, nil
}
type queuePriorityMatcher struct {
exact map[string]int
wildcard []wildcardMatcher
}
func (d *queuePriorityMatcher) GetPriority(queueName string) (int, bool) {
if priority, ok := d.exact[queueName]; ok {
// return priority for exact match
return priority, true
}
// find the longest match in wildcard patterns
longestMatchLen, matchedPriority := -1, 0
for _, matcher := range d.wildcard {
priority, matchedLen, ok := matcher.match(queueName)
if !ok {
continue
}
if matchedLen > longestMatchLen {
longestMatchLen = matchedLen
matchedPriority = priority
}
}
if longestMatchLen == -1 {
// match not found
return 0, false
}
return matchedPriority, true
}
func newWildcardMatcher(queueName string, priority int) (wildcardMatcher, error) {
// convert queueName with wildcards (*) to regex
regexPattern := "^" + regexp.QuoteMeta(queueName) + "$"
regexPattern = strings.ReplaceAll(regexPattern, `\*`, ".*")
re, err := regexp.Compile(regexPattern)
if err != nil {
return wildcardMatcher{}, fmt.Errorf("failed to compile regex for queue name %s: %w", queueName, err)
}
wildcardCount := strings.Count(queueName, "*")
return wildcardMatcher{
regexPattern: re,
exactPartLen: utf8.RuneCountInString(queueName) - wildcardCount,
priority: priority,
}, nil
}
type wildcardMatcher struct {
regexPattern *regexp.Regexp // regex pattern for matching queue names with wildcards
exactPartLen int // number of matching characters excluding wildcards. Used to determine the longest match
priority int // priority of the queue
}
func (w *wildcardMatcher) match(queueName string) (priority, matchedLen int, ok bool) {
if w.regexPattern.MatchString(queueName) {
return w.priority, w.exactPartLen, true
}
return 0, 0, false
}