forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
175 lines (145 loc) · 3.28 KB
/
sync.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
package logstash
import (
"time"
"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 syncClient struct {
*transport.Client
client *v2.SyncClient
stats *outputs.Stats
win *window
ttl time.Duration
ticker *time.Ticker
}
func newSyncClient(
conn *transport.Client,
stats *outputs.Stats,
config *Config,
) (*syncClient, error) {
c := &syncClient{
Client: conn,
stats: stats,
ttl: config.TTL,
}
if config.SlowStart {
c.win = newWindower(defaultStartMaxWindowSize, config.BulkMaxSize)
}
if c.ttl > 0 {
c.ticker = time.NewTicker(c.ttl)
}
var err error
enc := makeLogstashEventEncoder(config.Index)
c.client, err = v2.NewSyncClientWithConn(conn,
v2.JSONEncoder(enc),
v2.Timeout(config.Timeout),
v2.CompressionLevel(config.CompressionLevel),
)
if err != nil {
return nil, err
}
return c, nil
}
func (c *syncClient) Connect() error {
logp.Debug("logstash", "connect")
err := c.Client.Connect()
if err != nil {
return err
}
if c.ticker != nil {
c.ticker = time.NewTicker(c.ttl)
}
return nil
}
func (c *syncClient) Close() error {
if c.ticker != nil {
c.ticker.Stop()
}
logp.Debug("logstash", "close connection")
return c.Client.Close()
}
func (c *syncClient) reconnect() error {
if err := c.Client.Close(); err != nil {
logp.Err("error closing connection to logstash: %s, reconnecting...", err)
}
return c.Client.Connect()
}
func (c *syncClient) Publish(batch publisher.Batch) error {
events := batch.Events()
st := c.stats
st.NewBatch(len(events))
if len(events) == 0 {
batch.ACK()
return nil
}
for len(events) > 0 {
// check if we need to reconnect
if c.ticker != nil {
select {
case <-c.ticker.C:
if err := c.reconnect(); err != nil {
batch.Retry()
return err
}
// reset window size on reconnect
if c.win != nil {
c.win.windowSize = int32(defaultStartMaxWindowSize)
}
default:
}
}
var (
n int
err error
)
if c.win == nil {
n, err = c.sendEvents(events)
} else {
n, err = c.publishWindowed(events)
}
events = events[n:]
st.Acked(n)
debugf("%v events out of %v events sent to logstash. Continue sending",
n, len(events))
if err != nil {
// return batch to pipeline before reporting/counting error
batch.RetryEvents(events)
if c.win != nil {
c.win.shrinkWindow()
}
_ = c.Close()
logp.Err("Failed to publish events caused by: %v", err)
rest := len(events)
st.Failed(rest)
return err
}
}
batch.ACK()
return nil
}
func (c *syncClient) publishWindowed(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]
}
n, err := c.sendEvents(events)
if err != nil {
return n, err
}
c.win.tryGrowWindow(batchSize)
return n, nil
}
func (c *syncClient) sendEvents(events []publisher.Event) (int, error) {
window := make([]interface{}, len(events))
for i := range events {
window[i] = &events[i].Content
}
return c.client.Send(window)
}