-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.go
686 lines (618 loc) · 16.9 KB
/
queue.go
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package queue
import (
"encoding/json"
"sync"
"sync/atomic"
"time"
"github.com/koykov/bitset"
)
type Status uint32
const (
StatusNil Status = iota
StatusFail
StatusActive
StatusThrottle
StatusClose
flagBalanced = 0
flagLeaky = 1
)
// Queue is an implementation of balanced leaky queue.
//
// The queue balances among available workers [Config.WorkersMin...Config.WorkersMax] in realtime.
// Queue also has leaky feature: when queue is full and new items continue to flow, then leaked items will forward to
// DLQ (dead letter queue).
// For queues with variadic daily load exists special scheduler (see schedule.go) that allow to specify variadic queue
// params for certain time ranges.
type Queue struct {
bitset.Bitset
// Config instance.
config *Config
// ID of actual schedule rule. Contains -1 by default (no rule found).
schedID int
// The number of maximum workers that queue may contain considering all schedule rules and config params.
wmax uint32
// Actual queue status.
status Status
// Internal engine.
engine engine
mux sync.Mutex
// Workers pool.
workers []*worker
once sync.Once
// Counter of active workers.
workersUp int32
// Calibration lock counter.
c9nlock uint32
// Spinlock of queue.
spinlock int64
// Enqueue lock counter.
enqlock int64
err error
}
// item is a wrapper for queue element with retries count.
type item struct {
payload any
retries uint32
delay int64 // Delayed execution expire time (Unix ns timestamp).
deadline int64 // Deadline time (Unix ns timestamp).
subqi uint32 // Sub-queue index.
}
// realtimeParams describes queue params for current time.
type realtimeParams struct {
WorkersMin, WorkersMax uint32
WakeupFactor, SleepFactor float32
}
// New makes new queue instance and initialize it according config params.
func New(config *Config) (*Queue, error) {
if config == nil {
return nil, ErrNoConfig
}
q := &Queue{config: config}
q.once.Do(q.init)
return q, q.err
}
// Init queue.
func (q *Queue) init() {
if q.config == nil {
q.err = ErrNoConfig
q.status = StatusFail
return
}
// Make a copy of config instance to protect queue from changing params after start.
q.config = q.config.Copy()
c := q.config
// Check mandatory params.
if c.Capacity == 0 && c.QoS == nil {
q.err = ErrNoCapacity
q.status = StatusFail
return
}
if c.Worker == nil {
q.err = ErrNoWorker
q.status = StatusFail
return
}
if c.Clock == nil {
c.Clock = nativeClock{}
}
if c.MetricsWriter == nil {
// Use dummy MW.
c.MetricsWriter = DummyMetrics{}
}
// Check workers numbers params.
if c.Workers > 0 && c.WorkersMin == 0 {
c.WorkersMin = c.Workers
}
if c.Workers > 0 && c.WorkersMax == 0 {
c.WorkersMax = c.Workers
}
if c.WorkersMax < c.WorkersMin {
c.WorkersMin = c.WorkersMax
}
if c.WorkersMax == 0 {
q.err = ErrNoWorkers
q.status = StatusFail
return
}
// Check non-mandatory params and set default values if needed.
if c.ForceCalibrationLimit == 0 {
c.ForceCalibrationLimit = defaultForceCalibrationLimit
}
if c.WakeupFactor <= 0 {
c.WakeupFactor = defaultWakeupFactor
}
if c.WakeupFactor > defaultFactorLimit {
c.WakeupFactor = defaultFactorLimit
}
if c.SleepFactor <= 0 {
c.SleepFactor = defaultSleepFactor
}
if c.SleepFactor > defaultFactorLimit {
c.SleepFactor = defaultFactorLimit
}
if c.WakeupFactor < c.SleepFactor {
c.WakeupFactor = c.SleepFactor
}
if c.SleepInterval == 0 {
c.SleepInterval = defaultSleepInterval
}
if c.HeartbeatInterval == 0 {
c.HeartbeatInterval = defaultHeartbeatInterval
}
if c.LeakDirection == LeakDirectionFront && c.FrontLeakAttempts == 0 {
c.FrontLeakAttempts = defaultFrontLeakAttempts
}
// Create the engine.
switch {
case c.QoS != nil:
if q.err = c.QoS.Validate(); q.err != nil {
q.status = StatusFail
return
}
c.Capacity = c.QoS.SummingCapacity()
q.engine = &pq{}
case c.Streams > 0:
q.engine = &pfifo{}
default:
q.engine = &fifo{}
}
if q.err = q.engine.init(c); q.err != nil {
q.status = StatusFail
return
}
// Check flags.
q.SetBit(flagBalanced, c.WorkersMin < c.WorkersMax || c.Schedule != nil)
q.SetBit(flagLeaky, c.DLQ != nil)
// Check initial params.
q.wmax = q.workersMaxDaily()
var params realtimeParams
params, q.schedID = q.rtParams()
// Make workers pool/
q.workers = make([]*worker, q.wmax)
var i uint32
for i = 0; i < q.wmax; i++ {
q.mw().WorkerSleep(i)
q.workers[i] = makeWorker(i, c)
}
q.mw().WorkerSetup(0, 0, uint(params.WorkersMax))
// Start [0...workersMin] workers.
for i = 0; i < params.WorkersMin; i++ {
q.workers[i].signal(sigInit)
go q.workers[i].await(q)
}
q.workersUp = int32(params.WorkersMin)
if q.CheckBit(flagBalanced) {
// Init background heartbeat ticker.
tickerHB := time.NewTicker(c.HeartbeatInterval)
go func() {
for {
select {
case <-tickerHB.C:
// Calibrate queue on each tick in regular mode.
q.calibrate(false)
if q.Rate() == 0 && q.getStatus() == StatusClose {
tickerHB.Stop()
// Exit on empty stopped queue.
return
}
}
}
}()
}
// Queue is ready!
q.setStatus(StatusActive)
}
// Enqueue puts x to the queue.
func (q *Queue) Enqueue(x any) error {
q.once.Do(q.init)
// Check if enqueue is possible.
if status := q.getStatus(); status == StatusClose || status == StatusFail {
return ErrQueueClosed
}
atomic.AddInt64(&q.enqlock, 1)
defer atomic.AddInt64(&q.enqlock, -1)
if q.CheckBit(flagBalanced) {
defer atomic.AddInt64(&q.spinlock, -1)
// Consider spinlock and calibration limit on balanced queue.
if atomic.AddInt64(&q.spinlock, 1) >= int64(q.c().ForceCalibrationLimit) {
q.calibrate(true)
}
}
// Prepare item.
itm := item{payload: x}
if di := q.c().DelayInterval; di > 0 {
itm.delay = q.clk().Now().Add(di).UnixNano()
}
if di := q.c().DeadlineInterval; di > 0 {
itm.deadline = q.clk().Now().Add(di).UnixNano()
}
switch x.(type) {
case Job:
job := x.(Job)
if job.DelayInterval > 0 {
itm.delay = q.clk().Now().Add(job.DelayInterval).UnixNano()
}
if job.DeadlineInterval > 0 {
itm.deadline = q.clk().Now().Add(job.DeadlineInterval).UnixNano()
}
case *Job:
job := x.(*Job)
if job.DelayInterval > 0 {
itm.delay = q.clk().Now().Add(job.DelayInterval).UnixNano()
}
if job.DeadlineInterval > 0 {
itm.deadline = q.clk().Now().Add(job.DeadlineInterval).UnixNano()
}
}
return q.renqueue(&itm)
}
// Put wrapped item to the queue.
// This method also uses for enqueue retries (see Config.MaxRetries).
func (q *Queue) renqueue(itm *item) (err error) {
q.mw().QueuePut()
if q.CheckBit(flagLeaky) {
// Put item to the stream in leaky mode.
if !q.engine.enqueue(itm, false) {
// Leak the item to DLQ.
if q.c().LeakDirection == LeakDirectionFront {
// Front direction, first need to extract item to leak from queue front.
for i := uint32(0); i < q.c().FrontLeakAttempts; i++ {
itmf, _ := q.engine.dequeueSQ(itm.subqi)
if err = q.c().DLQ.Enqueue(itmf.payload); err != nil {
q.mw().QueueLost()
return
}
q.mw().QueueLeak(LeakDirectionFront.String())
if q.engine.enqueue(itm, false) {
return
} else {
continue
}
}
// Front leak failed, fallback to rear direction.
}
// Rear direction, just leak item.
err = q.c().DLQ.Enqueue(itm.payload)
q.mw().QueueLeak(LeakDirectionRear.String())
}
} else {
// Regular put (blocking mode).
q.engine.enqueue(itm, true)
}
return
}
// Size return actual size of the queue.
func (q *Queue) Size() int {
return q.engine.size()
}
// Capacity return max size of the queue.
func (q *Queue) Capacity() int {
return q.engine.cap()
}
// Rate returns size to capacity ratio.
func (q *Queue) Rate() float32 {
return float32(q.engine.size()) / float32(q.engine.cap())
}
// Close gracefully stops the queue.
//
// After receiving of close signal at least workersMin number of workers will work so long as queue has items.
// Enqueue of new items to queue will forbid.
func (q *Queue) Close() error {
return q.close(false)
}
// ForceClose closes the queue and immediately stops all active and sleeping workers.
//
// Remaining items in the queue will throw to the trash.
func (q *Queue) ForceClose() error {
return q.close(true)
}
func (q *Queue) close(force bool) error {
if q.getStatus() == StatusClose {
return ErrQueueClosed
}
if q.l() != nil {
msg := "caught close signal"
if force {
msg = "caught force close signal"
}
q.l().Printf(msg)
}
// Set the status.
q.setStatus(StatusClose)
// Wait till all enqueue operations will finish.
for atomic.LoadInt64(&q.enqlock) > 0 {
}
if force {
// Immediately stop all active/sleeping workers.
q.mux.Lock()
for i := int(q.wmax - 1); i >= 0; i-- {
switch q.workers[i].getStatus() {
case WorkerStatusActive:
q.workers[i].signal(sigForceStop)
atomic.AddInt32(&q.workersUp, -1)
case WorkerStatusSleep:
q.workers[i].signal(sigForceStop)
}
}
q.mux.Unlock()
// Throw all remaining items to DLQ or trash.
for q.engine.size() > 0 {
itm, _ := q.engine.dequeue()
if q.CheckBit(flagLeaky) {
_ = q.c().DLQ.Enqueue(itm.payload)
q.mw().QueueLeak(LeakDirectionFront.String())
} else {
q.mw().QueueLost()
}
}
}
// Close the stream.
// Please note, this is not the end for regular close case. Workers continue works while queue has items.
return q.engine.close(force)
}
// Internal calibration helper.
func (q *Queue) calibrate(force bool) {
// Check calibration lock before mutex lock.
if atomic.LoadUint32(&q.c9nlock) == 1 {
// Calibration is busy.
return
}
q.mux.Lock()
defer func() {
// Release calibration lock.
atomic.StoreUint32(&q.c9nlock, 0)
q.mux.Unlock()
}()
// Calibration is acquired.
atomic.StoreUint32(&q.c9nlock, 1)
// Reset spinlock immediately to reduce amount of threads waiting for calibrate.
atomic.StoreInt64(&q.spinlock, 0)
rate := q.Rate()
if q.l() != nil {
msg := "calibrate: rate %f, workers %d"
if force {
msg = "force calibrate: rate %f, workers %d"
}
q.l().Printf(msg, rate, atomic.LoadInt32(&q.workersUp))
}
// Check and stop pre-sleeping workers.
for i := q.c().WorkersMax - 1; i >= q.c().WorkersMin; i-- {
if q.workers[i].getStatus() == WorkerStatusSleep && q.workers[i].sleptEnough() {
q.workers[i].signal(sigStop)
}
}
// Check schedID change.
var (
params realtimeParams
schedID int
)
if params, schedID = q.rtParams(); schedID != q.schedID {
q.schedID = schedID
if q.l() != nil {
q.l().Printf("switch to schedID %d (workers %d/%d, wakeup factor %f, sleep factor %f)",
schedID, params.WorkersMin, params.WorkersMax, params.WakeupFactor, params.SleepFactor)
}
// Stop all workers in range [workersMax...wmax].
// wmax is a number of maximum workers queue may have.
// workersMax is a maximum number of workers queue may have in current time range.
if q.wmax > params.WorkersMax {
for i := q.wmax - 1; i >= params.WorkersMax; i-- {
if q.workers[i].getStatus() == WorkerStatusActive {
q.workers[i].stop(true)
atomic.AddInt32(&q.workersUp, -1)
}
}
}
// Check new params.WorkersMin exceeds number of active workers.
if wu := uint32(q.getWorkersUp()); params.WorkersMin > wu {
// Start params.WorkersMin-workersUp workers to satisfy queue.
target := params.WorkersMin - wu
var c uint32
for i := uint32(0); i < q.wmax; i++ {
switch q.workers[i].getStatus() {
case WorkerStatusIdle:
q.workers[i].signal(sigInit)
go q.workers[i].await(q)
case WorkerStatusSleep:
q.workers[i].signal(sigWakeup)
default:
continue
}
c++
atomic.AddInt32(&q.workersUp, 1)
if c == target {
break
}
}
}
// Calculate actual numbers of active, sleeping and idle workers.
var active, sleep, idle uint
for i := uint32(0); i < params.WorkersMax; i++ {
switch q.workers[i].getStatus() {
case WorkerStatusIdle:
idle++
case WorkerStatusSleep:
sleep++
case WorkerStatusActive:
active++
}
}
// Reinitialize workers counters in metrics.
q.mw().WorkerSetup(active, sleep, idle)
}
// Calibration issues.
switch {
case rate == 0 && q.getStatus() == StatusClose:
// Queue is closed and empty. Force stops all active or sleeping workers.
for i := uint32(0); i < params.WorkersMax; i++ {
if ws := q.workers[i].getStatus(); ws == WorkerStatusActive || ws == WorkerStatusSleep {
q.workers[i].signal(sigForceStop)
}
}
case rate >= params.WakeupFactor:
// Queue fullness rate exceeds wakeupFactor. Need to start first available idle or sleeping worker.
if uint32(q.getWorkersUp()) == params.WorkersMax {
return
}
for i := params.WorkersMin; i < params.WorkersMax; i++ {
ws := q.workers[i].getStatus()
if ws == WorkerStatusActive {
continue
}
if ws == WorkerStatusIdle {
q.workers[i].signal(sigInit)
go q.workers[i].await(q)
} else {
q.workers[i].signal(sigWakeup)
}
atomic.AddInt32(&q.workersUp, 1)
// By default, only one worker starts at once. That's why need to keep heartbeat param enough small (<=1s).
break
}
case rate <= params.SleepFactor:
// Queue fullness rate fell less than sleep factor. So need to put worker(-s) to sleep.
if uint32(q.getWorkersUp()) == params.WorkersMin {
return
}
var target, c int32
// Workers put to sleep by chunks of workersUp / 2.
if target = q.getWorkersUp() / 2; target == 0 {
target = 1
}
// Check SleepThreshold to improve target.
if st := int32(q.c().SleepThreshold); st > 0 && st < target {
target = st
}
for i := params.WorkersMax - 1; i >= params.WorkersMin; i-- {
if q.workers[i].getStatus() == WorkerStatusActive {
q.workers[i].signal(sigSleep)
c++
if uint32(atomic.AddInt32(&q.workersUp, -1)) == params.WorkersMin || c == target {
break
}
}
}
case rate == 1:
// Queue is full and throttled.
q.setStatus(StatusThrottle)
default:
// Restore active status after throttle.
if q.getStatus() == StatusThrottle {
q.setStatus(StatusActive)
}
}
}
// Get number maximum workers that queue may contain considering all schedule rules and config params.
func (q *Queue) workersMaxDaily() uint32 {
sched, conf := uint32(0), q.c().WorkersMax
if q.c().Schedule != nil {
sched = q.c().Schedule.WorkersMaxDaily()
}
if sched > conf {
return sched
}
return conf
}
// Get realtime queue params according schedule rules.
func (q *Queue) rtParams() (params realtimeParams, schedID int) {
c := q.c()
if c.Schedule != nil {
var schedParams ScheduleParams
if schedParams, schedID = c.Schedule.Get(); schedID != -1 {
params = realtimeParams(schedParams)
if params.WakeupFactor == 0 {
params.WakeupFactor = c.WakeupFactor
}
if params.SleepFactor == 0 {
params.SleepFactor = c.SleepFactor
}
return
}
}
schedID = -1
params.WorkersMin = c.WorkersMin
params.WorkersMax = c.WorkersMax
params.WakeupFactor = c.WakeupFactor
params.SleepFactor = c.SleepFactor
return
}
// Get number of active workers.
func (q *Queue) getWorkersUp() int32 {
return atomic.LoadInt32(&q.workersUp)
}
// Set status of the queue.
func (q *Queue) setStatus(status Status) {
atomic.StoreUint32((*uint32)(&q.status), uint32(status))
}
// Get status of the queue.
func (q *Queue) getStatus() Status {
return Status(atomic.LoadUint32((*uint32)(&q.status)))
}
func (q *Queue) String() string {
var out = struct {
Capacity uint64 `json:"capacity"`
Workers uint32 `json:"workers"`
Heartbeat time.Duration `json:"heartbeat"`
WorkersMin uint32 `json:"workers_min"`
WorkersMax uint32 `json:"workers_max"`
WakeupFactor float32 `json:"wakeup_factor"`
SleepFactor float32 `json:"sleep_factor"`
Status string `json:"status"`
FullnessRate float32 `json:"fullness_rate"`
WorkersIdle int `json:"workers_idle"`
WorkersActive int `json:"workers_active"`
WorkersSleep int `json:"workers_sleep"`
}{}
out.Capacity = q.config.Capacity
out.Workers = q.config.Workers
out.Heartbeat = q.config.HeartbeatInterval
out.WorkersMin = q.config.WorkersMin
out.WorkersMax = q.config.WorkersMax
out.WakeupFactor = q.config.WakeupFactor
out.SleepFactor = q.config.SleepFactor
switch q.status {
case StatusNil:
out.Status = "inactive"
case StatusFail:
out.Status = "fail"
case StatusActive:
out.Status = "active"
case StatusThrottle:
out.Status = "throttle"
case StatusClose:
out.Status = "close"
}
out.FullnessRate = q.Rate()
for _, w := range q.workers {
if w == nil {
out.WorkersIdle++
} else {
switch w.getStatus() {
case WorkerStatusIdle:
out.WorkersIdle++
case WorkerStatusActive:
out.WorkersActive++
default:
out.WorkersSleep++
}
}
}
b, _ := json.Marshal(out)
return string(b)
}
func (q *Queue) Error() error {
return q.err
}
func (q *Queue) c() *Config {
return q.config
}
func (q *Queue) clk() Clock {
return q.config.Clock
}
func (q *Queue) mw() MetricsWriter {
return q.config.MetricsWriter
}
func (q *Queue) l() Logger {
return q.config.Logger
}
var _ = New