forked from roadrunner-server/roadrunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_pool.go
370 lines (298 loc) · 7.74 KB
/
static_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
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
package roadrunner
import (
"fmt"
"github.com/pkg/errors"
"os/exec"
"sync"
"sync/atomic"
"time"
)
const (
// StopRequest can be sent by worker to indicate that restart is required.
StopRequest = "{\"stop\":true}"
)
// StaticPool controls worker creation, destruction and task routing. Pool uses fixed amount of workers.
type StaticPool struct {
// pool behaviour
cfg Config
// worker command creator
cmd func() *exec.Cmd
// creates and connects to workers
factory Factory
// active task executions
tmu *sync.Mutex
tasks sync.WaitGroup
// workers circular allocation buf
free chan *Worker
// number of workers expected to be dead in a buf.
numDead int64
// protects state of worker list, does not affect allocation
muw *sync.RWMutex
// all registered workers
workers []*Worker
// invalid declares set of workers to be removed from the pool.
remove *sync.Map
// pool is being destroyed
inDestroy int32
destroy chan interface{}
// lsn is optional callback to handle worker create/destruct/error events.
mul sync.Mutex
lsn func(event int, ctx interface{})
}
// NewPool creates new worker pool and task multiplexer. StaticPool will initiate with one worker.
func NewPool(cmd func() *exec.Cmd, factory Factory, cfg Config) (*StaticPool, error) {
if err := cfg.Valid(); err != nil {
return nil, errors.Wrap(err, "config")
}
p := &StaticPool{
cfg: cfg,
cmd: cmd,
factory: factory,
workers: make([]*Worker, 0, cfg.NumWorkers),
free: make(chan *Worker, cfg.NumWorkers),
destroy: make(chan interface{}),
tmu: &sync.Mutex{},
remove: &sync.Map{},
muw: &sync.RWMutex{},
}
// constant number of workers simplify logic
for i := int64(0); i < p.cfg.NumWorkers; i++ {
// to test if worker ready
w, err := p.createWorker()
if err != nil {
p.Destroy()
return nil, err
}
p.free <- w
}
return p, nil
}
// Listen attaches pool event controller.
func (p *StaticPool) Listen(l func(event int, ctx interface{})) {
p.mul.Lock()
defer p.mul.Unlock()
p.lsn = l
p.muw.Lock()
for _, w := range p.workers {
w.err.Listen(p.lsn)
}
p.muw.Unlock()
}
// Config returns associated pool configuration. Immutable.
func (p *StaticPool) Config() Config {
return p.cfg
}
// Workers returns worker list associated with the pool.
func (p *StaticPool) Workers() (workers []*Worker) {
p.muw.RLock()
defer p.muw.RUnlock()
workers = append(workers, p.workers...)
return workers
}
// Remove forces pool to remove specific worker.
func (p *StaticPool) Remove(w *Worker, err error) bool {
if w.State().Value() != StateReady && w.State().Value() != StateWorking {
// unable to remove inactive worker
return false
}
if _, ok := p.remove.Load(w); ok {
return false
}
p.remove.Store(w, err)
return true
}
// Exec one task with given payload and context, returns result or error.
func (p *StaticPool) Exec(rqs *Payload) (rsp *Payload, err error) {
p.tmu.Lock()
p.tasks.Add(1)
p.tmu.Unlock()
defer p.tasks.Done()
w, err := p.allocateWorker()
if err != nil {
return nil, errors.Wrap(err, "unable to allocate worker")
}
rsp, err = w.Exec(rqs)
if err != nil {
// soft job errors are allowed
if _, jobError := err.(JobError); jobError {
p.release(w)
return nil, err
}
p.discardWorker(w, err)
return nil, err
}
// worker want's to be terminated
if rsp.Body == nil && rsp.Context != nil && string(rsp.Context) == StopRequest {
p.discardWorker(w, err)
return p.Exec(rqs)
}
p.release(w)
return rsp, nil
}
// Destroy all underlying workers (but let them to complete the task).
func (p *StaticPool) Destroy() {
atomic.AddInt32(&p.inDestroy, 1)
p.tmu.Lock()
p.tasks.Wait()
close(p.destroy)
p.tmu.Unlock()
var wg sync.WaitGroup
for _, w := range p.Workers() {
wg.Add(1)
w.markInvalid()
go func(w *Worker) {
defer wg.Done()
p.destroyWorker(w, nil)
}(w)
}
wg.Wait()
}
// finds free worker in a given time interval. Skips dead workers.
func (p *StaticPool) allocateWorker() (w *Worker, err error) {
for i := atomic.LoadInt64(&p.numDead); i >= 0; i++ {
// this loop is required to skip issues with dead workers still being in a ring
// (we know how many workers).
select {
case w = <-p.free:
if w.State().Value() != StateReady {
// found expected dead worker
atomic.AddInt64(&p.numDead, ^int64(0))
continue
}
if err, remove := p.remove.Load(w); remove {
p.discardWorker(w, err)
// get next worker
i++
continue
}
return w, nil
case <-p.destroy:
return nil, fmt.Errorf("pool has been stopped")
default:
// enable timeout handler
}
timeout := time.NewTimer(p.cfg.AllocateTimeout)
select {
case <-timeout.C:
return nil, fmt.Errorf("worker timeout (%s)", p.cfg.AllocateTimeout)
case w = <-p.free:
timeout.Stop()
if w.State().Value() != StateReady {
atomic.AddInt64(&p.numDead, ^int64(0))
continue
}
if err, remove := p.remove.Load(w); remove {
p.discardWorker(w, err)
// get next worker
i++
continue
}
return w, nil
case <-p.destroy:
timeout.Stop()
return nil, fmt.Errorf("pool has been stopped")
}
}
return nil, fmt.Errorf("all workers are dead (%v)", p.cfg.NumWorkers)
}
// release releases or replaces the worker.
func (p *StaticPool) release(w *Worker) {
if p.cfg.MaxJobs != 0 && w.State().NumExecs() >= p.cfg.MaxJobs {
p.discardWorker(w, p.cfg.MaxJobs)
return
}
if err, remove := p.remove.Load(w); remove {
p.discardWorker(w, err)
return
}
p.free <- w
}
// creates new worker using associated factory. automatically
// adds worker to the worker list (background)
func (p *StaticPool) createWorker() (*Worker, error) {
w, err := p.factory.SpawnWorker(p.cmd())
if err != nil {
return nil, err
}
p.mul.Lock()
if p.lsn != nil {
w.err.Listen(p.lsn)
}
p.mul.Unlock()
p.throw(EventWorkerConstruct, w)
p.muw.Lock()
p.workers = append(p.workers, w)
p.muw.Unlock()
go p.watchWorker(w)
return w, nil
}
// gentry remove worker
func (p *StaticPool) discardWorker(w *Worker, caused interface{}) {
w.markInvalid()
go p.destroyWorker(w, caused)
}
// destroyWorker destroys workers and removes it from the pool.
func (p *StaticPool) destroyWorker(w *Worker, caused interface{}) {
go func() {
err := w.Stop()
if err != nil {
p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err})
}
}()
select {
case <-w.waitDone:
// worker is dead
p.throw(EventWorkerDestruct, w)
case <-time.NewTimer(p.cfg.DestroyTimeout).C:
// failed to stop process in given time
if err := w.Kill(); err != nil {
p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err})
}
p.throw(EventWorkerKill, w)
}
}
// watchWorker watches worker state and replaces it if worker fails.
func (p *StaticPool) watchWorker(w *Worker) {
err := w.Wait()
p.throw(EventWorkerDead, w)
// detaching
p.muw.Lock()
for i, wc := range p.workers {
if wc == w {
p.workers = append(p.workers[:i], p.workers[i+1:]...)
p.remove.Delete(w)
break
}
}
p.muw.Unlock()
// registering a dead worker
atomic.AddInt64(&p.numDead, 1)
// worker have died unexpectedly, pool should attempt to replace it with alive version safely
if err != nil {
p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err})
}
if !p.destroyed() {
nw, err := p.createWorker()
if err == nil {
p.free <- nw
return
}
// possible situation when major error causes all PHP scripts to die (for example dead DB)
if len(p.Workers()) == 0 {
p.throw(EventPoolError, err)
} else {
p.throw(EventWorkerError, WorkerError{Worker: w, Caused: err})
}
}
}
func (p *StaticPool) destroyed() bool {
return atomic.LoadInt32(&p.inDestroy) != 0
}
// throw invokes event handler if any.
func (p *StaticPool) throw(event int, ctx interface{}) {
p.mul.Lock()
if p.lsn != nil {
p.lsn(event, ctx)
}
p.mul.Unlock()
}