This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
forked from chancez/heka-nsq
/
output.go
377 lines (333 loc) · 9.57 KB
/
output.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
package nsq
import (
"errors"
"fmt"
"github.com/bitly/go-hostpool"
"github.com/bitly/go-nsq"
"github.com/mozilla-services/heka/pipeline"
"github.com/mozilla-services/heka/plugins/tcp"
"sync/atomic"
"time"
)
const (
ModeRoundRobin = iota
ModeHostPool
)
type NsqOutputConfig struct {
Addresses []string `toml:"addresses"`
Topic string `toml:"topic"`
Mode string `toml:"mode"`
RetryQueueSize uint64 `toml:"retry_queue_size"`
MaxMsgRetries uint64 `toml:"max_msg_retries"`
RetryOptions *pipeline.RetryOptions
// Set to true if the TCP connection should be tunneled through TLS.
// Requires additional Tls config section.
UseTls bool `toml:"use_tls"`
// Subsection for TLS configuration.
Tls tcp.TlsConfig
// The following fields config options for nsq.Config
// Deadlines for network reads and writes
ReadTimeout *int64 `toml:"read_timeout"`
WriteTimeout *int64 `toml:"write_timeout"`
// Identifiers sent to nsqd representing this client
// UserAgent is in the spirit of HTTP (default: "<client_library_name>/<version>")
ClientID *string `toml:"client_id"` // (defaults: short hostname)
Hostname *string `toml:"hostname"`
UserAgent *string `toml:"user_agent"`
// Duration of time between heartbeats. This must be less than ReadTimeout
HeartbeatInterval *int64 `toml:"heartbeat_interval"`
// Compression Settings
Deflate *bool `toml:"deflate"`
DeflateLevel *int `toml:"deflate_level"`
Snappy *bool `toml:"snappy"`
// Size of the buffer (in bytes) used by nsqd for buffering writes to this connection
OutputBufferSize *int64 `toml:"output_buffer_size"`
// Timeout used by nsqd before flushing buffered writes (set to 0 to disable).
//
// WARNING: configuring clients with an extremely low
// (< 25ms) output_buffer_timeout has a significant effect
// on nsqd CPU usage (particularly with > 50 clients connected).
OutputBufferTimeout *int64 `toml:"output_buffer_timeout"`
// Maximum number of messages to allow in flight (concurrency knob)
MaxInFlight *int `toml:"max_in_flight"`
// Maximum amount of time to backoff when processing fails 0 == no backoff
MaxBackoffDuration *int64 `toml:"max_backoff_duration"`
// The server-side message timeout for messages delivered to this client
MsgTimeout *int64 `toml:"msg_timeout"`
// secret for nsqd authentication (requires nsqd 0.2.29+)
AuthSecret *string `toml:"auth_secret"`
}
type NsqOutput struct {
*NsqOutputConfig
Mode int
runner pipeline.OutputRunner
producers map[string]Producer
config *nsq.Config
respChan chan *nsq.ProducerTransaction
counter uint64
hostPool hostpool.HostPool
retryChan chan RetryMsg
retryHelper *pipeline.RetryHelper
newProducer func(string, *nsq.Config) (Producer, error)
}
type RetryMsg struct {
count uint64
maxCount uint64
Body []byte
retryChan chan RetryMsg
}
func (m RetryMsg) Retry() error {
if m.count > m.maxCount {
return errors.New("Exceeded max retry attempts sending message. No longer requeuing message.")
}
m.retryChan <- m
m.count++
return nil
}
func (output *NsqOutput) ConfigStruct() interface{} {
return &NsqOutputConfig{
RetryQueueSize: 50,
RetryOptions: &pipeline.RetryOptions{
MaxRetries: 3,
Delay: "1s",
},
}
}
func (output *NsqOutput) SetNsqConfig(conf *NsqOutputConfig) (err error) {
nsqConfig := nsq.NewConfig()
if conf.ReadTimeout != nil {
nsqConfig.ReadTimeout = time.Duration(*conf.ReadTimeout) * time.Millisecond
}
if conf.WriteTimeout != nil {
nsqConfig.WriteTimeout = time.Duration(*conf.WriteTimeout) * time.Millisecond
}
if conf.ClientID != nil {
nsqConfig.ClientID = *conf.ClientID
}
if conf.Hostname != nil {
nsqConfig.Hostname = *conf.Hostname
}
if conf.UserAgent != nil {
nsqConfig.UserAgent = *conf.UserAgent
}
if conf.HeartbeatInterval != nil {
nsqConfig.HeartbeatInterval = time.Duration(*conf.HeartbeatInterval) * time.Millisecond
}
if conf.Deflate != nil {
nsqConfig.Deflate = *conf.Deflate
}
if conf.DeflateLevel != nil {
nsqConfig.DeflateLevel = *conf.DeflateLevel
}
if conf.Snappy != nil {
nsqConfig.Snappy = *conf.Snappy
}
if conf.OutputBufferSize != nil {
nsqConfig.OutputBufferSize = *conf.OutputBufferSize
}
if conf.OutputBufferTimeout != nil {
nsqConfig.OutputBufferTimeout = time.Duration(*conf.OutputBufferTimeout) * time.Millisecond
}
if conf.MaxInFlight != nil {
nsqConfig.MaxInFlight = *conf.MaxInFlight
}
if conf.MaxBackoffDuration != nil {
nsqConfig.MaxBackoffDuration = time.Duration(*conf.MaxBackoffDuration) * time.Millisecond
}
if conf.MsgTimeout != nil {
nsqConfig.MsgTimeout = time.Duration(*conf.MsgTimeout) * time.Millisecond
}
if conf.AuthSecret != nil {
nsqConfig.AuthSecret = *conf.AuthSecret
}
// Tls
if conf.UseTls {
tls, err := tcp.CreateGoTlsConfig(&conf.Tls)
if err != nil {
return err
}
output.config.TlsConfig = tls
}
output.config = nsqConfig
return
}
func (output *NsqOutput) Init(config interface{}) (err error) {
conf := config.(*NsqOutputConfig)
output.NsqOutputConfig = conf
if len(conf.Addresses) < 1 {
return errors.New("Need at least one nsqd address.")
}
err = output.SetNsqConfig(conf)
if err != nil {
return
}
switch conf.Mode {
case "round-robin":
output.Mode = ModeRoundRobin
case "hostpool":
output.Mode = ModeHostPool
}
if output.newProducer == nil {
output.newProducer = NewProducer
}
output.producers = make(map[string]Producer)
var producer Producer
for _, addr := range output.Addresses {
producer, err = output.newProducer(addr, output.config)
if err != nil {
break
}
producer.SetLogger(nil, nsq.LogLevelError)
output.producers[addr] = producer
}
// Cleanup all successful producer creations on error
if err != nil {
fmt.Println("error")
for _, producer = range output.producers {
if producer != nil {
producer.Stop()
}
}
return err
}
output.retryHelper, err = pipeline.NewRetryHelper(*output.RetryOptions)
if err != nil {
return
}
output.respChan = make(chan *nsq.ProducerTransaction, len(output.Addresses))
output.retryChan = make(chan RetryMsg, output.RetryQueueSize)
output.hostPool = hostpool.New(output.Addresses)
for i := 0; i < len(output.Addresses); i++ {
go output.responder()
}
return nil
}
func (output *NsqOutput) Run(runner pipeline.OutputRunner,
helper pipeline.PluginHelper) (err error) {
if runner.Encoder() == nil {
return errors.New("Encoder required.")
}
var (
pack *pipeline.PipelinePack
outgoing []byte
msg RetryMsg
)
output.runner = runner
inChan := runner.InChan()
ok := true
defer output.cleanup()
for ok {
select {
case pack, ok = <-inChan:
if !ok {
return nil
}
outgoing, err = output.runner.Encode(pack)
if err != nil {
runner.LogError(err)
} else {
err = output.sendMessage(outgoing)
if err != nil {
output.runner.LogError(err)
err = output.retryHelper.Wait()
if err != nil {
return
}
// Create a retry msg, and requeue it
msg := RetryMsg{Body: outgoing, retryChan: output.retryChan, maxCount: output.MaxMsgRetries}
err = msg.Retry()
if err != nil {
output.runner.LogError(err)
}
} else {
output.retryHelper.Reset()
}
}
pack.Recycle()
case msg, ok = <-output.retryChan:
if !ok {
return nil
}
err = output.sendMessage(msg.Body)
if err != nil {
output.runner.LogError(err)
err = output.retryHelper.Wait()
if err != nil {
return
}
// requeue the message
err = msg.Retry()
if err != nil {
output.runner.LogError(err)
}
} else {
output.retryHelper.Reset()
}
}
}
return nil
}
func (output *NsqOutput) cleanup() {
close(output.respChan)
close(output.retryChan)
for _, producer := range output.producers {
producer.Stop()
}
}
// sendMessage asyncronously publishes encoded pipeline packs to nsq. It will
// deliver messages to a corresponding nsq producer. If there is more than one
// producer it uses RoundRobin or HostPool strategies according to the config
// option.
func (output *NsqOutput) sendMessage(body []byte) (err error) {
switch output.Mode {
case ModeRoundRobin:
counter := atomic.AddUint64(&output.counter, 1)
idx := counter % uint64(len(output.Addresses))
addr := output.Addresses[idx]
p := output.producers[addr]
err = p.PublishAsync(output.Topic, body, output.respChan, body)
case ModeHostPool:
hostPoolResponse := output.hostPool.Get()
p := output.producers[hostPoolResponse.Host()]
err = p.PublishAsync(output.Topic, body, output.respChan, body, hostPoolResponse)
}
return
}
// responder handles the eventual response from the asyncronous publish.
// It handles retrying to send the message if there was an error
func (output *NsqOutput) responder() {
var (
body []byte
hostPoolResponse hostpool.HostPoolResponse
)
for t := range output.respChan {
switch output.Mode {
case ModeRoundRobin:
body = t.Args[0].([]byte)
case ModeHostPool:
body = t.Args[0].([]byte)
hostPoolResponse = t.Args[1].(hostpool.HostPoolResponse)
}
success := t.Error == nil
if hostPoolResponse != nil {
if success {
hostPoolResponse.Mark(nil)
} else {
hostPoolResponse.Mark(errors.New("failed"))
}
}
if !success {
output.runner.LogError(fmt.Errorf("Publishing message failed: %s", t.Error.Error()))
msg := RetryMsg{Body: body, retryChan: output.retryChan, maxCount: output.MaxMsgRetries}
err := msg.Retry()
if err != nil {
output.runner.LogError(fmt.Errorf("%s. No longer attempting to send message.", err.Error()))
}
}
}
}
func init() {
pipeline.RegisterPlugin("NsqOutput", func() interface{} {
return new(NsqOutput)
})
}