-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamodb_helper.go
257 lines (217 loc) · 6.42 KB
/
dynamodb_helper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"errors"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodbstreams"
"github.com/sirupsen/logrus"
)
const tableStatusWaitInterval = 1 * time.Second
func describeTable(tableName string, ddbClient *dynamodb.DynamoDB) (*dynamodb.DescribeTableOutput, error) {
var result *dynamodb.DescribeTableOutput
var err error
for i := 1; i <= maxRetries; i++ {
result, err = ddbClient.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(tableName),
})
if err != nil {
backoff(i, "Describe Table")
} else {
return result, err
}
}
return nil, err
}
func getStreamArn(tableName string, ddbClient *dynamodb.DynamoDB) (string, error) {
var result *dynamodb.DescribeTableOutput
var err error
result, err = describeTable(tableName, ddbClient)
if result != nil {
if result.Table.StreamSpecification == nil || *result.Table.StreamSpecification.StreamEnabled == false {
return "", errors.New("stream not enabled")
}
return *result.Table.LatestStreamArn, nil
}
return "", err
}
func (listener *streamListener) describeStream(lastShard string) (*dynamodbstreams.DescribeStreamOutput, error) {
var err error = nil
var streamDescription *dynamodbstreams.DescribeStreamOutput
input := &dynamodbstreams.DescribeStreamInput{
StreamArn: aws.String(listener.streamArn),
Limit: aws.Int64(100),
}
if lastShard != "" {
input.ExclusiveStartShardId = aws.String(lastShard)
}
for i := 1; i <= maxRetries; i++ {
streamDescription, err = listener.stream.DescribeStream(input)
if err != nil {
backoff(i, "DescribeStream")
} else {
return streamDescription, nil
}
}
return nil, err
}
func (listener *streamListener) getShardIterator(shardId string) (*string, error) {
shardIteratorInput := &dynamodbstreams.GetShardIteratorInput{
StreamArn: aws.String(listener.streamArn),
ShardIteratorType:aws.String("TRIM_HORIZON"),
ShardId: aws.String(shardId),
}
for i := 1; i <= maxRetries; i++ {
iterator, err := listener.stream.GetShardIterator(shardIteratorInput)
if err != nil {
backoff(i, "GetShardIterator")
} else {
return iterator.ShardIterator, nil
}
}
return nil, nil
}
func (listener *streamListener) getRecords(shardId *string, shardIterator string) (
*dynamodbstreams.GetRecordsOutput, error) {
var err error
var records *dynamodbstreams.GetRecordsOutput
for i := 1; i <= maxRetries; i++ {
records, err = listener.stream.GetRecords(
&dynamodbstreams.GetRecordsInput{
ShardIterator: aws.String(shardIterator),
})
if err != nil {
backoff(i, "GetRecords")
} else {
return records, nil
}
}
return nil, err
}
func getCapacity(table string, dynamodbClient *dynamodb.DynamoDB) (dynamodb.ProvisionedThroughput, error) {
var result = dynamodb.ProvisionedThroughput{}
var err error
var output *dynamodb.DescribeTableOutput
output, err = describeTable(table, dynamodbClient)
if err != nil {
logger.WithFields(logrus.Fields{
"Table": table,
"Error": err,
}).Error("Failed to get table capacity")
return result, err
}
result = dynamodb.ProvisionedThroughput{
ReadCapacityUnits: output.Table.ProvisionedThroughput.ReadCapacityUnits,
WriteCapacityUnits: output.Table.ProvisionedThroughput.WriteCapacityUnits,
}
return result, err
}
func updateCapacity(table string, dynamodbClient *dynamodb.DynamoDB, newThroughput *dynamodb.ProvisionedThroughput) (error) {
var err error
input := &dynamodb.UpdateTableInput {
TableName: aws.String(table),
ProvisionedThroughput: newThroughput,
}
for i := 1; i <= maxRetries; i++ {
_, err = dynamodbClient.UpdateTable(input)
if err != nil {
backoff(i, "Update Table")
} else {
return nil
}
}
logger.WithFields(logrus.Fields{
"Table": table,
"Error": err,
}).Error("Failed to update capacity")
return err
}
func getStreamStatus(table string, dynamodbClient *dynamodb.DynamoDB) (bool, error) {
var err error
var result *dynamodb.DescribeTableOutput
result, err = describeTable(table, dynamodbClient)
if err != nil {
return false, err
}
if result.Table.StreamSpecification != nil {
return *result.Table.StreamSpecification.StreamEnabled, nil
}
return false, err
}
func enableStream(table string, dynamodbClient *dynamodb.DynamoDB) error {
var err error
input := &dynamodb.UpdateTableInput{
TableName: aws.String(table),
StreamSpecification: &dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(true),
StreamViewType: aws.String(dynamodb.StreamViewTypeNewAndOldImages)},
}
for i := 1; i <= maxRetries; i++ {
_, err = dynamodbClient.UpdateTable(input)
if err != nil {
backoff(i, "Update Table")
} else {
return nil
}
}
return err
}
func disableStream(table string, dynamodbClient *dynamodb.DynamoDB) error {
var err error
input := &dynamodb.UpdateTableInput{
TableName: aws.String(table),
StreamSpecification: &dynamodb.StreamSpecification{StreamEnabled: aws.Bool(false)},
}
for i := 1; i <= maxRetries; i++ {
_, err = dynamodbClient.UpdateTable(input)
if err != nil {
backoff(i, "Update Table")
} else {
status := ""
for status != "ACTIVE" {
time.Sleep(tableStatusWaitInterval)
result, err := describeTable(table, dynamodbClient)
if err != nil {
return err
}
status = *result.Table.TableStatus
}
return nil
}
}
return err
}
func batchWrite(items map[string][]*dynamodb.WriteRequest, dynamodbClient *dynamodb.DynamoDB) (
*dynamodb.BatchWriteItemOutput, error) {
var err error
var output *dynamodb.BatchWriteItemOutput
input := &dynamodb.BatchWriteItemInput{
RequestItems: items,
ReturnConsumedCapacity: aws.String(dynamodb.ReturnConsumedCapacityTotal),
}
output, err = dynamodbClient.BatchWriteItem(input)
return output, err
}
func scanTable(table string, id int, totalSegments int, lastEvaluatedKey map[string]*dynamodb.AttributeValue,
dynamodbClient *dynamodb.DynamoDB) (*dynamodb.ScanOutput, error) {
var output *dynamodb.ScanOutput
var err error
input := &dynamodb.ScanInput{
TableName: aws.String(table),
ConsistentRead: aws.Bool(true),
Segment: aws.Int64(int64(id)),
TotalSegments: aws.Int64(int64(totalSegments)),
}
if len(lastEvaluatedKey) > 0 {
input.ExclusiveStartKey = lastEvaluatedKey
}
for i := 1; i <= maxRetries; i++ {
output, err = dynamodbClient.Scan(input)
if err != nil {
backoff(i, "Scan")
} else {
return output, nil
}
}
return nil, err
}