/
protocol_v2.go
566 lines (467 loc) · 15.5 KB
/
protocol_v2.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
package main
import (
"../nsq"
"../util"
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
"net"
"sync/atomic"
"time"
)
const maxTimeout = time.Hour
type ProtocolV2 struct {
nsq.Protocol
}
func init() {
protocols[string(nsq.MagicV2)] = &ProtocolV2{}
}
func (p *ProtocolV2) IOLoop(conn net.Conn) error {
var err error
var line []byte
client := NewClientV2(conn)
go p.messagePump(client)
for {
client.SetReadDeadline(time.Now().Add(nsqd.options.clientTimeout))
// ReadSlice does not allocate new space for the data each request
// ie. the returned slice is only valid until the next call to it
line, err = client.Reader.ReadSlice('\n')
if err != nil {
break
}
// trim the '\n'
line = line[:len(line)-1]
// optionally trim the '\r'
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
params := bytes.Split(line, []byte(" "))
if *verbose {
log.Printf("PROTOCOL(V2): [%s] %s", client, params)
}
response, err := p.Exec(client, params)
if err != nil {
context := ""
if parentErr := err.(nsq.ChildError).Parent(); parentErr != nil {
context = " - " + parentErr.Error()
}
log.Printf("ERROR: [%s] - %s%s", client, err.Error(), context)
err = p.Send(client, nsq.FrameTypeError, []byte(err.Error()))
if err != nil {
break
}
// errors of type FatalClientErr should forceably close the connection
if _, ok := err.(*nsq.FatalClientErr); ok {
break
}
continue
}
if response != nil {
err = p.Send(client, nsq.FrameTypeResponse, response)
if err != nil {
break
}
}
}
log.Printf("PROTOCOL(V2): [%s] exiting ioloop", client)
// TODO: gracefully send clients the close signal
conn.Close()
close(client.ExitChan)
return err
}
func (p *ProtocolV2) SendMessage(client *ClientV2, msg *nsq.Message, buf *bytes.Buffer) error {
if *verbose {
log.Printf("PROTOCOL(V2): writing msg(%s) to client(%s) - %s",
msg.Id, client, msg.Body)
}
buf.Reset()
err := msg.Write(buf)
if err != nil {
return err
}
client.Channel.StartInFlightTimeout(msg, client)
client.SendingMessage()
err = p.Send(client, nsq.FrameTypeMessage, buf.Bytes())
if err != nil {
return err
}
return nil
}
func (p *ProtocolV2) Send(client *ClientV2, frameType int32, data []byte) error {
client.Lock()
defer client.Unlock()
client.SetWriteDeadline(time.Now().Add(time.Second))
_, err := nsq.SendFramedResponse(client.Writer, frameType, data)
if err != nil {
return err
}
if frameType != nsq.FrameTypeMessage {
err = client.Writer.Flush()
}
return err
}
func (p *ProtocolV2) Flush(client *ClientV2) error {
client.Lock()
defer client.Unlock()
if client.Writer.Buffered() > 0 {
client.SetWriteDeadline(time.Now().Add(time.Second))
return client.Writer.Flush()
}
return nil
}
func (p *ProtocolV2) Exec(client *ClientV2, params [][]byte) ([]byte, error) {
switch {
case bytes.Equal(params[0], []byte("IDENTIFY")):
return p.IDENTIFY(client, params)
case bytes.Equal(params[0], []byte("SUB")):
return p.SUB(client, params)
case bytes.Equal(params[0], []byte("RDY")):
return p.RDY(client, params)
case bytes.Equal(params[0], []byte("FIN")):
return p.FIN(client, params)
case bytes.Equal(params[0], []byte("REQ")):
return p.REQ(client, params)
case bytes.Equal(params[0], []byte("CLS")):
return p.CLS(client, params)
case bytes.Equal(params[0], []byte("NOP")):
return p.NOP(client, params)
case bytes.Equal(params[0], []byte("PUB")):
return p.PUB(client, params)
case bytes.Equal(params[0], []byte("MPUB")):
return p.MPUB(client, params)
case bytes.Equal(params[0], []byte("TOUCH")):
return p.TOUCH(client, params)
}
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", fmt.Sprintf("invalid command %s", params[0]))
}
func (p *ProtocolV2) messagePump(client *ClientV2) {
var err error
var buf bytes.Buffer
var clientMsgChan chan *nsq.Message
var subChannel *Channel
var flusherChan <-chan time.Time
// v2 opportunistically buffers data to clients to reduce write system calls
// we force flush in two cases:
// 1. when the client is not ready to receive messages
// 2. we're buffered and the channel has nothing left to send us
// (ie. we would block in this loop anyway)
//
// NOTE: `flusher` is used to bound message latency for
// the pathological case of a channel on a low volume topic
// with >1 clients having >1 RDY counts
flusher := time.NewTicker(5 * time.Millisecond)
heartbeat := time.NewTicker(nsqd.options.clientTimeout / 2)
flushed := true
subEventChan := client.SubEventChan
for {
if subChannel == nil || !client.IsReadyForMessages() {
// the client is not ready to receive messages...
clientMsgChan = nil
flusherChan = nil
// force flush
err = p.Flush(client)
if err != nil {
goto exit
}
flushed = true
} else if flushed {
// last iteration we flushed...
// do not select on the flusher ticker channel
clientMsgChan = subChannel.clientMsgChan
flusherChan = nil
} else {
// we're buffered (if there isn't any more data we should flush)...
// select on the flusher ticker channel, too
clientMsgChan = subChannel.clientMsgChan
flusherChan = flusher.C
}
select {
case <-flusherChan:
// if this case wins, we're either starved
// or we won the race between other channels...
// in either case, force flush
err = p.Flush(client)
if err != nil {
goto exit
}
flushed = true
case subChannel = <-subEventChan:
// you can't subscribe anymore
subEventChan = nil
case <-client.ReadyStateChan:
case <-heartbeat.C:
err = p.Send(client, nsq.FrameTypeResponse, []byte("_heartbeat_"))
if err != nil {
log.Printf("PROTOCOL(V2): error sending heartbeat - %s", err.Error())
}
case msg, ok := <-clientMsgChan:
if !ok {
goto exit
}
err = p.SendMessage(client, msg, &buf)
if err != nil {
goto exit
}
flushed = false
case <-client.ExitChan:
goto exit
}
}
exit:
log.Printf("PROTOCOL(V2): [%s] exiting messagePump", client)
heartbeat.Stop()
flusher.Stop()
if subChannel != nil {
subChannel.RemoveClient(client)
}
if err != nil {
log.Printf("PROTOCOL(V2): [%s] messagePump error - %s", client, err.Error())
}
}
func (p *ProtocolV2) IDENTIFY(client *ClientV2, params [][]byte) ([]byte, error) {
var err error
if atomic.LoadInt32(&client.State) != nsq.StateInit {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot IDENTIFY in current state")
}
var bodyLen int32
err = binary.Read(client.Reader, binary.BigEndian, &bodyLen)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to read body size")
}
if int64(bodyLen) > nsqd.options.maxBodySize {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_BODY",
fmt.Sprintf("IDENTIFY body too big %d > %d", bodyLen, nsqd.options.maxBodySize))
}
body := make([]byte, bodyLen)
_, err = io.ReadFull(client.Reader, body)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to read body")
}
// body is a json structure with producer information
clientInfo := struct {
ShortId string `json:"short_id"`
LongId string `json:"long_id"`
}{}
err = json.Unmarshal(body, &clientInfo)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_BODY", "IDENTIFY failed to decode JSON body")
}
client.ShortIdentifier = clientInfo.ShortId
client.LongIdentifier = clientInfo.LongId
return []byte("OK"), nil
}
func (p *ProtocolV2) SUB(client *ClientV2, params [][]byte) ([]byte, error) {
if atomic.LoadInt32(&client.State) != nsq.StateInit {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot SUB in current state")
}
if len(params) < 3 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "SUB insufficient number of parameters")
}
topicName := string(params[1])
if !nsq.IsValidTopicName(topicName) {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_TOPIC",
fmt.Sprintf("SUB topic name '%s' is not valid", topicName))
}
channelName := string(params[2])
if !nsq.IsValidChannelName(channelName) {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_CHANNEL",
fmt.Sprintf("SUB channel name '%s' is not valid", channelName))
}
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel(channelName)
channel.AddClient(client)
atomic.StoreInt32(&client.State, nsq.StateSubscribed)
client.Channel = channel
// update message pump
client.SubEventChan <- channel
return []byte("OK"), nil
}
func (p *ProtocolV2) RDY(client *ClientV2, params [][]byte) ([]byte, error) {
state := atomic.LoadInt32(&client.State)
if state == nsq.StateClosing {
// just ignore ready changes on a closing channel
log.Printf("PROTOCOL(V2): [%s] ignoring RDY after CLS in state ClientStateV2Closing", client)
return nil, nil
}
if state != nsq.StateSubscribed {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot RDY in current state")
}
count := int64(1)
if len(params) > 1 {
b10, err := util.ByteToBase10(params[1])
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_INVALID",
fmt.Sprintf("RDY could not parse count %s", params[1]))
}
count = int64(b10)
}
if count < 0 || count > nsq.MaxReadyCount {
// this needs to be a fatal error otherwise clients would have
// inconsistent state
return nil, nsq.NewFatalClientErr(nil, "E_INVALID",
fmt.Sprintf("RDY count %d out of range 0-%d", count, nsq.MaxReadyCount))
}
client.SetReadyCount(count)
return nil, nil
}
func (p *ProtocolV2) FIN(client *ClientV2, params [][]byte) ([]byte, error) {
var id nsq.MessageID
state := atomic.LoadInt32(&client.State)
if state != nsq.StateSubscribed && state != nsq.StateClosing {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot FIN in current state")
}
if len(params) < 2 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "FIN insufficient number of params")
}
copy(id[:], params[1])
err := client.Channel.FinishMessage(client, id)
if err != nil {
return nil, nsq.NewClientErr(err, "E_FIN_FAILED",
fmt.Sprintf("FIN %s failed %s", id, err.Error()))
}
client.FinishedMessage()
return nil, nil
}
func (p *ProtocolV2) REQ(client *ClientV2, params [][]byte) ([]byte, error) {
var id nsq.MessageID
state := atomic.LoadInt32(&client.State)
if state != nsq.StateSubscribed && state != nsq.StateClosing {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot REQ in current state")
}
if len(params) < 3 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "REQ insufficient number of params")
}
copy(id[:], params[1])
timeoutMs, err := util.ByteToBase10(params[2])
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_INVALID",
fmt.Sprintf("REQ could not parse timeout %s", params[2]))
}
timeoutDuration := time.Duration(timeoutMs) * time.Millisecond
if timeoutDuration < 0 || timeoutDuration > maxTimeout {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID",
fmt.Sprintf("REQ timeout %d out of range 0-%d", timeoutDuration, maxTimeout))
}
err = client.Channel.RequeueMessage(client, id, timeoutDuration)
if err != nil {
return nil, nsq.NewClientErr(err, "E_REQ_FAILED",
fmt.Sprintf("REQ %s failed %s", id, err.Error()))
}
client.RequeuedMessage()
return nil, nil
}
func (p *ProtocolV2) CLS(client *ClientV2, params [][]byte) ([]byte, error) {
if atomic.LoadInt32(&client.State) != nsq.StateSubscribed {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot CLS in current state")
}
client.StartClose()
return []byte("CLOSE_WAIT"), nil
}
func (p *ProtocolV2) NOP(client *ClientV2, params [][]byte) ([]byte, error) {
return nil, nil
}
func (p *ProtocolV2) PUB(client *ClientV2, params [][]byte) ([]byte, error) {
var err error
var bodyLen int32
if len(params) < 2 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "PUB insufficient number of parameters")
}
topicName := string(params[1])
if !nsq.IsValidTopicName(topicName) {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_TOPIC",
fmt.Sprintf("PUB topic name '%s' is not valid", topicName))
}
err = binary.Read(client.Reader, binary.BigEndian, &bodyLen)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_MESSAGE", "PUB failed to read message body size")
}
if int64(bodyLen) > nsqd.options.maxMessageSize {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_MESSAGE",
fmt.Sprintf("PUB message too big %d > %d", bodyLen, nsqd.options.maxMessageSize))
}
messageBody := make([]byte, bodyLen)
_, err = io.ReadFull(client.Reader, messageBody)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_MESSAGE", "PUB failed to read message body")
}
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, messageBody)
err = topic.PutMessage(msg)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_PUB_FAILED", "PUB failed "+err.Error())
}
return []byte("OK"), nil
}
func (p *ProtocolV2) MPUB(client *ClientV2, params [][]byte) ([]byte, error) {
var err error
var bodyLen int32
var numMessages int32
var messageSize int32
if len(params) < 2 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "MPUB insufficient number of parameters")
}
topicName := string(params[1])
if !nsq.IsValidTopicName(topicName) {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_TOPIC",
fmt.Sprintf("E_BAD_TOPIC MPUB topic name '%s' is not valid", topicName))
}
err = binary.Read(client.Reader, binary.BigEndian, &bodyLen)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_BODY", "MPUB failed to read body size")
}
if int64(bodyLen) > nsqd.options.maxBodySize {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_BODY",
fmt.Sprintf("MPUB body too big %d > %d", bodyLen, nsqd.options.maxBodySize))
}
err = binary.Read(client.Reader, binary.BigEndian, &numMessages)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_BODY", "MPUB failed to read message count")
}
messages := make([]*nsq.Message, 0, numMessages)
for i := int32(0); i < numMessages; i++ {
err = binary.Read(client.Reader, binary.BigEndian, &messageSize)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_MESSAGE",
fmt.Sprintf("MPUB failed to read message(%d) body size", i))
}
if int64(messageSize) > nsqd.options.maxMessageSize {
return nil, nsq.NewFatalClientErr(nil, "E_BAD_MESSAGE",
fmt.Sprintf("MPUB message too big %d > %d", messageSize, nsqd.options.maxMessageSize))
}
msgBody := make([]byte, messageSize)
_, err = io.ReadFull(client.Reader, msgBody)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_BAD_MESSAGE", "MPUB failed to read message body")
}
messages = append(messages, nsq.NewMessage(<-nsqd.idChan, msgBody))
}
topic := nsqd.GetTopic(topicName)
// if we've made it this far we've validated all the input,
// the only possible error is that the topic is exiting during
// this next call (and no messages will be queued in that case)
err = topic.PutMessages(messages)
if err != nil {
return nil, nsq.NewFatalClientErr(err, "E_MPUB_FAILED", "MPUB failed "+err.Error())
}
return []byte("OK"), nil
}
func (p *ProtocolV2) TOUCH(client *ClientV2, params [][]byte) ([]byte, error) {
var id nsq.MessageID
state := atomic.LoadInt32(&client.State)
if state != nsq.StateSubscribed && state != nsq.StateClosing {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "cannot TOUCH in current state")
}
if len(params) < 2 {
return nil, nsq.NewFatalClientErr(nil, "E_INVALID", "TOUCH insufficient number of params")
}
copy(id[:], params[1])
err := client.Channel.TouchMessage(client, id)
if err != nil {
return nil, nsq.NewClientErr(err, "E_TOUCH_FAILED",
fmt.Sprintf("TOUCH %s failed %s", id, err.Error()))
}
return nil, nil
}