/
output.go
358 lines (320 loc) · 7.93 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
package flume
import (
"errors"
"fmt"
"github.com/chentao/heka-flume/flume"
"github.com/chentao/thrift/lib/go/thrift"
"github.com/mozilla-services/heka/message"
. "github.com/mozilla-services/heka/pipeline"
"regexp"
"runtime"
"sync"
"sync/atomic"
"time"
)
type FlumeOutput struct {
config *FlumeOutputConfig
name string
or OutputRunner
pConfig *PipelineConfig
bufferedOut *BufferedOutput
processMessageCount int64
dropMessageCount int64
backMessageCount int64
rh *RetryHelper
reportLock sync.Mutex
boErrorChan chan error
boExitChan chan error
backChan chan []byte
thriftAppender *ThriftAppender
thriftAppenderBack *ThriftAppender
}
type FlumeOutputConfig struct {
Address string
BatchSize int `toml:"batch_size"`
ConnectTimeout uint64 `toml:"connect_timeout"`
QueueMaxBufferSize uint64 `toml:"queue_max_buffer_size"`
QueueFullAction string `toml:"queue_full_action"`
}
func (o *FlumeOutput) ConfigStruct() interface{} {
return &FlumeOutputConfig{
BatchSize: 1000,
ConnectTimeout: 10000,
QueueMaxBufferSize: 0,
QueueFullAction: "block",
}
}
func (o *FlumeOutput) SetName(name string) {
o.name = name
}
func (o *FlumeOutput) Init(config interface{}) (err error) {
o.config = config.(*FlumeOutputConfig)
if len(o.config.Address) == 0 {
return fmt.Errorf("address must be specified")
}
switch o.config.QueueFullAction {
case "shutdown", "drop", "block":
default:
return fmt.Errorf("`queue_full_action` must be 'shutdown', 'drop', or 'block'")
}
o.thriftAppender, err = NewThriftAppender(o.config.Address, o.config.ConnectTimeout)
if err != nil {
return err
}
o.thriftAppenderBack, err = NewThriftAppender(o.config.Address, o.config.ConnectTimeout)
if err != nil {
return err
}
return
}
func (o *FlumeOutput) Run(or OutputRunner, h PluginHelper) (err error) {
var (
ok = true
pack *PipelinePack
inChan = or.InChan()
outBytes []byte
stopChan = make(chan bool, 1)
e error
count int
outBatch []*flume.ThriftFlumeEvent
)
o.boErrorChan = make(chan error, 5)
o.boExitChan = make(chan error)
if or.Encoder() == nil {
return errors.New("Encoder must be specified.")
}
re := regexp.MustCompile("\\W")
name := re.ReplaceAllString(or.Name(), "_")
o.rh, err = NewRetryHelper(RetryOptions{
MaxDelay: "5s",
MaxRetries: -1,
})
if err != nil {
return fmt.Errorf("can't create retry helper: %s", err.Error())
}
o.pConfig = h.PipelineConfig()
o.or = or
o.bufferedOut, err = NewBufferedOutput("output_queue", name, or, h, o.config.QueueMaxBufferSize)
if err != nil {
if err == QueueIsFull {
or.LogMessage("Queue capacity is already reached.")
} else {
return
}
}
o.bufferedOut.Start(o, o.boErrorChan, o.boExitChan, stopChan)
o.backChan = make(chan []byte, 100)
go o.backing()
for ok {
select {
case e := <-o.boErrorChan:
or.LogError(e)
case pack, ok = <-inChan:
if !ok {
if len(outBatch) > 0 {
o.sendBatch(outBatch)
}
stopChan <- true
// Make sure buffer isn't blocked on sending to outputError
select {
case e := <-o.boErrorChan:
or.LogError(e)
default:
}
<-o.boExitChan
close(o.backChan)
break
}
outBytes, e = or.Encode(pack)
pack.Recycle()
if e != nil {
or.LogError(e)
atomic.AddInt64(&o.dropMessageCount, 1)
continue
}
if outBytes != nil {
event := &flume.ThriftFlumeEvent{
Body: outBytes,
}
outBatch = append(outBatch, event)
if count++; count >= o.config.BatchSize {
if len(outBatch) > 0 {
o.sendBatch(outBatch)
}
count = 0
outBatch = outBatch[:0]
}
}
case err = <-o.boExitChan:
ok = false
}
}
return
}
func (o *FlumeOutput) sendBatch(batch []*flume.ThriftFlumeEvent) {
if len(batch) == 1 {
o.backChan <- batch[0].Body
return
}
err := o.thriftAppender.AppendBatch(batch)
if err == nil {
atomic.AddInt64(&o.processMessageCount, int64(len(batch)))
return
} else {
o.or.LogError(fmt.Errorf("Backing %d events: %v", len(batch), err))
for _, event := range batch {
o.backChan <- event.Body
}
}
}
func (o *FlumeOutput) SendRecord(buffer []byte) (err error) {
event := &flume.ThriftFlumeEvent{
Body: buffer,
}
err = o.thriftAppenderBack.Append(event)
if err == nil {
atomic.AddInt64(&o.processMessageCount, 1)
return nil
}
return fmt.Errorf("SendRecord fail: %v", err)
}
func (o *FlumeOutput) backing() {
for true {
select {
case backEvent, ok := <-o.backChan:
if !ok {
return
}
err := o.bufferedOut.QueueBytes(backEvent)
if err == nil {
atomic.AddInt64(&o.backMessageCount, 1)
} else if err == QueueIsFull {
o.or.LogError(err)
if !o.queueFull(backEvent, 1) {
return
}
} else if err != nil {
o.or.LogError(err)
atomic.AddInt64(&o.dropMessageCount, 1)
}
}
}
}
func (o *FlumeOutput) queueFull(buffer []byte, count int64) bool {
switch o.config.QueueFullAction {
case "block":
for o.rh.Wait() == nil {
if o.pConfig.Globals.IsShuttingDown() {
return false
}
blockErr := o.bufferedOut.QueueBytes(buffer)
if blockErr == nil {
atomic.AddInt64(&o.processMessageCount, count)
o.rh.Reset()
break
}
select {
case e := <-o.boErrorChan:
o.or.LogError(e)
default:
}
runtime.Gosched()
}
case "shutdown":
o.pConfig.Globals.ShutDown()
return false
case "drop":
atomic.AddInt64(&o.dropMessageCount, count)
}
return true
}
func init() {
RegisterPlugin("FlumeOutput", func() interface{} {
return new(FlumeOutput)
})
}
func (o *FlumeOutput) ReportMsg(msg *message.Message) error {
o.reportLock.Lock()
defer o.reportLock.Unlock()
message.NewInt64Field(msg, "ProcessMessageCount",
atomic.LoadInt64(&o.processMessageCount), "count")
message.NewInt64Field(msg, "DropMessageCount",
atomic.LoadInt64(&o.dropMessageCount), "count")
message.NewInt64Field(msg, "BackMessageCount",
atomic.LoadInt64(&o.backMessageCount), "count")
o.bufferedOut.ReportMsg(msg)
return nil
}
type ThriftAppender struct {
addr string
timeout uint64
client *flume.ThriftSourceProtocolClient
rh *RetryHelper
}
func NewThriftAppender(addr string, timeout uint64) (*ThriftAppender, error) {
rh, err := NewRetryHelper(RetryOptions{
MaxDelay: "5s",
MaxRetries: -1,
})
if err != nil {
return nil, fmt.Errorf("can't create retry helper: %s", err.Error())
}
return &ThriftAppender{
addr: addr,
timeout: timeout,
client: nil,
rh: rh,
}, nil
}
func (t *ThriftAppender) Connect() error {
if t.client != nil && t.client.Transport != nil {
if t.client.Transport.IsOpen() {
return nil
}
}
tSock, err := thrift.NewTSocketTimeout(t.addr, time.Duration(t.timeout)*time.Millisecond)
if err != nil {
return err
}
trans := thrift.NewTFramedTransport(tSock)
protoFactory := thrift.NewTCompactProtocolFactory()
c := flume.NewThriftSourceProtocolClientFactory(trans, protoFactory)
t.client = c
return t.client.Transport.Open()
}
func (t *ThriftAppender) Disconnect() error {
if t.client.Transport != nil {
err := t.client.Transport.Close()
if err != nil {
return err
}
t.client.Transport = nil
}
return nil
}
func (t *ThriftAppender) AppendBatch(batch []*flume.ThriftFlumeEvent) (err error) {
var status flume.Status
if err = t.Connect(); err == nil {
status, err = t.client.AppendBatch(batch)
if status == flume.Status_OK && err == nil {
return nil
}
if err != nil {
t.Disconnect()
}
}
return fmt.Errorf("AppendBatch fail, status:%s, err:%v", status.String(), err)
}
func (t *ThriftAppender) Append(event *flume.ThriftFlumeEvent) (err error) {
var status flume.Status
if err = t.Connect(); err == nil {
status, err = t.client.Append(event)
if status == flume.Status_OK && err == nil {
return nil
}
if err != nil {
t.Disconnect()
}
}
return fmt.Errorf("Append fail, status:%s, err:%v", status.String(), err)
}