forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
185 lines (152 loc) · 3.51 KB
/
consumer.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
package pipeline
import (
"github.com/elastic/beats/libbeat/common/atomic"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/publisher/queue"
)
// eventConsumer collects and forwards events from the queue to the outputs work queue.
// The eventConsumer is managed by the controller and receives additional pause signals
// from the retryer in case of too many events failing to be send or if retryer
// is receiving cancelled batches from outputs to be closed on output reloading.
type eventConsumer struct {
logger *logp.Logger
done chan struct{}
ctx *batchContext
pause atomic.Bool
wait atomic.Bool
sig chan consumerSignal
queue queue.Queue
consumer queue.Consumer
out *outputGroup
}
type consumerSignal struct {
tag consumerEventTag
consumer queue.Consumer
out *outputGroup
}
type consumerEventTag uint8
const (
sigConsumerCheck consumerEventTag = iota
sigConsumerUpdateOutput
sigConsumerUpdateInput
)
func newEventConsumer(
log *logp.Logger,
queue queue.Queue,
ctx *batchContext,
) *eventConsumer {
c := &eventConsumer{
logger: log,
done: make(chan struct{}),
sig: make(chan consumerSignal, 3),
out: nil,
queue: queue,
consumer: queue.Consumer(),
ctx: ctx,
}
c.pause.Store(true)
go c.loop(c.consumer)
return c
}
func (c *eventConsumer) close() {
c.consumer.Close()
close(c.done)
}
func (c *eventConsumer) sigWait() {
c.wait.Store(true)
c.sigHint()
}
func (c *eventConsumer) sigUnWait() {
c.wait.Store(false)
c.sigHint()
}
func (c *eventConsumer) sigPause() {
c.pause.Store(true)
c.sigHint()
}
func (c *eventConsumer) sigContinue() {
c.pause.Store(false)
c.sigHint()
}
func (c *eventConsumer) sigHint() {
// send signal to unblock a consumer trying to publish events.
// With flags being set atomically, multiple signals can be compressed into one
// signal -> drop if queue is not empty
select {
case c.sig <- consumerSignal{tag: sigConsumerCheck}:
default:
}
}
func (c *eventConsumer) updOutput(grp *outputGroup) {
// close consumer to break consumer worker from pipeline
c.consumer.Close()
// update output
c.sig <- consumerSignal{
tag: sigConsumerUpdateOutput,
out: grp,
}
// update eventConsumer with new queue connection
c.consumer = c.queue.Consumer()
c.sig <- consumerSignal{
tag: sigConsumerUpdateInput,
consumer: c.consumer,
}
}
func (c *eventConsumer) loop(consumer queue.Consumer) {
log := c.logger
log.Debug("start pipeline event consumer")
var (
out workQueue
batch *Batch
paused = true
)
handleSignal := func(sig consumerSignal) {
switch sig.tag {
case sigConsumerCheck:
case sigConsumerUpdateOutput:
c.out = sig.out
case sigConsumerUpdateInput:
consumer = sig.consumer
}
paused = c.paused()
if !paused && c.out != nil && batch != nil {
out = c.out.workQueue
} else {
out = nil
}
}
for {
if !paused && c.out != nil && consumer != nil && batch == nil {
out = c.out.workQueue
queueBatch, err := consumer.Get(c.out.batchSize)
if err != nil {
out = nil
consumer = nil
continue
}
batch = newBatch(c.ctx, queueBatch, c.out.timeToLive)
paused = c.paused()
if paused {
out = nil
}
}
select {
case sig := <-c.sig:
handleSignal(sig)
continue
default:
}
select {
case <-c.done:
log.Debug("stop pipeline event consumer")
return
case sig := <-c.sig:
handleSignal(sig)
case out <- batch:
batch = nil
}
}
}
func (c *eventConsumer) paused() bool {
return c.pause.Load() || c.wait.Load()
}