2021-04-27 15:51:26 +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-11-08 15:27:29 +00:00
// Package worker
2021-04-27 15:51:26 +00:00
// The implementation is derived from https://github.com/patrobinson/gokini
//
2022-12-16 23:07:22 +00:00
// # Copyright 2018 Patrick robinson
2021-04-27 15:51:26 +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.
package worker
import (
2021-11-08 15:27:29 +00:00
"context"
2021-04-27 15:51:26 +00:00
"errors"
"math"
"time"
2021-11-08 15:27:29 +00:00
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/aws/aws-sdk-go-v2/service/kinesis/types"
2021-04-27 15:51:26 +00:00
2021-12-21 19:49:47 +00:00
chk "github.com/vmware/vmware-go-kcl-v2/clientlibrary/checkpoint"
kcl "github.com/vmware/vmware-go-kcl-v2/clientlibrary/interfaces"
"github.com/vmware/vmware-go-kcl-v2/clientlibrary/metrics"
2021-04-27 15:51:26 +00:00
)
// PollingShardConsumer is responsible for polling data records from a (specified) shard.
// Note: PollingShardConsumer only deal with one shard.
type PollingShardConsumer struct {
commonShardConsumer
streamName string
stop * chan struct { }
consumerID string
mService metrics . MonitoringService
}
func ( sc * PollingShardConsumer ) getShardIterator ( ) ( * string , error ) {
startPosition , err := sc . getStartingPosition ( )
if err != nil {
return nil , err
}
2021-11-08 15:27:29 +00:00
2021-04-27 15:51:26 +00:00
shardIterArgs := & kinesis . GetShardIteratorInput {
ShardId : & sc . shard . ID ,
ShardIteratorType : startPosition . Type ,
StartingSequenceNumber : startPosition . SequenceNumber ,
Timestamp : startPosition . Timestamp ,
StreamName : & sc . streamName ,
}
2021-11-08 15:27:29 +00:00
iterResp , err := sc . kc . GetShardIterator ( context . TODO ( ) , shardIterArgs )
2021-04-27 15:51:26 +00:00
if err != nil {
return nil , err
}
2021-11-08 15:27:29 +00:00
2021-04-27 15:51:26 +00:00
return iterResp . ShardIterator , nil
}
2021-11-08 15:27:29 +00:00
// getRecords continuously poll one shard for data record
2021-04-27 15:51:26 +00:00
// Precondition: it currently has the lease on the shard.
func ( sc * PollingShardConsumer ) getRecords ( ) error {
defer sc . releaseLease ( )
log := sc . kclConfig . Logger
// If the shard is child shard, need to wait until the parent finished.
if err := sc . waitOnParentShard ( ) ; err != nil {
// If parent shard has been deleted by Kinesis system already, just ignore the error.
if err != chk . ErrSequenceIDNotFound {
log . Errorf ( "Error in waiting for parent shard: %v to finish. Error: %+v" , sc . shard . ParentShardId , err )
return err
}
}
shardIterator , err := sc . getShardIterator ( )
if err != nil {
log . Errorf ( "Unable to get shard iterator for %s: %v" , sc . shard . ID , err )
return err
}
// Start processing events and notify record processor on shard and starting checkpoint
input := & kcl . InitializationInput {
ShardId : sc . shard . ID ,
ExtendedSequenceNumber : & kcl . ExtendedSequenceNumber { SequenceNumber : aws . String ( sc . shard . GetCheckpoint ( ) ) } ,
}
sc . recordProcessor . Initialize ( input )
recordCheckpointer := NewRecordProcessorCheckpoint ( sc . shard , sc . checkpointer )
retriedErrors := 0
for {
2021-06-01 23:18:26 +00:00
if time . Now ( ) . UTC ( ) . After ( sc . shard . GetLeaseTimeout ( ) . Add ( - time . Duration ( sc . kclConfig . LeaseRefreshPeriodMillis ) * time . Millisecond ) ) {
2021-04-27 15:51:26 +00:00
log . Debugf ( "Refreshing lease on shard: %s for worker: %s" , sc . shard . ID , sc . consumerID )
err = sc . checkpointer . GetLease ( sc . shard , sc . consumerID )
if err != nil {
if errors . As ( err , & chk . ErrLeaseNotAcquired { } ) {
log . Warnf ( "Failed in acquiring lease on shard: %s for worker: %s" , sc . shard . ID , sc . consumerID )
return nil
}
// log and return error
log . Errorf ( "Error in refreshing lease on shard: %s for worker: %s. Error: %+v" ,
sc . shard . ID , sc . consumerID , err )
return err
}
}
getRecordsStartTime := time . Now ( )
2021-11-08 15:27:29 +00:00
log . Debugf ( "Trying to read %d record from iterator: %v" , sc . kclConfig . MaxRecords , aws . ToString ( shardIterator ) )
2021-04-27 15:51:26 +00:00
getRecordsArgs := & kinesis . GetRecordsInput {
2021-11-08 15:27:29 +00:00
Limit : aws . Int32 ( int32 ( sc . kclConfig . MaxRecords ) ) ,
2021-04-27 15:51:26 +00:00
ShardIterator : shardIterator ,
}
2021-11-08 15:27:29 +00:00
2021-04-27 15:51:26 +00:00
// Get records from stream and retry as needed
2021-11-08 15:27:29 +00:00
getResp , err := sc . kc . GetRecords ( context . TODO ( ) , getRecordsArgs )
2021-04-27 15:51:26 +00:00
if err != nil {
2021-11-08 15:27:29 +00:00
//aws-sdk-go-v2 https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md#error-handling
var throughputExceededErr * types . ProvisionedThroughputExceededException
var kmsThrottlingErr * types . KMSThrottlingException
if errors . As ( err , & throughputExceededErr ) || errors . As ( err , & kmsThrottlingErr ) {
2021-04-27 15:51:26 +00:00
log . Errorf ( "Error getting records from shard %v: %+v" , sc . shard . ID , err )
retriedErrors ++
// exponential backoff
// https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.Errors.html#Programming.Errors.RetryAndBackoff
time . Sleep ( time . Duration ( math . Exp2 ( float64 ( retriedErrors ) ) * 100 ) * time . Millisecond )
continue
}
log . Errorf ( "Error getting records from Kinesis that cannot be retried: %+v Request: %s" , err , getRecordsArgs )
return err
}
// reset the retry count after success
retriedErrors = 0
sc . processRecords ( getRecordsStartTime , getResp . Records , getResp . MillisBehindLatest , recordCheckpointer )
// The shard has been closed, so no new records can be read from it
if getResp . NextShardIterator == nil {
log . Infof ( "Shard %s closed" , sc . shard . ID )
shutdownInput := & kcl . ShutdownInput { ShutdownReason : kcl . TERMINATE , Checkpointer : recordCheckpointer }
sc . recordProcessor . Shutdown ( shutdownInput )
return nil
}
shardIterator = getResp . NextShardIterator
// Idle between each read, the user is responsible for checkpoint the progress
// This value is only used when no records are returned; if records are returned, it should immediately
// retrieve the next set of records.
2021-11-08 15:27:29 +00:00
if len ( getResp . Records ) == 0 && aws . ToInt64 ( getResp . MillisBehindLatest ) < int64 ( sc . kclConfig . IdleTimeBetweenReadsInMillis ) {
2021-04-27 15:51:26 +00:00
time . Sleep ( time . Duration ( sc . kclConfig . IdleTimeBetweenReadsInMillis ) * time . Millisecond )
}
select {
case <- * sc . stop :
shutdownInput := & kcl . ShutdownInput { ShutdownReason : kcl . REQUESTED , Checkpointer : recordCheckpointer }
sc . recordProcessor . Shutdown ( shutdownInput )
return nil
default :
}
}
}