-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
sqs_input.go
311 lines (268 loc) · 8.77 KB
/
sqs_input.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package awss3
import (
"context"
"fmt"
"sync"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
v2 "github.com/elastic/beats/v7/filebeat/input/v2"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/elastic-agent-libs/logp"
)
type sqsReaderInput struct {
config config
awsConfig awssdk.Config
sqs sqsAPI
s3 s3API
msgHandler sqsProcessor
log *logp.Logger
metrics *inputMetrics
// The Beats pipeline, used to create clients for event publication when
// creating the worker goroutines.
pipeline beat.Pipeline
// The expected region based on the queue URL
detectedRegion string
// Workers send on workRequestChan to indicate they're ready for the next
// message, and the reader loop replies on workResponseChan.
workRequestChan chan struct{}
workResponseChan chan types.Message
// workerWg is used to wait on worker goroutines during shutdown
workerWg sync.WaitGroup
}
// Simple wrapper to handle creation of internal channels
func newSQSReaderInput(config config, awsConfig awssdk.Config) *sqsReaderInput {
return &sqsReaderInput{
config: config,
awsConfig: awsConfig,
workRequestChan: make(chan struct{}, config.NumberOfWorkers),
workResponseChan: make(chan types.Message),
}
}
func (in *sqsReaderInput) Name() string { return inputName }
func (in *sqsReaderInput) Test(ctx v2.TestContext) error {
return nil
}
func (in *sqsReaderInput) Run(
inputContext v2.Context,
pipeline beat.Pipeline,
) error {
// Initialize everything for this run
err := in.setup(inputContext, pipeline)
if err != nil {
return err
}
// Start the main run loop
ctx := v2.GoContextFromCanceler(inputContext.Cancelation)
in.run(ctx)
in.cleanup()
return nil
}
// Apply internal initialization based on the parameters of Run, in
// preparation for calling run. setup and run are separate functions so
// tests can apply mocks and overrides before the run loop.
func (in *sqsReaderInput) setup(
inputContext v2.Context,
pipeline beat.Pipeline,
) error {
in.log = inputContext.Logger.With("queue_url", in.config.QueueURL)
in.pipeline = pipeline
in.detectedRegion = getRegionFromQueueURL(in.config.QueueURL)
if in.config.RegionName != "" {
// Configured region always takes precedence
in.awsConfig.Region = in.config.RegionName
} else if in.detectedRegion != "" {
// Only use detected region if there is no explicit region configured.
in.awsConfig.Region = in.detectedRegion
} else if in.config.AWSConfig.DefaultRegion != "" {
// If we can't find anything else, fall back on the default.
in.awsConfig.Region = in.config.AWSConfig.DefaultRegion
} else {
// If we can't find a usable region, return an error
return fmt.Errorf("region not specified and failed to get AWS region from queue_url: %w", errBadQueueURL)
}
in.sqs = &awsSQSAPI{
client: sqs.NewFromConfig(in.awsConfig, in.config.sqsConfigModifier),
queueURL: in.config.QueueURL,
apiTimeout: in.config.APITimeout,
visibilityTimeout: in.config.VisibilityTimeout,
longPollWaitTime: in.config.SQSWaitTime,
}
in.s3 = newAWSs3API(s3.NewFromConfig(in.awsConfig, in.config.s3ConfigModifier))
in.metrics = newInputMetrics(inputContext.ID, nil, in.config.NumberOfWorkers)
var err error
in.msgHandler, err = in.createEventProcessor()
if err != nil {
return fmt.Errorf("failed to initialize sqs reader: %w", err)
}
return nil
}
// Release internal resources created during setup (currently just metrics).
// This is its own function so tests can handle the run loop in isolation.
func (in *sqsReaderInput) cleanup() {
if in.metrics != nil {
in.metrics.Close()
}
}
// Create the main goroutines for the input (workers, message count monitor)
// and begin the run loop.
func (in *sqsReaderInput) run(ctx context.Context) {
in.logConfigSummary()
// Poll metrics periodically in the background
go messageCountMonitor{
sqs: in.sqs,
metrics: in.metrics,
}.run(ctx)
in.startWorkers(ctx)
in.readerLoop(ctx)
in.workerWg.Wait()
}
func (in *sqsReaderInput) readerLoop(ctx context.Context) {
// requestCount is the number of outstanding work requests that the
// reader will try to fulfill
requestCount := 0
for ctx.Err() == nil {
// Block to wait for more requests if requestCount is zero
requestCount += channelRequestCount(ctx, in.workRequestChan, requestCount == 0)
msgs := readSQSMessages(ctx, in.log, in.sqs, in.metrics, requestCount)
for _, msg := range msgs {
select {
case <-ctx.Done():
return
case in.workResponseChan <- msg:
requestCount--
}
}
}
}
type sqsWorker struct {
input *sqsReaderInput
client beat.Client
ackHandler *awsACKHandler
}
func (in *sqsReaderInput) newSQSWorker() (*sqsWorker, error) {
// Create a pipeline client scoped to this worker.
ackHandler := newAWSACKHandler()
client, err := in.pipeline.ConnectWith(beat.ClientConfig{
EventListener: ackHandler.pipelineEventListener(),
Processing: beat.ProcessingConfig{
// This input only produces events with basic types so normalization
// is not required.
EventNormalization: boolPtr(false),
},
})
if err != nil {
return nil, fmt.Errorf("connecting to pipeline: %w", err)
}
return &sqsWorker{
input: in,
client: client,
ackHandler: ackHandler,
}, nil
}
func (w *sqsWorker) run(ctx context.Context) {
defer w.client.Close()
defer w.ackHandler.Close()
for ctx.Err() == nil {
// Send a work request
select {
case <-ctx.Done():
// Shutting down
return
case w.input.workRequestChan <- struct{}{}:
}
// The request is sent, wait for a response
select {
case <-ctx.Done():
return
case msg := <-w.input.workResponseChan:
w.processMessage(ctx, msg)
}
}
}
func (w *sqsWorker) processMessage(ctx context.Context, msg types.Message) {
publishCount := 0
id := w.input.metrics.beginSQSWorker()
result := w.input.msgHandler.ProcessSQS(ctx, &msg, func(e beat.Event) {
w.client.Publish(e)
publishCount++
})
if publishCount == 0 {
// No events made it through (probably an error state), wrap up immediately
result.Done()
} else {
// Add this result's Done callback to the pending ACKs list
w.ackHandler.Add(publishCount, result.Done)
}
w.input.metrics.endSQSWorker(id)
}
func (in *sqsReaderInput) startWorkers(ctx context.Context) {
// Start the worker goroutines that will fetch messages via workRequestChan
// and workResponseChan until the input shuts down.
for i := 0; i < in.config.NumberOfWorkers; i++ {
in.workerWg.Add(1)
go func() {
defer in.workerWg.Done()
worker, err := in.newSQSWorker()
if err != nil {
in.log.Error(err)
return
}
go worker.run(ctx)
}()
}
}
func (in *sqsReaderInput) logConfigSummary() {
log := in.log
log.Infof("AWS api_timeout is set to %v.", in.config.APITimeout)
log.Infof("AWS region is set to %v.", in.awsConfig.Region)
if in.awsConfig.Region != in.detectedRegion {
log.Warnf("configured region disagrees with queue_url region (%q != %q): using %q", in.awsConfig.Region, in.detectedRegion, in.awsConfig.Region)
}
log.Infof("AWS SQS visibility_timeout is set to %v.", in.config.VisibilityTimeout)
log.Infof("AWS SQS number_of_workers is set to %v.", in.config.NumberOfWorkers)
if in.config.BackupConfig.GetBucketName() != "" {
log.Warnf("You have the backup_to_bucket functionality activated with SQS. Please make sure to set appropriate destination buckets " +
"or prefixes to avoid an infinite loop.")
}
}
func (in *sqsReaderInput) createEventProcessor() (sqsProcessor, error) {
fileSelectors := in.config.getFileSelectors()
s3EventHandlerFactory := newS3ObjectProcessorFactory(in.metrics, in.s3, fileSelectors, in.config.BackupConfig)
script, err := newScriptFromConfig(in.log.Named("sqs_script"), in.config.SQSScript)
if err != nil {
return nil, err
}
return newSQSS3EventProcessor(in.log.Named("sqs_s3_event"), in.metrics, in.sqs, script, in.config.VisibilityTimeout, in.config.SQSMaxReceiveCount, s3EventHandlerFactory), nil
}
// Read all pending requests and return their count. If block is true,
// waits until the result is at least 1, unless the context expires.
func channelRequestCount(
ctx context.Context,
requestChan chan struct{},
block bool,
) int {
requestCount := 0
if block {
// Wait until at least one request comes in.
select {
case <-ctx.Done():
return 0
case <-requestChan:
requestCount++
}
}
// Read as many requests as we can without blocking.
for {
select {
case <-requestChan:
requestCount++
default:
return requestCount
}
}
}