forked from darthberen/kinesis_client_library
/
dynamodb.go
243 lines (219 loc) · 6.38 KB
/
dynamodb.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
package kcl
import (
"fmt"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/aws/awserr"
"github.com/awslabs/aws-sdk-go/aws/awsutil"
"github.com/awslabs/aws-sdk-go/service/dynamodb"
)
type shardRecord struct {
ShardID string
Checkpoint string
LeaseExpiration int64
WorkerID string
}
func (s shardRecord) String() string {
return fmt.Sprintf("ShardID: %s, Checkpoint: %s, LeaseExpiration: %d, WorkerID: %s",
s.ShardID,
s.Checkpoint,
s.LeaseExpiration,
s.WorkerID,
)
}
type dynamo struct {
db *dynamodb.DynamoDB
tableName string
readCapacity int64
writeCapacity int64
}
func newDynamo(name string, readCapacity, writeCapacity int64) *dynamo {
cfg := aws.DefaultConfig
return &dynamo{
db: dynamodb.New(cfg),
tableName: name,
readCapacity: readCapacity,
writeCapacity: writeCapacity,
}
}
func (d *dynamo) ValidateTable() (err error) {
err = d.findTable()
if awserr, ok := err.(awserr.Error); ok {
log.WithField("error", awserr).Error("awserror: unable to describe table")
if awserr.Code() == "ResourceNotFoundException" {
log.Error("we should create the table here")
err = d.createTable()
}
} else {
}
return
}
func (d *dynamo) findTable() error {
input := &dynamodb.DescribeTableInput{
TableName: aws.String(d.tableName),
}
output, err := d.db.DescribeTable(input)
if err != nil {
return err
}
if !isValidTableSchema(output) {
return fmt.Errorf("dynamo: invalid table schema")
}
return nil
}
func (d *dynamo) createTable() (err error) {
tableDefinition := &dynamodb.CreateTableInput{
TableName: aws.String(d.tableName),
AttributeDefinitions: make([]*dynamodb.AttributeDefinition, 1, 1),
KeySchema: make([]*dynamodb.KeySchemaElement, 1, 1),
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Long(d.readCapacity),
WriteCapacityUnits: aws.Long(d.writeCapacity),
},
}
tableDefinition.KeySchema[0] = &dynamodb.KeySchemaElement{
AttributeName: aws.String("shard_id"),
KeyType: aws.String("HASH"),
}
tableDefinition.AttributeDefinitions[0] = &dynamodb.AttributeDefinition{
AttributeName: aws.String("shard_id"),
AttributeType: aws.String("S"),
}
var out *dynamodb.CreateTableOutput
out, err = d.db.CreateTable(tableDefinition)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"out": out,
}).Error("unable to create table")
return
}
if out != nil && out.TableDescription != nil {
log.WithFields(log.Fields{
"TableStatus": stringPtrToString(out.TableDescription.TableStatus),
"TableName": d.tableName,
}).Debug("created dynamodb table")
}
d.validateTableCreated()
return
}
// blocks until the table status comes back as "ACTIVE"
func (d *dynamo) validateTableCreated() {
input := &dynamodb.DescribeTableInput{
TableName: aws.String(d.tableName),
}
isActive := false
for !isActive {
time.Sleep(1 * time.Second)
if out, err := d.db.DescribeTable(input); err == nil {
log.WithField("status", awsutil.StringValue(out.Table.TableStatus)).Debug("got describe table output")
if stringPtrToString(out.Table.TableStatus) == "ACTIVE" {
isActive = true
}
}
}
}
func (d *dynamo) Checkpoint(shardID, seqNum, leaseExpiration, workerID string) (err error) {
attributes := map[string]*dynamodb.AttributeValue{
"shard_id": &dynamodb.AttributeValue{
S: aws.String(shardID),
},
"checkpoint": &dynamodb.AttributeValue{
S: aws.String(seqNum),
},
"lease_expiration": &dynamodb.AttributeValue{
N: aws.String(leaseExpiration),
},
"worker_id": &dynamodb.AttributeValue{
S: aws.String(workerID),
},
}
input := &dynamodb.PutItemInput{
TableName: aws.String(d.tableName),
Item: &attributes,
}
_, err = d.db.PutItem(input)
return
}
// TODO: handling unprocessed records - working? need more shards to test
func (d *dynamo) GetShardData(shards []string) (shardRecords map[string]*shardRecord, err error) {
funcName := "GetShardData"
shardRecords = make(map[string]*shardRecord)
// form the request for the records
keys := make([]*map[string]*dynamodb.AttributeValue, len(shards), len(shards))
for i, shard := range shards {
keys[i] = &map[string]*dynamodb.AttributeValue{
"shard_id": &dynamodb.AttributeValue{
S: aws.String(shard),
},
}
}
keysToProcess := &map[string]*dynamodb.KeysAndAttributes{
d.tableName: &dynamodb.KeysAndAttributes{
Keys: keys,
ProjectionExpression: aws.String("shard_id,checkpoint,lease_expiration,worker_id"),
ConsistentRead: aws.Boolean(true),
},
}
for keysToProcess != nil && len(*keysToProcess) > 0 {
input := &dynamodb.BatchGetItemInput{
RequestItems: keysToProcess,
}
var out *dynamodb.BatchGetItemOutput
out, err = d.db.BatchGetItem(input)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"function": funcName,
}).Error("unable to batch get items")
return
}
records := d.parseShardData(out)
for key, record := range records {
shardRecords[key] = record
}
keysToProcess = out.UnprocessedKeys
log.WithFields(log.Fields{
"function": funcName,
"keysToProcess": keysToProcess,
"length": len(*keysToProcess),
}).Debug("dynamo iteration")
}
return
}
func (d *dynamo) parseShardData(resp *dynamodb.BatchGetItemOutput) (shardRecords map[string]*shardRecord) {
funcName := "ParseShardData"
if resp == nil {
log.WithField("function", funcName).Error("resp is nil")
return
}
if resp.Responses == nil {
log.WithField("function", funcName).Error("resp.Responses is nil")
return
}
var records []*map[string]*dynamodb.AttributeValue
var ok bool
if records, ok = (*resp.Responses)[d.tableName]; !ok {
log.WithField("function", funcName).Error("could not find table")
return
}
if len(records) == 0 {
log.WithFields(log.Fields{
"function": funcName,
}).Debug("there are no records in dynamodb")
}
shardRecords = make(map[string]*shardRecord)
for _, record := range records {
shardID := stringPtrToString((*record)["shard_id"].S)
leaseExpiration, _ := strconv.ParseInt(stringPtrToString((*record)["lease_expiration"].N), 10, 64)
shardRecords[shardID] = &shardRecord{
ShardID: shardID,
Checkpoint: stringPtrToString((*record)["checkpoint"].S),
LeaseExpiration: leaseExpiration,
WorkerID: stringPtrToString((*record)["worker_id"].S),
}
}
return
}