forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sync.go
177 lines (147 loc) · 3.43 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
176
177
package logstash
import (
"time"
"github.com/elastic/beats/libbeat/beat"
"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
observer outputs.Observer
win *window
ttl time.Duration
ticker *time.Ticker
}
func newSyncClient(
beat beat.Info,
conn *transport.Client,
observer outputs.Observer,
config *Config,
) (*syncClient, error) {
c := &syncClient{
Client: conn,
observer: observer,
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(beat, 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 host %s: %s, reconnecting...", c.Host(), err)
}
return c.Client.Connect()
}
func (c *syncClient) Publish(batch publisher.Batch) error {
events := batch.Events()
st := c.observer
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)
}
debugf("%v events out of %v events sent to logstash host %s. Continue sending",
n, len(events), c.Host())
events = events[n:]
st.Acked(n)
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 host %s with window size %v",
batchSize, c.Host(), 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)
}