2016-02-03 05:04:22 +00:00
|
|
|
package connector
|
|
|
|
|
|
|
|
|
|
import (
|
2016-05-08 01:05:52 +00:00
|
|
|
"os"
|
2016-02-03 05:04:22 +00:00
|
|
|
|
2016-05-08 01:05:52 +00:00
|
|
|
"github.com/apex/log"
|
2016-02-03 05:04:22 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
|
|
|
|
)
|
|
|
|
|
|
2016-05-01 19:20:44 +00:00
|
|
|
// NewConsumer creates a new consumer with initialied kinesis connection
|
2016-05-08 01:05:52 +00:00
|
|
|
func NewConsumer(config Config) *Consumer {
|
|
|
|
|
config.setDefaults()
|
2016-05-01 05:23:35 +00:00
|
|
|
|
|
|
|
|
svc := kinesis.New(
|
|
|
|
|
session.New(
|
|
|
|
|
aws.NewConfig().WithMaxRetries(10),
|
|
|
|
|
),
|
2016-05-01 00:04:44 +00:00
|
|
|
)
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
return &Consumer{
|
2016-05-08 01:05:52 +00:00
|
|
|
svc: svc,
|
|
|
|
|
Config: config,
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-16 00:45:38 +00:00
|
|
|
// Consumer wraps the interaction with the Kinesis stream
|
2016-02-03 05:04:22 +00:00
|
|
|
type Consumer struct {
|
2016-05-08 01:05:52 +00:00
|
|
|
svc *kinesis.Kinesis
|
|
|
|
|
Config
|
2016-05-01 05:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-01 00:04:44 +00:00
|
|
|
// Start takes a handler and then loops over each of the shards
|
|
|
|
|
// processing each one with the handler.
|
2016-02-03 05:04:22 +00:00
|
|
|
func (c *Consumer) Start(handler Handler) {
|
2016-05-01 00:04:44 +00:00
|
|
|
resp, err := c.svc.DescribeStream(
|
|
|
|
|
&kinesis.DescribeStreamInput{
|
2016-05-08 01:05:52 +00:00
|
|
|
StreamName: aws.String(c.StreamName),
|
2016-05-01 00:04:44 +00:00
|
|
|
},
|
|
|
|
|
)
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
2016-05-08 01:05:52 +00:00
|
|
|
c.Logger.WithError(err).Error("DescribeStream")
|
|
|
|
|
os.Exit(1)
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, shard := range resp.StreamDescription.Shards {
|
|
|
|
|
go c.handlerLoop(*shard.ShardId, handler)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Consumer) handlerLoop(shardID string, handler Handler) {
|
2016-05-01 01:05:04 +00:00
|
|
|
buf := &Buffer{
|
2016-05-08 01:05:52 +00:00
|
|
|
MaxRecordCount: c.BufferSize,
|
2016-12-26 15:24:34 +00:00
|
|
|
shardID: shardID,
|
2016-05-01 01:05:04 +00:00
|
|
|
}
|
2016-05-08 01:05:52 +00:00
|
|
|
ctx := c.Logger.WithFields(log.Fields{
|
|
|
|
|
"shard": shardID,
|
|
|
|
|
})
|
2017-10-16 00:40:30 +00:00
|
|
|
shardIterator := c.getShardIterator(shardID)
|
2016-05-08 01:05:52 +00:00
|
|
|
|
2016-05-01 17:43:42 +00:00
|
|
|
ctx.Info("processing")
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
for {
|
2016-05-01 01:05:04 +00:00
|
|
|
resp, err := c.svc.GetRecords(
|
|
|
|
|
&kinesis.GetRecordsInput{
|
|
|
|
|
ShardIterator: shardIterator,
|
|
|
|
|
},
|
|
|
|
|
)
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-16 00:40:30 +00:00
|
|
|
ctx.WithError(err).Error("GetRecords")
|
|
|
|
|
shardIterator = c.getShardIterator(shardID)
|
|
|
|
|
continue
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(resp.Records) > 0 {
|
|
|
|
|
for _, r := range resp.Records {
|
2016-05-01 01:05:04 +00:00
|
|
|
buf.AddRecord(r)
|
2016-02-03 05:04:22 +00:00
|
|
|
|
2016-05-01 01:05:04 +00:00
|
|
|
if buf.ShouldFlush() {
|
|
|
|
|
handler.HandleRecords(*buf)
|
2016-05-08 01:05:52 +00:00
|
|
|
ctx.WithField("count", buf.RecordCount()).Info("flushed")
|
2016-12-04 08:08:06 +00:00
|
|
|
c.Checkpoint.SetCheckpoint(shardID, buf.LastSeq())
|
2016-05-01 01:05:04 +00:00
|
|
|
buf.Flush()
|
2016-02-08 21:21:54 +00:00
|
|
|
}
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-16 00:40:30 +00:00
|
|
|
if resp.NextShardIterator == nil || shardIterator == resp.NextShardIterator {
|
|
|
|
|
shardIterator = c.getShardIterator(shardID)
|
|
|
|
|
} else {
|
|
|
|
|
shardIterator = resp.NextShardIterator
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Consumer) getShardIterator(shardID string) *string {
|
|
|
|
|
params := &kinesis.GetShardIteratorInput{
|
|
|
|
|
ShardId: aws.String(shardID),
|
|
|
|
|
StreamName: aws.String(c.StreamName),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Checkpoint.CheckpointExists(shardID) {
|
2017-10-16 00:45:38 +00:00
|
|
|
params.ShardIteratorType = aws.String("AFTER_SEQUENCE_NUMBER")
|
2017-10-16 00:40:30 +00:00
|
|
|
params.StartingSequenceNumber = aws.String(c.Checkpoint.SequenceNumber())
|
|
|
|
|
} else {
|
2017-10-16 00:45:38 +00:00
|
|
|
params.ShardIteratorType = aws.String("TRIM_HORIZON")
|
2017-10-16 00:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := c.svc.GetShardIterator(params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Logger.WithError(err).Error("GetShardIterator")
|
|
|
|
|
os.Exit(1)
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
2017-10-16 00:40:30 +00:00
|
|
|
|
|
|
|
|
return resp.ShardIterator
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|