2018-08-17 13:03:25 +00:00
/ *
* Copyright ( c ) 2018 VMware , Inc .
*
* Permission is hereby granted , free of charge , to any person obtaining a copy of this software and
* associated documentation files ( the "Software" ) , to deal in the Software without restriction , including
* without limitation the rights to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is furnished to do
* so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software .
*
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR IMPLIED , INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY , FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT .
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY ,
* WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM , OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE .
* /
2021-10-22 01:28:41 +00:00
// Package worker
2018-08-17 13:03:25 +00:00
// The implementation is derived from https://github.com/patrobinson/gokini
//
2022-12-16 23:07:22 +00:00
// # Copyright 2018 Patrick robinson
2018-08-17 13:03:25 +00:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2018-04-17 16:25:41 +00:00
package worker
import (
2021-11-08 15:27:29 +00:00
"context"
2021-10-22 01:28:41 +00:00
"crypto/rand"
2021-01-26 04:30:10 +00:00
"errors"
2021-10-22 01:28:41 +00:00
"math/big"
2018-04-17 16:25:41 +00:00
"sync"
"time"
2021-11-08 15:27:29 +00:00
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
2018-04-17 16:25:41 +00:00
2021-12-21 19:49:47 +00:00
chk "github.com/vmware/vmware-go-kcl-v2/clientlibrary/checkpoint"
"github.com/vmware/vmware-go-kcl-v2/clientlibrary/config"
kcl "github.com/vmware/vmware-go-kcl-v2/clientlibrary/interfaces"
"github.com/vmware/vmware-go-kcl-v2/clientlibrary/metrics"
par "github.com/vmware/vmware-go-kcl-v2/clientlibrary/partition"
2018-04-17 16:25:41 +00:00
)
2022-12-16 23:07:22 +00:00
// Worker is the high level class that Kinesis applications use to start processing data. It initializes and oversees
// different components (e.g. syncing shard and lease information, tracking shard assignments, and processing data from
// the shards).
2018-04-17 16:25:41 +00:00
type Worker struct {
2021-04-27 15:51:26 +00:00
streamName string
regionName string
workerID string
consumerARN string
2018-04-17 16:25:41 +00:00
processorFactory kcl . IRecordProcessorFactory
kclConfig * config . KinesisClientLibConfiguration
2021-11-08 15:27:29 +00:00
kc * kinesis . Client
2019-04-22 20:55:39 +00:00
checkpointer chk . Checkpointer
2019-11-06 13:53:21 +00:00
mService metrics . MonitoringService
2018-04-17 16:25:41 +00:00
stop * chan struct { }
waitGroup * sync . WaitGroup
2019-05-20 13:57:32 +00:00
done bool
2018-04-17 16:25:41 +00:00
2021-10-22 01:28:41 +00:00
randomSeed int64
2019-11-29 18:59:35 +00:00
2021-06-01 23:18:26 +00:00
shardStatus map [ string ] * par . ShardStatus
shardStealInProgress bool
2018-04-17 16:25:41 +00:00
}
// NewWorker constructs a Worker instance for processing Kinesis stream data.
2019-11-06 13:53:21 +00:00
func NewWorker ( factory kcl . IRecordProcessorFactory , kclConfig * config . KinesisClientLibConfiguration ) * Worker {
2019-11-06 14:35:08 +00:00
mService := kclConfig . MonitoringService
if mService == nil {
2019-11-06 13:53:21 +00:00
// Replaces nil with noop monitor service (not emitting any metrics).
mService = metrics . NoopMonitoringService { }
}
return & Worker {
2018-04-17 16:25:41 +00:00
streamName : kclConfig . StreamName ,
regionName : kclConfig . RegionName ,
workerID : kclConfig . WorkerID ,
processorFactory : factory ,
kclConfig : kclConfig ,
2019-11-06 13:53:21 +00:00
mService : mService ,
2019-05-20 13:57:32 +00:00
done : false ,
2021-10-22 01:28:41 +00:00
randomSeed : time . Now ( ) . UTC ( ) . UnixNano ( ) ,
2018-04-17 16:25:41 +00:00
}
}
2019-07-08 22:20:33 +00:00
// WithKinesis is used to provide Kinesis service for either custom implementation or unit testing.
2021-11-08 15:27:29 +00:00
func ( w * Worker ) WithKinesis ( svc * kinesis . Client ) * Worker {
2019-07-08 22:20:33 +00:00
w . kc = svc
return w
}
// WithCheckpointer is used to provide a custom checkpointer service for non-dynamodb implementation
// or unit testing.
func ( w * Worker ) WithCheckpointer ( checker chk . Checkpointer ) * Worker {
w . checkpointer = checker
return w
}
2021-10-22 01:28:41 +00:00
// Start Run starts consuming data from the stream, and pass it to the application record processors.
2018-04-17 16:25:41 +00:00
func ( w * Worker ) Start ( ) error {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
2018-04-17 16:25:41 +00:00
if err := w . initialize ( ) ; err != nil {
2019-07-08 22:20:33 +00:00
log . Errorf ( "Failed to initialize Worker: %+v" , err )
2018-04-17 16:25:41 +00:00
return err
}
2018-04-21 04:07:11 +00:00
// Start monitoring service
2019-10-28 12:08:18 +00:00
log . Infof ( "Starting monitoring service." )
2018-08-27 19:23:20 +00:00
if err := w . mService . Start ( ) ; err != nil {
log . Errorf ( "Failed to start monitoring service: %+v" , err )
return err
}
2018-04-17 16:25:41 +00:00
2019-10-28 12:08:18 +00:00
log . Infof ( "Starting worker event loop." )
2019-11-15 03:53:34 +00:00
w . waitGroup . Add ( 1 )
go func ( ) {
defer w . waitGroup . Done ( )
// entering event loop
w . eventLoop ( )
} ( )
2018-04-17 16:25:41 +00:00
return nil
}
2021-10-22 01:28:41 +00:00
// Shutdown signals worker to shut down. Worker will try initiating shutdown of all record processors.
2018-04-17 16:25:41 +00:00
func ( w * Worker ) Shutdown ( ) {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
log . Infof ( "Worker shutdown in requested." )
2018-04-17 16:25:41 +00:00
2020-01-31 02:06:03 +00:00
if w . done || w . stop == nil {
2019-05-20 13:57:32 +00:00
return
}
2018-04-17 16:25:41 +00:00
close ( * w . stop )
2019-05-20 13:57:32 +00:00
w . done = true
2018-04-17 16:25:41 +00:00
w . waitGroup . Wait ( )
2018-04-21 04:07:11 +00:00
w . mService . Shutdown ( )
2019-10-28 12:08:18 +00:00
log . Infof ( "Worker loop is complete. Exiting from worker." )
2018-04-17 16:25:41 +00:00
}
// initialize
func ( w * Worker ) initialize ( ) error {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
log . Infof ( "Worker initialization in progress..." )
2018-04-17 16:25:41 +00:00
2021-11-08 15:27:29 +00:00
// Create default Kinesis client
2019-07-08 22:20:33 +00:00
if w . kc == nil {
// create session for Kinesis
2021-11-08 15:27:29 +00:00
log . Infof ( "Creating Kinesis client" )
2022-01-07 02:13:32 +00:00
resolver := aws . EndpointResolverWithOptionsFunc ( func ( service , region string , options ... interface { } ) ( aws . Endpoint , error ) {
2021-11-08 15:27:29 +00:00
return aws . Endpoint {
PartitionID : "aws" ,
URL : w . kclConfig . KinesisEndpoint ,
SigningRegion : w . regionName ,
} , nil
2019-07-08 22:20:33 +00:00
} )
2021-11-08 15:27:29 +00:00
cfg , err := awsConfig . LoadDefaultConfig (
context . TODO ( ) ,
awsConfig . WithRegion ( w . regionName ) ,
2022-01-07 02:13:32 +00:00
awsConfig . WithCredentialsProvider ( w . kclConfig . KinesisCredentials ) ,
awsConfig . WithEndpointResolverWithOptions ( resolver ) ,
2021-11-08 15:27:29 +00:00
awsConfig . WithRetryer ( func ( ) aws . Retryer {
return retry . AddWithMaxBackoffDelay ( retry . NewStandard ( ) , retry . DefaultMaxBackoff )
} ) ,
)
2019-07-08 22:20:33 +00:00
if err != nil {
// no need to move forward
2021-11-08 15:27:29 +00:00
log . Fatalf ( "Failed in loading Kinesis default config for creating Worker: %+v" , err )
2019-07-08 22:20:33 +00:00
}
2021-11-08 15:27:29 +00:00
w . kc = kinesis . NewFromConfig ( cfg )
2019-07-08 22:20:33 +00:00
} else {
2019-10-28 12:08:18 +00:00
log . Infof ( "Use custom Kinesis service." )
2019-07-08 22:20:33 +00:00
}
// Create default dynamodb based checkpointer implementation
if w . checkpointer == nil {
2019-10-28 12:08:18 +00:00
log . Infof ( "Creating DynamoDB based checkpointer" )
2019-07-08 22:20:33 +00:00
w . checkpointer = chk . NewDynamoCheckpoint ( w . kclConfig )
} else {
2019-10-28 12:08:18 +00:00
log . Infof ( "Use custom checkpointer implementation." )
2019-07-08 22:20:33 +00:00
}
2021-04-27 15:51:26 +00:00
if w . kclConfig . EnableEnhancedFanOutConsumer {
log . Debugf ( "Enhanced fan-out is enabled" )
2021-04-29 02:19:12 +00:00
w . consumerARN = w . kclConfig . EnhancedFanOutConsumerARN
if w . consumerARN == "" {
2021-04-27 15:51:26 +00:00
var err error
w . consumerARN , err = w . fetchConsumerARNWithRetry ( )
if err != nil {
log . Errorf ( "Failed to fetch consumer ARN for: %s, %v" , w . kclConfig . EnhancedFanOutConsumerName , err )
return err
}
}
}
2019-11-06 13:53:21 +00:00
err := w . mService . Init ( w . kclConfig . ApplicationName , w . streamName , w . workerID )
2018-04-17 16:25:41 +00:00
if err != nil {
2018-08-07 03:49:15 +00:00
log . Errorf ( "Failed to start monitoring service: %+v" , err )
2018-04-17 16:25:41 +00:00
}
2019-10-28 12:08:18 +00:00
log . Infof ( "Initializing Checkpointer" )
2018-04-17 16:25:41 +00:00
if err := w . checkpointer . Init ( ) ; err != nil {
log . Errorf ( "Failed to start Checkpointer: %+v" , err )
return err
}
2019-04-22 20:55:39 +00:00
w . shardStatus = make ( map [ string ] * par . ShardStatus )
2018-04-17 16:25:41 +00:00
stopChan := make ( chan struct { } )
w . stop = & stopChan
2019-10-27 15:43:21 +00:00
w . waitGroup = & sync . WaitGroup { }
2018-04-17 16:25:41 +00:00
2019-10-28 12:08:18 +00:00
log . Infof ( "Initialization complete." )
2018-04-21 04:07:11 +00:00
2018-04-17 16:25:41 +00:00
return nil
}
2021-04-27 15:51:26 +00:00
// newShardConsumer creates shard consumer for the specified shard
func ( w * Worker ) newShardConsumer ( shard * par . ShardStatus ) shardConsumer {
common := commonShardConsumer {
2018-04-17 16:25:41 +00:00
shard : shard ,
kc : w . kc ,
checkpointer : w . checkpointer ,
recordProcessor : w . processorFactory . CreateProcessor ( ) ,
kclConfig : w . kclConfig ,
mService : w . mService ,
2021-04-27 15:51:26 +00:00
}
if w . kclConfig . EnableEnhancedFanOutConsumer {
w . kclConfig . Logger . Infof ( "Start enhanced fan-out shard consumer for shard: %v" , shard . ID )
return & FanOutShardConsumer {
commonShardConsumer : common ,
consumerARN : w . consumerARN ,
consumerID : w . workerID ,
stop : w . stop ,
}
}
w . kclConfig . Logger . Infof ( "Start polling shard consumer for shard: %v" , shard . ID )
return & PollingShardConsumer {
commonShardConsumer : common ,
streamName : w . streamName ,
consumerID : w . workerID ,
stop : w . stop ,
mService : w . mService ,
2018-04-17 16:25:41 +00:00
}
}
// eventLoop
func ( w * Worker ) eventLoop ( ) {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
2020-01-22 11:26:22 +00:00
var foundShards int
2018-04-17 16:25:41 +00:00
for {
2019-11-29 20:27:05 +00:00
// Add [-50%, +50%] random jitter to ShardSyncIntervalMillis. When multiple workers
// starts at the same time, this decreases the probability of them calling
// kinesis.DescribeStream at the same time, and hit the hard-limit on aws API calls.
// On average the period remains the same so that doesn't affect behavior.
2021-10-22 01:28:41 +00:00
rnd , _ := rand . Int ( rand . Reader , big . NewInt ( int64 ( w . kclConfig . ShardSyncIntervalMillis ) ) )
shardSyncSleep := w . kclConfig . ShardSyncIntervalMillis / 2 + int ( rnd . Int64 ( ) )
2019-11-29 20:27:05 +00:00
2018-04-22 02:58:51 +00:00
err := w . syncShard ( )
2018-04-17 16:25:41 +00:00
if err != nil {
2019-11-29 20:27:05 +00:00
log . Errorf ( "Error syncing shards: %+v, Retrying in %d ms..." , err , shardSyncSleep )
2019-11-29 18:59:35 +00:00
time . Sleep ( time . Duration ( shardSyncSleep ) * time . Millisecond )
2018-08-17 13:03:25 +00:00
continue
2018-04-17 16:25:41 +00:00
}
2018-04-22 02:58:51 +00:00
2020-01-22 11:26:22 +00:00
if foundShards == 0 || foundShards != len ( w . shardStatus ) {
foundShards = len ( w . shardStatus )
log . Infof ( "Found %d shards" , foundShards )
}
2018-04-17 16:25:41 +00:00
2021-06-01 23:18:26 +00:00
// Count the number of leases held by this worker excluding the processed shard
2018-04-17 16:25:41 +00:00
counter := 0
for _ , shard := range w . shardStatus {
2021-04-27 15:51:26 +00:00
if shard . GetLeaseOwner ( ) == w . workerID && shard . GetCheckpoint ( ) != chk . ShardEnd {
2018-04-17 16:25:41 +00:00
counter ++
}
}
2018-08-07 03:49:15 +00:00
// max number of lease has not been reached yet
2018-04-17 16:25:41 +00:00
if counter < w . kclConfig . MaxLeasesForWorker {
for _ , shard := range w . shardStatus {
2018-08-07 03:49:15 +00:00
// already owner of the shard
2019-04-22 20:55:39 +00:00
if shard . GetLeaseOwner ( ) == w . workerID {
2018-04-17 16:25:41 +00:00
continue
}
err := w . checkpointer . FetchCheckpoint ( shard )
if err != nil {
2021-10-22 01:28:41 +00:00
// checkpoint may not exist yet is not an error condition.
2019-04-22 20:55:39 +00:00
if err != chk . ErrSequenceIDNotFound {
2021-07-23 12:03:31 +00:00
log . Warnf ( "Couldn't fetch checkpoint: %+v" , err )
2018-04-19 03:09:52 +00:00
// move on to next shard
continue
2018-04-17 16:25:41 +00:00
}
}
2018-04-19 03:09:52 +00:00
// The shard is closed and we have processed all records
2021-04-27 15:51:26 +00:00
if shard . GetCheckpoint ( ) == chk . ShardEnd {
2018-04-19 03:09:52 +00:00
continue
}
2021-06-01 23:18:26 +00:00
var stealShard bool
if w . kclConfig . EnableLeaseStealing && shard . ClaimRequest != "" {
upcomingStealingInterval := time . Now ( ) . UTC ( ) . Add ( time . Duration ( w . kclConfig . LeaseStealingIntervalMillis ) * time . Millisecond )
if shard . GetLeaseTimeout ( ) . Before ( upcomingStealingInterval ) && ! shard . IsClaimRequestExpired ( w . kclConfig ) {
if shard . ClaimRequest == w . workerID {
stealShard = true
log . Debugf ( "Stealing shard: %s" , shard . ID )
} else {
log . Debugf ( "Shard being stolen: %s" , shard . ID )
continue
}
}
}
2018-04-17 16:25:41 +00:00
err = w . checkpointer . GetLease ( shard , w . workerID )
if err != nil {
2018-04-19 03:09:52 +00:00
// cannot get lease on the shard
2021-01-26 04:30:10 +00:00
if ! errors . As ( err , & chk . ErrLeaseNotAcquired { } ) {
2019-10-28 12:08:18 +00:00
log . Errorf ( "Cannot get lease: %+v" , err )
2018-04-17 16:25:41 +00:00
}
2018-04-19 03:09:52 +00:00
continue
2018-04-17 16:25:41 +00:00
}
2021-06-01 23:18:26 +00:00
if stealShard {
log . Debugf ( "Successfully stole shard: %+v" , shard . ID )
w . shardStealInProgress = false
}
2018-04-19 03:09:52 +00:00
// log metrics on got lease
2018-04-17 16:25:41 +00:00
w . mService . LeaseGained ( shard . ID )
w . waitGroup . Add ( 1 )
2021-04-27 15:51:26 +00:00
go func ( shard * par . ShardStatus ) {
2019-10-27 15:43:21 +00:00
defer w . waitGroup . Done ( )
2021-04-27 15:51:26 +00:00
if err := w . newShardConsumer ( shard ) . getRecords ( ) ; err != nil {
2019-10-28 12:08:18 +00:00
log . Errorf ( "Error in getRecords: %+v" , err )
}
2021-04-27 15:51:26 +00:00
} ( shard )
2018-08-07 03:49:15 +00:00
// exit from for loop and not to grab more shard for now.
break
2018-04-17 16:25:41 +00:00
}
}
2021-06-01 23:18:26 +00:00
if w . kclConfig . EnableLeaseStealing {
err = w . rebalance ( )
if err != nil {
log . Warnf ( "Error in rebalance: %+v" , err )
}
}
2018-04-17 16:25:41 +00:00
select {
case <- * w . stop :
2019-10-28 12:08:18 +00:00
log . Infof ( "Shutting down..." )
2018-04-17 16:25:41 +00:00
return
2019-12-09 15:21:20 +00:00
case <- time . After ( time . Duration ( shardSyncSleep ) * time . Millisecond ) :
log . Debugf ( "Waited %d ms to sync shards..." , shardSyncSleep )
2018-04-17 16:25:41 +00:00
}
}
}
2021-06-01 23:18:26 +00:00
func ( w * Worker ) rebalance ( ) error {
log := w . kclConfig . Logger
workers , err := w . checkpointer . ListActiveWorkers ( w . shardStatus )
if err != nil {
log . Debugf ( "Error listing workers. workerID: %s. Error: %+v " , w . workerID , err )
return err
}
2021-10-22 01:28:41 +00:00
// Only attempt to steal one shard at time, to allow for linear convergence
2021-06-01 23:18:26 +00:00
if w . shardStealInProgress {
shardInfo := make ( map [ string ] bool )
err := w . getShardIDs ( "" , shardInfo )
if err != nil {
return err
}
for _ , shard := range w . shardStatus {
if shard . ClaimRequest != "" && shard . ClaimRequest == w . workerID {
log . Debugf ( "Steal in progress. workerID: %s" , w . workerID )
return nil
}
// Our shard steal was stomped on by a Checkpoint.
// We could deal with that, but instead just try again
w . shardStealInProgress = false
}
}
var numShards int
for _ , shards := range workers {
numShards += len ( shards )
}
numWorkers := len ( workers )
// 1:1 shards to workers is optimal, so we cannot possibly rebalance
if numWorkers >= numShards {
log . Debugf ( "Optimal shard allocation, not stealing any shards. workerID: %s, %v > %v. " , w . workerID , numWorkers , numShards )
return nil
}
currentShards , ok := workers [ w . workerID ]
var numCurrentShards int
if ! ok {
numCurrentShards = 0
numWorkers ++
} else {
numCurrentShards = len ( currentShards )
}
optimalShards := numShards / numWorkers
// We have more than or equal optimal shards, so no rebalancing can take place
if numCurrentShards >= optimalShards || numCurrentShards == w . kclConfig . MaxLeasesForWorker {
log . Debugf ( "We have enough shards, not attempting to steal any. workerID: %s" , w . workerID )
return nil
}
2021-10-22 01:28:41 +00:00
2021-06-01 23:18:26 +00:00
var workerSteal string
for worker , shards := range workers {
2021-10-22 01:28:41 +00:00
if worker != w . workerID && len ( shards ) > optimalShards {
2021-06-01 23:18:26 +00:00
workerSteal = worker
2021-10-22 01:28:41 +00:00
optimalShards = len ( shards )
2021-06-01 23:18:26 +00:00
}
}
// Not all shards are allocated so fallback to default shard allocation mechanisms
if workerSteal == "" {
log . Infof ( "Not all shards are allocated, not stealing any. workerID: %s" , w . workerID )
return nil
}
// Steal a random shard from the worker with the most shards
w . shardStealInProgress = true
2021-10-22 01:28:41 +00:00
rnd , _ := rand . Int ( rand . Reader , big . NewInt ( int64 ( len ( workers [ workerSteal ] ) ) ) )
randIndex := int ( rnd . Int64 ( ) )
2021-06-01 23:18:26 +00:00
shardToSteal := workers [ workerSteal ] [ randIndex ]
log . Debugf ( "Stealing shard %s from %s" , shardToSteal , workerSteal )
err = w . checkpointer . ClaimShard ( w . shardStatus [ shardToSteal . ID ] , w . workerID )
if err != nil {
w . shardStealInProgress = false
return err
}
return nil
}
2020-03-10 16:00:57 +00:00
// List all shards and store them into shardStatus table
2018-04-22 02:58:51 +00:00
// If shard has been removed, need to exclude it from cached shard status.
2020-03-10 16:00:57 +00:00
func ( w * Worker ) getShardIDs ( nextToken string , shardInfo map [ string ] bool ) error {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
2018-08-07 03:49:15 +00:00
2020-03-10 16:00:57 +00:00
args := & kinesis . ListShardsInput { }
// When you have a nextToken, you can't set the streamName
if nextToken != "" {
args . NextToken = aws . String ( nextToken )
} else {
args . StreamName = aws . String ( w . streamName )
2018-04-17 16:25:41 +00:00
}
2018-08-07 03:49:15 +00:00
2021-11-08 15:27:29 +00:00
listShards , err := w . kc . ListShards ( context . TODO ( ) , args )
2018-04-17 16:25:41 +00:00
if err != nil {
2020-03-10 16:00:57 +00:00
log . Errorf ( "Error in ListShards: %s Error: %+v Request: %s" , w . streamName , err , args )
2018-04-17 16:25:41 +00:00
return err
}
2020-03-10 16:00:57 +00:00
for _ , s := range listShards . Shards {
2018-04-22 02:58:51 +00:00
// record avail shardId from fresh reading from Kinesis
shardInfo [ * s . ShardId ] = true
2018-08-27 19:23:20 +00:00
2018-04-18 22:50:15 +00:00
// found new shard
2018-04-17 16:25:41 +00:00
if _ , ok := w . shardStatus [ * s . ShardId ] ; ! ok {
2018-09-05 03:32:45 +00:00
log . Infof ( "Found new shard with id %s" , * s . ShardId )
2019-04-22 20:55:39 +00:00
w . shardStatus [ * s . ShardId ] = & par . ShardStatus {
2019-02-09 16:23:54 +00:00
ID : * s . ShardId ,
2021-11-08 15:27:29 +00:00
ParentShardId : aws . ToString ( s . ParentShardId ) ,
2021-04-27 15:51:26 +00:00
Mux : & sync . RWMutex { } ,
2021-11-08 15:27:29 +00:00
StartingSequenceNumber : aws . ToString ( s . SequenceNumberRange . StartingSequenceNumber ) ,
EndingSequenceNumber : aws . ToString ( s . SequenceNumberRange . EndingSequenceNumber ) ,
2018-04-17 16:25:41 +00:00
}
}
}
2020-03-10 16:00:57 +00:00
if listShards . NextToken != nil {
2021-11-08 15:27:29 +00:00
err := w . getShardIDs ( aws . ToString ( listShards . NextToken ) , shardInfo )
2018-04-17 16:25:41 +00:00
if err != nil {
2020-03-10 16:00:57 +00:00
log . Errorf ( "Error in ListShards: %s Error: %+v Request: %s" , w . streamName , err , args )
2018-04-17 16:25:41 +00:00
return err
}
}
return nil
}
2018-04-22 02:58:51 +00:00
// syncShard to sync the cached shard info with actual shard info from Kinesis
func ( w * Worker ) syncShard ( ) error {
2019-10-28 12:08:18 +00:00
log := w . kclConfig . Logger
2018-04-22 02:58:51 +00:00
shardInfo := make ( map [ string ] bool )
err := w . getShardIDs ( "" , shardInfo )
2018-05-22 02:14:18 +00:00
if err != nil {
return err
}
2018-04-22 02:58:51 +00:00
for _ , shard := range w . shardStatus {
// The cached shard no longer existed, remove it.
if _ , ok := shardInfo [ shard . ID ] ; ! ok {
2018-04-23 19:40:39 +00:00
// remove the shard from local status cache
2018-04-22 02:58:51 +00:00
delete ( w . shardStatus , shard . ID )
2018-04-23 19:40:39 +00:00
// remove the shard entry in dynamoDB as well
// Note: syncShard runs periodically. we don't need to do anything in case of error here.
2018-08-27 19:23:20 +00:00
if err := w . checkpointer . RemoveLeaseInfo ( shard . ID ) ; err != nil {
log . Errorf ( "Failed to remove shard lease info: %s Error: %+v" , shard . ID , err )
}
2018-04-22 02:58:51 +00:00
}
}
2018-05-22 02:14:18 +00:00
return nil
2018-04-22 02:58:51 +00:00
}