-
Notifications
You must be signed in to change notification settings - Fork 6
/
xport_grpc_out.go
550 lines (477 loc) · 12.6 KB
/
xport_grpc_out.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
//
// This module provides for dialin from downstream clients wishing to
// consume anything handled by the pipeline over a grpc session.
package main
import (
"encoding/json"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"net"
"sync"
"time"
)
const (
// Maximum time in seconds to wait when draining queues on
// shutdown
DRAINTIMEOUT = 5
)
//
// Configuration block represents desired configuration of server.
type grpcOutLocalServerConfig struct {
name string
//
// Server socket to listen on
server string
//
// Desired output stream spec
streamSpec *dataMsgStreamSpec
//
// TLS configuration
tls bool
tls_pem string
tls_key string
tls_servername string
//
// Channel depth
dataChannelDepth int
//
// Logging?
logData bool
}
type grpcOutLocalServer struct {
cfg *grpcOutLocalServerConfig
//
// Control channel used to control the gRPC server
ctrlChan <-chan *ctrlMsg
//
// Data channels feeding output
dataChan <-chan dataMsg
//
// wait group used to wait on children draining and shutting down
childrenWG sync.WaitGroup
//
// Used to signal that all children have shutdown up to outer
// loop
childrenDone chan struct{}
//
// Setup a consistent log entry to build logging from
logctx *log.Entry
//
// Channels to the child connections
childrenMutex sync.Mutex
childrenIndex uint64
children map[uint64]*grpcOutConnToClient
}
func grpcOutLocalServerFromConfig(
cfg *grpcOutLocalServerConfig) *grpcOutLocalServer {
s := &grpcOutLocalServer{cfg: cfg}
s.childrenDone = make(chan struct{})
s.children = make(map[uint64]*grpcOutConnToClient)
return s
}
type grpcOutConnToClient struct {
endpoint *peer.Peer
dataChan chan *[]byte
}
//
// Add a channel
func (s *grpcOutLocalServer) addChild(client *grpcOutConnToClient) uint64 {
s.childrenMutex.Lock()
defer s.childrenMutex.Unlock()
s.childrenWG.Add(1)
s.childrenIndex++
s.children[s.childrenIndex] = client
return s.childrenIndex
}
//
// removeChildUnprotected removes child expecting caller
// to have set up mutex protection
func (s *grpcOutLocalServer) removeChildUnprotected(index uint64) {
c, ok := s.children[index]
if ok {
// delete is a noop if index did not exist, but we are in the
// if ok part because we also want to close the channel
delete(s.children, index)
close(c.dataChan)
}
}
//
// Remove a channel
func (s *grpcOutLocalServer) removeChild(index uint64) {
s.childrenMutex.Lock()
defer s.childrenMutex.Unlock()
s.removeChildUnprotected(index)
}
//
// Remove all channels
func (s *grpcOutLocalServer) removeAllChildren() {
s.childrenMutex.Lock()
defer s.childrenMutex.Unlock()
for k, _ := range s.children {
s.removeChildUnprotected(k)
}
}
//
// Remove all channels
func (s *grpcOutLocalServer) feedAllChildren(data *[]byte) {
s.childrenMutex.Lock()
defer s.childrenMutex.Unlock()
for _, v := range s.children {
if len(v.dataChan) == cap(v.dataChan) {
// Drop and count to avoid head of line blocking
grpcOutMetaMonitor.CountersDrops.WithLabelValues(
s.cfg.name, v.endpoint.Addr.String()).Inc()
} else {
v.dataChan <- data
grpcOutMetaMonitor.CountersMsgs.WithLabelValues(
s.cfg.name, v.endpoint.Addr.String()).Inc()
grpcOutMetaMonitor.CountersBytes.WithLabelValues(
s.cfg.name, v.endpoint.Addr.String()).Add(
float64(len(*data)))
}
}
}
//
// Conduit to connected client.
func (s *grpcOutLocalServer) Pull(req *SubJSONReqMsg, gs GRPCOut_PullServer) error {
var endpoint *peer.Peer
var err error
var ok bool
streamCtx := gs.Context()
if endpoint, ok = peer.FromContext(streamCtx); !ok {
endpoint = &peer.Peer{
Addr: &dummyPeer,
}
}
rep := &SubJSONRepMsg{
ReqId: req.ReqId,
}
msgChan := make(chan *[]byte, s.cfg.dataChannelDepth)
childId := s.addChild(&grpcOutConnToClient{
endpoint: endpoint,
dataChan: msgChan,
})
s.logctx.WithFields(
log.Fields{
"ReqId": req.ReqId,
"ChildId": childId,
"peer": endpoint.Addr.String(),
}).Info("Downstream connection pulling")
//
// Loop through waiting for messages from upstream, close or
// error.
for {
select {
case msg, ok := <-msgChan:
if !ok {
s.logctx.WithFields(
log.Fields{
"peer": endpoint.Addr.String(),
}).Info("Upstream channel closed")
goto out
}
//
// Handle collected message
rep.Data = *msg
if s.cfg.logData {
s.logctx.WithFields(
log.Fields{
"peer": endpoint.Addr.String(),
"reply": rep,
}).Debug("sending")
}
err = gs.Send(rep)
if err != nil {
grpcOutMetaMonitor.CountersErrors.WithLabelValues(
s.cfg.name, endpoint.Addr.String()).Inc()
s.logctx.WithError(err).WithFields(
log.Fields{
"peer": endpoint.Addr.String(),
}).Error("Failing to send to peer")
goto out
}
case <-streamCtx.Done():
s.logctx.WithFields(
log.Fields{
"peer": endpoint.Addr.String(),
}).Info("Client cancelled stream")
goto out
}
}
out:
s.removeChild(childId) // if necessary
s.childrenWG.Done()
s.logctx.WithFields(
log.Fields{
"peer": endpoint.Addr.String(),
}).Info("Quit client stream")
return nil
}
func (s *grpcOutLocalServer) stickyLoop(
ctx context.Context) {
var opts []grpc.ServerOption
if s.cfg.tls {
creds, err := credentials.NewServerTLSFromFile(
s.cfg.tls_pem, s.cfg.tls_key)
if err != nil {
s.logctx.WithFields(log.Fields{
"tls_pem": s.cfg.tls_pem,
"tls_key": s.cfg.tls_key,
}).WithError(err).Error(GRPCLOGPRE +
"TLS configured but failed setup, fatal, no retry")
return
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
RegisterGRPCOutServer(grpcServer, s)
lis, err := net.Listen("tcp", s.cfg.server)
if err != nil {
// We simply close the childrenDone channel to retry.
s.logctx.WithError(err).Error(
"Listen for downstream sessions failed, retrying")
close(s.childrenDone)
return
}
acceptFailed := make(chan struct{})
go func() {
//
// Serve returns if accept failed.
s.logctx.Info("Start accepting downstream sessions")
grpcServer.Serve(lis)
close(acceptFailed)
}()
//
// Caller can cancel (e.g. if we are shutting down section), or
// accept may fail. In either case, we will stop the server,
// indicate we're going and return. Caller will decide whether we
// get to come again or not.
//
for {
select {
case dM, ok := <-s.dataChan:
//
// Handle messages and then broadcast to downstream children
// to send out
if !ok {
// Channel has been closed. Our demise is near. SHUTDOWN
// is likely to be received soon on control channel. While
// the only message we receive is shutdown, this could
// have done without a control channel and instead should
// have used range over the channel.
//
s.dataChan = nil
continue
}
if s.cfg.logData {
s.logctx.WithFields(
log.Fields{
"peer": "common",
"msg": dM.getDataMsgDescription(),
}).Debug("collected message from upstream")
}
err, rawstream := dM.produceByteStream(s.cfg.streamSpec)
if err != nil {
s.logctx.WithError(err).WithFields(log.Fields{
"msg": dM.getDataMsgDescription(),
}).Error("gRPC out feeder loop, failed message")
} else {
s.feedAllChildren(&rawstream)
}
case <-ctx.Done():
//
// Serve returns if accept failed.
s.logctx.Info("Stop accepting downstream sessions (from conductor)")
goto out
case <-acceptFailed:
//
// Serve returns if accept failed.
s.logctx.Info("Stop accepting downstream sessions (accept failed)")
goto out
}
}
out:
s.removeAllChildren()
waitDone := make(chan struct{})
waitTimedOut := make(chan struct{})
go func() {
defer close(waitDone)
s.childrenWG.Wait()
}()
go func() {
defer close(waitTimedOut)
time.Sleep(time.Second * DRAINTIMEOUT)
// could have used timer, but want to close channel
}()
//
// Block waiting for all children to drain, or on timeout
select {
case <-waitTimedOut:
case <-waitDone:
}
grpcServer.Stop()
//
// Signal we are done to outermost
close(s.childrenDone)
return
}
func (s *grpcOutLocalServer) outermostLoop(
dataChan <-chan dataMsg,
ctrlChan <-chan *ctrlMsg) {
var cancel context.CancelFunc
var ctx context.Context
s.logctx = logger.WithFields(log.Fields{
"name": s.cfg.name,
"server": s.cfg.server,
"streamSpec": s.cfg.streamSpec,
})
s.dataChan = dataChan
s.ctrlChan = ctrlChan
go func() {
// Prime the pump, and kick off the first retry.
close(s.childrenDone)
}()
s.logctx.Info("gRPC out starting block")
for {
select {
case <-s.childrenDone:
//
// If we receive childrenDone signal here, we need to retry.
// Start by making a new channel.
ctx, cancel = context.WithCancel(context.Background())
s.childrenDone = make(chan struct{})
go s.stickyLoop(ctx)
//
// Ensure that if we bailed out right away we
// do not reschedule immediately.
time.Sleep(XPORT_GRPC_WAIT_TO_REDIAL * time.Second)
case msg := <-s.ctrlChan:
switch msg.id {
case REPORT:
//
// stats need cleaning up, we should be using prom
// scraping alone at this point
var stats msgStats
content, _ := json.Marshal(stats)
resp := &ctrlMsg{
id: ACK,
content: content,
respChan: nil,
}
msg.respChan <- resp
case SHUTDOWN:
s.logctx.WithFields(
log.Fields{
"ctrl_msg_id": msg.id,
}).Info("gRPC out loop, " +
"rxed SHUTDOWN, closing connections")
//
// Flag cancellation of binding and its connections
// and wait for cancellation to complete
// synchronously.
if cancel != nil {
s.logctx.Debug("gRPC out waiting for children")
cancel()
<-s.childrenDone
} else {
s.logctx.Debug("grpc out NOT waiting for children")
}
s.logctx.Debug("gRPC server notify conductor binding is closed")
resp := &ctrlMsg{
id: ACK,
respChan: nil,
}
msg.respChan <- resp
return
default:
s.logctx.Error("gRPC out block loop, unknown ctrl message")
}
}
}
}
type grpcOutputModule struct{}
func grpcOutputModuleNew() outputNodeModule {
return &grpcOutputModule{}
}
func (m *grpcOutputModule) configure(
name string,
nc nodeConfig) (error, chan<- dataMsg, chan<- *ctrlMsg) {
var cfg grpcOutLocalServerConfig
cfg.name = name
server, err := nc.config.GetString(name, "listen")
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
}).Error("'listen' option required for gRPC output")
return err, nil, nil
}
cfg.server = server
err, cfg.streamSpec = dataMsgStreamSpecFromConfig(nc, name)
if err != nil {
logger.WithError(err).WithFields(
log.Fields{
"name": name,
}).Error("'encoding' option for gRPC output")
return err, nil, nil
}
cfg.logData, _ = nc.config.GetBool(name, "logdata")
cfg.tls, _ = nc.config.GetBool(name, "tls")
cfg.tls_pem, _ = nc.config.GetString(name, "tls_pem")
cfg.tls_servername, _ = nc.config.GetString(name, "tls_servername")
cfg.tls_key, _ = nc.config.GetString(name, "tls_key")
cfg.dataChannelDepth, err = nc.config.GetInt(name, "datachanneldepth")
if err != nil {
cfg.dataChannelDepth = DATACHANNELDEPTH
}
ctrlChan := make(chan *ctrlMsg)
dataChan := make(chan dataMsg, cfg.dataChannelDepth)
// Kick off the real action. Get a server, and run it.
s := grpcOutLocalServerFromConfig(&cfg)
go s.outermostLoop(dataChan, ctrlChan)
return nil, dataChan, ctrlChan
}
type grpcOutMetaMonitorType struct {
CountersMsgs *prometheus.CounterVec
CountersBytes *prometheus.CounterVec
CountersErrors *prometheus.CounterVec
CountersDrops *prometheus.CounterVec
}
var grpcOutMetaMonitor *grpcOutMetaMonitorType
func init() {
grpcOutMetaMonitor = &grpcOutMetaMonitorType{
CountersMsgs: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "grpcOut_messages",
Help: "Messages",
},
[]string{"section", "peer"}),
CountersBytes: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "grpcOut_bytes",
Help: "Bytes",
},
[]string{"section", "peer"}),
CountersErrors: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "grpcOut_errors",
Help: "Errors",
},
[]string{"section", "peer"}),
CountersDrops: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "grpcOut_drops",
Help: "Drops",
},
[]string{"section", "peer"}),
}
// Dump content
prometheus.MustRegister(grpcOutMetaMonitor.CountersMsgs)
prometheus.MustRegister(grpcOutMetaMonitor.CountersBytes)
prometheus.MustRegister(grpcOutMetaMonitor.CountersErrors)
prometheus.MustRegister(grpcOutMetaMonitor.CountersDrops)
}