forked from clearbit/go-ddb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkpoint.go
89 lines (81 loc) · 2.25 KB
/
checkpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ddb
import (
"fmt"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
// Checkpoint wraps the interactions with dynamo for setting/getting checkpoints
type Checkpoint struct {
Svc *dynamodb.DynamoDB
Namespace string
TableName string
}
// row reprsents a record in dynamodb table
type row struct {
Namespace string `json:"namespace"`
Segment int `json:"segment"`
LastEvaluatedKey LastEvaluatedKey `json:"last_evaluated_key"`
}
// LastEvaluatedKey is the attribute value of the last evaluated key in a scan
type LastEvaluatedKey map[string]*dynamodb.AttributeValue
// Get returns the exclusive start key for current segment
func (c *Checkpoint) Get(segment int) LastEvaluatedKey {
resp, err := c.Svc.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(c.TableName),
ConsistentRead: aws.Bool(true),
Key: map[string]*dynamodb.AttributeValue{
"namespace": &dynamodb.AttributeValue{
S: aws.String(c.Namespace),
},
"segment": &dynamodb.AttributeValue{
N: aws.String(strconv.Itoa(segment)),
},
},
})
if err != nil {
if retriableError(err) {
c.Get(segment)
} else {
fmt.Printf("Checkpoint > Get > GetItem: %v", err)
return nil
}
}
item := row{}
dynamodbattribute.UnmarshalMap(resp.Item, &item)
return item.LastEvaluatedKey
}
// Set the lastEvaluatedKey as most recent checkpoint
func (c *Checkpoint) Set(segment int, lastEvaluatedKey LastEvaluatedKey) {
item, err := dynamodbattribute.MarshalMap(row{
Namespace: c.Namespace,
Segment: segment,
LastEvaluatedKey: lastEvaluatedKey,
})
if err != nil {
fmt.Printf("Checkpoint > Set > MarshalMap: %v", err)
return
}
_, err = c.Svc.PutItem(&dynamodb.PutItemInput{
TableName: aws.String(c.TableName),
Item: item,
})
if err != nil {
if retriableError(err) {
c.Set(segment, lastEvaluatedKey)
} else {
fmt.Printf("Checkpoint > Set > PutItem: %v", err)
}
}
return
}
func retriableError(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ProvisionedThroughputExceededException" {
return true
}
}
return false
}