/
worker.go
497 lines (441 loc) · 14.5 KB
/
worker.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
// Copyright 2016 Diffeo, Inc.
// This software is released under an MIT/X11 open source license.
// Package worker provides a library framework for processes that
// execute Coordinate work units.
package worker
import (
"context"
"fmt"
"net"
"os"
"runtime"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/diffeo/go-coordinate/coordinate"
"github.com/satori/go.uuid"
)
// Worker is foo.
type Worker struct {
// Namespace identifies the Coordinate namespace from which
// work is performed. This field is required when creating
// a Worker.
Namespace coordinate.Namespace
// Tasks defines the tasks this Worker is capable of running.
// Work specs must declare a runtime consistent with this
// worker's runtime settings (by default, that's "runtime: go"),
// and also have a "task:" field that names one of the tasks in
// this map. If a work spec has no "task:", the work spec name
// is looked up here instead.
//
// The task function is called with a context and a slice of
// at least one attempt. The context will be canceled when
// the worker is stopped or if one of the attempts is nearing
// is expiration time. The worker can take any reasonable action
// in response to being signaled, but generally it should stop
// doing further work and mark all of the attempts as failed.
//
// There is guaranteed to be at least one attempt. All attempts
// are for the same worker and for the same work spec.
Tasks map[string]func(context.Context, []coordinate.Attempt)
// WorkerID provides the name of the worker as seen through the
// Coordinate API. If unset, a worker ID will be generated.
WorkerID string
// Concurrency states how many sets of attempts should run in
// parallel. If unset, uses runtime.NumCPU().
Concurrency int
// PollInterval states how often the worker should try to get
// more work if the previous attempt(s) returned nothing. If
// unset, defaults to 1 second.
PollInterval time.Duration
// HeartbeatInterval states how often the worker should report
// its status in the Coordinate worker data, and check for
// work units that are about to expire. If unset, defaults to
// 15 seconds.
HeartbeatInterval time.Duration
// MaxAttempts limits the number of attempts that will be
// returned; it is exactly the
// coordinate.AttemptRequest.NumberOfWorkUnits parameter. If
// unset, use 100. Note that individual work specs can also
// limit the number of attempts by setting a "max_getwork"
// key.
MaxAttempts int
// ErrorHandler is called when an error occurs in the worker
// main loop.
ErrorHandler func(error)
// Clock defines a time source for the worker. If the
// Coordinate backend was created with an alternate time
// source, this should match that time source. Only test code
// should need to set this. If unset, uses a time source
// backed by real wall-clock time.
Clock clock.Clock
// Runtimes sets the language runtimes that should be used
// with this worker. When nil, a default value of ["go"] is
// used.
Runtimes []string
// parentWorker is a saved Coordinate worker object with ID
// WorkerID.
parentWorker coordinate.Worker
// childWorkers identifies the child workers.
childWorkers map[string]coordinate.Worker
// cancellations maps child worker ID to a cancellation function
// for that worker's context. These functions are specified to
// be idempotent.
cancellations *sync.Map // map[string]context.CancelFunc
// idleWorkers is an unordered list of child worker IDs that
// do not have work.
idleWorkers []string
// systemIdle is set if an attempt to get work returned
// nothing. In this case, there will not be another attempt
// to get work for PollDuration time.
systemIdle bool
}
var (
// expirationWarning is a duration such that, if less than
// this time is remaining to execute a work unit before it
// expires, the worker will be signaled.
expirationWarning = 30 * time.Second
// expirationAlarm is a duration such that, if less than this
// time is remaining to execute a work unit before it expires,
// it will be failed instead.
expirationAlarm = 15 * time.Second
)
// setDefaults sets default values for any Worker fields that are
// uninitialized.
func (w *Worker) setDefaults() {
if w.WorkerID == "" {
// May as well use a UUID here, "it's what we've always done"
w.WorkerID = uuid.NewV4().String()
}
if w.Concurrency == 0 {
w.Concurrency = runtime.NumCPU()
}
if w.PollInterval == time.Duration(0) {
w.PollInterval = time.Duration(1) * time.Second
}
if w.HeartbeatInterval == time.Duration(0) {
w.HeartbeatInterval = time.Duration(15) * time.Second
}
if w.MaxAttempts == 0 {
w.MaxAttempts = 100
}
if w.Clock == nil {
w.Clock = clock.New()
}
}
// bootstrap creates the minimum required object set for the worker
// object.
func (w *Worker) bootstrap() error {
w.childWorkers = make(map[string]coordinate.Worker)
w.cancellations = new(sync.Map)
// Get the parent worker
var err error
w.parentWorker, err = w.Namespace.Worker(w.WorkerID)
return err
}
// Run runs work units from Coordinate forever, or until the provided
// context is cancelled. If it returns, either there was a startup
// error connecting to Coordinate, in which case the corresponding
// error is returned, or execution was cancelled, returning nil. If
// there is an error while trying to get attempts it is ignored.
func (w *Worker) Run(ctx context.Context) error {
w.setDefaults()
if err := w.bootstrap(); err != nil {
return err
}
// This channel is signaled in doWork() after
// RequestAttempts() returns, with a true value if at least
// one attempt comes back. If it does signal true, it
// triggers another worker if possible.
gotWork := make(chan bool)
// This channel is signaled at the end of doWork() with the
// worker ID. If the most recent signal in gotWork was true,
// it triggers another worker if possible.
finished := make(chan string)
// This channel, if non-nil, is signaled every second, and
// triggers a worker. It has a channel only when gotWork
// transmits a false value.
var tick <-chan time.Time
var ticker *clock.Ticker
// This channel is signaled to run the heartbeat task.
var heartbeat <-chan time.Time
heartbeater := w.Clock.Ticker(w.HeartbeatInterval)
// We need to (asynchronously) kick off the world by telling
// ourselves that it's okay to get more work units.
go func() { gotWork <- true }()
// TODO(dmaze): check for and signal stale workers
//
// The Python parent does both of these together every
// heartbeat_interval (15 seconds).
//
// Also, the children in Python land register on creation and
// unregister on destruction; but they only run a single work
// unit or for 10 seconds. Note that "expired" doesn't carry
// a whole lot of meaning for workers, yet.
for {
select {
case <-ctx.Done():
// Shutting down
// TODO(dmaze): don't return *immediately*; wait
// for all of the workers to clean up
if ticker != nil {
ticker.Stop()
}
heartbeater.Stop()
return nil
case notIdle := <-gotWork:
// Some worker came back with its
// RequestAttempts() result. If notIdle is
// false, we expect the finished channel to
// immediately be signaled as well.
// If the "idle" bit changed, set/cancel the timer
if w.systemIdle && notIdle {
ticker.Stop()
ticker = nil
tick = nil
}
if (!w.systemIdle) && (!notIdle) {
ticker = w.Clock.Ticker(w.PollInterval)
tick = ticker.C
}
w.systemIdle = !notIdle
w.maybeDoWork(ctx, gotWork, finished, false)
case child := <-finished:
// Some worker finished.
w.returnIdleChild(child)
w.maybeDoWork(ctx, gotWork, finished, false)
case <-tick:
// The system is idle, and the clock tick
// fired. Forcibly start an idle child at
// this point. This will eventually cause
// gotWork to trigger, which will update the
// systemIdle flag and might trigger even more
// work.
w.maybeDoWork(ctx, gotWork, finished, true)
case <-heartbeat:
w.heartbeat()
w.findStaleUnits()
}
}
}
// getIdleChild returns the worker ID of a child worker that is not
// currently doing anything, or an empty string. If the idle workers
// list is empty but the child workers list could have another worker,
// creates a new child. Removes the returned worker from the idle
// workers list.
//
// Returns an empty string if there is no idle child and one cannot be
// created, including an error returned by the Coordinate backend.
func (w *Worker) getIdleChild() string {
// Something in the idle workers list? Just pick one
if len(w.idleWorkers) > 0 {
child := w.idleWorkers[0]
w.idleWorkers = w.idleWorkers[1:]
return child
}
// Can we support another worker? Create one
if len(w.childWorkers) < w.Concurrency {
id := uuid.NewV4().String()
child, err := w.Namespace.Worker(id)
if err == nil {
err = child.SetParent(w.parentWorker)
}
if err == nil {
w.childWorkers[id] = child
return id
}
if w.ErrorHandler != nil {
w.ErrorHandler(err)
}
return ""
}
// Otherwise we're busy
return ""
}
// returnIdleChild puts a child worker back into the idle children
// list, or if the system is idle, shuts down the worker.
func (w *Worker) returnIdleChild(id string) {
child := w.childWorkers[id]
if w.systemIdle {
delete(w.childWorkers, id)
w.cancellations.Delete(id)
err := child.Deactivate()
if err != nil && w.ErrorHandler != nil {
w.ErrorHandler(err)
}
} else {
w.idleWorkers = append(w.idleWorkers, id)
}
}
// maybeDoWork spawns a new goroutine to do work, if there is an idle
// child worker. If the system is idle, a new goroutine is never
// generated unless evenIfIdle is true.
func (w *Worker) maybeDoWork(ctx context.Context, gotWork chan<- bool, finished chan<- string, evenIfIdle bool) {
if w.systemIdle && !evenIfIdle {
return
}
child := w.getIdleChild()
if child == "" {
return
}
go w.doWork(ctx, child, w.childWorkers[child], gotWork, finished)
}
// doWork gets attempts and runs them. It assumes it is running in its
// own goroutine. It signals gotWork when the call to RequestAttempts
// returns, and signals finished immediately before returning.
func (w *Worker) doWork(ctx context.Context, id string, worker coordinate.Worker, gotWork chan<- bool, finished chan<- string) {
// When we finish, signal the finished channel with our own ID
defer func() {
finished <- id
}()
attempts, err := worker.RequestAttempts(coordinate.AttemptRequest{
Runtimes: w.runtimes(),
NumberOfWorkUnits: w.MaxAttempts,
})
if err != nil {
// Handle the error if we can, but otherwise act just like
// we got no attempts back
if w.ErrorHandler != nil {
w.ErrorHandler(err)
}
gotWork <- false
return
}
if len(attempts) == 0 {
// Nothing to do
gotWork <- false
return
}
// Otherwise we have actual work (and at least one attempt).
gotWork <- true
// See if we can find a task for the work spec
spec := attempts[0].WorkUnit().WorkSpec()
task := spec.Name()
data, err := spec.Data()
if err == nil {
aTask, present := data["task"]
if present {
bTask, ok := aTask.(string)
if ok {
task = bTask
}
}
}
// Try to find the task function
var taskFn func(context.Context, []coordinate.Attempt)
if err == nil {
taskFn = w.Tasks[task]
if taskFn == nil {
err = fmt.Errorf("No such task function %q", task)
}
}
if err == nil {
taskCtx, cancellation := context.WithCancel(ctx)
w.cancellations.Store(id, cancellation)
taskFn(taskCtx, attempts)
// It appears to be recommended to call this; calling
// it multiple times is documented to have no effect
cancellation()
} else {
failure := map[string]interface{}{
"traceback": err.Error(),
}
// Try to fail all the attempts, ignoring errors
for _, attempt := range attempts {
_ = attempt.Fail(failure)
}
}
}
// runtimes returns the configured runtimes for this worker. If no
// runtimes are configured, then a default value, ["go"], is returned.
func (w *Worker) runtimes() []string {
if w.Runtimes == nil {
return []string{"go"}
}
return w.Runtimes
}
// heartbeat reports the current status of the parent worker.
func (w *Worker) heartbeat() {
data := map[string]interface{}{
"cpus": runtime.NumCPU(),
"go": runtime.Version(),
"goroutines": runtime.NumGoroutine(),
"pid": os.Getpid(),
}
hostname, err := os.Hostname()
if err == nil {
data["hostname"] = hostname
}
interfaces, err := net.Interfaces()
if err == nil {
var ipaddrs []string
for _, interf := range interfaces {
addrs, err := interf.Addrs()
if err == nil {
for _, addr := range addrs {
ipaddrs = append(ipaddrs, addr.String())
}
}
}
if len(ipaddrs) > 0 {
data["ipaddrs"] = ipaddrs
}
}
// The Python worker publishes quite a bit of extra
// information here: "version" is the version of the
// coordinate package; "working_set" is the complete pip
// environment; "memory" is a dictionary with system memory
// statistics.
//
// I think this is rarely examined. Some of it is hard to get
// portably. Even the information that's retrieved above
// could be uninformative if a worker is run in a Docker
// container (e.g. the pid will always be 1 and the IP address
// an uninformative host-local address).
now := w.Clock.Now()
then := now.Add(time.Duration(15) * time.Minute)
err = w.parentWorker.Update(data, now, then, "RUN")
if err != nil && w.ErrorHandler != nil {
w.ErrorHandler(err)
}
}
// findStaleUnits looks for work units assigned to child workers of w
// that are about to run out of time, and cancels them.
func (w *Worker) findStaleUnits() {
// Scan our childrens' attempts to see if any are expiring
attempts, err := w.parentWorker.ChildAttempts()
if err != nil {
return
}
now := w.Clock.Now()
childrenToCancel := make(map[string]struct{})
for _, attempt := range attempts {
exp, err := attempt.ExpirationTime()
if err == nil {
remaining := exp.Sub(now)
if remaining < expirationWarning {
// TODO(dmaze): if the attempt just
// has a really short lease time, we
// should let at least half its
// possible time elapse
child := attempt.Worker().Name()
childrenToCancel[child] = struct{}{}
}
if remaining < expirationAlarm {
// Proactively fail the attempt
err = attempt.Fail(map[string]interface{}{
"traceback": "timed out",
})
}
}
}
// If anything is expiring (we have a list of child worker IDs,
// not specific attempts) call their cancellation functions
for child := range childrenToCancel {
cancellation, ok := w.cancellations.Load(child)
// *should* always be there but doesn't hurt to check
if ok {
cancellation.(context.CancelFunc)()
}
}
}