-
Notifications
You must be signed in to change notification settings - Fork 19
/
workerng.go
416 lines (351 loc) · 10.5 KB
/
workerng.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
package cocaine12
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"runtime"
"syscall"
"time"
)
const (
heartbeatTimeout = time.Second * 10
disownTimeout = time.Second * 5
coreConnectionTimeout = time.Second * 5
terminationTimeout = time.Second * 5
// ErrorNoEventHandler returns when there is no handler for a given event
ErrorNoEventHandler = 200
// ErrorPanicInHandler returns when a handler is recovered from panic
ErrorPanicInHandler = 100
)
var (
// ErrDisowned raises when the worker doesn't receive
// a heartbeat message during a heartbeat timeout
ErrDisowned = errors.New("disowned from cocaine-runtime")
// ErrNoCocaineEndpoint means that the worker doesn't know an endpoint
// to Cocaine
ErrNoCocaineEndpoint = errors.New("cocaine endpoint must be specified")
// ErrConnectionLost means that the connection between the worker and
// runtime has been lost
ErrConnectionLost = errors.New("the connection to runtime has been lost")
)
type requestStream interface {
push(*Message)
Close()
}
// Request provides an interface for a handler to get data
type Request interface {
Read(ctx context.Context) ([]byte, error)
}
// ResponseStream provides an interface for a handler to reply
type ResponseStream interface {
io.WriteCloser
// ZeroCopyWrite sends data to a client.
// Response takes the ownership of the buffer, so provided buffer must not be edited.
ZeroCopyWrite(data []byte) error
ErrorMsg(code int, message string) error
}
// Response provides an interface for a handler to reply
type Response ResponseStream
func trapRecoverAndClose(ctx context.Context, event string, response Response, printStack bool) {
if recoverInfo := recover(); recoverInfo != nil {
var stack []byte
if printStack {
stack = make([]byte, 4096)
stackSize := runtime.Stack(stack, false)
stack = stack[:stackSize]
}
response.ErrorMsg(
ErrorPanicInHandler,
fmt.Sprintf("Event: '%s', recover: %s, stack: \n%s\n", event, recoverInfo, stack),
)
return
}
response.Close()
}
// WorkerNG performs IO operations between an application
// and cocaine-runtime, dispatches incoming messages
type WorkerNG struct {
// Connection to cocaine-runtime
conn socketIO
// Id to introduce myself to cocaine-runtime
id string
// Each tick we shoud send a heartbeat as keep-alive
heartbeatTimer *time.Timer
// Timeout to receive a heartbeat reply
disownTimer *time.Timer
// Token manager
tokenManager TokenManager
// Map handlers to sessions
sessions map[uint64]requestStream
// handler
handler RequestHandler
// Notify Run about stop
stopped chan struct{}
// if set recoverTrap sends Stack
debug bool
// allow the worker to handle SIGUSR1 to print all goroutines stacks
stackSignalEnabled bool
// protocol version id
protoVersion int
// protocol dispatcher
dispatcher protocolDispather
// temination handler
terminationHandler TerminationHandler
}
// NewWorkerNG connects to the cocaine-runtime and create WorkerNG on top of this connection
func NewWorkerNG() (*WorkerNG, error) {
workerID := GetDefaults().UUID()
unixSocketEndpoint := GetDefaults().Endpoint()
if unixSocketEndpoint == "" {
return nil, ErrNoCocaineEndpoint
}
tokenManager, err := NewTokenManager(GetDefaults().ApplicationName(), GetDefaults().Token())
if err != nil {
return nil, fmt.Errorf("unable to create token manager: %v", err)
}
// Connect to cocaine-runtime over a unix socket
sock, err := newUnixConnection(unixSocketEndpoint, coreConnectionTimeout)
if err != nil {
return nil, fmt.Errorf("unable to connect to Cocaine via %s: %v",
unixSocketEndpoint, err)
}
return newWorkerNG(sock, workerID,
GetDefaults().Protocol(),
GetDefaults().Debug(),
tokenManager)
}
func newWorkerNG(conn socketIO, id string, protoVersion int, debug bool, tokenManager TokenManager) (*WorkerNG, error) {
w := &WorkerNG{
conn: conn,
id: id,
heartbeatTimer: time.NewTimer(heartbeatTimeout),
disownTimer: time.NewTimer(disownTimeout),
tokenManager: tokenManager,
sessions: make(map[uint64]requestStream),
stopped: make(chan struct{}),
debug: debug,
stackSignalEnabled: true,
protoVersion: protoVersion,
dispatcher: nil,
terminationHandler: nil,
}
switch w.protoVersion {
case v1:
w.dispatcher = newV1Protocol()
default:
return nil, fmt.Errorf("unsupported protocol version %d", w.protoVersion)
}
// NewTimer launches timer
// but it should be started after
// we send heartbeat message
w.disownTimer.Stop()
// It will be reset in onHeartbeat()
// after worker runs
w.heartbeatTimer.Stop()
// Send handshake to notify cocaine-runtime
// that we have started
if err := w.sendHandshake(); err != nil {
return nil, err
}
return w, nil
}
// SetDebug enables debug mode of the Worker.
// It allows to print Stack of a paniced handler
func (w *WorkerNG) SetDebug(debug bool) {
w.debug = debug
}
// EnableStackSignal allows/disallows the worker to catch
// SIGUSR1 to print all goroutines stacks. It's enabled by default.
// This function must be called before Worker.Run to take effect.
func (w *WorkerNG) EnableStackSignal(enable bool) {
w.stackSignalEnabled = enable
}
// Token returns the most recently viewed version of the authorization token.
func (w *WorkerNG) Token() Token {
return w.tokenManager.Token()
}
// Run makes the worker anounce itself to a cocaine-runtime
// as being ready to hadnle incoming requests and hablde them
// terminationHandler allows to attach handler which will be called
// when SIGTERM arrives
func (w *WorkerNG) Run(handler RequestHandler, terminationHandler TerminationHandler) error {
w.handler = handler
w.terminationHandler = terminationHandler
return w.loop()
}
// Stop makes the Worker stop handling requests
func (w *WorkerNG) Stop() {
if w.isStopped() {
return
}
w.tokenManager.Stop()
close(w.stopped)
w.conn.Close()
}
func (w *WorkerNG) isStopped() bool {
select {
case <-w.stopped:
return true
default:
}
return false
}
func (w *WorkerNG) loop() error {
// Send heartbeat to notify cocaine-runtime
// we are ready to work
w.onHeartbeatTimeout()
var stackSignal chan os.Signal
if w.stackSignalEnabled {
stackSignal = make(chan os.Signal, 1)
signal.Notify(stackSignal, syscall.SIGUSR1)
defer signal.Stop(stackSignal)
}
for {
select {
case msg, ok := <-w.conn.Read():
if !ok {
// either the connection is lost
// or the worker was stopped
select {
case <-w.stopped:
return nil
default:
return ErrConnectionLost
}
}
// non-blocking
if err := w.dispatcher.onMessage(w, msg); err != nil {
fmt.Printf("onMessage returns %v\n", err)
}
case <-w.heartbeatTimer.C:
// Reset (start) disown & heartbeat timers
// Send a heartbeat message to cocaine-runtime
w.onHeartbeatTimeout() // non-blocking
case <-w.disownTimer.C:
w.onDisownTimeout() // non-blocking
return ErrDisowned
case <-w.stopped:
return nil
case <-stackSignal:
w.printAllStacks()
}
}
}
// printAllStacks prints all stacks to stderr and writes to a file
func (w *WorkerNG) printAllStacks() {
stackTrace := dumpStack()
// print to stdout to have it in the logs
fmt.Printf("=== START STACKTRACE ===\n%s\n=== END STACKTRACE ===", stackTrace)
// to debug blocked workers. It will be removed somewhen
filename := fmt.Sprintf("%s-%d", GetDefaults().ApplicationName(), os.Getpid())
if err := ioutil.WriteFile(filename, stackTrace, 0660); err != nil {
fmt.Printf("unable to create the file with stacktraces %s: %v\n", filename, err)
}
}
// A reply to heartbeat is not arrived during disownTimeout,
// so it seems cocaine-runtime has died
func (w *WorkerNG) onDisownTimeout() {
w.Stop()
}
func (w *WorkerNG) onHeartbeatTimeout() {
// Wait for the reply until disown timeout comes
w.disownTimer.Reset(disownTimeout)
// Send next heartbeat over heartbeatTimeout
w.heartbeatTimer.Reset(heartbeatTimeout)
select {
case w.conn.Write() <- w.dispatcher.newHeartbeat():
case <-w.conn.IsClosed():
case <-time.After(disownTimeout):
}
}
// Send handshake message to cocaine-runtime
// It is needed to be called only once on a startup
// to notify runtime that we have started
func (w *WorkerNG) sendHandshake() error {
select {
case w.conn.Write() <- w.dispatcher.newHandshake(w.id):
case <-w.conn.IsClosed():
case <-time.After(disownTimeout):
return fmt.Errorf("unable to send a handshake for a long time")
}
return nil
}
// Message handlers
func (w *WorkerNG) onChoke(msg *Message) {
if reqStream, ok := w.sessions[msg.Session]; ok {
reqStream.Close()
delete(w.sessions, msg.Session)
}
}
func (w *WorkerNG) onChunk(msg *Message) {
if reqStream, ok := w.sessions[msg.Session]; ok {
reqStream.push(msg)
}
}
func (w *WorkerNG) onError(msg *Message) {
if reqStream, ok := w.sessions[msg.Session]; ok {
reqStream.push(msg)
}
}
func (w *WorkerNG) onInvoke(msg *Message) error {
event, ok := getEventName(msg)
if !ok {
// corrupted message
return fmt.Errorf("unable to get an event name from %s", msg.String())
}
var (
currentSession = msg.Session
ctx context.Context
)
ctx = context.Background()
if traceInfo, err := msg.Headers.getTraceData(); err == nil {
ctx = AttachTraceInfo(ctx, traceInfo)
}
responseStream := newResponse(w.dispatcher, currentSession, w.conn)
requestStream := newRequest(w.dispatcher)
w.sessions[currentSession] = requestStream
go func() {
// this trap catches a panic from a handler
// and checks if the response is closed.
defer trapRecoverAndClose(ctx, event, responseStream, w.debug)
ctx, closeHandlerSpan := NewSpan(ctx, event)
defer closeHandlerSpan()
w.handler(ctx, event, requestStream, responseStream)
}()
return nil
}
func (w *WorkerNG) onHeartbeat(msg *Message) {
// Reply to a heartbeat has been received,
// so we are not disowned & disownTimer must be stopped
// It will be launched when the next heartbeat is sent
w.disownTimer.Stop()
}
func (w *WorkerNG) onTerminate(msg *Message) {
if w.terminationHandler != nil {
ctx, cancelTimeout := context.WithTimeout(context.Background(), terminationTimeout)
onDone := make(chan struct{})
go func() {
w.terminationHandler(ctx)
close(onDone)
}()
select {
case <-onDone:
cancelTimeout()
case <-ctx.Done():
fmt.Printf("terminationHandler timeouted: %v\n", ctx.Err())
}
}
// According to spec we have time
// to prepare for being killed by cocaine-runtime
select {
case w.conn.Write() <- msg:
// reply with the same termination message
case <-w.conn.IsClosed():
case <-time.After(disownTimeout):
}
w.Stop()
}