-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
pipeline.go
292 lines (249 loc) · 9.27 KB
/
pipeline.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Package pipeline combines all publisher functionality (processors, queue,
// outputs) to create instances of complete publisher pipelines, beats can
// connect to publish events to.
package pipeline
import (
"fmt"
"time"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common/acker"
"github.com/elastic/beats/v7/libbeat/common/reload"
"github.com/elastic/beats/v7/libbeat/outputs"
"github.com/elastic/beats/v7/libbeat/publisher"
"github.com/elastic/beats/v7/libbeat/publisher/processing"
"github.com/elastic/beats/v7/libbeat/publisher/queue"
"github.com/elastic/beats/v7/libbeat/publisher/queue/diskqueue"
"github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue"
conf "github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/logp"
)
// Pipeline implementation providint all beats publisher functionality.
// The pipeline consists of clients, processors, a central queue, an output
// controller and the actual outputs.
// The queue implementing the queue.Queue interface is the most central entity
// to the pipeline, providing support for pushung, batching and pulling events.
// The pipeline adds different ACKing strategies and wait close support on top
// of the queue. For handling ACKs, the pipeline keeps track of filtered out events,
// to be ACKed to the client in correct order.
// The output controller configures a (potentially reloadable) set of load
// balanced output clients. Events will be pulled from the queue and pushed to
// the output clients using a shared work queue for the active outputs.Group.
// Processors in the pipeline are executed in the clients go-routine, before
// entering the queue. No filtering/processing will occur on the output side.
//
// For client connecting to this pipeline, the default PublishMode is
// OutputChooses.
type Pipeline struct {
beatInfo beat.Info
monitors Monitors
outputController *outputController
observer observer
// If waitCloseTimeout is positive, then the pipeline will wait up to the
// specified time when it is closed for pending events to be acknowledged.
waitCloseTimeout time.Duration
processors processing.Supporter
}
// Settings is used to pass additional settings to a newly created pipeline instance.
type Settings struct {
// WaitClose sets the maximum duration to block when clients or pipeline itself is closed.
// When and how WaitClose is applied depends on WaitCloseMode.
WaitClose time.Duration
WaitCloseMode WaitCloseMode
Processors processing.Supporter
InputQueueSize int
}
// WaitCloseMode enumerates the possible behaviors of WaitClose in a pipeline.
type WaitCloseMode uint8
const (
// NoWaitOnClose disable wait close in the pipeline. Clients can still
// selectively enable WaitClose when connecting to the pipeline.
NoWaitOnClose WaitCloseMode = iota
// WaitOnPipelineClose applies WaitClose to the pipeline itself, waiting for outputs
// to ACK any outstanding events. This is independent of Clients asking for
// ACK and/or WaitClose. Clients can still optionally configure WaitClose themselves.
WaitOnPipelineClose
)
// OutputReloader interface, that can be queried from an active publisher pipeline.
// The output reloader can be used to change the active output.
type OutputReloader interface {
Reload(
cfg *reload.ConfigWithMeta,
factory func(outputs.Observer, conf.Namespace) (outputs.Group, error),
) error
}
// New create a new Pipeline instance from a queue instance and a set of outputs.
// The new pipeline will take ownership of queue and outputs. On Close, the
// queue and outputs will be closed.
func New(
beat beat.Info,
monitors Monitors,
userQueueConfig conf.Namespace,
out outputs.Group,
settings Settings,
) (*Pipeline, error) {
if monitors.Logger == nil {
monitors.Logger = logp.NewLogger("publish")
}
p := &Pipeline{
beatInfo: beat,
monitors: monitors,
observer: nilObserver,
waitCloseTimeout: settings.WaitClose,
processors: settings.Processors,
}
if settings.WaitCloseMode == WaitOnPipelineClose && settings.WaitClose > 0 {
p.waitCloseTimeout = settings.WaitClose
}
if monitors.Metrics != nil {
p.observer = newMetricsObserver(monitors.Metrics)
}
// Convert the raw queue config to a parsed Settings object that will
// be used during queue creation. This lets us fail immediately on startup
// if there's a configuration problem.
queueType := defaultQueueType
if b := userQueueConfig.Name(); b != "" {
queueType = b
}
queueFactory, err := queueFactoryForUserConfig(queueType, userQueueConfig.Config())
if err != nil {
return nil, err
}
output, err := newOutputController(beat, monitors, p.observer, queueFactory, settings.InputQueueSize)
if err != nil {
return nil, err
}
p.outputController = output
p.outputController.Set(out)
return p, nil
}
// Close stops the pipeline, outputs and queue.
// If WaitClose with WaitOnPipelineClose mode is configured, Close will block
// for a duration of WaitClose, if there are still active events in the pipeline.
// Note: clients must be closed before calling Close.
func (p *Pipeline) Close() error {
log := p.monitors.Logger
log.Debug("close pipeline")
// Note: active clients are not closed / disconnected.
p.outputController.WaitClose(p.waitCloseTimeout)
p.observer.cleanup()
return nil
}
// Connect creates a new client with default settings.
func (p *Pipeline) Connect() (beat.Client, error) {
return p.ConnectWith(beat.ClientConfig{})
}
// ConnectWith create a new Client for publishing events to the pipeline.
// The client behavior on close and ACK handling can be configured by setting
// the appropriate fields in the passed ClientConfig.
// If not set otherwise the defaut publish mode is OutputChooses.
//
// It is responsibility of the caller to close the client.
func (p *Pipeline) ConnectWith(cfg beat.ClientConfig) (beat.Client, error) {
var (
canDrop bool
eventFlags publisher.EventFlags
)
err := validateClientConfig(&cfg)
if err != nil {
return nil, err
}
switch cfg.PublishMode {
case beat.GuaranteedSend:
eventFlags = publisher.GuaranteedSend
case beat.DropIfFull:
canDrop = true
}
waitClose := cfg.WaitClose
processors, err := p.createEventProcessing(cfg.Processing, publishDisabled)
if err != nil {
return nil, err
}
client := &client{
logger: p.monitors.Logger,
clientListener: cfg.ClientListener,
processors: processors,
eventFlags: eventFlags,
canDrop: canDrop,
observer: p.observer,
}
client.isOpen.Store(true)
ackHandler := cfg.EventListener
var waiter *clientCloseWaiter
if waitClose > 0 {
waiter = newClientCloseWaiter(waitClose)
if ackHandler == nil {
ackHandler = waiter
} else {
ackHandler = acker.Combine(waiter, ackHandler)
}
}
producerCfg := queue.ProducerConfig{
ACK: func(count int) {
client.observer.eventsACKed(count)
if ackHandler != nil {
ackHandler.ACKEvents(count)
}
},
}
if ackHandler == nil {
ackHandler = acker.Nil()
}
client.eventListener = ackHandler
client.waiter = waiter
client.producer = p.outputController.queueProducer(producerCfg)
if client.producer == nil {
// This can only happen if the pipeline was shut down while clients
// were still waiting to connect.
return nil, fmt.Errorf("client failed to connect because the pipeline is shutting down")
}
p.observer.clientConnected()
return client, nil
}
func (p *Pipeline) createEventProcessing(cfg beat.ProcessingConfig, noPublish bool) (beat.Processor, error) {
if p.processors == nil {
return nil, nil
}
return p.processors.Create(cfg, noPublish)
}
// OutputReloader returns a reloadable object for the output section of this pipeline
func (p *Pipeline) OutputReloader() OutputReloader {
return p.outputController
}
// Parses the given config and returns a QueueFactory based on it.
// This helper exists to frontload config parsing errors: if there is an
// error in the queue config, we want it to show up as fatal during
// initialization, even if the queue itself isn't created until later.
func queueFactoryForUserConfig(queueType string, userConfig *conf.C) (queue.QueueFactory, error) {
switch queueType {
case memqueue.QueueType:
settings, err := memqueue.SettingsForUserConfig(userConfig)
if err != nil {
return nil, err
}
return memqueue.FactoryForSettings(settings), nil
case diskqueue.QueueType:
settings, err := diskqueue.SettingsForUserConfig(userConfig)
if err != nil {
return nil, err
}
return diskqueue.FactoryForSettings(settings), nil
default:
return nil, fmt.Errorf("unrecognized queue type '%v'", queueType)
}
}