forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
670 lines (562 loc) · 16.1 KB
/
channel.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
package main
import (
"bytes"
"container/heap"
"errors"
"github.com/bitly/go-nsq"
"github.com/bitly/nsq/util"
"github.com/bitly/nsq/util/pqueue"
"log"
"math"
"strings"
"sync"
"sync/atomic"
"time"
)
// the amount of time a worker will wait when idle
const defaultWorkerWait = 100 * time.Millisecond
type Consumer interface {
UnPause()
Pause()
Close() error
TimedOutMessage()
Stats() ClientStats
Empty()
}
// Channel represents the concrete type for a NSQ channel (and also
// implements the Queue interface)
//
// There can be multiple channels per topic, each with there own unique set
// of subscribers (clients).
//
// Channels maintain all client and message metadata, orchestrating in-flight
// messages, timeouts, requeueing, etc.
type Channel struct {
// 64bit atomic vars need to be first for proper alignment on 32bit platforms
requeueCount uint64
messageCount uint64
timeoutCount uint64
sync.RWMutex
topicName string
name string
context *Context
backend BackendQueue
incomingMsgChan chan *nsq.Message
memoryMsgChan chan *nsq.Message
clientMsgChan chan *nsq.Message
exitChan chan int
waitGroup util.WaitGroupWrapper
exitFlag int32
// state tracking
clients map[int64]Consumer
paused int32
ephemeralChannel bool
deleteCallback func(*Channel)
deleter sync.Once
// Stats tracking
e2eProcessingLatencyStream *util.Quantile
// TODO: these can be DRYd up
deferredMessages map[nsq.MessageID]*pqueue.Item
deferredPQ pqueue.PriorityQueue
deferredMutex sync.Mutex
inFlightMessages map[nsq.MessageID]*pqueue.Item
inFlightPQ pqueue.PriorityQueue
inFlightMutex sync.Mutex
// stat counters
bufferedCount int32
}
type inFlightMessage struct {
msg *nsq.Message
clientID int64
ts time.Time
}
// NewChannel creates a new instance of the Channel type and returns a pointer
func NewChannel(topicName string, channelName string, context *Context,
deleteCallback func(*Channel)) *Channel {
c := &Channel{
topicName: topicName,
name: channelName,
incomingMsgChan: make(chan *nsq.Message, 1),
memoryMsgChan: make(chan *nsq.Message, context.nsqd.options.MemQueueSize),
clientMsgChan: make(chan *nsq.Message),
exitChan: make(chan int),
clients: make(map[int64]Consumer),
deleteCallback: deleteCallback,
context: context,
}
if len(context.nsqd.options.E2EProcessingLatencyPercentiles) > 0 {
c.e2eProcessingLatencyStream = util.NewQuantile(
context.nsqd.options.E2EProcessingLatencyWindowTime,
context.nsqd.options.E2EProcessingLatencyPercentiles,
)
}
c.initPQ()
if strings.HasSuffix(channelName, "#ephemeral") {
c.ephemeralChannel = true
c.backend = NewDummyBackendQueue()
} else {
// backend names, for uniqueness, automatically include the topic... <topic>:<channel>
backendName := topicName + ":" + channelName
c.backend = NewDiskQueue(backendName,
context.nsqd.options.DataPath,
context.nsqd.options.MaxBytesPerFile,
context.nsqd.options.SyncEvery,
context.nsqd.options.SyncTimeout)
}
go c.messagePump()
c.waitGroup.Wrap(func() { c.router() })
c.waitGroup.Wrap(func() { c.deferredWorker() })
c.waitGroup.Wrap(func() { c.inFlightWorker() })
go c.context.nsqd.Notify(c)
return c
}
func (c *Channel) initPQ() {
pqSize := int(math.Max(1, float64(c.context.nsqd.options.MemQueueSize)/10))
c.inFlightMessages = make(map[nsq.MessageID]*pqueue.Item)
c.deferredMessages = make(map[nsq.MessageID]*pqueue.Item)
c.inFlightMutex.Lock()
c.inFlightPQ = pqueue.New(pqSize)
c.inFlightMutex.Unlock()
c.deferredMutex.Lock()
c.deferredPQ = pqueue.New(pqSize)
c.deferredMutex.Unlock()
}
// Exiting returns a boolean indicating if this channel is closed/exiting
func (c *Channel) Exiting() bool {
return atomic.LoadInt32(&c.exitFlag) == 1
}
// Delete empties the channel and closes
func (c *Channel) Delete() error {
return c.exit(true)
}
// Close cleanly closes the Channel
func (c *Channel) Close() error {
return c.exit(false)
}
func (c *Channel) exit(deleted bool) error {
if !atomic.CompareAndSwapInt32(&c.exitFlag, 0, 1) {
return errors.New("exiting")
}
if deleted {
log.Printf("CHANNEL(%s): deleting", c.name)
// since we are explicitly deleting a channel (not just at system exit time)
// de-register this from the lookupd
go c.context.nsqd.Notify(c)
} else {
log.Printf("CHANNEL(%s): closing", c.name)
}
// this forceably closes client connections
for _, client := range c.clients {
client.Close()
}
close(c.exitChan)
// handle race condition w/ things writing into incomingMsgChan
c.Lock()
close(c.incomingMsgChan)
c.Unlock()
// synchronize the close of router() and pqWorkers (2)
c.waitGroup.Wait()
if deleted {
// empty the queue (deletes the backend files, too)
c.Empty()
return c.backend.Delete()
}
// write anything leftover to disk
c.flush()
return c.backend.Close()
}
func (c *Channel) Empty() error {
c.Lock()
defer c.Unlock()
c.initPQ()
for _, client := range c.clients {
client.Empty()
}
clientMsgChan := c.clientMsgChan
for {
select {
case _, ok := <-clientMsgChan:
if !ok {
// c.clientMsgChan may be closed while in this loop
// so just remove it from the select so we can make progress
clientMsgChan = nil
}
case <-c.memoryMsgChan:
default:
goto finish
}
}
finish:
return c.backend.Empty()
}
// flush persists all the messages in internal memory buffers to the backend
// it does not drain inflight/deferred because it is only called in Close()
func (c *Channel) flush() error {
var msgBuf bytes.Buffer
// messagePump is responsible for closing the channel it writes to
// this will read until its closed (exited)
for msg := range c.clientMsgChan {
log.Printf("CHANNEL(%s): recovered buffered message from clientMsgChan", c.name)
WriteMessageToBackend(&msgBuf, msg, c.backend)
}
if len(c.memoryMsgChan) > 0 || len(c.inFlightMessages) > 0 || len(c.deferredMessages) > 0 {
log.Printf("CHANNEL(%s): flushing %d memory %d in-flight %d deferred messages to backend",
c.name, len(c.memoryMsgChan), len(c.inFlightMessages), len(c.deferredMessages))
}
for {
select {
case msg := <-c.memoryMsgChan:
err := WriteMessageToBackend(&msgBuf, msg, c.backend)
if err != nil {
log.Printf("ERROR: failed to write message to backend - %s", err.Error())
}
default:
goto finish
}
}
finish:
for _, item := range c.inFlightMessages {
msg := item.Value.(*inFlightMessage).msg
err := WriteMessageToBackend(&msgBuf, msg, c.backend)
if err != nil {
log.Printf("ERROR: failed to write message to backend - %s", err.Error())
}
}
for _, item := range c.deferredMessages {
msg := item.Value.(*nsq.Message)
err := WriteMessageToBackend(&msgBuf, msg, c.backend)
if err != nil {
log.Printf("ERROR: failed to write message to backend - %s", err.Error())
}
}
return nil
}
func (c *Channel) Depth() int64 {
return int64(len(c.memoryMsgChan)) + c.backend.Depth() + int64(atomic.LoadInt32(&c.bufferedCount))
}
func (c *Channel) Pause() error {
return c.doPause(true)
}
func (c *Channel) UnPause() error {
return c.doPause(false)
}
func (c *Channel) doPause(pause bool) error {
if pause {
atomic.StoreInt32(&c.paused, 1)
} else {
atomic.StoreInt32(&c.paused, 0)
}
c.RLock()
for _, client := range c.clients {
if pause {
client.Pause()
} else {
client.UnPause()
}
}
c.RUnlock()
c.context.nsqd.Lock()
defer c.context.nsqd.Unlock()
// pro-actively persist metadata so in case of process failure
// nsqd won't suddenly (un)pause a channel
return c.context.nsqd.PersistMetadata()
}
func (c *Channel) IsPaused() bool {
return atomic.LoadInt32(&c.paused) == 1
}
// PutMessage writes to the appropriate incoming message channel
// (which will be routed asynchronously)
func (c *Channel) PutMessage(msg *nsq.Message) error {
c.RLock()
defer c.RUnlock()
if atomic.LoadInt32(&c.exitFlag) == 1 {
return errors.New("exiting")
}
c.incomingMsgChan <- msg
atomic.AddUint64(&c.messageCount, 1)
return nil
}
// TouchMessage resets the timeout for an in-flight message
func (c *Channel) TouchMessage(clientID int64, id nsq.MessageID) error {
item, err := c.popInFlightMessage(clientID, id)
if err != nil {
return err
}
c.removeFromInFlightPQ(item)
ifMsg := item.Value.(*inFlightMessage)
currentTimeout := time.Unix(0, item.Priority)
newTimeout := currentTimeout.Add(c.context.nsqd.options.MsgTimeout)
if newTimeout.Add(c.context.nsqd.options.MsgTimeout).Sub(ifMsg.ts) >= c.context.nsqd.options.MaxMsgTimeout {
// we would have gone over, set to the max
newTimeout = ifMsg.ts.Add(c.context.nsqd.options.MaxMsgTimeout)
}
item.Priority = newTimeout.UnixNano()
err = c.pushInFlightMessage(item)
if err != nil {
return err
}
c.addToInFlightPQ(item)
return nil
}
// FinishMessage successfully discards an in-flight message
func (c *Channel) FinishMessage(clientID int64, id nsq.MessageID) error {
item, err := c.popInFlightMessage(clientID, id)
if err != nil {
return err
}
c.removeFromInFlightPQ(item)
if c.e2eProcessingLatencyStream != nil {
c.e2eProcessingLatencyStream.Insert(item.Value.(*inFlightMessage).msg.Timestamp)
}
return nil
}
// RequeueMessage requeues a message based on `time.Duration`, ie:
//
// `timeoutMs` == 0 - requeue a message immediately
// `timeoutMs` > 0 - asynchronously wait for the specified timeout
// and requeue a message (aka "deferred requeue")
//
func (c *Channel) RequeueMessage(clientID int64, id nsq.MessageID, timeout time.Duration) error {
// remove from inflight first
item, err := c.popInFlightMessage(clientID, id)
if err != nil {
return err
}
c.removeFromInFlightPQ(item)
msg := item.Value.(*inFlightMessage).msg
if timeout == 0 {
return c.doRequeue(msg)
}
// deferred requeue
return c.StartDeferredTimeout(msg, timeout)
}
// AddClient adds a client to the Channel's client list
func (c *Channel) AddClient(clientID int64, client Consumer) {
c.Lock()
defer c.Unlock()
_, ok := c.clients[clientID]
if ok {
return
}
c.clients[clientID] = client
}
// RemoveClient removes a client from the Channel's client list
func (c *Channel) RemoveClient(clientID int64) {
c.Lock()
defer c.Unlock()
_, ok := c.clients[clientID]
if !ok {
return
}
delete(c.clients, clientID)
if len(c.clients) == 0 && c.ephemeralChannel == true {
go c.deleter.Do(func() { c.deleteCallback(c) })
}
}
func (c *Channel) StartInFlightTimeout(msg *nsq.Message, clientID int64) error {
now := time.Now()
value := &inFlightMessage{msg, clientID, now}
absTs := now.Add(c.context.nsqd.options.MsgTimeout).UnixNano()
item := &pqueue.Item{Value: value, Priority: absTs}
err := c.pushInFlightMessage(item)
if err != nil {
return err
}
c.addToInFlightPQ(item)
return nil
}
func (c *Channel) StartDeferredTimeout(msg *nsq.Message, timeout time.Duration) error {
absTs := time.Now().Add(timeout).UnixNano()
item := &pqueue.Item{Value: msg, Priority: absTs}
err := c.pushDeferredMessage(item)
if err != nil {
return err
}
c.addToDeferredPQ(item)
return nil
}
// doRequeue performs the low level operations to requeue a message
func (c *Channel) doRequeue(msg *nsq.Message) error {
c.RLock()
defer c.RUnlock()
if atomic.LoadInt32(&c.exitFlag) == 1 {
return errors.New("exiting")
}
c.incomingMsgChan <- msg
atomic.AddUint64(&c.requeueCount, 1)
return nil
}
// pushInFlightMessage atomically adds a message to the in-flight dictionary
func (c *Channel) pushInFlightMessage(item *pqueue.Item) error {
c.Lock()
defer c.Unlock()
id := item.Value.(*inFlightMessage).msg.Id
_, ok := c.inFlightMessages[id]
if ok {
return errors.New("ID already in flight")
}
c.inFlightMessages[id] = item
return nil
}
// popInFlightMessage atomically removes a message from the in-flight dictionary
func (c *Channel) popInFlightMessage(clientID int64, id nsq.MessageID) (*pqueue.Item, error) {
c.Lock()
defer c.Unlock()
item, ok := c.inFlightMessages[id]
if !ok {
return nil, errors.New("ID not in flight")
}
if item.Value.(*inFlightMessage).clientID != clientID {
return nil, errors.New("client does not own message")
}
delete(c.inFlightMessages, id)
return item, nil
}
func (c *Channel) addToInFlightPQ(item *pqueue.Item) {
c.inFlightMutex.Lock()
defer c.inFlightMutex.Unlock()
heap.Push(&c.inFlightPQ, item)
}
func (c *Channel) removeFromInFlightPQ(item *pqueue.Item) {
c.inFlightMutex.Lock()
defer c.inFlightMutex.Unlock()
if item.Index == -1 {
// this item has already been Pop'd off the pqueue
return
}
heap.Remove(&c.inFlightPQ, item.Index)
}
func (c *Channel) pushDeferredMessage(item *pqueue.Item) error {
c.Lock()
defer c.Unlock()
// TODO: these map lookups are costly
id := item.Value.(*nsq.Message).Id
_, ok := c.deferredMessages[id]
if ok {
return errors.New("ID already deferred")
}
c.deferredMessages[id] = item
return nil
}
func (c *Channel) popDeferredMessage(id nsq.MessageID) (*pqueue.Item, error) {
c.Lock()
defer c.Unlock()
// TODO: these map lookups are costly
item, ok := c.deferredMessages[id]
if !ok {
return nil, errors.New("ID not deferred")
}
delete(c.deferredMessages, id)
return item, nil
}
func (c *Channel) addToDeferredPQ(item *pqueue.Item) {
c.deferredMutex.Lock()
defer c.deferredMutex.Unlock()
heap.Push(&c.deferredPQ, item)
}
// Router handles the muxing of incoming Channel messages, either writing
// to the in-memory channel or to the backend
func (c *Channel) router() {
var msgBuf bytes.Buffer
for msg := range c.incomingMsgChan {
select {
case c.memoryMsgChan <- msg:
default:
err := WriteMessageToBackend(&msgBuf, msg, c.backend)
if err != nil {
log.Printf("CHANNEL(%s) ERROR: failed to write message to backend - %s", c.name, err.Error())
// theres not really much we can do at this point, you're certainly
// going to lose messages...
}
}
}
log.Printf("CHANNEL(%s): closing ... router", c.name)
}
// messagePump reads messages from either memory or backend and writes
// to the client output go channel
//
// it is also performs in-flight accounting and initiates the auto-requeue
// goroutine
func (c *Channel) messagePump() {
var msg *nsq.Message
var buf []byte
var err error
for {
// do an extra check for closed exit before we select on all the memory/backend/exitChan
// this solves the case where we are closed and something else is draining clientMsgChan into
// backend. we don't want to reverse that
if atomic.LoadInt32(&c.exitFlag) == 1 {
goto exit
}
select {
case msg = <-c.memoryMsgChan:
case buf = <-c.backend.ReadChan():
msg, err = nsq.DecodeMessage(buf)
if err != nil {
log.Printf("ERROR: failed to decode message - %s", err.Error())
continue
}
case <-c.exitChan:
goto exit
}
msg.Attempts++
atomic.StoreInt32(&c.bufferedCount, 1)
c.clientMsgChan <- msg
atomic.StoreInt32(&c.bufferedCount, 0)
// the client will call back to mark as in-flight w/ it's info
}
exit:
log.Printf("CHANNEL(%s): closing ... messagePump", c.name)
close(c.clientMsgChan)
}
func (c *Channel) deferredWorker() {
c.pqWorker(&c.deferredPQ, &c.deferredMutex, func(item *pqueue.Item) {
msg := item.Value.(*nsq.Message)
_, err := c.popDeferredMessage(msg.Id)
if err != nil {
return
}
c.doRequeue(msg)
})
}
func (c *Channel) inFlightWorker() {
c.pqWorker(&c.inFlightPQ, &c.inFlightMutex, func(item *pqueue.Item) {
clientID := item.Value.(*inFlightMessage).clientID
msg := item.Value.(*inFlightMessage).msg
_, err := c.popInFlightMessage(clientID, msg.Id)
if err != nil {
return
}
atomic.AddUint64(&c.timeoutCount, 1)
client, ok := c.clients[clientID]
if ok {
client.TimedOutMessage()
}
c.doRequeue(msg)
})
}
// generic loop (executed in a goroutine) that periodically wakes up to walk
// the priority queue and call the callback
func (c *Channel) pqWorker(pq *pqueue.PriorityQueue, mutex *sync.Mutex, callback func(item *pqueue.Item)) {
ticker := time.NewTicker(defaultWorkerWait)
for {
select {
case <-ticker.C:
case <-c.exitChan:
goto exit
}
now := time.Now().UnixNano()
for {
mutex.Lock()
item, _ := pq.PeekAndShift(now)
mutex.Unlock()
if item == nil {
break
}
callback(item)
}
}
exit:
log.Printf("CHANNEL(%s): closing ... pqueue worker", c.name)
ticker.Stop()
}