2017-11-20 16:21:40 +00:00
|
|
|
package consumer
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
import (
|
2017-11-20 16:21:40 +00:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2017-11-23 16:49:37 +00:00
|
|
|
"io/ioutil"
|
2017-11-22 18:46:39 +00:00
|
|
|
"log"
|
2017-11-20 16:21:40 +00:00
|
|
|
"sync"
|
2016-02-03 05:04:22 +00:00
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/kinesis"
|
|
|
|
|
)
|
|
|
|
|
|
2018-06-08 15:40:42 +00:00
|
|
|
// ScanError signals the consumer if we should continue scanning for next record
|
|
|
|
|
// and whether to checkpoint.
|
|
|
|
|
type ScanError struct {
|
|
|
|
|
Error error
|
|
|
|
|
StopScan bool
|
|
|
|
|
SkipCheckpoint bool
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// Record is an alias of record returned from kinesis library
|
2017-11-21 16:58:16 +00:00
|
|
|
type Record = kinesis.Record
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// Client interface is used for interacting with kinesis stream
|
|
|
|
|
type Client interface {
|
|
|
|
|
GetShardIDs(string) ([]string, error)
|
|
|
|
|
GetRecords(ctx context.Context, streamName, shardID, lastSeqNum string) (<-chan *Record, <-chan error, error)
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 01:44:42 +00:00
|
|
|
// Counter interface is used for exposing basic metrics from the scanner
|
2017-11-22 22:09:22 +00:00
|
|
|
type Counter interface {
|
|
|
|
|
Add(string, int64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type noopCounter struct{}
|
|
|
|
|
|
|
|
|
|
func (n noopCounter) Add(string, int64) {}
|
|
|
|
|
|
2017-11-23 01:44:42 +00:00
|
|
|
// Checkpoint interface used track consumer progress in the stream
|
|
|
|
|
type Checkpoint interface {
|
2017-11-23 04:01:31 +00:00
|
|
|
Get(streamName, shardID string) (string, error)
|
|
|
|
|
Set(streamName, shardID, sequenceNumber string) error
|
2017-11-23 01:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type noopCheckpoint struct{}
|
|
|
|
|
|
2017-11-23 04:01:31 +00:00
|
|
|
func (n noopCheckpoint) Set(string, string, string) error { return nil }
|
|
|
|
|
func (n noopCheckpoint) Get(string, string) (string, error) { return "", nil }
|
2017-11-23 01:44:42 +00:00
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
// Option is used to override defaults when creating a new Consumer
|
|
|
|
|
type Option func(*Consumer) error
|
2016-05-01 05:23:35 +00:00
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
// WithCheckpoint overrides the default checkpoint
|
2017-11-23 01:44:42 +00:00
|
|
|
func WithCheckpoint(checkpoint Checkpoint) Option {
|
2017-11-20 16:21:40 +00:00
|
|
|
return func(c *Consumer) error {
|
|
|
|
|
c.checkpoint = checkpoint
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithLogger overrides the default logger
|
2017-11-22 18:46:39 +00:00
|
|
|
func WithLogger(logger *log.Logger) Option {
|
2017-11-20 16:21:40 +00:00
|
|
|
return func(c *Consumer) error {
|
|
|
|
|
c.logger = logger
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 22:09:22 +00:00
|
|
|
// WithCounter overrides the default counter
|
|
|
|
|
func WithCounter(counter Counter) Option {
|
|
|
|
|
return func(c *Consumer) error {
|
|
|
|
|
c.counter = counter
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// WithClient overrides the default client
|
|
|
|
|
func WithClient(client Client) Option {
|
|
|
|
|
return func(c *Consumer) error {
|
|
|
|
|
c.client = client
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
// New creates a kinesis consumer with default settings. Use Option to override
|
|
|
|
|
// any of the optional attributes.
|
2017-11-27 00:00:11 +00:00
|
|
|
func New(streamName string, opts ...Option) (*Consumer, error) {
|
|
|
|
|
if streamName == "" {
|
2017-11-21 16:58:16 +00:00
|
|
|
return nil, fmt.Errorf("must provide stream name")
|
2017-11-20 16:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// new consumer with no-op checkpoint, counter, and logger
|
2017-11-20 16:21:40 +00:00
|
|
|
c := &Consumer{
|
2017-11-27 00:00:11 +00:00
|
|
|
streamName: streamName,
|
2017-11-23 01:44:42 +00:00
|
|
|
checkpoint: &noopCheckpoint{},
|
2017-11-23 01:52:41 +00:00
|
|
|
counter: &noopCounter{},
|
2017-11-23 16:49:37 +00:00
|
|
|
logger: log.New(ioutil.Discard, "", log.LstdFlags),
|
2017-11-27 02:16:32 +00:00
|
|
|
client: NewKinesisClient(),
|
2017-11-20 16:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// override defaults
|
2017-11-20 16:21:40 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
|
if err := opt(c); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c, nil
|
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 {
|
2017-11-20 16:21:40 +00:00
|
|
|
streamName string
|
2017-11-27 00:00:11 +00:00
|
|
|
client Client
|
2017-11-22 18:46:39 +00:00
|
|
|
logger *log.Logger
|
2017-11-23 01:44:42 +00:00
|
|
|
checkpoint Checkpoint
|
2017-11-22 22:09:22 +00:00
|
|
|
counter Counter
|
2016-05-01 05:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
// Scan scans each of the shards of the stream, calls the callback
|
2017-11-20 19:45:30 +00:00
|
|
|
// func with each of the kinesis records.
|
2018-06-08 15:40:42 +00:00
|
|
|
func (c *Consumer) Scan(ctx context.Context, fn func(*Record) ScanError) error {
|
2017-11-27 00:00:11 +00:00
|
|
|
shardIDs, err := c.client.GetShardIDs(c.streamName)
|
2016-02-03 05:04:22 +00:00
|
|
|
if err != nil {
|
2017-11-27 00:00:11 +00:00
|
|
|
return fmt.Errorf("get shards error: %v", err)
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
if len(shardIDs) == 0 {
|
2017-11-23 19:29:58 +00:00
|
|
|
return fmt.Errorf("no shards available")
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2017-11-23 16:49:37 +00:00
|
|
|
var (
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
errc = make(chan error, 1)
|
|
|
|
|
)
|
2017-11-27 00:00:11 +00:00
|
|
|
wg.Add(len(shardIDs))
|
2017-11-20 16:21:40 +00:00
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
// process each shard in goroutine
|
|
|
|
|
for _, shardID := range shardIDs {
|
2017-11-20 16:21:40 +00:00
|
|
|
go func(shardID string) {
|
|
|
|
|
defer wg.Done()
|
2017-11-23 19:29:58 +00:00
|
|
|
|
|
|
|
|
if err := c.ScanShard(ctx, shardID, fn); err != nil {
|
2017-11-23 16:49:37 +00:00
|
|
|
select {
|
|
|
|
|
case errc <- fmt.Errorf("shard %s error: %v", shardID, err):
|
|
|
|
|
// first error to occur
|
|
|
|
|
default:
|
|
|
|
|
// error has already occured
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-23 19:29:58 +00:00
|
|
|
|
2017-11-20 16:21:40 +00:00
|
|
|
cancel()
|
2017-11-27 00:00:11 +00:00
|
|
|
}(shardID)
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
2017-11-20 16:21:40 +00:00
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-11-23 16:49:37 +00:00
|
|
|
close(errc)
|
|
|
|
|
return <-errc
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-20 19:45:30 +00:00
|
|
|
// ScanShard loops over records on a specific shard, calls the callback func
|
2017-11-23 19:29:58 +00:00
|
|
|
// for each record and checkpoints the progress of scan.
|
|
|
|
|
// Note: Returning `false` from the callback func will end the scan.
|
2018-06-08 15:40:42 +00:00
|
|
|
func (c *Consumer) ScanShard(ctx context.Context, shardID string, fn func(*Record) ScanError) (err error) {
|
2017-11-23 04:01:31 +00:00
|
|
|
lastSeqNum, err := c.checkpoint.Get(c.streamName, shardID)
|
2017-11-21 16:58:16 +00:00
|
|
|
if err != nil {
|
2017-11-23 16:49:37 +00:00
|
|
|
return fmt.Errorf("get checkpoint error: %v", err)
|
2017-11-21 16:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
c.logger.Println("scanning", shardID, lastSeqNum)
|
|
|
|
|
// get records
|
|
|
|
|
recc, errc, err := c.client.GetRecords(ctx, c.streamName, shardID, lastSeqNum)
|
2017-11-23 16:49:37 +00:00
|
|
|
if err != nil {
|
2017-11-27 00:00:11 +00:00
|
|
|
return fmt.Errorf("get records error: %v", err)
|
2017-10-16 00:40:30 +00:00
|
|
|
}
|
2017-11-27 00:00:11 +00:00
|
|
|
// loop records
|
|
|
|
|
for r := range recc {
|
2018-06-08 15:40:42 +00:00
|
|
|
scanError := fn(r)
|
|
|
|
|
// It will be nicer if this can be reported with checkpoint error
|
|
|
|
|
err = scanError.Error
|
|
|
|
|
|
|
|
|
|
// Skip invalid state
|
|
|
|
|
if scanError.StopScan && scanError.SkipCheckpoint {
|
|
|
|
|
continue
|
2017-11-27 00:00:11 +00:00
|
|
|
}
|
2017-10-16 00:40:30 +00:00
|
|
|
|
2018-06-08 15:40:42 +00:00
|
|
|
if scanError.StopScan {
|
|
|
|
|
break
|
|
|
|
|
}
|
2017-10-16 00:40:30 +00:00
|
|
|
|
2018-06-08 15:40:42 +00:00
|
|
|
if !scanError.SkipCheckpoint {
|
|
|
|
|
c.counter.Add("records", 1)
|
|
|
|
|
err := c.checkpoint.Set(c.streamName, shardID, *r.SequenceNumber)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("set checkpoint error: %v", err)
|
|
|
|
|
}
|
2017-11-27 00:00:11 +00:00
|
|
|
}
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|
2017-10-16 00:40:30 +00:00
|
|
|
|
2017-11-27 00:00:11 +00:00
|
|
|
c.logger.Println("exiting", shardID)
|
|
|
|
|
return <-errc
|
2016-02-03 05:04:22 +00:00
|
|
|
}
|