/
adapter.go
292 lines (225 loc) · 7.72 KB
/
adapter.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
/*
Copyright 2022 TriggerMesh Inc.
Licensed 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 awssqssource
import (
"context"
"runtime"
"strconv"
"strings"
"sync"
"time"
"go.uber.org/zap"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
pkgadapter "knative.dev/eventing/pkg/adapter/v2"
"knative.dev/pkg/logging"
"github.com/triggermesh/triggermesh/pkg/apis/sources"
"github.com/triggermesh/triggermesh/pkg/sources/adapter/common"
"github.com/triggermesh/triggermesh/pkg/sources/adapter/common/health"
)
const (
logfieldMsgID = "msgID"
logfieldMsgIDs = "msgIDs"
)
// envConfig is a set parameters sourced from the environment for the source's
// adapter.
type envConfig struct {
pkgadapter.EnvConfig
ARN string `envconfig:"ARN" required:"true"`
// Assume this IAM Role when access keys provided.
AssumeIamRole string `envconfig:"AWS_ASSUME_ROLE_ARN"`
// Name of a message processor which takes care of converting SQS
// messages to CloudEvents.
//
// Supported values: [ default s3 eventbridge ]
MessageProcessor string `envconfig:"SQS_MESSAGE_PROCESSOR" default:"default"`
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html
// Visibility timeout to set on all messages received by this event source.
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html
VisibilityTimeout *time.Duration `envconfig:"SQS_VISIBILITY_TIMEOUT"`
// Allows overriding common CloudEvents attributes.
CEOverrideSource string `envconfig:"CE_SOURCE"`
CEOverrideType string `envconfig:"CE_TYPE"`
// The environment variables below aren't read from the envConfig struct
// by the AWS SDK, but rather directly using os.Getenv().
// They are nevertheless listed here for documentation purposes.
_ string `envconfig:"AWS_ACCESS_KEY_ID"`
_ string `envconfig:"AWS_SECRET_ACCESS_KEY"`
}
// adapter implements the source's adapter.
type adapter struct {
logger *zap.SugaredLogger
mt *pkgadapter.MetricTag
sr *statsReporter
sqsClient sqsiface.SQSAPI
ceClient cloudevents.Client
arn arn.ARN
msgPrcsr MessageProcessor
visibilityTimeoutSeconds *int64
processQueue chan *sqs.Message
deleteQueue chan *sqs.Message
deletePeriod time.Duration
}
// NewEnvConfig satisfies pkgadapter.EnvConfigConstructor.
func NewEnvConfig() pkgadapter.EnvConfigAccessor {
return &envConfig{}
}
// NewAdapter satisfies pkgadapter.AdapterConstructor.
func NewAdapter(ctx context.Context, envAcc pkgadapter.EnvConfigAccessor, ceClient cloudevents.Client) pkgadapter.Adapter {
logger := logging.FromContext(ctx)
mustRegisterStatsView()
mt := &pkgadapter.MetricTag{
ResourceGroup: sources.AWSSQSSourceResource.String(),
Namespace: envAcc.GetNamespace(),
Name: envAcc.GetName(),
}
env := envAcc.(*envConfig)
arn := common.MustParseARN(env.ARN)
var msgPrcsr MessageProcessor
switch env.MessageProcessor {
case "s3":
msgPrcsr = &s3MessageProcessor{
ceSourceFallback: arn.String(),
}
case "eventbridge":
msgPrcsr = &eventbridgeMessageProcessor{
ceSource: env.CEOverrideSource,
ceSourceFallback: arn.String(),
}
case "default":
msgPrcsr = &defaultMessageProcessor{
ceSource: arn.String(),
}
default:
panic("Unsupported message processor " + strconv.Quote(env.MessageProcessor))
}
var visibilityTimeoutSeconds *int64
if vt := env.VisibilityTimeout; vt != nil {
if *vt < 0 || *vt > 12*time.Hour {
logger.Warn("Ignoring out of bounds visibility timeout (", *vt, ")")
} else {
vts := durationInSeconds(*vt)
visibilityTimeoutSeconds = &vts
}
}
sess := session.Must(session.NewSession(aws.NewConfig().
WithRegion(arn.Region).
WithEndpointResolver(common.EndpointResolver(arn.Partition)),
))
config := &aws.Config{}
if env.AssumeIamRole != "" {
config.Credentials = stscreds.NewCredentials(sess, env.AssumeIamRole)
}
// allocate generous buffer sizes to limit blocking on surges of new
// messages coming from receivers
const batchSizePerProc = 9
queueBufferSizeProcess := maxReceiveMsgBatchSize * runtime.GOMAXPROCS(-1) * batchSizePerProc
queueBufferSizeDelete := queueBufferSizeProcess
sr := mustNewStatsReporter(mt)
sr.reportQueueCapacityProcess(queueBufferSizeProcess)
sr.reportQueueCapacityDelete(queueBufferSizeDelete)
return &adapter{
logger: logger,
mt: mt,
sr: sr,
sqsClient: sqs.New(sess, config),
ceClient: ceClient,
arn: arn,
msgPrcsr: msgPrcsr,
visibilityTimeoutSeconds: visibilityTimeoutSeconds,
processQueue: make(chan *sqs.Message, queueBufferSizeProcess),
deleteQueue: make(chan *sqs.Message, queueBufferSizeDelete),
deletePeriod: maxDeleteMsgPeriod,
}
}
// Start implements adapter.Adapter.
func (a *adapter) Start(ctx context.Context) error {
go health.Start(ctx)
url, err := a.queueLookup(a.arn.Resource)
if err != nil {
a.logger.Errorw("Unable to find URL of SQS queue "+a.arn.Resource, zap.Error(err))
return err
}
health.MarkReady()
queueURL := *url.QueueUrl
a.logger.Infof("Listening to SQS queue at URL: %s", queueURL)
msgCtx, cancel := context.WithCancel(pkgadapter.ContextWithMetricTag(ctx, a.mt))
defer cancel()
var wg sync.WaitGroup
// This event source spends most of its time waiting for the network,
// so we can run more than one of each receiver|processor|deleter for
// each available thread.
const instancesPerProc = 3
for i := 0; i < runtime.GOMAXPROCS(-1)*instancesPerProc; i++ {
// TODO(antoineco): spawn and terminate receivers dynamically
// based on the current amount of messages being processed to
// optimize costs generated by ReceiveMessage API requests.
// https://github.com/triggermesh/triggermesh/issues/227
wg.Add(1)
go func() {
defer wg.Done()
a.runMessagesReceiver(msgCtx, queueURL)
}()
wg.Add(1)
go func() {
defer wg.Done()
a.runMessagesProcessor(msgCtx)
}()
wg.Add(1)
go func() {
defer wg.Done()
a.runMessagesDeleter(msgCtx, queueURL)
}()
}
<-ctx.Done()
cancel()
a.logger.Debug("Waiting for message handlers to terminate")
wg.Wait()
return nil
}
// queueLookup finds the URL for a given queue name in the user's account.
// Needs to be an exact match to queue name and queue must be unique name in the AWS account.
func (a *adapter) queueLookup(queueName string) (*sqs.GetQueueUrlOutput, error) {
return a.sqsClient.GetQueueUrl(&sqs.GetQueueUrlInput{
QueueName: &queueName,
})
}
// prettifyBatchResultErrors returns a pretty string representing a list of
// batch failures.
func prettifyBatchResultErrors(errs []*sqs.BatchResultErrorEntry) string {
if len(errs) == 0 {
return ""
}
var errStr strings.Builder
errStr.WriteByte('[')
for i, f := range errs {
errStr.WriteString(f.String())
if i+1 < len(errs) {
errStr.WriteByte(',')
}
}
errStr.WriteByte(']')
return errStr.String()
}
// durationInSeconds returns a duration as a number of seconds truncated
// towards zero.
func durationInSeconds(d time.Duration) int64 {
// converting a floating-point number to an integer discards
// the fraction (truncation towards zero)
return int64(d.Seconds())
}