-
Notifications
You must be signed in to change notification settings - Fork 23
/
wfq.go
468 lines (397 loc) · 12.4 KB
/
wfq.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
package scheduler
import (
"container/heap"
"container/list"
"context"
"fmt"
"strconv"
"sync"
"time"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
)
// Internal structure for tracking the request in the scheduler queue.
type queuedRequest struct {
fInfo *flowInfo
ready chan struct{} // Ready signal -- true = schedule, false = cancel/timeout
flowID string // Flow ID
vft float64 // Virtual finish time
cost float64 // Cost of the request (invPriority * tokens)
onHeap bool // Whether the request is on the heap or not
}
////////
// Memory pool for heapRequest(s).
var qRequestPool sync.Pool
func newHeapRequest() interface{} {
qRequest := new(queuedRequest)
qRequest.ready = make(chan struct{}, 1)
return qRequest
}
func getHeapRequest() *queuedRequest {
return qRequestPool.Get().(*queuedRequest)
}
func putHeapRequest(qRequest *queuedRequest) {
qRequestPool.Put(qRequest)
}
////////
type requestHeap []*queuedRequest
// make sure we implement the heap interface.
var _ heap.Interface = &requestHeap{}
// Len returns the number of heap requests in the scheduler queue.
func (h *requestHeap) Len() int {
return len(*h)
}
// Less compares heap requests by their virtual finish time.
// It's a min-heap -- i.e. requests with smallest vft are popped first.
// That's why we need to invert priority values - lower priority requests get larger vft values.
func (h *requestHeap) Less(i, j int) bool {
return (*h)[i].vft < (*h)[j].vft
}
// Swap swaps two heap requests in the scheduler queue.
func (h *requestHeap) Swap(i, j int) {
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
}
// Push appends a heap request to the scheduler queue.
func (h *requestHeap) Push(x interface{}) {
request := x.(*queuedRequest)
*h = append(*h, request)
}
// Pop removes the next heap request from the scheduler queue and returns the request.
func (h *requestHeap) Pop() interface{} {
old := *h
n := len(old)
request := old[n-1]
*h = old[0 : n-1]
old[n-1] = nil
return request
}
////////
type flowInfo struct {
queue *list.List
vt float64
refCnt int
requestOnHeap bool
}
var fInfoPool sync.Pool
func newFlowInfo() interface{} {
fInfo := new(flowInfo)
fInfo.queue = list.New()
return fInfo
}
func getFlowInfo() *flowInfo {
return fInfoPool.Get().(*flowInfo)
}
func putFlowInfo(fInfo *flowInfo) {
fInfoPool.Put(fInfo)
}
////////
func init() {
qRequestPool.New = newHeapRequest
fInfoPool.New = newFlowInfo
}
////////
// WFQScheduler : Weighted Fair Queue Scheduler.
type WFQScheduler struct {
clk clockwork.Clock
lastAccessTime time.Time
manager TokenManager
// metrics
metrics *WFQMetrics
// flows
flows map[string]*flowInfo
requests requestHeap
vt float64 // virtual time
// generation helps close the queue in face of concurrent requests leaving the queue while new requests also arrive.
generation uint64
lock sync.Mutex
queueOpen bool // This tracks overload state
}
// NewWFQScheduler creates a new weighted fair queue scheduler.
func NewWFQScheduler(clk clockwork.Clock, tokenManger TokenManager, metrics *WFQMetrics) Scheduler {
sched := new(WFQScheduler)
sched.queueOpen = false
sched.generation = 0
sched.clk = clk
sched.lastAccessTime = sched.clk.Now()
sched.vt = 0
sched.flows = make(map[string]*flowInfo)
sched.manager = tokenManger
if metrics != nil {
sched.metrics = metrics
}
return sched
}
func (sched *WFQScheduler) updateMetricsAndReturnDecision(accepted bool, request *Request) bool {
if accepted {
sched.metrics.AcceptedTokensCounter.Add(float64(request.Tokens) / 1000)
}
sched.metrics.IncomingTokensCounter.Add(float64(request.Tokens) / 1000)
return accepted
}
// Schedule blocks until the request is scheduled or until timeout.
// Return value - true: Accept, false: Reject.
func (sched *WFQScheduler) Schedule(ctx context.Context, request *Request) (accepted bool) {
if request.Tokens == 0 {
return sched.updateMetricsAndReturnDecision(true, request)
}
sched.lock.Lock()
queueOpen := sched.queueOpen
sched.lastAccessTime = sched.clk.Now()
sched.lock.Unlock()
if sched.manager.PreprocessRequest(ctx, request) {
return sched.updateMetricsAndReturnDecision(true, request)
}
// try to schedule right now
if !queueOpen {
ok := sched.manager.TakeIfAvailable(ctx, float64(request.Tokens))
if ok {
// we got the tokens, no need to queue
return sched.updateMetricsAndReturnDecision(true, request)
}
}
// Unable to schedule right now, so queue the request
qRequest := sched.queueRequest(ctx, request)
// scheduler is in overload situation and we have to wait for ready signal and tokens
select {
case <-qRequest.ready:
return sched.updateMetricsAndReturnDecision(sched.scheduleRequest(ctx, request, qRequest), request)
case <-ctx.Done():
sched.cancelRequest(qRequest)
return sched.updateMetricsAndReturnDecision(false, request)
}
}
// Construct FlowID by appending RequestLabel and Priority.
func (sched *WFQScheduler) flowID(fairnessLabel string, priority float64, generation uint64) string {
return fmt.Sprintf("%s_%s_%d", fairnessLabel, strconv.FormatFloat(priority, 'f', -1, 64), generation)
}
// Attempt to queue this request.
//
// Returns whether request was admitted right away without queueing.
// If admitted == false, might return a valid heapRequest
// If admitted == false and qRequest == nil, request was neither admitted nor
// queued (rejected right away).
func (sched *WFQScheduler) queueRequest(ctx context.Context, request *Request) (qRequest *queuedRequest) {
sched.lock.Lock()
defer sched.lock.Unlock()
firstRequest := false
// check if this is the first request entering this queue
if !sched.queueOpen {
firstRequest = true
sched.queueOpen = true
// reset sched virtual time
sched.vt = 0
}
// Proceed to queueing
qRequest = getHeapRequest()
flowID := sched.flowID(request.FairnessLabel, request.InvPriority, sched.generation)
qRequest.flowID = flowID
cost := float64(request.Tokens) * request.InvPriority
// Get FlowInfo
fInfo, ok := sched.flows[flowID]
if !ok {
fInfo = getFlowInfo()
sched.setFlowsGauge(float64(len(sched.flows)))
fInfo.vt = sched.vt
sched.flows[flowID] = fInfo
}
// Increment reference counter
fInfo.refCnt++
// Store flowInfo pointer in the request
qRequest.fInfo = fInfo
// Store the cost of the request
qRequest.cost = cost
if !firstRequest {
if !fInfo.requestOnHeap {
qRequest.vft = fInfo.vt + cost
qRequest.onHeap = true
heap.Push(&sched.requests, qRequest)
sched.setRequestsGauge(float64(sched.requests.Len()))
fInfo.requestOnHeap = true
} else {
// push to flow queue
fInfo.queue.PushBack(qRequest)
}
} else {
// This is the only request in queue at this time, wake it up
qRequest.ready <- struct{}{}
}
return qRequest
}
// adjust queue counters. Note: qRequest pointer should not be used after calling this function as it will get recycled via Pool.
func (sched *WFQScheduler) scheduleRequest(ctx context.Context, request *Request, qRequest *queuedRequest) (allowed bool) {
// This request has been selected to be executed next
waitTime, allowed := sched.manager.Take(ctx, float64(request.Tokens))
// check if we need to wait
if allowed && waitTime > 0 {
// check whether ctx has deadline
// and if deadline is less than waitTime
// return tokens immediately
if dl, o := ctx.Deadline(); o {
if dl.Sub(sched.clk.Now()) < waitTime {
allowed = false
returnCtx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
go func(cancel context.CancelFunc) {
defer cancel()
sched.manager.Return(returnCtx, float64(request.Tokens))
}(cancel)
}
}
if allowed {
timer := sched.clk.NewTimer(waitTime)
defer timer.Stop()
select {
case <-ctx.Done():
allowed = false
returnCtx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
// return the tokens
go func(cancel context.CancelFunc) {
defer cancel()
sched.manager.Return(returnCtx, float64(request.Tokens))
}(cancel)
case <-timer.Chan():
}
}
}
sched.lock.Lock()
defer sched.lock.Unlock()
if allowed {
// move the flow's VT forward
qRequest.fInfo.vt += qRequest.cost
// set new virtual time of scheduler
sched.vt = qRequest.fInfo.vt
}
// This request is responsible for waking up the next request
sched.wakeNextRequest(qRequest.fInfo)
sched.cleanup(qRequest)
return
}
func (sched *WFQScheduler) wakeNextRequest(fInfo *flowInfo) {
// load next request for this flow onto the heap
if !fInfo.requestOnHeap {
elm := fInfo.queue.Front()
if elm != nil {
fInfo.queue.Remove(elm)
nextReq := elm.Value.(*queuedRequest)
nextReq.vft = fInfo.vt + nextReq.cost
heap.Push(&sched.requests, nextReq)
nextReq.onHeap = true
fInfo.requestOnHeap = true
sched.setRequestsGauge(float64(sched.requests.Len()))
}
}
// no more requests?
if sched.requests.Len() == 0 {
// close the queue
sched.generation++
sched.queueOpen = false
return
}
// Pop from queue and wake next request
qRequest := heap.Pop(&sched.requests).(*queuedRequest)
sched.setRequestsGauge(float64(sched.requests.Len()))
qRequest.onHeap = false
qRequest.fInfo.requestOnHeap = false
// wake up this request
qRequest.ready <- struct{}{}
}
func (sched *WFQScheduler) cancelRequest(qRequest *queuedRequest) {
sched.lock.Lock()
defer sched.lock.Unlock()
select {
case <-qRequest.ready:
// This request is responsible for waking up the next request
sched.wakeNextRequest(qRequest.fInfo)
default:
// remove from heap
if qRequest.onHeap {
qRequest.onHeap = false
for i := 0; i < sched.requests.Len(); i++ {
if sched.requests[i] == qRequest {
// replace with the next request in the flow
elm := qRequest.fInfo.queue.Front()
if elm != nil {
qRequest.fInfo.queue.Remove(elm)
nextReq := elm.Value.(*queuedRequest)
nextReq.vft = qRequest.fInfo.vt + nextReq.cost
sched.requests[i] = nextReq
nextReq.onHeap = true
qRequest.fInfo.requestOnHeap = true
} else {
// swap with the last element in the heap
sched.requests.Swap(i, sched.requests.Len()-1)
// trim the last element from the slice
sched.requests = sched.requests[:sched.requests.Len()-1]
qRequest.fInfo.requestOnHeap = false
}
// Fix the heap
if i < sched.requests.Len() {
heap.Fix(&sched.requests, i)
}
break
}
}
} else {
// search within the flow queue
var next *list.Element
for elm := qRequest.fInfo.queue.Front(); elm != nil; elm = next {
request := elm.Value.(*queuedRequest)
next = elm.Next()
if request == qRequest {
qRequest.fInfo.queue.Remove(elm)
break
}
}
}
}
sched.cleanup(qRequest)
}
// queueRequest is going to be recycled and must not be used
// after calling this function.
func (sched *WFQScheduler) cleanup(qRequest *queuedRequest) {
// decrement reference counter
qRequest.fInfo.refCnt--
// check if the flow is empty
if qRequest.fInfo.refCnt == 0 {
// delete the flow
delete(sched.flows, qRequest.flowID)
// send flowInfo back to the Pool
putFlowInfo(qRequest.fInfo)
sched.setFlowsGauge(float64(len(sched.flows)))
}
putHeapRequest(qRequest)
}
// Revert returns tokens to the token bucket.
func (sched *WFQScheduler) Revert(ctx context.Context, tokens uint64) {
sched.manager.Return(ctx, float64(tokens))
}
func (sched *WFQScheduler) setFlowsGauge(v float64) {
if sched.metrics != nil && sched.metrics.FlowsGauge != nil {
sched.metrics.FlowsGauge.Set(v)
}
}
func (sched *WFQScheduler) setRequestsGauge(v float64) {
if sched.metrics != nil && sched.metrics.HeapRequestsGauge != nil {
sched.metrics.HeapRequestsGauge.Set(v)
}
}
// Info returns the last access time and number of requests that are currently in the queue.
func (sched *WFQScheduler) Info() (time.Time, int) {
sched.lock.Lock()
defer sched.lock.Unlock()
return sched.lastAccessTime, sched.requests.Len()
}
// GetPendingFlows returns the number of flows in the scheduler.
func (sched *WFQScheduler) GetPendingFlows() int {
return len(sched.flows)
}
// GetPendingRequests returns the number of requests in the scheduler.
func (sched *WFQScheduler) GetPendingRequests() int {
return len(sched.requests)
}
// WFQMetrics holds metrics related to internal workings of WFQScheduler.
type WFQMetrics struct {
FlowsGauge prometheus.Gauge
HeapRequestsGauge prometheus.Gauge
IncomingTokensCounter prometheus.Counter
AcceptedTokensCounter prometheus.Counter
}