vmware-go-kcl-v2/clientlibrary/checkpoint/dynamodb-checkpointer.go

580 lines
20 KiB
Go
Raw Normal View History

/*
* 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 14:00:48 +00:00
// Package checkpoint
// The implementation is derived from https://github.com/patrobinson/gokini
//
// Copyright 2018 Patrick robinson.
//
// 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 checkpoint
import (
2021-11-08 15:27:29 +00:00
"context"
"errors"
"fmt"
"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/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/vmware/vmware-go-kcl-v2/clientlibrary/config"
par "github.com/vmware/vmware-go-kcl-v2/clientlibrary/partition"
"github.com/vmware/vmware-go-kcl-v2/logger"
)
const (
// NumMaxRetries is the max times of doing retry
NumMaxRetries = 10
)
var (
NoLeaseOwnerErr = errors.New("no LeaseOwner in checkpoints table")
)
// DynamoCheckpoint implements the Checkpoint interface using DynamoDB as a backend
type DynamoCheckpoint struct {
log logger.Logger
TableName string
leaseTableReadCapacity int64
leaseTableWriteCapacity int64
LeaseDuration int
svc DynamoDBAPI
2021-11-11 22:11:30 +00:00
kclConfig *config.KinesisClientLibConfiguration
Retries int
lastLeaseSync time.Time
}
func NewDynamoCheckpoint(kclConfig *config.KinesisClientLibConfiguration) *DynamoCheckpoint {
checkpointer := &DynamoCheckpoint{
log: kclConfig.Logger,
TableName: kclConfig.TableName,
leaseTableReadCapacity: int64(kclConfig.InitialLeaseTableReadCapacity),
leaseTableWriteCapacity: int64(kclConfig.InitialLeaseTableWriteCapacity),
LeaseDuration: kclConfig.FailoverTimeMillis,
kclConfig: kclConfig,
Retries: NumMaxRetries,
}
return checkpointer
}
// WithDynamoDB is used to provide DynamoDB service
func (checkpointer *DynamoCheckpoint) WithDynamoDB(svc DynamoDBAPI) *DynamoCheckpoint {
checkpointer.svc = svc
return checkpointer
}
// Init initialises the DynamoDB Checkpoint
func (checkpointer *DynamoCheckpoint) Init() error {
checkpointer.log.Infof("Creating DynamoDB session")
2021-11-08 15:27:29 +00:00
if checkpointer.svc == nil {
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == dynamodb.ServiceID && len(checkpointer.kclConfig.DynamoDBEndpoint) > 0 {
return aws.Endpoint{
PartitionID: "aws",
URL: checkpointer.kclConfig.DynamoDBEndpoint,
SigningRegion: checkpointer.kclConfig.RegionName,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
2021-11-08 15:27:29 +00:00
})
2021-11-08 15:27:29 +00:00
cfg, err := awsConfig.LoadDefaultConfig(
context.TODO(),
awsConfig.WithRegion(checkpointer.kclConfig.RegionName),
awsConfig.WithCredentialsProvider(checkpointer.kclConfig.DynamoDBCredentials),
awsConfig.WithEndpointResolverWithOptions(resolver),
2021-11-08 15:27:29 +00:00
awsConfig.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxBackoffDelay(retry.NewStandard(), retry.DefaultMaxBackoff)
}),
)
2021-11-08 15:27:29 +00:00
if err != nil {
checkpointer.log.Fatalf("unable to load SDK config, %v", err)
}
checkpointer.svc = dynamodb.NewFromConfig(cfg)
}
2019-10-04 01:45:34 +00:00
if !checkpointer.doesTableExist() {
return checkpointer.createTable()
}
2021-11-08 15:27:29 +00:00
return nil
}
// GetLease attempts to gain a lock on the given shard
func (checkpointer *DynamoCheckpoint) GetLease(shard *par.ShardStatus, newAssignTo string) error {
newLeaseTimeout := time.Now().Add(time.Duration(checkpointer.LeaseDuration) * time.Millisecond).UTC()
newLeaseTimeoutString := newLeaseTimeout.Format(time.RFC3339Nano)
currentCheckpoint, err := checkpointer.getItem(shard.ID)
if err != nil {
return err
}
isClaimRequestExpired := shard.IsClaimRequestExpired(checkpointer.kclConfig)
var claimRequest string
if checkpointer.kclConfig.EnableLeaseStealing {
2021-11-08 15:27:29 +00:00
if currentCheckpointClaimRequest, ok := currentCheckpoint[ClaimRequestKey]; ok &&
currentCheckpointClaimRequest.(*types.AttributeValueMemberS).Value != "" {
claimRequest = currentCheckpointClaimRequest.(*types.AttributeValueMemberS).Value
if newAssignTo != claimRequest && !isClaimRequestExpired {
checkpointer.log.Debugf("another worker: %s has a claim on this shard. Not going to renew the lease", claimRequest)
return errors.New(ErrShardClaimed)
}
}
}
assignedVar, assignedToOk := currentCheckpoint[LeaseOwnerKey]
leaseVar, leaseTimeoutOk := currentCheckpoint[LeaseTimeoutKey]
var conditionalExpression string
2021-11-08 15:27:29 +00:00
var expressionAttributeValues map[string]types.AttributeValue
if !leaseTimeoutOk || !assignedToOk {
conditionalExpression = "attribute_not_exists(AssignedTo)"
} else {
2021-11-08 15:27:29 +00:00
assignedTo := assignedVar.(*types.AttributeValueMemberS).Value
leaseTimeout := leaseVar.(*types.AttributeValueMemberS).Value
currentLeaseTimeout, err := time.Parse(time.RFC3339Nano, leaseTimeout)
if err != nil {
return err
}
if checkpointer.kclConfig.EnableLeaseStealing {
if time.Now().UTC().Before(currentLeaseTimeout) && assignedTo != newAssignTo && !isClaimRequestExpired {
return ErrLeaseNotAcquired{"current lease timeout not yet expired"}
}
} else {
if time.Now().UTC().Before(currentLeaseTimeout) && assignedTo != newAssignTo {
return ErrLeaseNotAcquired{"current lease timeout not yet expired"}
}
}
checkpointer.log.Debugf("Attempting to get a lock for shard: %s, leaseTimeout: %s, assignedTo: %s, newAssignedTo: %s", shard.ID, currentLeaseTimeout, assignedTo, newAssignTo)
conditionalExpression = "ShardID = :id AND AssignedTo = :assigned_to AND LeaseTimeout = :lease_timeout"
2021-11-08 15:27:29 +00:00
expressionAttributeValues = map[string]types.AttributeValue{
":id": &types.AttributeValueMemberS{
Value: shard.ID,
},
2021-11-08 15:27:29 +00:00
":assigned_to": &types.AttributeValueMemberS{
Value: assignedTo,
},
2021-11-08 15:27:29 +00:00
":lease_timeout": &types.AttributeValueMemberS{
Value: leaseTimeout,
},
}
}
2021-11-08 15:27:29 +00:00
marshalledCheckpoint := map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shard.ID,
},
2021-11-08 15:27:29 +00:00
LeaseOwnerKey: &types.AttributeValueMemberS{
Value: newAssignTo,
},
2021-11-08 15:27:29 +00:00
LeaseTimeoutKey: &types.AttributeValueMemberS{
Value: newLeaseTimeoutString,
},
}
if len(shard.ParentShardId) > 0 {
2021-11-08 15:27:29 +00:00
marshalledCheckpoint[ParentShardIdKey] = &types.AttributeValueMemberS{
Value: shard.ParentShardId,
}
}
if checkpoint := shard.GetCheckpoint(); checkpoint != "" {
2021-11-08 15:27:29 +00:00
marshalledCheckpoint[SequenceNumberKey] = &types.AttributeValueMemberS{
Value: checkpoint,
}
}
if checkpointer.kclConfig.EnableLeaseStealing {
if claimRequest != "" && claimRequest == newAssignTo && !isClaimRequestExpired {
if expressionAttributeValues == nil {
2021-11-08 15:27:29 +00:00
expressionAttributeValues = make(map[string]types.AttributeValue)
}
conditionalExpression = conditionalExpression + " AND ClaimRequest = :claim_request"
2021-11-08 15:27:29 +00:00
expressionAttributeValues[":claim_request"] = &types.AttributeValueMemberS{
Value: claimRequest,
}
}
}
err = checkpointer.conditionalUpdate(conditionalExpression, expressionAttributeValues, marshalledCheckpoint)
if err != nil {
2021-11-08 15:27:29 +00:00
var conditionalCheckErr *types.ConditionalCheckFailedException
if errors.As(err, &conditionalCheckErr) {
return ErrLeaseNotAcquired{conditionalCheckErr.ErrorMessage()}
}
return err
}
shard.Mux.Lock()
shard.AssignedTo = newAssignTo
shard.LeaseTimeout = newLeaseTimeout
shard.Mux.Unlock()
return nil
}
// CheckpointSequence writes a checkpoint at the designated sequence ID
func (checkpointer *DynamoCheckpoint) CheckpointSequence(shard *par.ShardStatus) error {
leaseTimeout := shard.GetLeaseTimeout().UTC().Format(time.RFC3339Nano)
2021-11-08 15:27:29 +00:00
marshalledCheckpoint := map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shard.ID,
},
2021-11-08 15:27:29 +00:00
SequenceNumberKey: &types.AttributeValueMemberS{
Value: shard.GetCheckpoint(),
},
2021-11-08 15:27:29 +00:00
LeaseOwnerKey: &types.AttributeValueMemberS{
Value: shard.GetLeaseOwner(),
},
2021-11-08 15:27:29 +00:00
LeaseTimeoutKey: &types.AttributeValueMemberS{
Value: leaseTimeout,
},
}
if len(shard.ParentShardId) > 0 {
2021-11-08 15:27:29 +00:00
marshalledCheckpoint[ParentShardIdKey] = &types.AttributeValueMemberS{Value: shard.ParentShardId}
}
return checkpointer.saveItem(marshalledCheckpoint)
}
// FetchCheckpoint retrieves the checkpoint for the given shard
func (checkpointer *DynamoCheckpoint) FetchCheckpoint(shard *par.ShardStatus) error {
checkpoint, err := checkpointer.getItem(shard.ID)
if err != nil {
return err
}
sequenceID, ok := checkpoint[SequenceNumberKey]
if !ok {
return ErrSequenceIDNotFound
}
2021-11-08 15:27:29 +00:00
checkpointer.log.Debugf("Retrieved Shard Iterator %s", sequenceID.(*types.AttributeValueMemberS).Value)
shard.SetCheckpoint(sequenceID.(*types.AttributeValueMemberS).Value)
if assignedTo, ok := checkpoint[LeaseOwnerKey]; ok {
2021-11-08 15:27:29 +00:00
shard.SetLeaseOwner(assignedTo.(*types.AttributeValueMemberS).Value)
}
// Use up-to-date leaseTimeout to avoid ConditionalCheckFailedException when claiming
2021-11-08 15:27:29 +00:00
if leaseTimeout, ok := checkpoint[LeaseTimeoutKey]; ok && leaseTimeout.(*types.AttributeValueMemberS).Value != "" {
currentLeaseTimeout, err := time.Parse(time.RFC3339Nano, leaseTimeout.(*types.AttributeValueMemberS).Value)
if err != nil {
return err
}
shard.LeaseTimeout = currentLeaseTimeout
}
return nil
}
// RemoveLeaseInfo to remove lease info for shard entry in dynamoDB because the shard no longer exists in Kinesis
func (checkpointer *DynamoCheckpoint) RemoveLeaseInfo(shardID string) error {
err := checkpointer.removeItem(shardID)
if err != nil {
checkpointer.log.Errorf("Error in removing lease info for shard: %s, Error: %+v", shardID, err)
} else {
checkpointer.log.Infof("Lease info for shard: %s has been removed.", shardID)
}
return err
}
// RemoveLeaseOwner to remove lease owner for the shard entry
func (checkpointer *DynamoCheckpoint) RemoveLeaseOwner(shardID string) error {
input := &dynamodb.UpdateItemInput{
TableName: aws.String(checkpointer.TableName),
2021-11-08 15:27:29 +00:00
Key: map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shardID,
},
},
UpdateExpression: aws.String("remove " + LeaseOwnerKey),
2021-11-08 15:27:29 +00:00
ExpressionAttributeValues: map[string]types.AttributeValue{
":assigned_to": &types.AttributeValueMemberS{
Value: checkpointer.kclConfig.WorkerID,
},
},
ConditionExpression: aws.String("AssignedTo = :assigned_to"),
}
2021-11-08 15:27:29 +00:00
_, err := checkpointer.svc.UpdateItem(context.TODO(), input)
return err
}
// GetLeaseOwner returns current lease owner of given shard in checkpoints table
func (checkpointer *DynamoCheckpoint) GetLeaseOwner(shardID string) (string, error) {
currentCheckpoint, err := checkpointer.getItem(shardID)
if err != nil {
return "", err
}
assignedVar, assignedToOk := currentCheckpoint[LeaseOwnerKey]
if !assignedToOk {
return "", NoLeaseOwnerErr
}
return assignedVar.(*types.AttributeValueMemberS).Value, nil
}
// ListActiveWorkers returns a map of workers and their shards
func (checkpointer *DynamoCheckpoint) ListActiveWorkers(shardStatus map[string]*par.ShardStatus) (map[string][]*par.ShardStatus, error) {
err := checkpointer.syncLeases(shardStatus)
if err != nil {
return nil, err
}
workers := map[string][]*par.ShardStatus{}
for _, shard := range shardStatus {
if shard.GetCheckpoint() == ShardEnd {
continue
}
leaseOwner := shard.GetLeaseOwner()
if leaseOwner == "" {
checkpointer.log.Debugf("Shard Not Assigned Error. ShardID: %s, WorkerID: %s", shard.ID, checkpointer.kclConfig.WorkerID)
return nil, ErrShardNotAssigned
}
2021-11-08 15:27:29 +00:00
if w, ok := workers[leaseOwner]; ok {
workers[leaseOwner] = append(w, shard)
} else {
workers[leaseOwner] = []*par.ShardStatus{shard}
}
}
return workers, nil
}
// ClaimShard places a claim request on a shard to signal a steal attempt
func (checkpointer *DynamoCheckpoint) ClaimShard(shard *par.ShardStatus, claimID string) error {
err := checkpointer.FetchCheckpoint(shard)
if err != nil && err != ErrSequenceIDNotFound {
return err
}
leaseTimeoutString := shard.GetLeaseTimeout().Format(time.RFC3339Nano)
conditionalExpression := `ShardID = :id AND LeaseTimeout = :lease_timeout AND attribute_not_exists(ClaimRequest)`
2021-11-08 15:27:29 +00:00
expressionAttributeValues := map[string]types.AttributeValue{
":id": &types.AttributeValueMemberS{
Value: shard.ID,
},
2021-11-08 15:27:29 +00:00
":lease_timeout": &types.AttributeValueMemberS{
Value: leaseTimeoutString,
},
}
2021-11-08 15:27:29 +00:00
marshalledCheckpoint := map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shard.ID,
},
2021-11-08 15:27:29 +00:00
LeaseTimeoutKey: &types.AttributeValueMemberS{
Value: leaseTimeoutString,
},
2021-11-08 15:27:29 +00:00
SequenceNumberKey: &types.AttributeValueMemberS{
Value: shard.Checkpoint,
},
2021-11-08 15:27:29 +00:00
ClaimRequestKey: &types.AttributeValueMemberS{
Value: claimID,
},
}
if leaseOwner := shard.GetLeaseOwner(); leaseOwner == "" {
conditionalExpression += " AND attribute_not_exists(AssignedTo)"
} else {
2021-11-08 15:27:29 +00:00
marshalledCheckpoint[LeaseOwnerKey] = &types.AttributeValueMemberS{Value: leaseOwner}
conditionalExpression += "AND AssignedTo = :assigned_to"
2021-11-08 15:27:29 +00:00
expressionAttributeValues[":assigned_to"] = &types.AttributeValueMemberS{Value: leaseOwner}
}
if checkpoint := shard.GetCheckpoint(); checkpoint == "" {
conditionalExpression += " AND attribute_not_exists(Checkpoint)"
} else if checkpoint == ShardEnd {
conditionalExpression += " AND Checkpoint <> :checkpoint"
2021-11-08 15:27:29 +00:00
expressionAttributeValues[":checkpoint"] = &types.AttributeValueMemberS{Value: ShardEnd}
} else {
conditionalExpression += " AND Checkpoint = :checkpoint"
2021-11-08 15:27:29 +00:00
expressionAttributeValues[":checkpoint"] = &types.AttributeValueMemberS{Value: checkpoint}
}
if shard.ParentShardId == "" {
conditionalExpression += " AND attribute_not_exists(ParentShardId)"
} else {
2021-11-08 15:27:29 +00:00
marshalledCheckpoint[ParentShardIdKey] = &types.AttributeValueMemberS{Value: shard.ParentShardId}
conditionalExpression += " AND ParentShardId = :parent_shard"
2021-11-08 15:27:29 +00:00
expressionAttributeValues[":parent_shard"] = &types.AttributeValueMemberS{Value: shard.ParentShardId}
}
return checkpointer.conditionalUpdate(conditionalExpression, expressionAttributeValues, marshalledCheckpoint)
}
func (checkpointer *DynamoCheckpoint) syncLeases(shardStatus map[string]*par.ShardStatus) error {
log := checkpointer.kclConfig.Logger
if (checkpointer.lastLeaseSync.Add(time.Duration(checkpointer.kclConfig.LeaseSyncingTimeIntervalMillis) * time.Millisecond)).After(time.Now()) {
return nil
}
checkpointer.lastLeaseSync = time.Now()
input := &dynamodb.ScanInput{
ProjectionExpression: aws.String(fmt.Sprintf("%s,%s,%s", LeaseKeyKey, LeaseOwnerKey, SequenceNumberKey)),
2021-11-08 15:27:29 +00:00
Select: "SPECIFIC_ATTRIBUTES",
TableName: aws.String(checkpointer.kclConfig.TableName),
}
2021-11-08 15:27:29 +00:00
scanOutput, err := checkpointer.svc.Scan(context.TODO(), input)
if err != nil {
log.Debugf("Error performing DynamoDB Scan. Error: %+v ", err)
return err
}
2021-11-08 15:27:29 +00:00
results := scanOutput.Items
for _, result := range results {
shardId, foundShardId := result[LeaseKeyKey]
assignedTo, foundAssignedTo := result[LeaseOwnerKey]
checkpoint, foundCheckpoint := result[SequenceNumberKey]
if !foundShardId || !foundAssignedTo || !foundCheckpoint {
continue
}
if shard, ok := shardStatus[shardId.(*types.AttributeValueMemberS).Value]; ok {
shard.SetLeaseOwner(assignedTo.(*types.AttributeValueMemberS).Value)
shard.SetCheckpoint(checkpoint.(*types.AttributeValueMemberS).Value)
}
}
if err != nil {
log.Debugf("Error performing SyncLeases. Error: %+v ", err)
return err
}
log.Debugf("Lease sync completed. Next lease sync will occur in %s", time.Duration(checkpointer.kclConfig.LeaseSyncingTimeIntervalMillis)*time.Millisecond)
return nil
}
func (checkpointer *DynamoCheckpoint) createTable() error {
input := &dynamodb.CreateTableInput{
2021-11-08 15:27:29 +00:00
AttributeDefinitions: []types.AttributeDefinition{
{
AttributeName: aws.String(LeaseKeyKey),
2021-11-08 15:27:29 +00:00
AttributeType: types.ScalarAttributeTypeS,
},
},
2021-11-08 15:27:29 +00:00
KeySchema: []types.KeySchemaElement{
{
AttributeName: aws.String(LeaseKeyKey),
2021-11-08 15:27:29 +00:00
KeyType: types.KeyTypeHash,
},
},
2021-11-08 15:27:29 +00:00
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(checkpointer.leaseTableReadCapacity),
WriteCapacityUnits: aws.Int64(checkpointer.leaseTableWriteCapacity),
},
TableName: aws.String(checkpointer.TableName),
}
2021-11-08 15:27:29 +00:00
_, err := checkpointer.svc.CreateTable(context.Background(), input)
return err
}
func (checkpointer *DynamoCheckpoint) doesTableExist() bool {
input := &dynamodb.DescribeTableInput{
TableName: aws.String(checkpointer.TableName),
}
2021-11-08 15:27:29 +00:00
_, err := checkpointer.svc.DescribeTable(context.Background(), input)
return err == nil
}
2021-11-08 15:27:29 +00:00
func (checkpointer *DynamoCheckpoint) saveItem(item map[string]types.AttributeValue) error {
return checkpointer.putItem(&dynamodb.PutItemInput{
TableName: aws.String(checkpointer.TableName),
Item: item,
})
}
2021-11-08 15:27:29 +00:00
func (checkpointer *DynamoCheckpoint) conditionalUpdate(conditionExpression string, expressionAttributeValues map[string]types.AttributeValue, item map[string]types.AttributeValue) error {
return checkpointer.putItem(&dynamodb.PutItemInput{
ConditionExpression: aws.String(conditionExpression),
TableName: aws.String(checkpointer.TableName),
Item: item,
ExpressionAttributeValues: expressionAttributeValues,
})
}
func (checkpointer *DynamoCheckpoint) putItem(input *dynamodb.PutItemInput) error {
2021-11-08 15:27:29 +00:00
_, err := checkpointer.svc.PutItem(context.Background(), input)
return err
}
2021-11-08 15:27:29 +00:00
func (checkpointer *DynamoCheckpoint) getItem(shardID string) (map[string]types.AttributeValue, error) {
item, err := checkpointer.svc.GetItem(context.Background(), &dynamodb.GetItemInput{
TableName: aws.String(checkpointer.TableName),
ConsistentRead: aws.Bool(true),
Key: map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shardID,
},
},
})
2021-11-08 15:27:29 +00:00
// fix problem when starts the environment from scratch (dynamo table is empty)
if item == nil {
return nil, err
}
return item.Item, err
}
func (checkpointer *DynamoCheckpoint) removeItem(shardID string) error {
2021-11-08 15:27:29 +00:00
_, err := checkpointer.svc.DeleteItem(context.Background(), &dynamodb.DeleteItemInput{
TableName: aws.String(checkpointer.TableName),
2021-11-08 15:27:29 +00:00
Key: map[string]types.AttributeValue{
LeaseKeyKey: &types.AttributeValueMemberS{
Value: shardID,
},
},
})
2021-11-08 15:27:29 +00:00
return err
}