-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmetafora.go
458 lines (403 loc) · 11.1 KB
/
metafora.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
package metafora
import (
"fmt"
"math/rand"
"runtime"
"sort"
"sync"
"time"
)
var (
// balance calls are randomized and this is the upper bound of the random
// amount
balanceJitterMax = 10 * int64(time.Second)
)
// Consumer is the core Metafora task runner.
type Consumer struct {
// Func to create new handlers
handler HandlerFunc
// Map of task:Handler
running map[string]*runtask
// Mutex to protect access to running
runL sync.Mutex
// WaitGroup for running handlers and consumer goroutines
hwg sync.WaitGroup
// WaitGroup so Shutdown() can block on Run() exiting fully
runwg sync.WaitGroup
runwgL sync.Mutex
bal Balancer
balEvery time.Duration
coord Coordinator
im *ignoremgr
stop chan struct{} // closed by Shutdown to cause Run to exit
tasks chan Task // channel for watcher to send tasks to main loop
// Set by command handler, read anywhere via Consumer.frozen()
freezeL sync.Mutex
freeze bool
}
var BalanceEvery = 15 * time.Minute //TODO make balance wait configurable
// NewConsumer returns a new consumer and calls Init on the Balancer and Coordinator.
func NewConsumer(coord Coordinator, h HandlerFunc, b Balancer) (*Consumer, error) {
c := &Consumer{
running: make(map[string]*runtask),
handler: h,
bal: b,
balEvery: BalanceEvery,
coord: coord,
stop: make(chan struct{}),
tasks: make(chan Task),
}
c.im = ignorer(c.tasks, c.stop)
// initialize balancer with the consumer and a prefixed logger
b.Init(c)
if err := coord.Init(&coordinatorContext{c}); err != nil {
return nil, err
}
return c, nil
}
// Run is the core run loop of Metafora. It is responsible for calling into the
// Coordinator to claim work and Balancer to rebalance work.
//
// Run blocks until Shutdown is called or an internal error occurs.
func (c *Consumer) Run() {
Debug(c, " Starting consumer")
// Increment run wait group so Shutdown() can block on Run() exiting fully.
c.runwgL.Lock()
c.runwg.Add(1)
c.runwgL.Unlock()
defer c.runwg.Done()
// chans for core goroutines to communicate with main loop
balance := make(chan bool)
cmdChan := make(chan Command)
// Balance is called by the main loop when the balance channel is ticked
go func() {
randInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int63n
for {
select {
case <-c.stop:
// Shutdown has been called.
return
case <-time.After(c.balEvery + time.Duration(randInt(balanceJitterMax))):
select {
case balance <- true:
// Ticked balance
case <-c.stop:
// Shutdown has been called.
return
}
}
}
}()
// Watch for new tasks in a goroutine
go c.watcher()
// Watch for new commands in a goroutine
go func() {
defer close(cmdChan)
for {
cmd, err := c.coord.Command()
if err != nil {
panic(fmt.Errorf("coordinator returned an error during command: %v", err))
}
if cmd == nil {
Debug(c, " Command coordinator exited")
return
}
// Send command to watcher (or shutdown)
select {
case <-c.stop:
return
case cmdChan <- cmd:
}
}
}()
// Make sure Run() cleans up on exit (stops coordinator, releases tasks, etc)
defer c.shutdown()
// Main Loop ensures events are processed synchronously
for {
if c.Frozen() {
// Only recv commands while frozen
select {
case <-c.stop:
// Shutdown has been called.
return
case cmd, ok := <-cmdChan:
if !ok {
Debug(c, " Command channel closed. Exiting main loop.")
return
}
Debugf("%s Received command: %s", c, cmd)
c.handleCommand(cmd)
}
continue
}
select {
case <-c.stop:
// Shutdown has been called.
return
case <-balance:
c.balance()
case task := <-c.tasks:
tid := task.ID()
if c.ignored(tid) {
Debugf("%s task=%q ignored", c, tid)
continue
}
if until, ok := c.bal.CanClaim(task); !ok {
Infof("%s Balancer rejected task=%q until %s", c, tid, until)
c.ignore(task, until)
break
}
if !c.coord.Claim(task) {
Debugf("%s Coordinator unable to claim task=%q", c, tid)
break
}
c.claimed(task)
case cmd, ok := <-cmdChan:
if !ok {
Debug(c, " Command channel closed. Exiting main loop.")
return
}
c.handleCommand(cmd)
}
}
}
func (c *Consumer) watcher() {
// The watcher dying unexpectedly should close the consumer to cause a
// shutdown.
defer c.close()
err := c.coord.Watch(c.tasks)
if err != nil {
panic(fmt.Errorf("coordinator returned an error during watch: %v", err))
}
}
func (c *Consumer) balance() {
tasks := c.bal.Balance()
if len(tasks) > 0 {
Infof("%s balancer releasing %d tasks: %v", c, len(tasks), tasks)
}
for _, task := range tasks {
// Actually release the rebalanced task.
c.stopTask(task)
}
}
// close the c.stop channel which signals for the consumer to shutdown.
func (c *Consumer) close() {
// acquire the runL lock to make sure we don't race with claimed()'s <-c.stop
// check
c.runL.Lock()
defer c.runL.Unlock()
select {
case <-c.stop:
// already stopped
default:
Debug("Stopping Run loop")
close(c.stop)
}
}
// shutdown is the actual shutdown logic called when Run() exits.
func (c *Consumer) shutdown() {
c.close()
// Build list of of currently running tasks
runningtasks := c.Tasks()
Infof("Sending stop signal to %d handler(s)", len(runningtasks))
for _, rt := range runningtasks {
c.stopTask(rt.Task().ID())
}
Info(c, " Waiting for handlers to exit")
c.hwg.Wait()
Debug("Closing Coordinator ", c)
c.coord.Close()
}
// Shutdown stops the main Run loop, calls Stop on all handlers, and calls
// Close on the Coordinator. Running tasks will be released for other nodes to
// claim.
func (c *Consumer) Shutdown() {
c.close()
// Wait for task handlers to exit.
c.hwg.Wait()
// Make sure Run() exits, otherwise Shutdown() might exit before
// coord.Close() is called.
c.runwgL.Lock()
c.runwg.Wait()
c.runwgL.Unlock()
}
// Tasks returns a lexicographically sorted list of running Task IDs.
func (c *Consumer) Tasks() []RunningTask {
c.runL.Lock()
defer c.runL.Unlock()
// Create a sorted list of task IDs
ids := make([]string, len(c.running))
i := 0
for id := range c.running {
ids[i] = id
i++
}
sort.Strings(ids)
// Add tasks in lexicographic order
t := make([]RunningTask, len(ids))
for i, id := range ids {
t[i] = c.running[id]
}
return t
}
// claimed starts a handler for a claimed task. It is the only method to
// manipulate c.running and closes the task channel when a handler's Run
// method exits.
func (c *Consumer) claimed(task Task) {
h := c.handler(task)
tid := task.ID()
Debugf("%s is attempting to start task=%q", c, tid)
// Associate handler with taskID
// **This is the only place tasks should be added to c.running**
c.runL.Lock()
defer c.runL.Unlock()
select {
case <-c.stop:
// We're closing, don't bother starting this task
c.coord.Release(task)
return
default:
}
if _, ok := c.running[tid]; ok {
// If a coordinator returns an already claimed task from Watch(), then it's
// a coordinator (or broker) bug.
Warnf("%s Attempted to claim already running task %s", c, tid)
return
}
rt := newTask(task, h)
c.running[tid] = rt
// This must be done in the runL lock after the stop chan check so Shutdown
// doesn't close(stop) and start Wait()ing concurrently.
// See "Note" http://golang.org/pkg/sync/#WaitGroup.Add
c.hwg.Add(1)
// Start handler in its own goroutine
go func() {
defer c.hwg.Done() // Must be run after task exit and Done/Release called
// Run the task
Infof("%s Task %q started", c, tid)
done := c.runTask(h.Run, tid)
var status string
if done {
status = "done"
c.coord.Done(task)
} else {
status = "released"
c.coord.Release(task)
}
stopped := rt.Stopped()
if stopped.IsZero() {
// Task exited on its own
Infof("%s Task %q exited (%s)", c, tid, status)
} else {
// Task exited due to Stop() being called
Infof("%s Task %q exited (%s) after %s", c, tid, status, time.Since(stopped))
}
// **This is the only place tasks should be removed from c.running**
c.runL.Lock()
delete(c.running, tid)
c.runL.Unlock()
}()
// Pause slightly after a successful claim to give starting tasks some
// breathing room and to bias the next claim toward a node that lost this
// one.
time.Sleep(10 * time.Millisecond)
}
// runTask executes a handler's Run method and recovers from panic()s.
func (c *Consumer) runTask(run func() bool, task string) bool {
done := false
func() {
defer func() {
if err := recover(); err != nil {
stack := make([]byte, 50*1024)
sz := runtime.Stack(stack, false)
Errorf("%s Handler %s panic()'d: %v\n%s", c, task, err, stack[:sz])
// panics are considered fatal errors. Make sure the task isn't
// rescheduled.
done = true
}
}()
done = run()
}()
return done
}
// stopTask asynchronously calls the task handlers' Stop method. While stopTask
// calls don't block, calls to task handler's Stop method are serialized with a
// lock.
func (c *Consumer) stopTask(taskID string) {
c.runL.Lock()
task, ok := c.running[taskID]
c.runL.Unlock()
if !ok {
// This can happen if a task completes during Balance() and is not an error.
Warnf("%s tried to release a non-running task=%q", c, taskID)
return
}
// all handler methods must be wrapped in a recover to prevent a misbehaving
// handler from crashing the entire consumer
go func() {
defer func() {
if err := recover(); err != nil {
stack := make([]byte, 50*1024)
sz := runtime.Stack(stack, false)
Errorf("%s Handler %s panic()'d on Stop: %v\n%s", c, taskID, err, stack[:sz])
}
}()
// Serialize calls to Stop as a convenience to handler implementors.
task.stop()
}()
}
// Frozen returns true if Metafora is no longer watching for new tasks or
// rebalancing.
//
// Metafora will remain frozen until receiving an Unfreeze command or it is
// restarted (frozen state is not persisted).
func (c *Consumer) Frozen() bool {
c.freezeL.Lock()
r := c.freeze
c.freezeL.Unlock()
return r
}
func (c *Consumer) handleCommand(cmd Command) {
switch cmd.Name() {
case cmdFreeze:
if c.Frozen() {
Info(c, " Ignoring freeze command: already frozen")
return
}
Info(c, " Freezing")
c.freezeL.Lock()
c.freeze = true
c.freezeL.Unlock()
case cmdUnfreeze:
if !c.Frozen() {
Info(c, " Ignoring unfreeze command: not frozen")
return
}
Info(c, " Unfreezing")
c.freezeL.Lock()
c.freeze = false
c.freezeL.Unlock()
case cmdBalance:
Info(c, " Balancing due to command")
c.balance()
Debug(c, " Finished balancing due to command")
case cmdStopTask:
taskI, ok := cmd.Parameters()["task"]
task, ok2 := taskI.(string)
if !ok || !ok2 {
Error(c, " Stop task command didn't contain a valid task", c)
return
}
Infof("%s Stopping task %s due to command", c, task)
c.stopTask(task)
default:
Warnf("%s Discarding unknown command: %s", c, cmd.Name())
}
}
func (c *Consumer) ignored(taskID string) bool { return c.im.ignored(taskID) }
func (c *Consumer) ignore(t Task, until time.Time) { c.im.add(t, until) }
// Ignores is a list of all ignored tasks.
func (c *Consumer) Ignores() []string { return c.im.all() }
func (c *Consumer) String() string {
return c.coord.Name()
}