forked from twitchscience/kinsumer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shard_consumer.go
411 lines (360 loc) · 15.3 KB
/
shard_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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2016 Twitch Interactive
package kinsumer
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/pkg/errors"
)
const (
// getRecordsLimit is the max number of records in a single request. This effectively limits the
// total processing speed to getRecordsLimit*5/n where n is the number of parallel clients trying
// to consume from the same kinesis stream
getRecordsLimit = 10000 // 10,000 is the max according to the docs
// maxErrorRetries is how many times we will retry on a shard error
//maxErrorRetries = 3
// errorSleepDuration is how long we sleep when an error happens, this is multiplied by the number
// of retries to give a minor backoff behavior
errorSleepDuration = 1 * time.Second
)
// getShardIterator gets a shard iterator after the last sequence number we read or at the start of the stream
func (k *Kinsumer) getShardIterator(streamName string, shardID string, sequenceNumber string, iteratorType string) (string, error) {
// Use "AFTER_SEQUENCE_NUMBER" because we already processed the record at
// the given sequenceNumber
shardIteratorType := kinesis.ShardIteratorTypeAfterSequenceNumber
// If we do not have a sequenceNumber yet we need to get a shardIterator
// from the horizon
ps := aws.String(sequenceNumber)
if sequenceNumber == "" {
k.logger.Debugf("No Sequence Number, starting from %s, Shard ID: %s", iteratorType, shardID)
shardIteratorType = iteratorType
ps = nil
}
resp, err := k.kinesis.GetShardIterator(&kinesis.GetShardIteratorInput{
ShardId: aws.String(shardID),
ShardIteratorType: &shardIteratorType,
StartingSequenceNumber: ps,
StreamName: aws.String(streamName),
})
return aws.StringValue(resp.ShardIterator), err
}
// subscribeToShard gets an event stream after the last sequence number we read or at the start of the stream
// When the SubscribeToShard call succeeds, your consumer starts receiving events of type
// SubscribeToShardEvent over the HTTP/2 connection for up to 5 minutes, after which time
// you need to call SubscribeToShard again to renew the subscription if you want to
// continue to receive records.
func (k *Kinsumer) subscribeToShard(consumerARN *string, shardID string, sequenceNumber string, iteratorType string) (*kinesis.SubscribeToShardOutput, error) {
// Use "AFTER_SEQUENCE_NUMBER" because we already processed the record at
// the given sequenceNumber
shardIteratorType := kinesis.ShardIteratorTypeAfterSequenceNumber
// If we do not have a sequenceNumber yet we need to get a shardIterator
// from the horizon
ps := aws.String(sequenceNumber)
if sequenceNumber == "" {
k.logger.Debugf("No Sequence Number, starting from %s, Shard ID: %s", iteratorType, shardID)
shardIteratorType = iteratorType
ps = nil
}
// Subscribe to shard
out, err := k.kinesis.SubscribeToShard(&kinesis.SubscribeToShardInput{
ConsumerARN: consumerARN,
ShardId: aws.String(shardID),
StartingPosition: &kinesis.StartingPosition{
SequenceNumber: ps,
Type: &shardIteratorType,
},
})
return out, err
}
// getRecords returns the next records and shard iterator from the given shard iterator
func getRecords(k kinesisiface.KinesisAPI, iterator string) (records []*kinesis.Record, nextIterator string, lag time.Duration, err error) {
params := &kinesis.GetRecordsInput{
Limit: aws.Int64(getRecordsLimit),
ShardIterator: aws.String(iterator),
}
output, err := k.GetRecords(params)
if err != nil {
return nil, "", 0, err
}
records = output.Records
nextIterator = aws.StringValue(output.NextShardIterator)
lag = time.Duration(aws.Int64Value(output.MillisBehindLatest)) * time.Millisecond
return records, nextIterator, lag, nil
}
// captureShard blocks until we capture the given shardID
func (k *Kinsumer) captureShard(shardID string) (*Checkpointer, error) {
// Attempt to capture the shard in dynamo
for {
// Ask the checkpointer to capture the shard
checkpointer, err := Capture(
shardID,
k.checkpointTableName,
k.dynamodb,
k.clientName,
k.clientID,
k.maxAgeForClientRecord,
k.config.stats,
k.logger)
if err != nil {
return nil, err
}
if checkpointer != nil {
return checkpointer, nil
}
// Throttle requests so that we don't hammer dynamo
select {
case <-k.stop:
// If we are told to stop consuming we should stop attempting to capture
return nil, nil
case <-time.After(k.config.throttleDelay):
}
}
}
// consume is a blocking call that captures then consumes the given shard in a loop.
// It is also responsible for writing out the checkpoint updates to dynamo.
// TODO: There are no tests for this file. Not sure how to even unit test this.
func (k *Kinsumer) consume(shardID string) {
defer k.waitGroup.Done()
// capture the checkpointer
checkpointer, err := k.captureShard(shardID)
if err != nil {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "captureShard", Error: err, Level: FatalLevel}
return
}
// if we failed to capture the checkpointer but there was no errors
// we must have stopped, so don't process this shard at all
if checkpointer == nil {
return
}
sequenceNumber := checkpointer.GetSequenceNumber()
// Get the starting shard iterator
iterator, err := k.getShardIterator(k.streamName, shardID, sequenceNumber, k.config.shardIteratorType)
if err != nil {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "getShardIterator", Error: err, Level: FatalLevel}
return
}
k.logger.Debugf("getShardIterator, SequenceNumber: %s, Shard ID: %s", sequenceNumber, shardID)
// no throttle on the first request.
nextThrottle := time.After(0)
retryCount := 0
var lastSeqNum string
mainloop:
for {
// Handle async actions, and throttle requests to keep kinesis happy
select {
case <-k.stop:
return
case <-nextThrottle:
}
// Reset the nextThrottle
nextThrottle = time.After(k.config.throttleDelay)
// Get records from kinesis
records, next, lag, err := getRecords(k.kinesis, iterator)
if err != nil {
retryCount++
k.logger.Debugf("Failed to get records... retrying (%v/%v)", retryCount, k.config.shardRetryLimit)
if awsErr, ok := err.(awserr.Error); ok {
k.logger.Debugf("Failed to get records, AWS error: %v, %v", awsErr.Message(), awsErr.OrigErr())
// Critical AWS error
if strings.Contains(awsErr.Message(), "Signature expired") {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "getRecords", Error: errors.New("signature expired"), Level: FatalLevel}
return
}
}
// Close shard if maximum number of retries is exceeded.
// If we can't get records from this shard, we will close the entire app, instead of this single shard
// because the checkpointer would need to be released, and the responsibility for that was moved to the application.
if retryCount >= k.config.shardRetryLimit {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "getRecords",
Error: fmt.Errorf("failed to get records, retry limit exceeded (%v/%v)", retryCount, k.config.shardRetryLimit),
Level: FatalLevel,
}
return
}
// casting retryCount here to time.Duration purely for the multiplication, there is
// no meaning to retryCount nanoseconds
time.Sleep(errorSleepDuration * time.Duration(retryCount))
continue mainloop
}
retryCount = 0
// If the shard has been closed, the shard iterator can't return more data and GetRecords
// returns null in NextShardIterator.
if next == "" {
if len(records) > 0 {
// This should not happen, if it does then the AWS docs on how this works
// is misleading and we need to move this logic AFTER the RecordLoop: which
// will open us up to the possibility that the app will checkpoint before
// we mark a checkpointer as almost finished.
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "getRecords", Error: errors.Errorf("records found in an event with a nil NextShardIterator!"), Level: FatalLevel}
return
}
k.endOfShard(checkpointer, lastSeqNum)
return
}
iterator = next
// Put all the records we got onto the channel
k.config.stats.EventsFromKinesis(len(records), shardID, lag)
if len(records) > 0 {
retrievedAt := time.Now()
for _, record := range records {
RecordLoop:
// Loop until we stop or the record is consumed
// We pass the checkpointer with the record
for {
select {
case <-k.stop:
return
case k.records <- &ConsumedRecord{
Record: record,
Checkpointer: checkpointer,
retrievedAt: retrievedAt,
}:
break RecordLoop
}
}
}
// Update the last sequence number we saw, in case we reached the end of the stream.
lastSeqNum = aws.StringValue(records[len(records)-1].SequenceNumber)
}
}
}
// consumeWithFanOut is a blocking call that captures then consumes the given shard in a loop.
// It is also responsible for writing out the checkpoint updates to dynamo.
// TODO: There are no tests for this file. Not sure how to even unit test this.
func (k *Kinsumer) consumeWithFanOut(shardID string, registered *kinesis.RegisterStreamConsumerOutput) {
defer k.waitGroup.Done()
// capture the checkpointer
checkpointer, err := k.captureShard(shardID)
if err != nil {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "captureShard", Error: err, Level: FatalLevel}
return
}
// if we failed to capture the checkpointer but there was no errors
// we must have stopped, so don't process this shard at all
if checkpointer == nil {
return
}
sequenceNumber := checkpointer.GetSequenceNumber()
// Subscribe to the shard using enhanced fan-out
out, err := k.subscribeToShard(registered.Consumer.ConsumerARN, shardID, sequenceNumber, k.config.shardIteratorType)
if err != nil {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "subscribeToShard", Error: err, Level: FatalLevel}
return
}
k.logger.Debugf("subscribeToShard, SequenceNumber: %v, Shard ID: %v", sequenceNumber, shardID)
// A shard iterator expires 5 minutes after it is returned to the requester.
// You need to call SubscribeToShard again to renew the subscription if you
// want to continue to receive records. If you call SubscribeToShard with
// the same ConsumerARN and ShardId within 5 seconds of a successful call,
// you'll get a ResourceInUseException.
subscribeInterval := 4 * time.Minute
nextSubscribe := time.After(subscribeInterval)
var lastSeqNum string
for {
var evt kinesis.SubscribeToShardEventStreamEvent
select {
case <-k.stop:
return
case <-nextSubscribe:
k.logger.Debugf("re-subscribeToShard Shard %s at continuation sequence number %s", shardID, sequenceNumber)
var err error
out, err = k.subscribeToShard(registered.Consumer.ConsumerARN, shardID, sequenceNumber, k.config.shardIteratorType)
if err != nil {
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "subscribeToShard", Error: err, Level: FatalLevel}
return
}
nextSubscribe = time.After(subscribeInterval)
continue
// Receive records from kinesis
case evt = <-out.GetEventStream().Events():
}
// A nil event does NOT necessarily mean we finished processing this shard.
// This sometimes happens in rapid succession.
if evt == nil {
k.logger.Tracef("kinesis pushed a nil record to the kinsumer from shard %s", shardID)
continue
}
event := evt.(*kinesis.SubscribeToShardEvent)
// If event.ContinuationSequenceNumber returns null, it indicates that a shard
// split or merge has occurred that involved this shard. This shard is now in a
// CLOSED state, and you have read all available data records from this shard
if event.ContinuationSequenceNumber == nil {
if len(event.Records) > 0 {
// This should not happen, if it does then the AWS docs on how this works
// is misleading and we need to move this logic AFTER the RecordLoop: which
// will open us up to the possibility that the app will checkpoint before
// we mark a checkpointer as almost finished.
k.shardErrors <- ShardConsumerSignal{ShardID: shardID, Action: "subscribeToShard", Error: errors.Errorf("records found in an event with a nil ContinuationSequenceNumber!"), Level: FatalLevel}
return
}
k.endOfShard(checkpointer, lastSeqNum)
return
}
// Use this as SequenceNumber in the next call to SubscribeToShard.
// Use ContinuationSequenceNumber for checkpointing because it captures
// your shard progress even when no data is written to the shard.
sequenceNumber = *event.ContinuationSequenceNumber
records := event.Records
// Kinesis doesn't mind pushing us events with zero records
if len(records) == 0 {
k.logger.Tracef("No records in this push %s", *event.ContinuationSequenceNumber)
continue
}
// Report the lag and number of events we got to our stats receiver
lag := time.Duration(aws.Int64Value(event.MillisBehindLatest)) * time.Millisecond
k.config.stats.EventsFromKinesis(len(records), shardID, lag)
// Send all the records we got down the channel
retrievedAt := time.Now()
for _, record := range records {
RecordLoop:
// Loop until we stop or the record is consumed
// We pass the checkpointer with the record
for {
select {
case <-k.stop:
return
case k.records <- &ConsumedRecord{
Record: record,
Checkpointer: checkpointer,
retrievedAt: retrievedAt,
}:
break RecordLoop
}
}
// Update the last sequence number we saw, in case we reached the end of the stream.
lastSeqNum = aws.StringValue(records[len(records)-1].SequenceNumber)
}
}
}
// endOfShard marks the shard's checkpointer as finsihed an releases it if it deems
// necessary. Start and end are values used to determine if we started at the end of a shard.
func (k *Kinsumer) endOfShard(checkpointer *Checkpointer, finalSeqNumber string) {
shardID := checkpointer.GetShardID()
k.logger.Debugf("End of shard detected. Marking this checkpointer for %s as Finished", shardID)
// Mark the checkpointer as finished. The application is responsible
// for proecessing the records associated with it, calling Commit() with
// with the finalSeqNumber so that it gets marked as finished in dynamo,
// checking if this checkpointer is finished and, if so, releasing it.
checkpointer.Finish(finalSeqNumber)
// This means that we have a checkpoint in the database that
// starts at the end of a shard. We can end up in the situation
// if the application fails to Finish a checkpointer.
if finalSeqNumber == "" {
// Releasing a checkpointer is the reponsibility of the application that is
// using this library. If we have a checkpoint at the end of a shard that means
// the app processed everything and checkpointed it but has not yet released it.
// We should let the application handle closing
// this out but there is an edge case when refreshing shards that the app will
// drop his reference to this checkpointer before he has the chance to release it.
// In the off case that the app attempts to checkpoint after we released it the
// checkpoint attempt will be ignored because we dont care to checkpoint a
// checkpointer that is already completed. Same goes if the app tries to release it.
k.logger.Warnf("Looks like this checkpointer that was left open for shard %s. Releasing it now", shardID)
checkpointer.CommitWithSequenceNumber(finalSeqNumber)
checkpointer.Release()
}
}