-
Notifications
You must be signed in to change notification settings - Fork 14
/
runner.go
702 lines (628 loc) · 19.9 KB
/
runner.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package worker
import (
"context"
"runtime/pprof"
"strings"
"sync"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"gopkg.in/tomb.v2"
)
const (
// DefaultRestartDelay holds the default length of time that a worker
// will wait between exiting and being restarted by a Runner.
DefaultRestartDelay = 3 * time.Second
ErrAborted = errors.ConstError("aborted waiting for worker")
ErrDead = errors.ConstError("worker runner is not running")
)
// StartFunc is the type alias of the function that creates a worker.
type StartFunc = func(context.Context) (Worker, error)
// Runner runs a set of workers, restarting them as necessary
// when they fail.
type Runner struct {
tomb tomb.Tomb
startc chan startReq
stopc chan string
donec chan doneInfo
startedc chan startInfo
params RunnerParams
// isDying is maintained by the run goroutine.
// When it is dying (whether as a result of being killed or due to a
// fatal error), all existing workers are killed, no new workers
// will be started, and the loop will exit when all existing
// workers have stopped.
isDying bool
// finalError is maintained by the run goroutine.
// finalError holds the error that will be returned
// when the runner finally exits.
finalError error
// mu guards the fields below it. Note that the
// run goroutine only locks the mutex when
// it changes workers, not when it reads it. It can do this
// because it's the only goroutine that changes it.
mu sync.Mutex
// workersChangedCond is notified whenever the
// current workers state changes.
workersChangedCond sync.Cond
// workers holds the current set of workers.
workers map[string]*workerInfo
// notifyStarted is used only for test synchronisation.
// As the worker startInfo values are processed, the worker is sent
// down this channel if this channel is not nil.
notifyStarted chan<- Worker
}
// workerInfo holds information on one worker id.
type workerInfo struct {
// worker holds the current Worker instance. This field is
// guarded by the Runner.mu mutex so it can be inspected
// by Runner.Worker calls.
worker Worker
// The following fields are maintained by the
// run goroutine.
labels pprof.LabelSet
// start holds the function to create the worker.
// If this is nil, the worker has been stopped
// and will be removed when its goroutine exits.
start StartFunc
// restartDelay holds the length of time that runWorker
// will wait before calling the start function.
restartDelay time.Duration
// stopping holds whether the worker is currently
// being killed. The runWorker goroutine will
// still exist while this is true.
stopping bool
// done is used to signal when the worker has finished
// running and is removed from the runner.
done chan struct{}
// started holds the time the worker was started.
started time.Time
}
func (i *workerInfo) status() string {
if i.stopping && i.worker != nil {
return "stopping"
}
if i.worker == nil {
return "stopped"
}
return "started"
}
type startReq struct {
ctx context.Context
id string
start StartFunc
reply chan error
}
type startInfo struct {
id string
worker Worker
}
type doneInfo struct {
id string
err error
}
// Logger represents the various logging methods used by the runner.
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Errorf(string, ...interface{})
}
// Clock represents the methods needed from the clock.
type Clock interface {
Now() time.Time
After(time.Duration) <-chan time.Time
}
// RunnerParams holds the parameters for a NewRunner call.
type RunnerParams struct {
// Name is a human-readable identifier for the work func. It should be
// used to identify the work that the catacomb is tasked with managing.
// This is used to annotate the pprof profile.
Name string
// IsFatal is called when a worker exits. If it returns
// true, all the other workers will be stopped and the runner
// itself will finish.
//
// If IsFatal is nil, all errors will be treated as fatal.
IsFatal func(error) bool
// ShouldRestart is called when a worker exits. If it returns
// false, the worker will be removed from the runner. All other
// workers will continue to run.
ShouldRestart func(error) bool
// When the runner exits because one or more workers have
// returned a fatal error, only the most important one,
// will be returned. MoreImportant should report whether
// err0 is more important than err1.
//
// If MoreImportant is nil, the first error reported will be
// returned.
MoreImportant func(err0, err1 error) bool
// RestartDelay holds the length of time the runner will
// wait after a worker has exited with a non-fatal error
// before it is restarted.
// If this is zero, DefaultRestartDelay will be used.
RestartDelay time.Duration
// Clock is used for timekeeping. If it's nil, clock.WallClock
// will be used.
Clock Clock
// Logger is used to provide an implementation for where the logging
// messages go for the runner. If it's nil, no logging output.
Logger Logger
}
// Validate checks that the RunnerParams are valid.
func (p RunnerParams) Validate() error {
if p.Name == "" {
return errors.NotValidf("Name is required")
}
return nil
}
// NewRunner creates a new Runner. When a worker finishes, if its error
// is deemed fatal (determined by calling isFatal), all the other workers
// will be stopped and the runner itself will finish. Of all the fatal errors
// returned by the stopped workers, only the most important one,
// determined by calling moreImportant, will be returned from
// Runner.Wait. Non-fatal errors will not be returned.
//
// The function isFatal(err) returns whether err is a fatal error. The
// function moreImportant(err0, err1) returns whether err0 is considered
// more important than err1.
func NewRunner(p RunnerParams) (*Runner, error) {
if err := p.Validate(); err != nil {
return nil, err
}
if p.IsFatal == nil {
p.IsFatal = func(error) bool {
return true
}
}
if p.ShouldRestart == nil {
p.ShouldRestart = func(error) bool {
return true
}
}
if p.MoreImportant == nil {
p.MoreImportant = func(err0, err1 error) bool {
return true
}
}
if p.RestartDelay == 0 {
p.RestartDelay = DefaultRestartDelay
}
if p.Clock == nil {
p.Clock = clock.WallClock
}
if p.Logger == nil {
p.Logger = noopLogger{}
}
runner := &Runner{
startc: make(chan startReq),
stopc: make(chan string),
donec: make(chan doneInfo),
startedc: make(chan startInfo),
params: p,
workers: make(map[string]*workerInfo),
}
runner.workersChangedCond.L = &runner.mu
runner.tomb.Go(runner.run)
return runner, nil
}
// StartWorker starts a worker running associated with the given id.
// The startFunc function will be called to create the worker;
// when the worker exits, an AlreadyExists error will be returned.
//
// StartWorker returns ErrDead if the runner is not running.
func (runner *Runner) StartWorker(ctx context.Context, id string, startFunc StartFunc) error {
// Note: we need the reply channel so that when StartWorker
// returns, we're guaranteed that the worker is installed
// when we return, so Worker will see it if called
// immediately afterwards.
reply := make(chan error)
select {
case runner.startc <- startReq{ctx, id, startFunc, reply}:
// We're certain to get a reply because the startc channel is synchronous
// so if we succeed in sending on it, we know that the run goroutine has entered
// the startc arm of the select, and that calls startWorker (which never blocks)
// and then immediately sends any error to the reply channel.
return <-reply
case <-ctx.Done():
case <-runner.tomb.Dead():
}
return ErrDead
}
// StopWorker stops the worker associated with the given id.
// It does nothing if there is no such worker.
//
// StopWorker returns ErrDead if the runner is not running.
func (runner *Runner) StopWorker(id string) error {
select {
case runner.stopc <- id:
return nil
case <-runner.tomb.Dead():
}
return ErrDead
}
// StopAndRemoveWorker stops the worker and returns any error reported by
// the worker, waiting for the worker to be no longer known to the runner.
// If it was stopped while waiting, StopAndRemoveWorker will return ErrAborted.
//
// StopAndRemoveWorker returns ErrDead if the runner is not running.
func (runner *Runner) StopAndRemoveWorker(id string, abort <-chan struct{}) error {
w, done, err := runner.workerInfo(id, abort)
if err != nil {
return err
}
workerErr := Stop(w)
select {
case <-abort:
case <-done:
return workerErr
}
return ErrAborted
}
// Wait implements Worker.Wait
func (runner *Runner) Wait() error {
return runner.tomb.Wait()
}
// Kill implements Worker.Kill
func (runner *Runner) Kill() {
runner.params.Logger.Debugf("killing runner %p", runner)
runner.tomb.Kill(nil)
}
// Worker returns the current worker for the given id.
// If a worker has been started with the given id but is
// not currently available, it will wait until it is available,
// stopping waiting if it receives a value on the stop channel.
//
// If there is no worker started with the given id, Worker
// will return ErrNotFound. If it was aborted while
// waiting, Worker will return ErrAborted. If the runner
// has been killed while waiting, Worker will return ErrDead.
func (runner *Runner) Worker(id string, abort <-chan struct{}) (Worker, error) {
w, _, err := runner.workerInfo(id, abort)
return w, err
}
// WorkerNames returns the names of the current workers.
// They are returned in no particular order and they might not exists when
// the Worker request is made.
func (runner *Runner) WorkerNames() []string {
runner.mu.Lock()
defer runner.mu.Unlock()
names := make([]string, 0, len(runner.workers))
for name := range runner.workers {
names = append(names, name)
}
return names
}
func (runner *Runner) workerInfo(id string, abort <-chan struct{}) (Worker, <-chan struct{}, error) {
runner.mu.Lock()
// getWorker returns the current worker for the id
// and reports an ErrNotFound error if the worker
// isn't found.
getWorker := func() (Worker, <-chan struct{}, error) {
info := runner.workers[id]
if info == nil {
// No entry for the id means the worker
// will never become available.
return nil, nil, errors.NotFoundf("worker %q", id)
}
return info.worker, info.done, nil
}
if w, done, err := getWorker(); err != nil || w != nil {
// The worker is immediately available (or we know it's
// not going to become available). No need
// to block waiting for it.
runner.mu.Unlock()
// If it wasn't found, it's possible that's because
// the whole thing has shut down, so
// check for dying so that we don't mislead
// our caller.
select {
case <-runner.tomb.Dying():
return nil, nil, ErrDead
default:
}
return w, done, err
}
type workerResult struct {
w Worker
done <-chan struct{}
err error
}
wc := make(chan workerResult, 1)
stopped := make(chan struct{})
go func() {
defer runner.mu.Unlock()
for {
select {
case <-stopped:
return
default:
// Note: sync.Condition.Wait unlocks the mutex before
// waiting, then locks it again before returning.
runner.workersChangedCond.Wait()
if w, done, err := getWorker(); err != nil || w != nil {
wc <- workerResult{w, done, err}
return
}
}
}
}()
select {
case w := <-wc:
if errors.Is(w.err, errors.NotFound) {
// If it wasn't found, it's possible that's because
// the whole thing has shut down, so
// check for dying so that we don't mislead
// our caller.
select {
case <-runner.tomb.Dying():
return nil, nil, ErrDead
default:
}
}
return w.w, w.done, w.err
case <-runner.tomb.Dying():
return nil, nil, ErrDead
case <-abort:
}
// Stop our wait goroutine.
// Strictly speaking this can wake up more waiting Worker calls
// than needed, but this shouldn't be a problem as in practice
// almost all the time Worker should not need to start the
// goroutine.
close(stopped)
runner.workersChangedCond.Broadcast()
return nil, nil, ErrAborted
}
func (runner *Runner) run() error {
tombDying := runner.tomb.Dying()
for {
if runner.isDying && len(runner.workers) == 0 {
return runner.finalError
}
select {
case <-tombDying:
runner.params.Logger.Infof("runner is dying")
runner.isDying = true
runner.killAll()
tombDying = nil
case req := <-runner.startc:
runner.params.Logger.Debugf("start %q", req.id)
req.reply <- runner.startWorker(req)
case id := <-runner.stopc:
runner.params.Logger.Debugf("stop %q", id)
runner.killWorker(id)
case info := <-runner.startedc:
runner.params.Logger.Debugf("%q started", info.id)
runner.setWorker(info.id, info.worker)
if runner.notifyStarted != nil {
runner.notifyStarted <- info.worker
}
case info := <-runner.donec:
runner.params.Logger.Debugf("%q done: %v", info.id, info.err)
runner.workerDone(info)
}
runner.workersChangedCond.Broadcast()
}
}
// startWorker responds when a worker has been started
// by calling StartWorker.
func (runner *Runner) startWorker(req startReq) error {
if runner.isDying {
runner.params.Logger.Infof("ignoring start request for %q when dying", req.id)
return nil
}
info := runner.workers[req.id]
if info != nil {
return errors.AlreadyExistsf("worker %q", req.id)
}
labels := pprof.Labels(
"type", "worker",
"name", runner.params.Name,
// id is the worker id, which is unique for each worker, this is
// supplied by the caller of StartWorker.
"id", req.id,
)
runner.mu.Lock()
defer runner.mu.Unlock()
runner.workers[req.id] = &workerInfo{
labels: labels,
start: req.start,
restartDelay: runner.params.RestartDelay,
started: runner.params.Clock.Now().UTC(),
done: make(chan struct{}, 1),
}
pprof.Do(runner.tomb.Context(req.ctx), labels, func(ctx context.Context) {
go runner.runWorker(ctx, 0, req.id, req.start)
})
return nil
}
type panicError interface {
error
StackTrace() []string
Panicked() bool
}
// workerDone responds when a worker has finished or failed
// to start. It maintains the runner.finalError field and
// restarts the worker if necessary.
func (runner *Runner) workerDone(info doneInfo) {
params := runner.params
workerInfo := runner.workers[info.id]
if !workerInfo.stopping && info.err == nil {
params.Logger.Debugf("removing %q from known workers", info.id)
runner.removeWorker(info.id, workerInfo.done)
return
}
if info.err != nil {
errStr := info.err.Error()
if errWithStack, ok := info.err.(panicError); ok && errWithStack.Panicked() {
// Panics should always have the full stacktrace in the error log.
errStr = strings.Join(append([]string{errStr}, errWithStack.StackTrace()...), "\n")
}
params.Logger.Debugf("error %q: %s", info.id, errStr)
if params.IsFatal(info.err) {
params.Logger.Errorf("fatal error %q: %s", info.id, errStr)
if runner.finalError == nil || params.MoreImportant(info.err, runner.finalError) {
runner.finalError = info.err
}
runner.removeWorker(info.id, workerInfo.done)
if !runner.isDying {
runner.isDying = true
runner.killAll()
}
return
} else {
params.Logger.Infof("non-fatal error %q: %s", info.id, errStr)
}
if !params.ShouldRestart(info.err) {
params.Logger.Debugf("removing %q from known workers", info.id)
runner.removeWorker(info.id, workerInfo.done)
return
}
params.Logger.Errorf("exited %q: %s", info.id, errStr)
}
if workerInfo.start == nil {
params.Logger.Debugf("no restart, removing %q from known workers", info.id)
// The worker has been deliberately stopped;
// we can now remove it from the list of workers.
runner.removeWorker(info.id, workerInfo.done)
return
}
pprof.Do(runner.tomb.Context(context.Background()), workerInfo.labels, func(ctx context.Context) {
go runner.runWorker(ctx, workerInfo.restartDelay, info.id, workerInfo.start)
})
workerInfo.restartDelay = params.RestartDelay
}
// removeWorker removes the worker with the given id from the
// set of current workers. This should only be called when
// the worker is not running.
func (runner *Runner) removeWorker(id string, removed chan<- struct{}) {
runner.mu.Lock()
delete(runner.workers, id)
removed <- struct{}{}
runner.mu.Unlock()
}
// setWorker sets the worker associated with the given id.
func (runner *Runner) setWorker(id string, w Worker) {
runner.mu.Lock()
defer runner.mu.Unlock()
info := runner.workers[id]
info.worker = w
if runner.isDying || info.stopping {
// We're dying or the worker has already been
// stopped, so kill it already.
runner.killWorkerLocked(id)
}
}
// killAll stops all the current workers.
func (runner *Runner) killAll() {
runner.mu.Lock()
defer runner.mu.Unlock()
for id := range runner.workers {
runner.killWorkerLocked(id)
}
}
// killWorker stops the worker with the given id, and
// marks it so that it will not start again unless explicitly started
// with StartWorker.
func (runner *Runner) killWorker(id string) {
runner.mu.Lock()
defer runner.mu.Unlock()
runner.killWorkerLocked(id)
}
// killWorkerLocked is like killWorker except that it expects
// the runner.mu mutex to be held already.
func (runner *Runner) killWorkerLocked(id string) {
info := runner.workers[id]
if info == nil {
return
}
info.stopping = true
info.start = nil
if info.worker != nil {
runner.params.Logger.Debugf("killing %q", id)
info.worker.Kill()
info.worker = nil
} else {
runner.params.Logger.Debugf("couldn't kill %q, not yet started", id)
}
}
// runWorker starts the given worker after waiting for the given delay.
func (runner *Runner) runWorker(ctx context.Context, delay time.Duration, id string, start StartFunc) {
if delay > 0 {
runner.params.Logger.Infof("restarting %q in %v", id, delay)
// TODO(rog) provide a way of interrupting this
// so that it can be stopped when a worker is removed.
select {
case <-runner.tomb.Dying():
runner.donec <- doneInfo{id, nil}
return
case <-runner.params.Clock.After(delay):
}
}
runner.params.Logger.Infof("start %q", id)
// Defensively ensure that we get reasonable behaviour
// if something calls Goexit inside the worker (this can
// happen if something calls check.Assert) - if we don't
// do this, then this will usually turn into a hard-to-find
// deadlock when the runner is stopped but the runWorker
// goroutine will never signal that it's finished.
normal := false
defer func() {
if normal {
return
}
// Since normal isn't true, it means that something
// inside start must have called panic or runtime.Goexit.
// If it's a panic, we let it panic; if it's called Goexit,
// we'll just return an error, enabling this functionality
// to be tested.
if err := recover(); err != nil {
panic(err)
}
runner.params.Logger.Infof("%q called runtime.Goexit unexpectedly", id)
runner.donec <- doneInfo{id, errors.Errorf("runtime.Goexit called in running worker - probably inappropriate Assert")}
}()
worker, err := start(ctx)
normal = true
if err == nil {
runner.startedc <- startInfo{id, worker}
err = worker.Wait()
}
runner.params.Logger.Infof("stopped %q, err: %v", id, err)
runner.donec <- doneInfo{id, err}
}
type reporter interface {
Report() map[string]interface{}
}
// Report implements Reporter.
func (runner *Runner) Report() map[string]interface{} {
workers := make(map[string]interface{})
runner.mu.Lock()
defer runner.mu.Unlock()
for id, info := range runner.workers {
worker := info.worker
workerReport := map[string]interface{}{
KeyState: info.status(),
}
if !info.started.IsZero() {
workerReport[KeyLastStart] = info.started.Format("2006-01-02 15:04:05")
}
if worker != nil {
if r, ok := worker.(reporter); ok {
if report := r.Report(); len(report) > 0 {
workerReport[KeyReport] = report
}
}
}
workers[id] = workerReport
}
return map[string]interface{}{
"workers": workers,
}
}
type noopLogger struct{}
func (noopLogger) Debugf(string, ...interface{}) {}
func (noopLogger) Infof(string, ...interface{}) {}
func (noopLogger) Errorf(string, ...interface{}) {}