forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
423 lines (364 loc) · 10.4 KB
/
client.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
package logstash
import (
"bytes"
"compress/zlib"
"encoding/binary"
"encoding/json"
"errors"
"expvar"
"io"
"math"
"net"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
)
// Metrics that can retrieved through the expvar web interface.
var (
ackedEvents = expvar.NewInt("libbeatLogstashPublishedAndAckedEvents")
eventsNotAcked = expvar.NewInt("libbeatLogstashPublishedButNotAckedEvents")
publishEventsCallCount = expvar.NewInt("libbeatLogstashPublishEventsCallCount")
)
// lumberjackClient implements the ProtocolClient interface to be used
// with different mode. The client implements slow start with low window sizes +
// window size backoff in case of long running transactions.
//
// it is suggested to use lumberjack in conjunction with roundRobinConnectionMode
// if logstash becomes unresponsive
type lumberjackClient struct {
TransportClient
windowSize int
maxOkWindowSize int // max window size sending was successful for
maxWindowSize int
timeout time.Duration
countTimeoutErr int
compressLevel int
// the beat name, already json encoded
beat []byte
}
const (
minWindowSize int = 1
defaultStartMaxWindowSize int = 10
maxAllowedTimeoutErr int = 3
)
// errors
var (
// ErrProtocolError is returned if an protocol error was detected in the
// conversation with lumberjack server.
ErrProtocolError = errors.New("lumberjack protocol error")
)
var (
codeVersion byte = '2'
codeWindowSize = []byte{codeVersion, 'W'}
codeJSONDataFrame = []byte{codeVersion, 'J'}
codeCompressed = []byte{codeVersion, 'C'}
)
func newLumberjackClient(
conn TransportClient,
compressLevel int,
maxWindowSize int,
timeout time.Duration,
beat string,
) (*lumberjackClient, error) {
// validate by creating and discarding zlib writer with configured level
if compressLevel > 0 {
tmp := bytes.NewBuffer(nil)
w, err := zlib.NewWriterLevel(tmp, compressLevel)
if err != nil {
return nil, err
}
w.Close()
}
encodedBeat, err := json.Marshal(beat)
if err != nil {
return nil, err
}
return &lumberjackClient{
TransportClient: conn,
windowSize: defaultStartMaxWindowSize,
timeout: timeout,
maxWindowSize: maxWindowSize,
compressLevel: compressLevel,
beat: encodedBeat,
}, nil
}
func (l *lumberjackClient) Connect(timeout time.Duration) error {
logp.Debug("logstash", "connect")
return l.TransportClient.Connect(timeout)
}
func (l *lumberjackClient) Close() error {
logp.Debug("logstash", "close connection")
return l.TransportClient.Close()
}
func (l *lumberjackClient) PublishEvent(event common.MapStr) error {
_, err := l.PublishEvents([]common.MapStr{event})
return err
}
// PublishEvents sends all events to logstash. On error a slice with all events
// not published or confirmed to be processed by logstash will be returned.
func (l *lumberjackClient) PublishEvents(
events []common.MapStr,
) ([]common.MapStr, error) {
publishEventsCallCount.Add(1)
totalNumberOfEvents := len(events)
for len(events) > 0 {
n, err := l.publishWindowed(events)
logp.Debug("logstash", "%v events out of %v events sent to logstash. Continue sending ...", n, len(events))
events = events[n:]
if err != nil {
eventsNotAcked.Add(int64(len(events)))
ackedEvents.Add(int64(totalNumberOfEvents - len(events)))
return events, err
}
}
ackedEvents.Add(int64(totalNumberOfEvents))
return nil, nil
}
// publishWindowed published events with current maximum window size to logstash
// returning the total number of events sent (due to window size, or acks until
// failure).
func (l *lumberjackClient) publishWindowed(events []common.MapStr) (int, error) {
if len(events) == 0 {
return 0, nil
}
batchSize := len(events)
debug("Try to publish %v events to logstash with window size %v",
batchSize, l.windowSize)
// prepare message payload
if batchSize > l.windowSize {
events = events[:l.windowSize]
}
// serialize all raw events into output buffer, removing all events encoding failed for
count, payload, err := l.serializeEvents(events)
if err != nil {
return 0, err
}
if count == 0 {
// encoding of all events failed. Let's stop here and report all events
// as exported so no one tries to send/encode the same events once again
// The compress/encode function already prints critical per failed encoding
// failure.
return len(events), nil
}
// send window size:
if err = l.sendWindowSize(count); err != nil {
return l.onFail(0, err)
}
if l.compressLevel > 0 {
err = l.sendCompressed(payload)
} else {
_, err = l.Write(payload)
}
if err != nil {
return l.onFail(0, err)
}
// wait for ACK (accept partial ACK to reset timeout)
// reset timeout timer for every ACK received.
var ackSeq uint32
for ackSeq < count {
// read until all acks
ackSeq, err = l.readACK()
if err != nil {
return l.onFail(int(ackSeq), err)
}
}
l.tryGrowWindowSize(batchSize)
return len(events), nil
}
func (l *lumberjackClient) onFail(n int, err error) (int, error) {
l.shrinkWindow()
// if timeout error, back off and ignore error
nerr, ok := err.(net.Error)
if !ok || !nerr.Timeout() {
// no timeout error, close connection and return error
_ = l.Close()
return n, err
}
// if we've seen 3 consecutive timeout errors, close connection
l.countTimeoutErr++
if l.countTimeoutErr >= maxAllowedTimeoutErr {
l.countTimeoutErr = 0 // reset error counter and close connection
_ = l.Close()
return n, err
}
// timeout error. events. Send
// mode might try to publish again with reduce window size or ask another
// client to send events
return n, nil
}
// Increase window size by factor 1.5 until max window size
// (window size grows exponentially)
// TODO: use duration until ACK to estimate an ok max window size value
func (l *lumberjackClient) tryGrowWindowSize(batchSize int) {
if l.windowSize <= batchSize {
if l.maxOkWindowSize < l.windowSize {
logp.Debug("logstash", "update max ok window size: %v < %v", l.maxOkWindowSize, l.windowSize)
l.maxOkWindowSize = l.windowSize
newWindowSize := int(math.Ceil(1.5 * float64(l.windowSize)))
logp.Debug("logstash", "increase window size to: %v", newWindowSize)
if l.windowSize <= batchSize && batchSize < newWindowSize {
logp.Debug("logstash", "set to batchSize: %v", batchSize)
newWindowSize = batchSize
}
if newWindowSize > l.maxWindowSize {
logp.Debug("logstash", "set to max window size: %v", l.maxWindowSize)
newWindowSize = l.maxWindowSize
}
l.windowSize = newWindowSize
} else if l.windowSize < l.maxOkWindowSize {
logp.Debug("logstash", "update current window size: %v", l.windowSize)
l.windowSize = int(math.Ceil(1.5 * float64(l.windowSize)))
if l.windowSize > l.maxOkWindowSize {
logp.Debug("logstash", "set to max ok window size: %v", l.maxOkWindowSize)
l.windowSize = l.maxOkWindowSize
}
}
}
}
func (l *lumberjackClient) shrinkWindow() {
l.windowSize = l.windowSize / 2
if l.windowSize < minWindowSize {
l.windowSize = minWindowSize
}
}
func (l *lumberjackClient) serializeEvents(
events []common.MapStr,
) (uint32, []byte, error) {
buf := bytes.NewBuffer(nil)
if l.compressLevel > 0 {
w, _ := zlib.NewWriterLevel(buf, l.compressLevel)
count, err := l.doSerializeEvents(w, events)
if err != nil {
return 0, nil, err
}
if err := w.Close(); err != nil {
debug("Finalizing zlib compression failed with: %s", err)
return 0, nil, err
}
return count, buf.Bytes(), nil
}
count, err := l.doSerializeEvents(buf, events)
return count, buf.Bytes(), err
}
func (l *lumberjackClient) doSerializeEvents(out io.Writer, events []common.MapStr) (uint32, error) {
var sequence uint32
for _, event := range events {
sequence++
err := l.writeDataFrame(event, sequence, out)
if err != nil {
logp.Critical("failed to encode event: %v", err)
sequence-- //forget this last broken event and continue
}
}
return sequence, nil
}
func (l *lumberjackClient) readACK() (uint32, error) {
if err := l.SetDeadline(time.Now().Add(l.timeout)); err != nil {
return 0, err
}
response := make([]byte, 6)
ackbytes := 0
for ackbytes < 6 {
n, err := l.Read(response[ackbytes:])
if err != nil {
return 0, err
}
ackbytes += n
}
isACK := response[0] == codeVersion && response[1] == 'A'
if !isACK {
return 0, ErrProtocolError
}
seq := binary.BigEndian.Uint32(response[2:])
return seq, nil
}
func (l *lumberjackClient) sendWindowSize(window uint32) error {
if err := l.SetDeadline(time.Now().Add(l.timeout)); err != nil {
return err
}
if _, err := l.Write(codeWindowSize); err != nil {
return err
}
return writeUint32(l, window)
}
func (l *lumberjackClient) sendCompressed(payload []byte) error {
if err := l.SetDeadline(time.Now().Add(l.timeout)); err != nil {
return err
}
if _, err := l.Write(codeCompressed); err != nil {
return err
}
if err := writeUint32(l, uint32(len(payload))); err != nil {
return err
}
_, err := l.Write(payload)
return err
}
func (l *lumberjackClient) writeDataFrame(
event common.MapStr,
seq uint32,
out io.Writer,
) error {
// Write JSON Data Frame:
// version: uint8 = '2'
// code: uint8 = 'J'
// seq: uint32
// payloadLen (bytes): uint32
// payload: JSON document
jsonEvent, err := serializeEvent(event, l.beat)
if err != nil {
debug("Fail to convert the event to JSON: %s", err)
return err
}
if _, err := out.Write(codeJSONDataFrame); err != nil { // version + code
return err
}
if err := writeUint32(out, seq); err != nil {
return err
}
if err := writeUint32(out, uint32(len(jsonEvent))); err != nil {
return err
}
if _, err := out.Write(jsonEvent); err != nil {
return err
}
return nil
}
func serializeEvent(event common.MapStr, beat []byte) ([]byte, error) {
buf := bytes.NewBuffer(nil)
buf.WriteRune('{')
if _, hasMeta := event["@metadata"]; !hasMeta {
typ := event["type"].(string)
buf.WriteString(`"@metadata":{"type":`)
tmp, err := json.Marshal(typ)
if err != nil {
return nil, err
}
buf.Write(tmp)
buf.WriteString(`,"beat":`)
buf.Write(beat)
buf.WriteString(`},`)
}
for k, v := range event {
// append key
tmp, err := json.Marshal(k)
if err != nil {
return nil, err
}
buf.Write(tmp)
buf.WriteRune(':')
// append value
tmp, err = json.Marshal(v)
if err != nil {
return nil, err
}
buf.Write(tmp)
buf.WriteRune(',')
}
b := buf.Bytes()
b[len(b)-1] = '}'
return b, nil
}
func writeUint32(out io.Writer, v uint32) error {
return binary.Write(out, binary.BigEndian, v)
}