forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
async.go
245 lines (207 loc) · 4.5 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
235
236
237
238
239
240
241
242
243
244
245
package logstash
import (
"sync/atomic"
"time"
"github.com/elastic/go-lumber/client/v2"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
)
type asyncClient struct {
*transport.Client
client *v2.AsyncClient
host string
win *window
connect func() error
}
type msgRef struct {
client *asyncClient
count int32
batch []outputs.Data
err error
cb func([]outputs.Data, error)
win *window
batchSize int
}
func newAsyncLumberjackClient(
conn *transport.Client,
addr string,
config *logstashConfig,
) (*asyncClient, error) {
c := &asyncClient{
Client: conn,
host: addr,
}
if config.SlowStart {
maxWindowSize := config.BulkMaxSize
c.win = newWindower(defaultStartMaxWindowSize, maxWindowSize)
}
queueSize := config.Pipelining - 1
timeout := config.Timeout
compressLevel := config.CompressionLevel
enc, err := makeLogstashEventEncoder(config.Index)
if err != nil {
return nil, err
}
c.connect = func() error {
err := c.Client.Connect()
if err == nil {
c.client, err = v2.NewAsyncClientWithConn(c.Client,
queueSize,
v2.JSONEncoder(enc),
v2.Timeout(timeout),
v2.CompressionLevel(compressLevel))
}
return err
}
return c, nil
}
func (c *asyncClient) Connect(timeout time.Duration) error {
logp.Debug("logstash", "connect to logstash host %v", c.host)
return c.connect()
}
func (c *asyncClient) Close() error {
logp.Debug("logstash", "close connection to logstash host %v", c.host)
if c.client != nil {
err := c.client.Close()
c.client = nil
return err
}
return c.Client.Close()
}
func (c *asyncClient) AsyncPublishEvent(
cb func(error),
data outputs.Data,
) error {
return c.client.Send(
func(seq uint32, err error) {
cb(err)
},
[]interface{}{data},
)
}
func (c *asyncClient) AsyncPublishEvents(
cb func([]outputs.Data, error),
data []outputs.Data,
) error {
publishEventsCallCount.Add(1)
if len(data) == 0 {
debug("send nil")
cb(nil, nil)
return nil
}
ref := newMsgRef(c, data, cb)
defer ref.dec()
for len(data) > 0 {
var (
n int
err error
)
if c.win == nil {
n = len(data)
err = c.sendEvents(ref, data)
} else {
n, err = c.publishWindowed(ref, data)
}
debug("%v events out of %v events sent to logstash host %s. Continue sending",
n, len(data), c.host)
data = data[n:]
if err != nil {
_ = c.Close()
return err
}
}
return nil
}
func (c *asyncClient) publishWindowed(
ref *msgRef,
data []outputs.Data,
) (int, error) {
batchSize := len(data)
windowSize := c.win.get()
debug("Try to publish %v events to logstash host %v with window size %v",
batchSize, c.host, windowSize)
// prepare message payload
if batchSize > windowSize {
data = data[:windowSize]
}
err := c.sendEvents(ref, data)
if err != nil {
return 0, err
}
return len(data), nil
}
func (c *asyncClient) sendEvents(ref *msgRef, data []outputs.Data) error {
window := make([]interface{}, len(data))
for i, d := range data {
window[i] = d
}
ref.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 newMsgRef(
client *asyncClient,
data []outputs.Data,
cb func([]outputs.Data, error),
) *msgRef {
r := &msgRef{
client: client,
count: 1,
batch: data,
batchSize: len(data),
win: client.win,
cb: cb,
err: nil,
}
debug("msgref(%p) new: batch=%p, cb=%p", r, &r.batch[0], cb)
return r
}
func (r *msgRef) inc() {
count := atomic.AddInt32(&r.count, 1)
debug("msgref(%p) inc -> %v", r, count)
}
func (r *msgRef) done(n uint32) {
debug("msgref(%p) done(%v)", r, n)
ackedEvents.Add(int64(n))
r.batch = r.batch[n:]
if r.win != nil {
r.win.tryGrowWindow(r.batchSize)
}
r.dec()
}
func (r *msgRef) fail(n uint32, err error) {
debug("msgref(%p) fail(%v, %v)", r, n, err)
ackedEvents.Add(int64(n))
if r.err == nil {
r.err = err
}
r.batch = r.batch[n:]
if r.win != nil {
r.win.shrinkWindow()
}
r.dec()
}
func (r *msgRef) dec() {
i := atomic.AddInt32(&r.count, -1)
debug("msgref(%p) dec -> %v", r, i)
if i > 0 {
return
}
err := r.err
if err != nil {
eventsNotAcked.Add(int64(len(r.batch)))
logp.Err("Failed to publish events (host: %v) caused by: %v", r.client.host, err)
debug("msgref(%p) exec callback(%p, %v)", r, &r.batch[0], err)
r.cb(r.batch, err)
return
}
debug("msgref(%p) exec callback(nil, nil)", r)
r.cb(nil, nil)
}