-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconsumer.go
556 lines (493 loc) · 15.4 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package gokini
import (
"fmt"
"math"
"math/rand"
"os"
"sync"
"time"
"github.com/pkg/errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
const (
defaultEmptyRecordBackoffMs = 500
defaultMillisecondsBackoffClaim = 30000
defaultEventLoopSleepMs = 1000
// ErrCodeKMSThrottlingException is defined in the API Reference https://docs.aws.amazon.com/sdk-for-go/api/service/kinesis/#Kinesis.GetRecords
// But it's not a constant?
ErrCodeKMSThrottlingException = "KMSThrottlingException"
)
// RecordConsumer is the interface consumers will implement
type RecordConsumer interface {
Init(string) error
ProcessRecords([]*Records, *KinesisConsumer)
Shutdown()
}
// Records is structure for Kinesis Records
type Records struct {
Data []byte `json:"data"`
PartitionKey string `json:"partitionKey"`
SequenceNumber string `json:"sequenceNumber"`
ShardID string `json:"shardID"`
}
type shardStatus struct {
ID string
Checkpoint string
AssignedTo string
LeaseTimeout time.Time
ParentShardID *string
Closed bool
ClaimRequest *string
readyToBeClosed bool
sync.Mutex
}
// KinesisConsumer contains all the configuration and functions necessary to start the Kinesis Consumer
type KinesisConsumer struct {
StreamName string
ShardIteratorType string
RecordConsumer RecordConsumer
EmptyRecordBackoffMs int
LeaseDuration int
Monitoring MonitoringConfiguration
DisableAutomaticCheckpoints bool
Retries *int
IgnoreShardOrdering bool
TableName string
DynamoReadCapacityUnits *int64
DynamoWriteCapacityUnits *int64
DynamoBillingMode *string
Session *session.Session // Setting session means Retries is ignored
millisecondsBackoffClaim int
eventLoopSleepMs int
svc kinesisiface.KinesisAPI
checkpointer Checkpointer
stop *chan struct{}
shardStatus map[string]*shardStatus
consumerID string
mService monitoringService
shardStealInProgress bool
sync.WaitGroup
}
var defaultRetries = 5
// StartConsumer starts the RecordConsumer, calls Init and starts sending records to ProcessRecords
func (kc *KinesisConsumer) StartConsumer() error {
rand.Seed(time.Now().UnixNano())
// Set Defaults
if kc.EmptyRecordBackoffMs == 0 {
kc.EmptyRecordBackoffMs = defaultEmptyRecordBackoffMs
}
kc.consumerID = uuid.New().String()
err := kc.Monitoring.init(kc.StreamName, kc.consumerID, kc.Session)
if err != nil {
log.Errorf("Failed to start monitoring service: %s", err)
}
kc.mService = kc.Monitoring.service
if kc.millisecondsBackoffClaim == 0 {
kc.millisecondsBackoffClaim = defaultMillisecondsBackoffClaim
}
if kc.eventLoopSleepMs == 0 {
kc.eventLoopSleepMs = defaultEventLoopSleepMs
}
retries := defaultRetries
if kc.Retries != nil {
retries = *kc.Retries
}
if kc.Session == nil {
log.Debugln("Creating AWS Session", kc.consumerID)
kc.Session, err = session.NewSessionWithOptions(
session.Options{
Config: aws.Config{Retryer: client.DefaultRetryer{NumMaxRetries: retries}},
SharedConfigState: session.SharedConfigEnable,
},
)
if err != nil {
return err
}
}
if kc.svc == nil && kc.checkpointer == nil {
if endpoint := os.Getenv("KINESIS_ENDPOINT"); endpoint != "" {
kc.Session.Config.Endpoint = aws.String(endpoint)
}
kc.svc = kinesis.New(kc.Session)
kc.checkpointer = &DynamoCheckpoint{
ReadCapacityUnits: kc.DynamoReadCapacityUnits,
WriteCapacityUnits: kc.DynamoWriteCapacityUnits,
BillingMode: kc.DynamoBillingMode,
TableName: kc.TableName,
Retries: retries,
LeaseDuration: kc.LeaseDuration,
Session: kc.Session,
}
}
log.Debugf("Initializing Checkpointer")
if err := kc.checkpointer.Init(); err != nil {
return errors.Wrapf(err, "Failed to start Checkpointer")
}
kc.shardStatus = make(map[string]*shardStatus)
stopChan := make(chan struct{})
kc.stop = &stopChan
err = kc.getShardIDs("")
if err != nil {
log.Errorf("Error getting Kinesis shards: %s", err)
return err
}
go kc.eventLoop()
return nil
}
func (kc *KinesisConsumer) eventLoop() {
for {
log.Debug("Getting shards")
err := kc.getShardIDs("")
if err != nil {
log.Errorf("Error getting Kinesis shards: %s", err)
// Back-off?
time.Sleep(500 * time.Millisecond)
}
log.Debugf("Found %d shards", len(kc.shardStatus))
for _, shard := range kc.shardStatus {
// We already own this shard so carry on
if shard.AssignedTo == kc.consumerID {
continue
}
err := kc.checkpointer.FetchCheckpoint(shard)
if err != nil {
if err != ErrSequenceIDNotFound {
log.Error(err)
continue
}
}
var stealShard bool
if shard.ClaimRequest != nil {
if shard.LeaseTimeout.Before(time.Now().Add(time.Millisecond * time.Duration(kc.millisecondsBackoffClaim))) {
if *shard.ClaimRequest != kc.consumerID {
log.Debugln("Shard being stolen", shard.ID)
continue
} else {
stealShard = true
log.Debugln("Stealing shard", shard.ID)
}
}
}
err = kc.checkpointer.GetLease(shard, kc.consumerID)
if err != nil {
if err.Error() != ErrLeaseNotAquired {
log.Error(err)
}
continue
}
if stealShard {
log.Debugln("Successfully stole shard", shard.ID)
kc.shardStealInProgress = false
}
kc.mService.leaseGained(shard.ID)
kc.RecordConsumer.Init(shard.ID)
log.Debugf("Starting consumer for shard %s on %s", shard.ID, shard.AssignedTo)
kc.Add(1)
go kc.getRecords(shard.ID)
}
err = kc.rebalance()
if err != nil {
log.Warn(err)
}
select {
case <-*kc.stop:
log.Info("Shutting down")
return
case <-time.After(time.Duration(kc.eventLoopSleepMs) * time.Millisecond):
}
}
}
// Shutdown stops consuming records gracefully
func (kc *KinesisConsumer) Shutdown() {
close(*kc.stop)
kc.Wait()
kc.RecordConsumer.Shutdown()
}
func (kc *KinesisConsumer) getShardIDs(startShardID string) error {
args := &kinesis.DescribeStreamInput{
StreamName: aws.String(kc.StreamName),
}
if startShardID != "" {
args.ExclusiveStartShardId = aws.String(startShardID)
}
streamDesc, err := kc.svc.DescribeStream(args)
if err != nil {
return err
}
if *streamDesc.StreamDescription.StreamStatus != "ACTIVE" {
return errors.New("Stream not active")
}
var lastShardID string
for _, s := range streamDesc.StreamDescription.Shards {
if _, ok := kc.shardStatus[*s.ShardId]; !ok {
log.Debugf("Found shard with id %s", *s.ShardId)
kc.shardStatus[*s.ShardId] = &shardStatus{
ID: *s.ShardId,
ParentShardID: s.ParentShardId,
}
}
lastShardID = *s.ShardId
}
if *streamDesc.StreamDescription.HasMoreShards {
err := kc.getShardIDs(lastShardID)
if err != nil {
return err
}
}
return nil
}
func (kc *KinesisConsumer) getShardIterator(shard *shardStatus) (*string, error) {
err := kc.checkpointer.FetchCheckpoint(shard)
if err != nil && err != ErrSequenceIDNotFound {
return nil, err
}
if shard.Checkpoint == "" {
shardIterArgs := &kinesis.GetShardIteratorInput{
ShardId: &shard.ID,
ShardIteratorType: &kc.ShardIteratorType,
StreamName: &kc.StreamName,
}
iterResp, err := kc.svc.GetShardIterator(shardIterArgs)
if err != nil {
return nil, err
}
return iterResp.ShardIterator, nil
}
shardIterArgs := &kinesis.GetShardIteratorInput{
ShardId: &shard.ID,
ShardIteratorType: aws.String("AFTER_SEQUENCE_NUMBER"),
StartingSequenceNumber: &shard.Checkpoint,
StreamName: &kc.StreamName,
}
iterResp, err := kc.svc.GetShardIterator(shardIterArgs)
if err != nil {
return nil, err
}
return iterResp.ShardIterator, nil
}
func (kc *KinesisConsumer) getRecords(shardID string) {
defer kc.Done()
shard := kc.shardStatus[shardID]
shardIterator, err := kc.getShardIterator(shard)
if err != nil {
kc.RecordConsumer.Shutdown()
log.Errorf("Unable to get shard iterator for %s: %s", shardID, err)
return
}
var retriedErrors int
for {
getRecordsStartTime := time.Now()
err := kc.checkpointer.FetchCheckpoint(shard)
if err != nil && err != ErrSequenceIDNotFound {
log.Errorln("Error fetching checkpoint", err)
time.Sleep(time.Duration(defaultEventLoopSleepMs) * time.Second)
continue
}
if shard.ClaimRequest != nil {
// Claim request means another worker wants to steal our shard. So let the lease lapse
log.Infof("Shard %s has been stolen from us", shardID)
return
}
if time.Now().UTC().After(shard.LeaseTimeout.Add(-5 * time.Second)) {
err = kc.checkpointer.GetLease(shard, kc.consumerID)
if err != nil {
if err.Error() == ErrLeaseNotAquired {
shard.Lock()
defer shard.Unlock()
shard.AssignedTo = ""
kc.mService.leaseLost(shard.ID)
log.Debugln("Lease lost for shard", shard.ID, kc.consumerID)
return
}
log.Warnln("Error renewing lease", err)
time.Sleep(time.Duration(1) * time.Second)
continue
}
}
getRecordsArgs := &kinesis.GetRecordsInput{
ShardIterator: shardIterator,
}
getResp, err := kc.svc.GetRecords(getRecordsArgs)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == kinesis.ErrCodeProvisionedThroughputExceededException || awsErr.Code() == ErrCodeKMSThrottlingException {
log.Errorf("Error getting records from shard %v: %v", shardID, err)
retriedErrors++
time.Sleep(time.Duration(2^retriedErrors*100) * time.Millisecond)
continue
}
}
// This is an exception we cannot handle and therefore we exit
panic(fmt.Sprintf("Error getting records from Kinesis that cannot be retried: %s\nRequest: %s", err, getRecordsArgs))
}
retriedErrors = 0
var records []*Records
var recordBytes int64
for _, r := range getResp.Records {
record := &Records{
Data: r.Data,
PartitionKey: *r.PartitionKey,
SequenceNumber: *r.SequenceNumber,
ShardID: shardID,
}
records = append(records, record)
recordBytes += int64(len(record.Data))
log.Tracef("Processing record %s", *r.SequenceNumber)
}
processRecordsStartTime := time.Now()
kc.RecordConsumer.ProcessRecords(records, kc)
// Convert from nanoseconds to milliseconds
processedRecordsTiming := time.Since(processRecordsStartTime) / 1000000
kc.mService.recordProcessRecordsTime(shard.ID, float64(processedRecordsTiming))
if len(records) == 0 {
time.Sleep(time.Duration(kc.EmptyRecordBackoffMs) * time.Millisecond)
} else if !kc.DisableAutomaticCheckpoints {
kc.Checkpoint(shardID, *getResp.Records[len(getResp.Records)-1].SequenceNumber)
}
kc.mService.incrRecordsProcessed(shard.ID, len(records))
kc.mService.incrBytesProcessed(shard.ID, recordBytes)
kc.mService.millisBehindLatest(shard.ID, float64(*getResp.MillisBehindLatest))
// Convert from nanoseconds to milliseconds
getRecordsTime := time.Since(getRecordsStartTime) / 1000000
kc.mService.recordGetRecordsTime(shard.ID, float64(getRecordsTime))
// The shard has been closed, so no new records can be read from it
if getResp.NextShardIterator == nil {
log.Debugf("Shard %s closed", shardID)
shard := kc.shardStatus[shardID]
shard.Lock()
shard.readyToBeClosed = true
shard.Unlock()
if !kc.DisableAutomaticCheckpoints {
kc.Checkpoint(shardID, *getResp.Records[len(getResp.Records)-1].SequenceNumber)
}
return
}
shardIterator = getResp.NextShardIterator
select {
case <-*kc.stop:
log.Infoln("Received stop signal, stopping record consumer for", shardID)
return
case <-time.After(1 * time.Nanosecond):
}
}
}
// Checkpoint records the sequence number for the given shard ID as being processed
func (kc *KinesisConsumer) Checkpoint(shardID string, sequenceNumber string) error {
shard := kc.shardStatus[shardID]
shard.Lock()
shard.Checkpoint = sequenceNumber
shard.Unlock()
// If shard is closed and we've read all records from the shard, mark the shard as closed
if shard.readyToBeClosed {
var err error
shard.Closed, err = kc.shardIsEmpty(shard)
if err != nil {
return err
}
}
return kc.checkpointer.CheckpointSequence(shard)
}
func (kc *KinesisConsumer) shardIsEmpty(shard *shardStatus) (empty bool, err error) {
iterResp, err := kc.svc.GetShardIterator(&kinesis.GetShardIteratorInput{
ShardId: &shard.ID,
ShardIteratorType: aws.String("AFTER_SEQUENCE_NUMBER"),
StartingSequenceNumber: &shard.Checkpoint,
StreamName: &kc.StreamName,
})
if err != nil {
return
}
recordsResp, err := kc.svc.GetRecords(&kinesis.GetRecordsInput{
ShardIterator: iterResp.ShardIterator,
})
if err != nil {
return
}
if len(recordsResp.Records) == 0 {
empty = true
}
return
}
func (kc *KinesisConsumer) rebalance() error {
workers, err := kc.checkpointer.ListActiveWorkers()
if err != nil {
log.Debugln("Error listing workings", kc.consumerID, err)
return err
}
// Only attempt to steal one shard at at time, to allow for linear convergence
if kc.shardStealInProgress {
err := kc.getShardIDs("")
if err != nil {
return err
}
for _, shard := range kc.shardStatus {
if shard.ClaimRequest != nil && *shard.ClaimRequest == kc.consumerID {
log.Debugln("Steal in progress", kc.consumerID)
return nil
}
// Our shard steal was stomped on by a Checkpoint.
// We could deal with that, but instead just try again
kc.shardStealInProgress = false
}
}
var numShards float64
for _, shards := range workers {
numShards += float64(len(shards))
}
numWorkers := float64(len(workers))
// 1:1 shards to workers is optimal, so we cannot possibly rebalance
if numWorkers >= numShards {
log.Debugln("Optimal shard allocation, not stealing any shards", numWorkers, ">", numShards, kc.consumerID)
return nil
}
currentShards, ok := workers[kc.consumerID]
var numCurrentShards float64
if !ok {
numCurrentShards = 0
numWorkers++
} else {
numCurrentShards = float64(len(currentShards))
}
optimalShards := math.Floor(numShards / numWorkers)
log.Debugln("Number of shards", numShards)
log.Debugln("Number of workers", numWorkers)
log.Debugln("Optimal shards", optimalShards)
log.Debugln("Current shards", numCurrentShards)
// We have more than or equal optimal shards, so no rebalancing can take place
if numCurrentShards >= optimalShards {
log.Debugln("We have enough shards, not attempting to steal any", kc.consumerID)
return nil
}
maxShards := int(optimalShards)
var workerSteal *string
for w, shards := range workers {
if len(shards) > maxShards {
workerSteal = &w
maxShards = len(shards)
}
}
// Not all shards are allocated so fallback to default shard allocation mechanisms
if workerSteal == nil {
log.Debugln("Not all shards are allocated, not stealing any", kc.consumerID)
return nil
}
// Steal a random shard from the worker with the most shards
kc.shardStealInProgress = true
randIndex := rand.Perm(len(workers[*workerSteal]))[0]
shardToSteal := workers[*workerSteal][randIndex]
log.Debugln("Stealing shard", shardToSteal, "from", *workerSteal)
err = kc.checkpointer.ClaimShard(&shardStatus{
ID: shardToSteal,
}, kc.consumerID)
if err != nil {
kc.shardStealInProgress = false
}
return err
}