forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.go
234 lines (195 loc) · 4.23 KB
/
async.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
package logstash
import (
"net"
"time"
"github.com/elastic/beats/libbeat/common/atomic"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/libbeat/publisher"
"github.com/elastic/go-lumber/client/v2"
)
type asyncClient struct {
*transport.Client
stats *outputs.Stats
client *v2.AsyncClient
win *window
connect func() error
}
type msgRef struct {
client *asyncClient
count atomic.Uint32
batch publisher.Batch
slice []publisher.Event
err error
win *window
batchSize int
}
func newAsyncClient(
conn *transport.Client,
stats *outputs.Stats,
config *Config,
) (*asyncClient, error) {
c := &asyncClient{
Client: conn,
stats: stats,
}
if config.SlowStart {
c.win = newWindower(defaultStartMaxWindowSize, config.BulkMaxSize)
}
if config.TTL != 0 {
logp.Warn(`The async Logstash client does not support the "ttl" option`)
}
enc := makeLogstashEventEncoder(config.Index)
queueSize := config.Pipelining - 1
timeout := config.Timeout
compressLvl := config.CompressionLevel
clientFactory := makeClientFactory(queueSize, timeout, enc, compressLvl)
var err error
c.client, err = clientFactory(c.Client)
if err != nil {
return nil, err
}
c.connect = func() error {
err := c.Client.Connect()
if err == nil {
c.client, err = clientFactory(c.Client)
}
return err
}
return c, nil
}
func makeClientFactory(
queueSize int,
timeout time.Duration,
enc func(interface{}) ([]byte, error),
compressLvl int,
) func(net.Conn) (*v2.AsyncClient, error) {
return func(conn net.Conn) (*v2.AsyncClient, error) {
return v2.NewAsyncClientWithConn(conn, queueSize,
v2.JSONEncoder(enc),
v2.Timeout(timeout),
v2.CompressionLevel(compressLvl),
)
}
}
func (c *asyncClient) Connect() error {
logp.Debug("logstash", "connect")
return c.connect()
}
func (c *asyncClient) Close() error {
logp.Debug("logstash", "close connection")
if c.client != nil {
err := c.client.Close()
c.client = nil
return err
}
return c.Client.Close()
}
func (c *asyncClient) Publish(batch publisher.Batch) error {
st := c.stats
events := batch.Events()
st.NewBatch(len(events))
if len(events) == 0 {
batch.ACK()
return nil
}
ref := &msgRef{
client: c,
count: atomic.MakeUint32(1),
batch: batch,
slice: events,
batchSize: len(events),
win: c.win,
err: nil,
}
defer ref.dec()
for len(events) > 0 {
var (
n int
err error
)
if c.win == nil {
n = len(events)
err = c.sendEvents(ref, events)
} else {
n, err = c.publishWindowed(ref, events)
}
debugf("%v events out of %v events sent to logstash. Continue sending",
n, len(events))
events = events[n:]
if err != nil {
_ = c.Close()
return err
}
}
return nil
}
func (c *asyncClient) publishWindowed(
ref *msgRef,
events []publisher.Event,
) (int, error) {
batchSize := len(events)
windowSize := c.win.get()
debugf("Try to publish %v events to logstash with window size %v",
batchSize, windowSize)
// prepare message payload
if batchSize > windowSize {
events = events[:windowSize]
}
err := c.sendEvents(ref, events)
if err != nil {
return 0, err
}
return len(events), nil
}
func (c *asyncClient) sendEvents(ref *msgRef, events []publisher.Event) error {
window := make([]interface{}, len(events))
for i := range events {
window[i] = &events[i].Content
}
ref.count.Inc()
return c.client.Send(ref.callback, window)
}
func (r *msgRef) callback(seq uint32, err error) {
if err != nil {
r.fail(seq, err)
} else {
r.done(seq)
}
}
func (r *msgRef) done(n uint32) {
r.client.stats.Acked(int(n))
r.slice = r.slice[n:]
if r.win != nil {
r.win.tryGrowWindow(r.batchSize)
}
r.dec()
}
func (r *msgRef) fail(n uint32, err error) {
if r.err == nil {
r.err = err
}
r.slice = r.slice[n:]
if r.win != nil {
r.win.shrinkWindow()
}
r.client.stats.Acked(int(n))
r.dec()
}
func (r *msgRef) dec() {
i := r.count.Dec()
if i > 0 {
return
}
if L := len(r.slice); L > 0 {
r.client.stats.Failed(L)
}
err := r.err
if err == nil {
r.batch.ACK()
return
}
r.batch.RetryEvents(r.slice)
logp.Err("Failed to publish events caused by: %v", err)
}