forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
258 lines (216 loc) · 5.56 KB
/
publish.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
package beater
import (
"sync"
"sync/atomic"
"time"
"github.com/elastic/beats/filebeat/input"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"
)
type logPublisher interface {
Start()
Stop()
}
type syncLogPublisher struct {
client publisher.Client
in, out chan []*input.FileEvent
done chan struct{}
wg sync.WaitGroup
}
type asyncLogPublisher struct {
client publisher.Client
in, out chan []*input.FileEvent
// list of in-flight batches
active batchList
stopping bool
done chan struct{}
wg sync.WaitGroup
}
// eventsBatch is used to store sorted list of actively published log lines.
// Implements `outputs.Signalerr` interface for marking batch as finished
type eventsBatch struct {
next *eventsBatch
flag int32
events []*input.FileEvent
}
type batchList struct {
head, tail *eventsBatch
}
type batchStatus int32
const (
defaultGCTimeout = 1 * time.Second
)
const (
batchInProgress batchStatus = iota
batchSuccess
batchFailed
batchCanceled
)
func newPublisher(
async bool,
in, out chan []*input.FileEvent,
client publisher.Client,
) logPublisher {
if async {
return newAsyncLogPublisher(in, out, client)
}
return newSyncLogPublisher(in, out, client)
}
func newSyncLogPublisher(
in, out chan []*input.FileEvent,
client publisher.Client,
) *syncLogPublisher {
return &syncLogPublisher{
in: in,
out: out,
client: client,
done: make(chan struct{}),
}
}
func (p *syncLogPublisher) Start() {
p.wg.Add(1)
go func() {
defer p.wg.Done()
logp.Info("Start sending events to output")
for {
var events []*input.FileEvent
select {
case <-p.done:
return
case events = <-p.in:
}
pubEvents := make([]common.MapStr, 0, len(events))
for _, event := range events {
// Only send event with bytes read. 0 Bytes means state update only
if event.Bytes > 0 {
pubEvents = append(pubEvents, event.ToMapStr())
}
}
p.client.PublishEvents(pubEvents, publisher.Sync, publisher.Guaranteed)
logp.Info("Events sent: %d", len(events))
// Tell the registrar that we've successfully sent these events
select {
case <-p.done:
return
case p.out <- events:
}
}
}()
}
func (p *syncLogPublisher) Stop() {
close(p.done)
p.client.Close()
p.wg.Wait()
}
func newAsyncLogPublisher(
in, out chan []*input.FileEvent,
client publisher.Client,
) *asyncLogPublisher {
return &asyncLogPublisher{
in: in,
out: out,
client: client,
done: make(chan struct{}),
}
}
func (p *asyncLogPublisher) Start() {
p.wg.Add(1)
go func() {
defer p.wg.Done()
logp.Info("Start sending events to output")
// short gc timer, in case no logs are received from spooler the queued
// bulkEvents can still be cleaned up and forwarded to the registrar
ticker := time.NewTicker(defaultGCTimeout)
for {
select {
case <-p.done:
return
case events := <-p.in:
pubEvents := make([]common.MapStr, len(events))
for i, event := range events {
pubEvents[i] = event.ToMapStr()
}
batch := &eventsBatch{
flag: 0,
events: events,
}
p.client.PublishEvents(pubEvents,
publisher.Signal(batch), publisher.Guaranteed)
p.active.append(batch)
case <-ticker.C:
}
p.collect()
}
}()
}
func (p *asyncLogPublisher) Stop() {
close(p.done)
p.client.Close()
p.wg.Wait()
}
// collect collects finished bulk-Events in order and forward processed batches
// to registrar. Reports to registrar are guaranteed to be in same order
// as bulk-Events have been received by the spooler
func (p *asyncLogPublisher) collect() bool {
for batch := p.active.head; batch != nil; batch = batch.next {
state := batchStatus(atomic.LoadInt32(&batch.flag))
if state == batchInProgress && !p.stopping {
break
}
if state == batchFailed {
// with guaranteed enabled this must must not happen.
msg := "Failed to process batch"
logp.Critical(msg)
panic(msg)
}
// remove batch from active list
p.active.head = batch.next
if batch.next == nil {
p.active.tail = nil
}
// Batches get marked as canceled, if publisher pipeline is shutting down
// In this case we do not want to send any more batches to the registrar
if state == batchCanceled {
p.stopping = true
}
if p.stopping {
logp.Info("Shutting down - No registrar update for potentially published batch.")
// if in failing state keep cleaning up queue
continue
}
// Tell the registrar that we've successfully publish the last batch events.
// If registrar queue is blocking (quite unlikely), but stop signal has been
// received in the meantime (by closing p.done), we do not wait for
// registrar picking up the current batch. Instead prefer to shut-down and
// resend the last published batch on next restart, basically taking advantage
// of send-at-last-once semantics in order to speed up cleanup on shutdown.
select {
case <-p.done:
logp.Info("Shutting down - No registrar update for successfully published batch.")
return false
case p.out <- batch.events:
}
}
return true
}
func (b *eventsBatch) Completed() {
atomic.StoreInt32(&b.flag, int32(batchSuccess))
}
func (b *eventsBatch) Failed() {
logp.Err("Failed to publish batch. Stop updating registrar.")
atomic.StoreInt32(&b.flag, int32(batchFailed))
}
func (b *eventsBatch) Canceled() {
logp.Info("In-flight batch has been canceled during shutdown")
atomic.StoreInt32(&b.flag, int32(batchCanceled))
}
func (l *batchList) append(b *eventsBatch) {
if l.head == nil {
l.head = b
} else {
l.tail.next = b
}
b.next = nil
l.tail = b
}