forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsq_to_nsq.go
412 lines (345 loc) · 10.8 KB
/
nsq_to_nsq.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
// This is an NSQ client that reads the specified topic/channel
// and re-publishes the messages to destination nsqd via TCP
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/bitly/go-hostpool"
"github.com/bitly/go-nsq"
"github.com/bitly/go-simplejson"
"github.com/bitly/nsq/internal/app"
"github.com/bitly/nsq/internal/protocol"
"github.com/bitly/nsq/internal/version"
"github.com/bitly/timer_metrics"
)
const (
ModeRoundRobin = iota
ModeHostPool
)
var (
showVersion = flag.Bool("version", false, "print version string")
topic = flag.String("topic", "", "nsq topic")
channel = flag.String("channel", "nsq_to_nsq", "nsq channel")
destTopic = flag.String("destination-topic", "", "destination nsq topic")
maxInFlight = flag.Int("max-in-flight", 200, "max number of messages to allow in flight")
statusEvery = flag.Int("status-every", 250, "the # of requests between logging status (per destination), 0 disables")
mode = flag.String("mode", "hostpool", "the upstream request mode options: round-robin, hostpool (default), epsilon-greedy")
consumerOpts = app.StringArray{}
producerOpts = app.StringArray{}
nsqdTCPAddrs = app.StringArray{}
lookupdHTTPAddrs = app.StringArray{}
destNsqdTCPAddrs = app.StringArray{}
whitelistJSONFields = app.StringArray{}
requireJSONField = flag.String("require-json-field", "", "for JSON messages: only pass messages that contain this field")
requireJSONValue = flag.String("require-json-value", "", "for JSON messages: only pass messages in which the required field has this value")
// TODO: remove, deprecated
maxBackoffDuration = flag.Duration("max-backoff-duration", 120*time.Second, "(deprecated) use --consumer-opt=max_backoff_duration,X")
)
func init() {
// TODO: remove, deprecated
flag.Var(&consumerOpts, "reader-opt", "(deprecated) use --consumer-opt")
flag.Var(&consumerOpts, "consumer-opt", "option to passthrough to nsq.Consumer (may be given multiple times, see http://godoc.org/github.com/bitly/go-nsq#Config)")
flag.Var(&producerOpts, "producer-opt", "option to passthrough to nsq.Producer (may be given multiple times, see http://godoc.org/github.com/bitly/go-nsq#Config)")
flag.Var(&nsqdTCPAddrs, "nsqd-tcp-address", "nsqd TCP address (may be given multiple times)")
flag.Var(&destNsqdTCPAddrs, "destination-nsqd-tcp-address", "destination nsqd TCP address (may be given multiple times)")
flag.Var(&lookupdHTTPAddrs, "lookupd-http-address", "lookupd HTTP address (may be given multiple times)")
flag.Var(&whitelistJSONFields, "whitelist-json-field", "for JSON messages: pass this field (may be given multiple times)")
}
type PublishHandler struct {
// 64bit atomic vars need to be first for proper alignment on 32bit platforms
counter uint64
addresses app.StringArray
producers map[string]*nsq.Producer
mode int
hostPool hostpool.HostPool
respChan chan *nsq.ProducerTransaction
requireJSONValueParsed bool
requireJSONValueIsNumber bool
requireJSONNumber float64
perAddressStatus map[string]*timer_metrics.TimerMetrics
timermetrics *timer_metrics.TimerMetrics
}
func (ph *PublishHandler) responder() {
var msg *nsq.Message
var startTime time.Time
var address string
var hostPoolResponse hostpool.HostPoolResponse
for t := range ph.respChan {
switch ph.mode {
case ModeRoundRobin:
msg = t.Args[0].(*nsq.Message)
startTime = t.Args[1].(time.Time)
hostPoolResponse = nil
address = t.Args[2].(string)
case ModeHostPool:
msg = t.Args[0].(*nsq.Message)
startTime = t.Args[1].(time.Time)
hostPoolResponse = t.Args[2].(hostpool.HostPoolResponse)
address = hostPoolResponse.Host()
}
success := t.Error == nil
if hostPoolResponse != nil {
if !success {
hostPoolResponse.Mark(errors.New("failed"))
} else {
hostPoolResponse.Mark(nil)
}
}
if success {
msg.Finish()
} else {
msg.Requeue(-1)
}
ph.perAddressStatus[address].Status(startTime)
ph.timermetrics.Status(startTime)
}
}
func (ph *PublishHandler) shouldPassMessage(jsonMsg *simplejson.Json) (bool, bool) {
pass := true
backoff := false
if *requireJSONField == "" {
return pass, backoff
}
if *requireJSONValue != "" && !ph.requireJSONValueParsed {
// cache conversion in case needed while filtering json
var err error
ph.requireJSONNumber, err = strconv.ParseFloat(*requireJSONValue, 64)
ph.requireJSONValueIsNumber = (err == nil)
ph.requireJSONValueParsed = true
}
jsonVal, ok := jsonMsg.CheckGet(*requireJSONField)
if !ok {
pass = false
if *requireJSONValue != "" {
log.Printf("ERROR: missing field to check required value")
backoff = true
}
} else if *requireJSONValue != "" {
// if command-line argument can't convert to float, then it can't match a number
// if it can, also integers (up to 2^53 or so) can be compared as float64
if strVal, err := jsonVal.String(); err == nil {
if strVal != *requireJSONValue {
pass = false
}
} else if ph.requireJSONValueIsNumber {
floatVal, err := jsonVal.Float64()
if err != nil || ph.requireJSONNumber != floatVal {
pass = false
}
} else {
// json value wasn't a plain string, and argument wasn't a number
// give up on comparisons of other types
pass = false
}
}
return pass, backoff
}
func filterMessage(jsonMsg *simplejson.Json, rawMsg []byte) ([]byte, error) {
if len(whitelistJSONFields) == 0 {
// no change
return rawMsg, nil
}
msg, err := jsonMsg.Map()
if err != nil {
return nil, errors.New("json is not an object")
}
newMsg := make(map[string]interface{}, len(whitelistJSONFields))
for _, key := range whitelistJSONFields {
value, ok := msg[key]
if ok {
// avoid printing int as float (go 1.0)
switch tvalue := value.(type) {
case float64:
ivalue := int64(tvalue)
if float64(ivalue) == tvalue {
newMsg[key] = ivalue
} else {
newMsg[key] = tvalue
}
default:
newMsg[key] = value
}
}
}
newRawMsg, err := json.Marshal(newMsg)
if err != nil {
return nil, fmt.Errorf("unable to marshal filtered message %v", newMsg)
}
return newRawMsg, nil
}
func (ph *PublishHandler) HandleMessage(m *nsq.Message) error {
var err error
msgBody := m.Body
if *requireJSONField != "" || len(whitelistJSONFields) > 0 {
var jsonMsg *simplejson.Json
jsonMsg, err = simplejson.NewJson(m.Body)
if err != nil {
log.Printf("ERROR: Unable to decode json: %s", m.Body)
return nil
}
if pass, backoff := ph.shouldPassMessage(jsonMsg); !pass {
if backoff {
return errors.New("backoff")
}
return nil
}
msgBody, err = filterMessage(jsonMsg, m.Body)
if err != nil {
log.Printf("ERROR: filterMessage() failed: %s", err)
return err
}
}
startTime := time.Now()
switch ph.mode {
case ModeRoundRobin:
counter := atomic.AddUint64(&ph.counter, 1)
idx := counter % uint64(len(ph.addresses))
addr := ph.addresses[idx]
p := ph.producers[addr]
err = p.PublishAsync(*destTopic, msgBody, ph.respChan, m, startTime, addr)
case ModeHostPool:
hostPoolResponse := ph.hostPool.Get()
p := ph.producers[hostPoolResponse.Host()]
err = p.PublishAsync(*destTopic, msgBody, ph.respChan, m, startTime, hostPoolResponse)
if err != nil {
hostPoolResponse.Mark(err)
}
}
if err != nil {
return err
}
m.DisableAutoResponse()
return nil
}
func hasArg(s string) bool {
for _, arg := range os.Args {
if strings.Contains(arg, s) {
return true
}
}
return false
}
func main() {
var selectedMode int
flag.Parse()
if *showVersion {
fmt.Printf("nsq_to_nsq v%s\n", version.Binary)
return
}
if *topic == "" || *channel == "" {
log.Fatal("--topic and --channel are required")
}
if *destTopic == "" {
*destTopic = *topic
}
if !protocol.IsValidTopicName(*topic) {
log.Fatal("--topic is invalid")
}
if !protocol.IsValidTopicName(*destTopic) {
log.Fatal("--destination-topic is invalid")
}
if !protocol.IsValidChannelName(*channel) {
log.Fatal("--channel is invalid")
}
if len(nsqdTCPAddrs) == 0 && len(lookupdHTTPAddrs) == 0 {
log.Fatal("--nsqd-tcp-address or --lookupd-http-address required")
}
if len(nsqdTCPAddrs) > 0 && len(lookupdHTTPAddrs) > 0 {
log.Fatal("use --nsqd-tcp-address or --lookupd-http-address not both")
}
if len(destNsqdTCPAddrs) == 0 {
log.Fatal("--destination-nsqd-tcp-address required")
}
switch *mode {
case "round-robin":
selectedMode = ModeRoundRobin
case "hostpool", "epsilon-greedy":
selectedMode = ModeHostPool
}
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
defaultUA := fmt.Sprintf("nsq_to_nsq/%s go-nsq/%s", version.Binary, nsq.VERSION)
cCfg := nsq.NewConfig()
cCfg.UserAgent = defaultUA
err := app.ParseOpts(cCfg, consumerOpts)
if err != nil {
log.Fatal(err)
}
cCfg.MaxInFlight = *maxInFlight
// TODO: remove, deprecated
if hasArg("max-backoff-duration") {
log.Printf("WARNING: --max-backoff-duration is deprecated in favor of --consumer-opt=max_backoff_duration,X")
cCfg.MaxBackoffDuration = *maxBackoffDuration
}
pCfg := nsq.NewConfig()
pCfg.UserAgent = defaultUA
err = app.ParseOpts(pCfg, producerOpts)
if err != nil {
log.Fatal(err)
}
consumer, err := nsq.NewConsumer(*topic, *channel, cCfg)
if err != nil {
log.Fatal(err)
}
producers := make(map[string]*nsq.Producer)
for _, addr := range destNsqdTCPAddrs {
producer, err := nsq.NewProducer(addr, pCfg)
if err != nil {
log.Fatalf("failed creating producer %s", err)
}
producers[addr] = producer
}
perAddressStatus := make(map[string]*timer_metrics.TimerMetrics)
if len(destNsqdTCPAddrs) == 1 {
// disable since there is only one address
perAddressStatus[destNsqdTCPAddrs[0]] = timer_metrics.NewTimerMetrics(0, "")
} else {
for _, a := range destNsqdTCPAddrs {
perAddressStatus[a] = timer_metrics.NewTimerMetrics(*statusEvery,
fmt.Sprintf("[%s]:", a))
}
}
hostPool := hostpool.New(destNsqdTCPAddrs)
if *mode == "epsilon-greedy" {
hostPool = hostpool.NewEpsilonGreedy(destNsqdTCPAddrs, 0, &hostpool.LinearEpsilonValueCalculator{})
}
handler := &PublishHandler{
addresses: destNsqdTCPAddrs,
producers: producers,
mode: selectedMode,
hostPool: hostPool,
respChan: make(chan *nsq.ProducerTransaction, len(destNsqdTCPAddrs)),
perAddressStatus: perAddressStatus,
timermetrics: timer_metrics.NewTimerMetrics(*statusEvery, "[aggregate]:"),
}
consumer.AddConcurrentHandlers(handler, len(destNsqdTCPAddrs))
for i := 0; i < len(destNsqdTCPAddrs); i++ {
go handler.responder()
}
err = consumer.ConnectToNSQDs(nsqdTCPAddrs)
if err != nil {
log.Fatal(err)
}
err = consumer.ConnectToNSQLookupds(lookupdHTTPAddrs)
if err != nil {
log.Fatal(err)
}
for {
select {
case <-consumer.StopChan:
return
case <-termChan:
consumer.Stop()
}
}
}