forked from bigpigeon/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
214 lines (177 loc) · 4.16 KB
/
retry.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
package pipeline
import (
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher"
)
// retryer is responsible for accepting and managing failed send attempts. It
// will also accept not yet published events from outputs being dynamically closed
// by the controller. Cancelled batches will be forwarded to the new workQueue,
// without updating the events retry counters.
// If too many batches (number of outputs/3) are stored in the retry buffer,
// will the consumer be paused, until some batches have been processed by some
// outputs.
type retryer struct {
logger *logp.Logger
observer outputObserver
done chan struct{}
consumer *eventConsumer
sig chan retryerSignal
out workQueue
in retryQueue
}
type retryQueue chan batchEvent
type retryerSignal struct {
tag retryerEventTag
channel workQueue
}
type batchEvent struct {
tag retryerBatchTag
batch *Batch
}
type retryerEventTag uint8
const (
sigRetryerOutputAdded retryerEventTag = iota
sigRetryerOutputRemoved
sigRetryerUpdateOutput
)
type retryerBatchTag uint8
const (
retryBatch retryerBatchTag = iota
cancelledBatch
)
func newRetryer(
log *logp.Logger,
observer outputObserver,
out workQueue,
c *eventConsumer,
) *retryer {
r := &retryer{
logger: log,
observer: observer,
done: make(chan struct{}),
sig: make(chan retryerSignal, 3),
in: retryQueue(make(chan batchEvent, 3)),
out: out,
consumer: c,
}
go r.loop()
return r
}
func (r *retryer) close() {
close(r.done)
}
func (r *retryer) sigOutputAdded() {
r.sig <- retryerSignal{tag: sigRetryerOutputAdded}
}
func (r *retryer) sigOutputRemoved() {
r.sig <- retryerSignal{tag: sigRetryerOutputRemoved}
}
func (r *retryer) updOutput(ch workQueue) {
r.sig <- retryerSignal{
tag: sigRetryerUpdateOutput,
channel: ch,
}
}
func (r *retryer) retry(b *Batch) {
r.in <- batchEvent{tag: retryBatch, batch: b}
}
func (r *retryer) cancelled(b *Batch) {
r.in <- batchEvent{tag: cancelledBatch, batch: b}
}
func (r *retryer) loop() {
var (
out workQueue
consumerBlocked bool
active *Batch
activeSize int
buffer []*Batch
numOutputs int
log = r.logger
)
for {
select {
case <-r.done:
return
case evt := <-r.in:
var (
countFailed int
countDropped int
batch = evt.batch
countRetry = len(batch.events)
)
if evt.tag == retryBatch {
countFailed = len(batch.events)
r.observer.eventsFailed(countFailed)
decBatch(batch)
countRetry = len(batch.events)
countDropped = countFailed - countRetry
r.observer.eventsDropped(countDropped)
}
if len(batch.events) == 0 {
log.Info("Drop batch")
batch.Drop()
} else {
out = r.out
buffer = append(buffer, batch)
out = r.out
active = buffer[0]
activeSize = len(active.events)
if !consumerBlocked {
consumerBlocked = blockConsumer(numOutputs, len(buffer))
if consumerBlocked {
log.Info("retryer: send wait signal to consumer")
r.consumer.sigWait()
log.Info(" done")
}
}
}
case out <- active:
r.observer.eventsRetry(activeSize)
buffer = buffer[1:]
active, activeSize = nil, 0
if len(buffer) == 0 {
out = nil
} else {
active = buffer[0]
activeSize = len(active.events)
}
if consumerBlocked {
consumerBlocked = blockConsumer(numOutputs, len(buffer))
if !consumerBlocked {
log.Info("retryer: send unwait-signal to consumer")
r.consumer.sigUnWait()
log.Info(" done")
}
}
case sig := <-r.sig:
switch sig.tag {
case sigRetryerUpdateOutput:
r.out = sig.channel
case sigRetryerOutputAdded:
numOutputs++
case sigRetryerOutputRemoved:
numOutputs--
}
}
}
}
func blockConsumer(numOutputs, numBatches int) bool {
return numBatches/3 >= numOutputs
}
func decBatch(batch *Batch) {
if batch.ttl <= 0 {
return
}
batch.ttl--
if batch.ttl > 0 {
return
}
// filter for evens with guaranteed send flags
events := batch.events[:0]
for _, event := range batch.events {
if (event.Flags & publisher.GuaranteedSend) == publisher.GuaranteedSend {
events = append(events, event)
}
}
batch.events = events
}