-
Notifications
You must be signed in to change notification settings - Fork 0
/
topic.go
350 lines (300 loc) · 7.97 KB
/
topic.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
package main
import (
"bytes"
"errors"
"github.com/bitly/nsq/nsq"
"github.com/bitly/nsq/util"
"log"
"sync"
"sync/atomic"
)
type Topic struct {
// 64bit atomic vars need to be first for proper alignment on 32bit platforms
messageCount uint64
sync.RWMutex
name string
channelMap map[string]*Channel
backend BackendQueue
incomingMsgChan chan *nsq.Message
memoryMsgChan chan *nsq.Message
exitChan chan int
channelUpdateChan chan int
waitGroup util.WaitGroupWrapper
exitFlag int32
options *nsqdOptions
context *Context
}
// Topic constructor
func NewTopic(topicName string, context *Context) *Topic {
diskQueue := NewDiskQueue(topicName,
context.nsqd.options.dataPath,
context.nsqd.options.maxBytesPerFile,
context.nsqd.options.syncEvery,
context.nsqd.options.syncTimeout)
t := &Topic{
name: topicName,
channelMap: make(map[string]*Channel),
backend: diskQueue,
incomingMsgChan: make(chan *nsq.Message, 1),
memoryMsgChan: make(chan *nsq.Message, context.nsqd.options.memQueueSize),
exitChan: make(chan int),
channelUpdateChan: make(chan int),
context: context,
}
t.waitGroup.Wrap(func() { t.router() })
t.waitGroup.Wrap(func() { t.messagePump() })
go t.context.nsqd.Notify(t)
return t
}
// Exiting returns a boolean indicating if this topic is closed/exiting
func (t *Topic) Exiting() bool {
return atomic.LoadInt32(&t.exitFlag) == 1
}
// GetChannel performs a thread safe operation
// to return a pointer to a Channel object (potentially new)
// for the given Topic
func (t *Topic) GetChannel(channelName string) *Channel {
t.Lock()
channel, isNew := t.getOrCreateChannel(channelName)
t.Unlock()
if isNew {
// update messagePump state
select {
case t.channelUpdateChan <- 1:
case <-t.exitChan:
}
}
return channel
}
// this expects the caller to handle locking
func (t *Topic) getOrCreateChannel(channelName string) (*Channel, bool) {
channel, ok := t.channelMap[channelName]
if !ok {
deleteCallback := func(c *Channel) {
t.DeleteExistingChannel(c.name)
}
channel = NewChannel(t.name, channelName, t.context, deleteCallback)
t.channelMap[channelName] = channel
log.Printf("TOPIC(%s): new channel(%s)", t.name, channel.name)
return channel, true
}
return channel, false
}
func (t *Topic) GetExistingChannel(channelName string) (*Channel, error) {
t.RLock()
defer t.RUnlock()
channel, ok := t.channelMap[channelName]
if !ok {
return nil, errors.New("channel does not exist")
}
return channel, nil
}
// DeleteExistingChannel removes a channel from the topic only if it exists
func (t *Topic) DeleteExistingChannel(channelName string) error {
t.Lock()
channel, ok := t.channelMap[channelName]
if !ok {
t.Unlock()
return errors.New("channel does not exist")
}
delete(t.channelMap, channelName)
// not defered so that we can continue while the channel async closes
t.Unlock()
log.Printf("TOPIC(%s): deleting channel %s", t.name, channel.name)
// delete empties the channel before closing
// (so that we dont leave any messages around)
channel.Delete()
// update messagePump state
select {
case t.channelUpdateChan <- 1:
case <-t.exitChan:
}
return nil
}
// PutMessage writes to the appropriate incoming message channel
func (t *Topic) PutMessage(msg *nsq.Message) error {
t.RLock()
defer t.RUnlock()
if atomic.LoadInt32(&t.exitFlag) == 1 {
return errors.New("exiting")
}
t.incomingMsgChan <- msg
atomic.AddUint64(&t.messageCount, 1)
return nil
}
func (t *Topic) PutMessages(messages []*nsq.Message) error {
t.RLock()
defer t.RUnlock()
if atomic.LoadInt32(&t.exitFlag) == 1 {
return errors.New("exiting")
}
for _, m := range messages {
t.incomingMsgChan <- m
atomic.AddUint64(&t.messageCount, 1)
}
return nil
}
func (t *Topic) Depth() int64 {
return int64(len(t.memoryMsgChan)) + t.backend.Depth()
}
// messagePump selects over the in-memory and backend queue and
// writes messages to every channel for this topic
func (t *Topic) messagePump() {
var msg *nsq.Message
var buf []byte
var err error
var chans []*Channel
var memoryMsgChan chan *nsq.Message
var backendChan chan []byte
t.RLock()
for _, c := range t.channelMap {
chans = append(chans, c)
}
t.RUnlock()
if len(chans) > 0 {
memoryMsgChan = t.memoryMsgChan
backendChan = t.backend.ReadChan()
}
for {
select {
case msg = <-memoryMsgChan:
case buf = <-backendChan:
msg, err = nsq.DecodeMessage(buf)
if err != nil {
log.Printf("ERROR: failed to decode message - %s", err.Error())
continue
}
case <-t.channelUpdateChan:
chans = make([]*Channel, 0)
t.RLock()
for _, c := range t.channelMap {
chans = append(chans, c)
}
t.RUnlock()
if len(chans) == 0 {
memoryMsgChan = nil
backendChan = nil
} else {
memoryMsgChan = t.memoryMsgChan
backendChan = t.backend.ReadChan()
}
continue
case <-t.exitChan:
goto exit
}
for i, channel := range chans {
chanMsg := msg
// copy the message because each channel
// needs a unique instance but...
// fastpath to avoid copy if its the first channel
// (the topic already created the first copy)
if i > 0 {
chanMsg = nsq.NewMessage(msg.Id, msg.Body)
chanMsg.Timestamp = msg.Timestamp
}
err := channel.PutMessage(chanMsg)
if err != nil {
log.Printf("TOPIC(%s) ERROR: failed to put msg(%s) to channel(%s) - %s", t.name, msg.Id, channel.name, err.Error())
}
}
}
exit:
log.Printf("TOPIC(%s): closing ... messagePump", t.name)
}
// router handles muxing of Topic messages including
// proxying messages to memory or backend
func (t *Topic) router() {
var msgBuf bytes.Buffer
for msg := range t.incomingMsgChan {
select {
case t.memoryMsgChan <- msg:
default:
err := WriteMessageToBackend(&msgBuf, msg, t.backend)
if err != nil {
log.Printf("ERROR: failed to write message to backend - %s", err.Error())
// theres not really much we can do at this point, you're certainly
// going to lose messages...
}
}
}
log.Printf("TOPIC(%s): closing ... router", t.name)
}
// Delete empties the topic and all its channels and closes
func (t *Topic) Delete() error {
return t.exit(true)
}
// Close persists all outstanding topic data and closes all its channels
func (t *Topic) Close() error {
return t.exit(false)
}
func (t *Topic) exit(deleted bool) error {
if !atomic.CompareAndSwapInt32(&t.exitFlag, 0, 1) {
return errors.New("exiting")
}
if deleted {
log.Printf("TOPIC(%s): deleting", t.name)
// since we are explicitly deleting a topic (not just at system exit time)
// de-register this from the lookupd
go t.context.nsqd.Notify(t)
} else {
log.Printf("TOPIC(%s): closing", t.name)
}
close(t.exitChan)
t.Lock()
close(t.incomingMsgChan)
t.Unlock()
// synchronize the close of router() and messagePump()
t.waitGroup.Wait()
if deleted {
t.Lock()
for _, channel := range t.channelMap {
delete(t.channelMap, channel.name)
channel.Delete()
}
t.Unlock()
// empty the queue (deletes the backend files, too)
t.Empty()
return t.backend.Delete()
}
// close all the channels
for _, channel := range t.channelMap {
err := channel.Close()
if err != nil {
// we need to continue regardless of error to close all the channels
log.Printf("ERROR: channel(%s) close - %s", channel.name, err.Error())
}
}
// write anything leftover to disk
t.flush()
return t.backend.Close()
}
func (t *Topic) Empty() error {
for {
select {
case <-t.memoryMsgChan:
default:
goto finish
}
}
finish:
return t.backend.Empty()
}
func (t *Topic) flush() error {
var msgBuf bytes.Buffer
if len(t.memoryMsgChan) > 0 {
log.Printf("TOPIC(%s): flushing %d memory messages to backend", t.name, len(t.memoryMsgChan))
}
for {
select {
case msg := <-t.memoryMsgChan:
err := WriteMessageToBackend(&msgBuf, msg, t.backend)
if err != nil {
log.Printf("ERROR: failed to write message to backend - %s", err.Error())
}
default:
goto finish
}
}
finish:
return nil
}