/
reactor.go
737 lines (666 loc) · 17.2 KB
/
reactor.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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
package reactor
import (
"context"
"errors"
"fmt"
"github.com/moontrade/kirana/pkg/counter"
"github.com/moontrade/kirana/pkg/hashmap"
"github.com/moontrade/kirana/pkg/mpmc"
"github.com/moontrade/kirana/pkg/pmath"
"github.com/moontrade/kirana/pkg/runtimex"
"github.com/moontrade/kirana/pkg/timex"
"github.com/moontrade/kirana/pkg/util"
"github.com/moontrade/kirana/pkg/wyhash"
"github.com/panjf2000/ants"
"math"
"runtime"
"sync"
"sync/atomic"
"time"
)
type Stats struct {
ticks counter.Counter
ticksDur counter.TimeCounter
ticksDurMin counter.Counter
ticksDurMax counter.Counter
spawns counter.Counter
spawnsDur counter.TimeCounter
wakes counter.Counter
wakesDur counter.TimeCounter
wakeLists counter.Counter
wakesListsDur counter.TimeCounter
wakeListsWakes counter.Counter
wakesListsWakesDur counter.TimeCounter
wakeListsInvokes counter.Counter
invokes counter.Counter
invokesDur counter.TimeCounter
flushes counter.Counter
flushesDur counter.TimeCounter
skew counter.Counter
skewDur counter.TimeCounter
droppedDur counter.TimeCounter
level1Ticks counter.Counter
level1TicksDur counter.TimeCounter
level1TicksDurMin counter.Counter
level1TicksDurMax counter.Counter
level2Ticks counter.Counter
level2TicksDur counter.TimeCounter
level2TicksDurMin counter.Counter
level2TicksDurMax counter.Counter
level3Ticks counter.Counter
level3TicksDur counter.TimeCounter
level3TicksDurMin counter.Counter
level3TicksDurMax counter.Counter
pidSwitches counter.Counter
}
type Runnable interface{}
var (
ErrQueueFull = errors.New("queue full")
ErrStop = errors.New("stop")
)
const (
DefaultInvokeQueueSize = 1024 * 1
DefaultWakeQueueSize = 1024 * 1
DefaultSpawnQueueSize = 1024 * 1
)
type Config struct {
Name string
Level1Wheel Wheel
Level2Wheel Wheel
Level3Wheel Wheel
InvokeQSize int
WakeQSize int
SpawnQSize int
LockOSThread bool
}
// Reactor runs all tasks on a single goroutine. It has an optimized timing mechanism
// with a fixed tickDur duration and a fixed interval duration. The interval is broken down
// into slots for better resource allocation. For example, a tickDur duration of 4ms with 5
// slots gives an interval duration of 20ms with each 4ms handling ~20% of the load. The
// timing is constantly adjusting to ensure the tickDur duration is accurate from the start
// adjusting for CPU Time.
// In addition, there is a lock-free MPMC queue that accepts invokes to run immediately
// without having to wait for a Tick.
type Reactor struct {
Stats
id int
now int64
size counter.Counter
//currentTick counter.Counter
idCounter counter.Counter
state int64
config Config
wakeQ *mpmc.BoundedWake[Task]
wakeListQ *mpmc.BoundedWake[WakeList]
spawnQ *mpmc.BoundedWake[Task]
invokeQ *mpmc.BoundedWake[func()]
timer chan Tick
lastTick int64
currentTick int64
tickWheel Wheel
level2Wheel Wheel
level3Wheel Wheel
tickDur time.Duration
ticksPerLevel2 int64
ticksPerLevel3 int64
tasks *hashmap.SyncMap[int64, *Task]
ctx context.Context
cancel context.CancelFunc
tickCount counter.Counter
nextTick counter.Counter
wakeCh chan int64
pid int32
gid uint64
wg sync.WaitGroup
}
func NewReactor(config Config) (*Reactor, error) {
if config.Name == "" {
config.Name = "loop"
}
if config.InvokeQSize <= 4 {
config.InvokeQSize = DefaultInvokeQueueSize
}
if config.WakeQSize <= 4 {
config.WakeQSize = DefaultWakeQueueSize
}
if config.SpawnQSize <= 4 {
config.SpawnQSize = DefaultSpawnQueueSize
}
config.InvokeQSize = pmath.CeilToPowerOf2(config.InvokeQSize)
config.WakeQSize = pmath.CeilToPowerOf2(config.WakeQSize)
config.SpawnQSize = pmath.CeilToPowerOf2(config.SpawnQSize)
if len(config.Level1Wheel.durations) == 0 {
config.Level1Wheel = NewWheel(Millis250)
}
if len(config.Level2Wheel.durations) == 0 {
config.Level2Wheel = NewWheel(Seconds)
}
if len(config.Level3Wheel.durations) == 0 {
config.Level3Wheel = NewWheel(Minutes)
}
if config.Level2Wheel.tickDur%config.Level1Wheel.tickDur != 0 {
return nil, fmt.Errorf("seconds Tick not evenly divisible by millisecond Tick: %s mod %s = %s",
config.Level2Wheel.tickDur, config.Level1Wheel.tickDur, config.Level2Wheel.tickDur%config.Level1Wheel.tickDur)
}
if config.Level3Wheel.tickDur%config.Level1Wheel.tickDur != 0 {
return nil, fmt.Errorf("minutes Tick not evenly divisible by millisecond Tick: %s mod %s = %s",
config.Level3Wheel.tickDur, config.Level1Wheel.tickDur, config.Level3Wheel.tickDur%config.Level1Wheel.tickDur)
}
wakeCh := make(chan int64, 1)
ctx, cancel := context.WithCancel(context.Background())
w := &Reactor{
config: config,
tickDur: config.Level1Wheel.tickDur,
tickWheel: config.Level1Wheel,
level2Wheel: config.Level2Wheel,
ticksPerLevel2: int64(config.Level2Wheel.tickDur / config.Level1Wheel.tickDur),
level3Wheel: config.Level3Wheel,
ticksPerLevel3: int64(config.Level3Wheel.tickDur / config.Level1Wheel.tickDur),
wakeCh: wakeCh,
tasks: hashmap.NewSyncMap[int64, *Task](8, 1024, wyhash.Int64),
wakeQ: mpmc.NewBoundedWake[Task](int64(config.WakeQSize), wakeCh),
wakeListQ: mpmc.NewBoundedWake[WakeList](int64(config.WakeQSize), wakeCh),
spawnQ: mpmc.NewBoundedWake[Task](int64(config.SpawnQSize), wakeCh),
invokeQ: mpmc.NewBoundedWake[func()](int64(config.InvokeQSize), wakeCh),
timer: make(chan Tick, 1),
ctx: ctx,
cancel: cancel,
}
w.id = reactors.AppendIndex(w)
return w, nil
}
func (r *Reactor) CheckGID() bool {
return r.gid == runtimex.GoroutineID()
}
func (r *Reactor) ID() int { return r.id }
func (r *Reactor) Now() int64 { return r.now }
func (r *Reactor) SnapshotStats() Stats {
return r.Stats
}
func (r *Reactor) Start() {
if !atomic.CompareAndSwapInt64(&r.state, 0, 1) {
return
}
go r.run()
}
func (r *Reactor) Duration(ticks int64) time.Duration {
return r.tickDur * time.Duration(ticks)
}
func (r *Reactor) Ticks(duration time.Duration) int64 {
if duration <= 0 {
return 0
}
return int64(duration) / int64(r.tickDur)
}
func (r *Reactor) Wake(task *Task) error {
if task == nil {
return errors.New("task is nil")
}
reactor := task.reactor
if reactor == nil {
return errors.New("task is not scheduled")
}
if reactor != r {
return reactor.Wake(task)
}
r.wakeQ.Enqueue(task)
return nil
}
func (r *Reactor) WakeAfter(task *Task, after time.Duration) error {
if task == nil {
return errors.New("task is nil")
}
if after <= 0 {
return r.Wake(task)
}
reactor := task.reactor
if reactor == nil {
return errors.New("task is not scheduled")
}
task.wakeAfter = after
if reactor != r {
return reactor.Wake(task)
}
if !r.wakeQ.Enqueue(task) {
return ErrQueueFull
} else {
return nil
}
}
func (r *Reactor) wakeList(list *WakeList) error {
if list == nil {
return errors.New("nil slots")
}
if list.Len() == 0 {
return nil
}
if list.reactor != r {
return list.reactor.wakeList(list)
}
if !r.wakeListQ.Enqueue(list) {
return ErrQueueFull
} else {
return nil
}
}
func (r *Reactor) Invoke(fn func()) bool {
if fn == nil {
return false
}
return r.invokeQ.EnqueueUnsafeTimeout(runtimex.FuncToPointer(fn), time.Second*5)
}
func (r *Reactor) InvokeRef(fn *func()) bool {
if fn == nil {
return false
}
return r.invokeQ.EnqueueUnsafe(runtimex.FuncToPointer(*fn))
}
func (r *Reactor) InvokeBlocking(fn func()) bool {
if fn == nil {
return false
}
return EnqueueBlocking(fn)
}
func (r *Reactor) Spawn(future Future) (*Task, error) {
if future == nil {
return nil, errors.New("nil future")
}
task := taskPool.Get()
task.init(r.idCounter.Incr(), r, future)
if provider, ok := future.(FutureTask); ok {
provider.SetTask(task)
}
if !r.spawnQ.Enqueue(task) {
return nil, ErrQueueFull
}
return task, nil
}
func (r *Reactor) SpawnInterval(future Future, interval time.Duration) (*Task, error) {
if future == nil {
return nil, errors.New("nil future")
}
if interval < 0 {
interval = 0
}
task := taskPool.Get()
task.init(r.idCounter.Incr(), r, future)
task.interval = interval
if provider, ok := future.(FutureTask); ok {
provider.SetTask(task)
}
if !r.spawnQ.Enqueue(task) {
return nil, ErrQueueFull
}
return task, nil
}
func (r *Reactor) SpawnWorkerFn(fn func()) error {
return ants.Submit(fn)
}
func (r *Reactor) run() {
defer func() {
e := recover()
if e != nil {
//logger.Error(util.PanicToError(e))
}
}()
if r.config.LockOSThread {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
r.gid, r.pid = runtimex.GIDPID()
tick, err := initTicker(r.tickDur).Register(r.tickDur, r, r.wakeCh)
if err != nil {
panic(err)
}
defer func() {
_ = tick.Close()
}()
for {
select {
case v := <-r.wakeCh:
r.onWakeMessage(v)
}
}
}
func (r *Reactor) onWakeMessage(v int64) {
r.maybeProcessTick(v)
r.now = timex.NanoTime()
for r.flushQueues() > 0 {
r.now = timex.NanoTime()
}
}
func (r *Reactor) maybeProcessTick(v int64) {
if v < 1 || v == r.lastTick {
return
}
r.currentTick = v
if r.lastTick < v-1 {
r.catchup(r.lastTick, r.currentTick)
}
r.lastTick = r.currentTick
r.processTick(r.currentTick)
}
func (r *Reactor) processTick(tick int64) {
interval := int64(r.tickDur)
start := timex.NanoTime()
begin := start
r.tick(tick, begin)
end := timex.NanoTime()
elapsed := end - begin
// Stats
r.ticks.Incr()
r.ticksDur.Add(elapsed)
if r.ticksDurMin == 0 || r.ticksDurMin.Load() > elapsed {
r.ticksDurMin.Store(elapsed)
}
if r.ticksDurMax.Load() < elapsed {
r.ticksDurMax.Store(elapsed)
}
begin = end
r.flushQueues()
end = timex.NanoTime()
r.flushesDur.Add(end - begin)
elapsed = end - start
r.flushes.Add(1)
if elapsed > interval {
r.skew.Incr()
r.skewDur.Add(elapsed)
r.rebalance()
}
}
func (r *Reactor) onSpawn(task *Task) {
r.pollStart(r.now, task)
}
func (r *Reactor) onWake(task *Task) {
r.pollWake(r.now, task)
}
func (r *Reactor) onWakeList(list *WakeList) {
r.pollWakeList(r.now, list)
}
func (r *Reactor) onFn(task func()) {
r.invoke(task)
}
func (r *Reactor) flushQueues() int {
total := 0
if !r.wakeListQ.IsEmpty() {
count := r.wakeListQ.DequeueMany(math.MaxUint32, r.onWakeList)
total += count
r.wakeLists.Add(int64(count))
}
if !r.invokeQ.IsEmpty() {
count := r.invokeQ.DequeueManyDeref(math.MaxUint32, r.onFn)
total += count
r.invokes.Add(int64(count))
}
if !r.wakeQ.IsEmpty() {
count := r.wakeQ.DequeueMany(math.MaxUint32, r.onWake)
total += count
r.wakes.Add(int64(count))
}
if !r.spawnQ.IsEmpty() {
count := r.spawnQ.DequeueMany(math.MaxUint32, r.onSpawn)
total += count
r.spawns.Add(int64(count))
}
return total
}
func (r *Reactor) catchup(lastTick, currentTick int64) {
//logger.Warn("skew detected of %d ticks", currentTick-1-lastTick)
println("skew detected of %d ticks", currentTick-1-lastTick)
//logger.Warn("catching up...")
now := timex.NanoTime()
for nextTick := lastTick + 1; nextTick <= currentTick; nextTick++ {
r.tick(nextTick, now)
}
}
func (r *Reactor) rebalance() {
//logger.Warn("rebalancing...")
//logger.Warn("rebalanced")
}
func (r *Reactor) tick(tick int64, now int64) {
r.tickWheel.tick(now, r.onTick)
if tick%r.ticksPerLevel2 == 0 {
//logger.Debug("level 2 wheel Tick", tick)
r.level2Wheel.tick(now, r.onTick)
}
if tick%r.ticksPerLevel3 == 0 {
//logger.Debug("level 3 wheel Tick")
r.level3Wheel.tick(now, r.onTick)
}
}
func (r *Reactor) onTick(now int64, list *taskSwapList, slot *taskSwapSlot, task *Task) bool {
if slot.wake {
r.pollWake(now, task)
return false
}
return r.pollInterval(now, list, task)
}
func (r *Reactor) schedule(task *Task, delay time.Duration, wake bool) {
if delay < r.tickWheel.maxDur && r.tickWheel.tickDur > 0 {
r.tickWheel.schedule(task, delay, wake)
} else if delay < r.level2Wheel.maxDur && r.level2Wheel.tickDur > 0 {
r.level2Wheel.schedule(task, delay, wake)
} else if delay < r.level3Wheel.maxDur && r.level3Wheel.tickDur > 0 {
r.level3Wheel.schedule(task, delay, wake)
}
}
func (r *Reactor) stopTask(time int64, task *Task) {
defer func() {
task.remove()
if e := recover(); e != nil {
err := util.PanicToError(e)
_ = err
//logger.Error(err, "Reactor.invoke panic")
}
}()
_, ok := r.tasks.Delete(task.id)
if !ok {
return
}
task.stop = true
task.clearSlots()
if pc, ok := task.future.(PollClose); ok {
err := pc.PollClose(CloseEvent{
Task: task,
Time: time,
Reason: nil,
})
if err != nil {
//logger.Warn(err)
}
}
}
func (r *Reactor) invoke(fn func()) {
defer func() {
if e := recover(); e != nil {
err := util.PanicToError(e)
_ = err
//logger.Error(err, "Reactor.invoke panic")
}
}()
if fn != nil {
fn()
}
}
func (r *Reactor) pollStart(now int64, task *Task) {
defer func() {
if e := recover(); e != nil {
err := util.PanicToError(e)
_ = err
//logger.Error(err, "Reactor.pollInvoke panic")
}
}()
task.started = now
err := task.future.Poll(Context{
Task: task,
Time: now,
Reason: ReasonStart,
})
if err != nil {
if err == ErrStop {
task.stop = true
} else {
//logger.Warn(err)
}
}
if task.stop {
r.stopTask(now, task)
return
}
r.tasks.Put(task.id, task)
if task.wakeAfter > 0 {
r.schedule(task, task.wakeAfter, true)
}
if task.interval > 0 {
r.schedule(task, task.interval, false)
}
}
func (r *Reactor) pollWakeList(now int64, list *WakeList) {
//for list.onWake(now) > 0 {
list.onWake(now)
count := list.wakes.wake(now, r.onTaskSlotWake)
r.wakeListsWakes.Add(count)
count = list.funcs.wake(now, r.onFuncSlotWake)
r.wakeListsInvokes.Add(count)
//}
}
func (r *Reactor) onTaskSlotWake(slot *TaskSlot) {
t := slot.task
if t == nil {
return
}
r.pollWake(r.now, t)
}
func (r *Reactor) onFuncSlotWake(slot *FuncSlot) {
fn := slot.Value
if fn == nil {
return
}
r.invoke(fn)
}
func (r *Reactor) pollWake(now int64, task *Task) {
if task.stop {
return
}
defer func() {
if e := recover(); e != nil {
err := util.PanicToError(e)
_ = err
//logger.Error(err, "Reactor.pollInvoke panic")
}
}()
interval := task.interval
wakeAfter := task.wakeAfter
if wakeAfter > 0 {
task.wakeAfter = 0
r.schedule(task, wakeAfter, true)
return
}
task.wakes++
err := task.future.Poll(Context{
Task: task,
Time: now,
Reason: ReasonWake,
})
if err != nil {
if err == ErrStop {
task.stop = true
} else {
//logger.Warn(err)
}
}
if task.stop {
r.stopTask(now, task)
return
}
newWakeAfter := task.wakeAfter
if newWakeAfter != wakeAfter {
if newWakeAfter > 0 {
r.schedule(task, newWakeAfter, true)
}
}
nextInterval := task.interval
// Interval change requested?
if nextInterval != interval {
if nextInterval <= 0 {
task.interval = 0
}
}
}
func (r *Reactor) pollInterval(now int64, list *taskSwapList, task *Task) bool {
if task.stop || task.interval == 0 || list.dur != task.interval {
// remove
return false
}
defer func() {
if e := recover(); e != nil {
err := util.PanicToError(e)
_ = err
//logger.Error(err, "Reactor.pollInterval panic")
}
}()
interval := task.interval
wakeAfter := task.wakeAfter
task.intervals++
err := task.future.Poll(Context{
Task: task,
Time: now,
Interval: interval,
Reason: ReasonInterval,
})
if err != nil {
if err == ErrStop {
task.stop = true
} else {
//logger.Warn(err)
}
}
if task.stop {
r.stopTask(now, task)
// remove
return false
}
newWakeAfter := task.wakeAfter
if newWakeAfter != wakeAfter {
if newWakeAfter > 0 {
r.schedule(task, newWakeAfter, true)
}
}
// Interval change requested?
nextInterval := task.interval
if nextInterval != interval {
if nextInterval <= 0 {
task.interval = 0
return false
}
// Schedule new interval
r.schedule(task, nextInterval, false)
// Remove from this taskSwapList
return false
}
return true
}
func (r *Reactor) Print() {
avg := time.Duration(r.ticksDur.Load()) / time.Duration(r.currentTick)
fmt.Println("Size ", r.size.Load())
fmt.Println("ProcessorID ", r.pid)
fmt.Println("ProcessorID Switches ", r.pidSwitches.Load())
//fmt.Println("Capacity ", r.cap)
fmt.Println("Ticks ", r.currentTick)
//fmt.Println("Ticks Dur ", Time.Duration(r.ticksDur.Load()))
fmt.Println("Tick Avg Dur ", time.Duration(r.ticksDur.Load())/time.Duration(r.currentTick))
//fmt.Println("Skew ", r.skew.Load())
//fmt.Println("Skew Dur ", Time.Duration(r.skewDur.Load()))
//fmt.Println("Dropped Dur ", r.droppedDur.Load())
////fmt.Println("Ticks Dur ", Time.Duration(r.ticksDur.Load()))
//fmt.Println("Jobs ", r.invokes.Load())
////fmt.Println("Ticks Dur ", Time.Duration(r.ticksDur.Load()))
//fmt.Println("Jobs Avg Dur ", Time.Duration(r.invokesDur.Load())/Time.Duration(r.invokes.Load()))
//fmt.Println("Interval ", r.tickDur)
fmt.Println("Tick CPU ", float64(avg)/float64(r.tickDur))
fmt.Println("Min ", time.Duration(r.ticksDurMin.Load()))
fmt.Println("Max ", time.Duration(r.ticksDurMax.Load()))
//for i, slots := range r.tickWheel {
// fmt.Println("Ring: ", i, " Size: ", slots.activeSize)
//}
}