-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
339 lines (264 loc) · 6.47 KB
/
scheduler.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
package scheduler
import (
"context"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/hansmi/baamhackl/internal/prioqueue"
"github.com/jonboulle/clockwork"
)
var seqCounter int64
func nextSequenceNumber() int64 {
seq := atomic.AddInt64(&seqCounter, 1)
if seq == 0 {
return nextSequenceNumber()
}
return seq
}
type Scheduler struct {
mu sync.Mutex
slots int
// Tasks sorted by insertion order.
tasksByOrder prioqueue.PrioQueue
// Tasks sorted by due time.
tasksByTime prioqueue.PrioQueue
loopActiveCount int32
loopStopRequested bool
loopNotification event
loopFinished event
taskContext context.Context
taskContextCancel context.CancelFunc
taskActiveCount int
taskFinished event
}
// New creates a new scheduler instance. The slot count defaults to the number
// CPUs in the system.
func New() *Scheduler {
s := &Scheduler{
slots: runtime.NumCPU(),
tasksByOrder: prioqueue.PrioQueue{
Less: func(a, b any) bool {
lhs := a.(*Task)
rhs := b.(*Task)
return (lhs.seq - rhs.seq) < 0
},
},
tasksByTime: prioqueue.PrioQueue{
Less: func(a, b any) bool {
lhs := a.(*Task)
rhs := b.(*Task)
return lhs.nextAfter.Before(rhs.nextAfter)
},
},
loopNotification: newEvent(),
loopFinished: newEvent(),
taskFinished: newEvent(),
}
s.taskContext, s.taskContextCancel = context.WithCancel(context.Background())
return s
}
// SetSlots changes the number of tasks running concurrently.
func (s *Scheduler) SetSlots(count int) {
if count < 1 {
count = 1
}
s.mu.Lock()
s.slots = count
s.mu.Unlock()
s.loopNotification.Set()
}
// enqueue adds a task to the appropriate queue. If a due time is set the
// time-sorted queue is used, the insertion order queue otherwise.
func (s *Scheduler) enqueue(t *Task) {
if t.seq == 0 {
panic("task lacks a sequence number")
}
if !(t.seqItem == nil && t.timeItem == nil) {
panic("task already in queue")
}
if t.nextAfter.IsZero() {
t.seqItem = s.tasksByOrder.Push(t)
} else {
t.timeItem = s.tasksByTime.Push(t)
}
}
// ScheduleOption is a function configuring a task.
type ScheduleOption func(*Task)
// NextAfterDuration configures the task to only run after the given amount of
// wall time.
func NextAfterDuration(d time.Duration) ScheduleOption {
return func(t *Task) {
t.nextAfter = clock.Now().Add(d)
}
}
// Add a new task function to the scheduler. Unless configured otherwise
// through an option tasks are started in the order they're added.
func (s *Scheduler) Add(fn TaskFunc, opts ...ScheduleOption) {
if fn == nil {
panic("Function is nil")
}
t := &Task{
fn: fn,
seq: nextSequenceNumber(),
}
for _, opt := range opts {
opt(t)
}
s.mu.Lock()
s.enqueue(t)
s.mu.Unlock()
s.loopNotification.Set()
}
// Check whether there's a task waiting to be run. The task is removed from its
// queue. Returns nil if no task is ready to run. In that case the second
// return value is the amount of time to wait before the next task is due.
func (s *Scheduler) popNextLocked() (*Task, time.Duration) {
remaining := time.Duration(-1)
if nextByTime := s.tasksByTime.Peek(); nextByTime != nil {
remaining = nextByTime.(*Task).nextAfter.Sub(clock.Now())
if remaining <= 0 {
runtask := s.tasksByTime.Pop().(*Task)
runtask.timeItem = nil
return runtask, -1
}
}
var runtask *Task
if nextByOrder := s.tasksByOrder.Pop(); nextByOrder != nil {
runtask = nextByOrder.(*Task)
runtask.seqItem = nil
}
return runtask, remaining
}
func (s *Scheduler) loop() {
var nextTaskTimer clockwork.Timer
for {
s.mu.Lock()
if s.loopStopRequested {
s.mu.Unlock()
return
}
var runtask *Task
var nextTaskDueCh <-chan time.Time
if s.taskActiveCount < s.slots {
var remaining time.Duration
runtask, remaining = s.popNextLocked()
if remaining > 0 {
if nextTaskTimer == nil {
nextTaskTimer = clock.NewTimer(remaining)
} else {
if !nextTaskTimer.Stop() {
select {
case <-nextTaskTimer.Chan():
default:
}
}
nextTaskTimer.Reset(remaining)
}
nextTaskDueCh = nextTaskTimer.Chan()
}
}
if runtask == nil {
s.mu.Unlock()
// Wait for next task
select {
case <-nextTaskDueCh:
case <-s.loopNotification.Chan():
}
continue
}
// Launch new task
go func() {
s.taskActiveCount++
s.mu.Unlock()
s.runTask(runtask)
}()
}
}
func (s *Scheduler) runTask(t *Task) {
finished := t.run(s.taskContext)
s.mu.Lock()
s.taskActiveCount--
if !finished {
s.enqueue(t)
}
if s.tasksByOrder.Len() > 0 || s.tasksByTime.Len() > 0 {
s.loopNotification.Set()
}
s.mu.Unlock()
s.taskFinished.Set()
}
// Start launches the scheduler loop in a separate goroutine before returning.
// Tasks already in the queue will be run in the same order as they would be if
// they were added later.
func (s *Scheduler) Start() {
s.mu.Lock()
if s.loopStopRequested || s.loopActiveCount != 0 {
s.mu.Unlock()
panic("Scheduler may only start once")
}
go func() {
s.loopActiveCount++
s.mu.Unlock()
defer func() {
s.mu.Lock()
s.loopActiveCount--
s.mu.Unlock()
s.loopFinished.Set()
}()
s.loop()
}()
}
// Quiesce waits until all tasks have been run or the context is cancelled.
// Tasks are not affected on context cancellation. If tasks are added
// concurrently the behaviour is unspecified.
func (s *Scheduler) Quiesce(ctx context.Context) error {
s.loopNotification.Set()
for {
s.mu.Lock()
if s.taskActiveCount == 0 && s.tasksByOrder.Len() == 0 && s.tasksByTime.Len() == 0 {
s.mu.Unlock()
return nil
}
s.mu.Unlock()
select {
case <-s.loopFinished.Chan():
case <-s.taskFinished.Chan():
case <-ctx.Done():
return ctx.Err()
}
}
}
// Stop waits until currently running tasks have finished or the context is
// cancelled. The latter will also cancel the context for running tasks. Tasks
// not yet started will be abandoned.
func (s *Scheduler) Stop(ctx context.Context) error {
defer s.taskContextCancel()
s.mu.Lock()
s.loopStopRequested = true
s.mu.Unlock()
// Wake up loop so it can terminate
s.loopNotification.Set()
var err error
ctxDoneChan := ctx.Done()
for {
s.mu.Lock()
if s.loopActiveCount == 0 && s.taskActiveCount == 0 {
s.tasksByOrder.Clear()
s.tasksByTime.Clear()
s.mu.Unlock()
break
}
s.mu.Unlock()
select {
case <-s.loopFinished.Chan():
case <-s.taskFinished.Chan():
case <-ctxDoneChan:
err = ctx.Err()
ctxDoneChan = nil
// Cancel active tasks
s.taskContextCancel()
}
}
return err
}