-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient.go
278 lines (235 loc) · 6.42 KB
/
gradient.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
package conc
import (
"context"
"log"
"math"
"math/rand"
"sync"
"time"
)
const DefaultMaxConcurrency = 20
type GradientOpts func(*GradientController)
func WithRTTTolerance(rttt float64) GradientOpts {
return func(g *GradientController) {
g.rttTolerance = rttt
}
}
func WithSmoothing(s float64) GradientOpts {
return func(g *GradientController) {
g.smoothing = s
}
}
func WithQueueSize(q func(uint) uint) GradientOpts {
return func(g *GradientController) {
g.queueSize = q
}
}
func WithProbeInterval(i uint) GradientOpts {
return func(g *GradientController) {
g.probeInterval = i
}
}
// WithBackoffRatio sets
func WithBackoffRatio(b float64) GradientOpts {
return func(g *GradientController) {
g.backoffRatio = b
}
}
func WithInitialLimit(b uint) GradientOpts {
return func(g *GradientController) {
g.initial = b
}
}
func WithMinLimit(b uint) GradientOpts {
return func(g *GradientController) {
g.min = b
}
}
func WithMaxLimit(b uint) GradientOpts {
return func(g *GradientController) {
g.max = b
}
}
func WithRandomSource(s rand.Source) GradientOpts {
return func(g *GradientController) {
g.rand = rand.New(s)
}
}
const highlyRandomInt = 42
// NewGradientController creates a new GradientController. Call the Start()
// method to make it run. Once done, make sure to call Stop() to clear upp
// resources. After stopped, the controller can be started again if you want
// to.
func NewGradientController(n Notifier, pool *WorkerPool, opts ...GradientOpts) *GradientController {
c := &GradientController{
notif: n,
pool: pool,
initial: 1,
min: 1,
max: DefaultMaxConcurrency,
rttTolerance: 2.0,
smoothing: 0.2,
queueSize: sqrt,
probeInterval: 1000,
backoffRatio: 0.9,
rand: rand.New(rand.NewSource(highlyRandomInt)),
}
for _, o := range opts {
o(c)
}
// Check argument invariants.
if c.min > c.max {
panic("min can't be greater than max.")
}
if c.initial < c.min {
panic("initial can't be less than min.")
}
if c.initial > c.max {
panic("initial can't be greater than max.")
}
return c
}
// GradientController delegates concurrency limits to SimplifiedController,
// adding basic limits such as minimum and maximum concurrency.
type GradientController struct {
notif Notifier
pool *WorkerPool
initial uint
min uint
max uint
// Inspired by [1].
//
// [1] https://github.com/Netflix/concurrency-limits/blob/18692b09e55a0574bea94d92e95a03c3e89012d2/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/GradientLimit.java
rttTolerance float64
smoothing float64
queueSize func(uint) uint
probeInterval uint
backoffRatio float64
rand *rand.Rand
// Variables that are modified by the control loop.
//
// TODO: Refactor the update method out into a separate type to avoid these variables bloating this type.
resetRTTCounter uint
resetNoLoadRTT bool
noLoadRTT time.Duration
// The following variables are instantiated at start.
quitChan chan struct{}
wg sync.WaitGroup
}
func (c *GradientController) Start() {
c.quitChan = make(chan struct{})
c.wg = sync.WaitGroup{}
c.wg.Add(1)
go func() {
defer c.wg.Done()
c.pool.Incr(c.initial)
c.resetRTTCounter = c.nextResetCounter()
c.resetNoLoadRTT = true
for {
select {
case <-c.quitChan:
return
case r := <-c.notif.NotifyChan():
c.adjust(c.update(r), true)
case <-c.notif.NoWorkChan():
// TODO: Can this be done in a better way? Are we shutting down
// worker goroutines too fast or too slow?
if newLimit := c.pool.WantedN() - 1; newLimit >= c.min {
c.adjust(newLimit, false)
}
}
}
}()
}
func (c *GradientController) update(r Execution) uint {
// This function is hugely inspired by [1].
//
// [1] https://github.com/Netflix/concurrency-limits/blob/18692b09e55a0574bea94d92e95a03c3e89012d2/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limit/GradientLimit.java#L259
currLimit := c.pool.WantedN()
queueSize := c.queueSize(currLimit)
c.resetRTTCounter--
if c.resetRTTCounter <= 0 {
c.resetRTTCounter = c.nextResetCounter()
c.resetNoLoadRTT = true
return queueSize
}
if c.resetNoLoadRTT || c.noLoadRTT > r.RTT {
c.noLoadRTT = r.RTT
c.resetNoLoadRTT = false
}
// TODO: Remove this line and make this configurable to be logged or not.
log.Println("Reported latency:", r.RTT, "NoLoadRTT:", c.noLoadRTT)
gradient := maxf(0.5, minf(1.0, c.rttTolerance*float64(c.noLoadRTT)/float64(r.RTT)))
fcurrLimit := float64(currLimit)
var newLimit float64
if r.Err != nil {
newLimit = fcurrLimit * c.backoffRatio
} else {
newLimit = fcurrLimit*gradient + float64(queueSize)
}
if newLimit < fcurrLimit {
newLimit = (1-c.smoothing)*fcurrLimit + c.smoothing*newLimit
}
return max(queueSize, uint(newLimit))
}
func (c *GradientController) nextResetCounter() uint {
return c.probeInterval + uint(c.rand.Intn(int(c.probeInterval)))
}
func (c *GradientController) adjust(newLimit uint, settle bool) {
newLimit = max(newLimit, c.min)
newLimit = min(newLimit, c.max)
// TODO: Remove this line and make this configurable to be logged or not.
log.Println("New limit:", newLimit)
// TODO: Move this if case into a new function in the orchestrator and make
// Incr/Decr unexported
currLimit := c.pool.WantedN()
if newLimit > currLimit {
c.pool.Incr(newLimit - currLimit)
} else if currLimit > newLimit {
c.pool.Decr(currLimit - newLimit)
} else /* currLimit==newLimit */ {
return
}
if settle {
// TODO: Support for injecting a custom context for the
// GradientController. Stop() function should cancel it.
c.pool.SettleDown(context.TODO())
}
}
func min(a, b uint) uint {
if a < b {
return a
}
return b
}
func max(a, b uint) uint {
if a > b {
return a
}
return b
}
func minf(a, b float64) float64 {
if a < b {
return a
}
return b
}
func maxf(a, b float64) float64 {
if a > b {
return a
}
return b
}
func (c *GradientController) Stop(ctx context.Context) {
// First close the orchestrator so that it doesn't mess with this thread when we are shutting down all goroutines...
close(c.quitChan)
c.wg.Wait()
// ...then we ask the orchestrator to shut down all worker threads.
c.pool.Decr(c.pool.WantedN())
c.pool.SettleDown(ctx)
}
func sqrt(x uint) uint {
// TODO: Make this faster by having a lookup table for common x, similarly
// to what the concurrency-limits library does.
return uint(math.Round(math.Sqrt(float64(x))))
}