-
Notifications
You must be signed in to change notification settings - Fork 453
/
pooled_worker_pool.go
192 lines (168 loc) · 5.94 KB
/
pooled_worker_pool.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package sync
import (
"fmt"
"math"
"sync"
"sync/atomic"
"github.com/MichaelTJones/pcg"
"github.com/uber-go/tally"
)
const (
numGoroutinesGaugeSampleRate = 1000
)
type pooledWorkerPool struct {
sync.Mutex
numRoutinesAtomic int64
numWorkingRoutinesAtomic int64
numRoutinesGauge tally.Gauge
numWorkingRoutinesGauge tally.Gauge
growOnDemand bool
workChs []chan Work
numShards int64
killWorkerProbability float64
nowFn NowFn
}
// NewPooledWorkerPool creates a new worker pool.
func NewPooledWorkerPool(size int, opts PooledWorkerPoolOptions) (PooledWorkerPool, error) {
if size <= 0 {
return nil, fmt.Errorf("pooled worker pool size too small: %d", size)
}
numShards := opts.NumShards()
if int64(size) < numShards {
numShards = int64(size)
}
workChs := make([]chan Work, numShards)
for i := range workChs {
workChs[i] = make(chan Work, int64(size)/numShards)
}
return &pooledWorkerPool{
numRoutinesAtomic: 0,
numWorkingRoutinesAtomic: 0,
numRoutinesGauge: opts.InstrumentOptions().MetricsScope().Gauge("num-routines"),
numWorkingRoutinesGauge: opts.InstrumentOptions().MetricsScope().Gauge("num-working-routines"),
growOnDemand: opts.GrowOnDemand(),
workChs: workChs,
numShards: numShards,
killWorkerProbability: opts.KillWorkerProbability(),
nowFn: opts.NowFn(),
}, nil
}
func (p *pooledWorkerPool) Init() {
rng := pcg.NewPCG64() // Just use default seed here
for _, workCh := range p.workChs {
for i := 0; i < cap(workCh); i++ {
p.spawnWorker(rng.Random(), nil, workCh, true)
}
}
}
func (p *pooledWorkerPool) Go(work Work) {
var (
// Use time.Now() to avoid excessive synchronization
currTime = p.nowFn().UnixNano()
workChIdx = currTime % p.numShards
workCh = p.workChs[workChIdx]
)
if currTime%numGoroutinesGaugeSampleRate == 0 {
p.emitNumRoutines()
p.emitNumWorkingRoutines()
}
if !p.growOnDemand {
workCh <- work
return
}
select {
case workCh <- work:
default:
// If the queue for the worker we were assigned to is full,
// allocate a new goroutine to do the work and then
// assign it to be a temporary additional worker for the queue.
// This allows the worker pool to accommodate "bursts" of
// traffic. Also, it reduces the need for operators to tune the size
// of the pool for a given workload. If the pool is initially
// sized too small, it will eventually grow to accommodate the
// workload, and if the workload decreases the killWorkerProbability
// will slowly shrink the pool back down to its original size because
// workers created in this manner will not spawn their replacement
// before killing themselves.
p.spawnWorker(uint64(currTime), work, workCh, false)
}
}
func (p *pooledWorkerPool) spawnWorker(
seed uint64, initialWork Work, workCh chan Work, spawnReplacement bool) {
go func() {
p.incNumRoutines()
if initialWork != nil {
initialWork()
}
// RNG per worker to avoid synchronization.
var (
rng = pcg.NewPCG64().Seed(seed, seed*2, seed*3, seed*4)
// killWorkerProbability is a float but but the PCG RNG only
// generates uint64s so we need to identify the uint64 number
// that corresponds to the equivalent probability assuming we're
// generating random numbers in the entire uint64 range. For example,
// if the max uint64 was 1000 and we had a killWorkerProbability of 0.15
// then the killThreshold should be 0.15 * 1000 = 150 if we want a randomly
// chosen number between 0 and 1000 to have a 15% chance of being below
// the selected threshold.
killThreshold = uint64(p.killWorkerProbability * float64(math.MaxUint64))
)
for f := range workCh {
p.incNumWorkingRoutines()
f()
p.decNumWorkingRoutines()
if rng.Random() < killThreshold {
if spawnReplacement {
p.spawnWorker(rng.Random(), nil, workCh, true)
}
p.decNumRoutines()
return
}
}
}()
}
func (p *pooledWorkerPool) emitNumRoutines() {
numRoutines := float64(p.getNumRoutines())
p.numRoutinesGauge.Update(numRoutines)
}
func (p *pooledWorkerPool) incNumRoutines() {
atomic.AddInt64(&p.numRoutinesAtomic, 1)
}
func (p *pooledWorkerPool) decNumRoutines() {
atomic.AddInt64(&p.numRoutinesAtomic, -1)
}
func (p *pooledWorkerPool) getNumRoutines() int64 {
return atomic.LoadInt64(&p.numRoutinesAtomic)
}
func (p *pooledWorkerPool) emitNumWorkingRoutines() {
numRoutines := float64(p.getNumWorkingRoutines())
p.numWorkingRoutinesGauge.Update(numRoutines)
}
func (p *pooledWorkerPool) incNumWorkingRoutines() {
atomic.AddInt64(&p.numWorkingRoutinesAtomic, 1)
}
func (p *pooledWorkerPool) decNumWorkingRoutines() {
atomic.AddInt64(&p.numWorkingRoutinesAtomic, -1)
}
func (p *pooledWorkerPool) getNumWorkingRoutines() int64 {
return atomic.LoadInt64(&p.numWorkingRoutinesAtomic)
}