-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamodb.go
725 lines (602 loc) · 18.7 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Package dynamodb contains the DynamoDB store implementation.
package dynamodb
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/kvtools/valkeyrie"
"github.com/kvtools/valkeyrie/store"
)
// StoreName the name of the store.
const StoreName = "dynamodb"
const (
// DefaultReadCapacityUnits default read capacity used to create table.
DefaultReadCapacityUnits = 2
// DefaultWriteCapacityUnits default write capacity used to create table.
DefaultWriteCapacityUnits = 2
// DeleteTreeTimeoutSeconds the maximum time we retry a write batch.
DeleteTreeTimeoutSeconds = 30
)
const (
partitionKey = "id"
revisionAttribute = "version"
encodedValueAttribute = "encoded_value"
ttlAttribute = "expiration_time"
)
const (
defaultLockTTL = 20 * time.Second
dynamodbDefaultTimeout = 10 * time.Second
)
var (
// ErrBucketOptionMissing is returned when bucket config option is missing.
ErrBucketOptionMissing = errors.New("missing dynamodb bucket/table name")
// ErrMultipleEndpointsUnsupported is returned when more than one endpoint is provided.
ErrMultipleEndpointsUnsupported = errors.New("dynamodb only supports one endpoint")
// ErrDeleteTreeTimeout delete batch timed out.
ErrDeleteTreeTimeout = errors.New("delete batch timed out")
// ErrLockAcquireCancelled stop called before lock was acquired.
ErrLockAcquireCancelled = errors.New("stop called before lock was acquired")
)
// Register register a store provider in valkeyrie for AWS DynamoDB.
// registers AWS DynamoDB to Valkeyrie.
func init() {
valkeyrie.Register(StoreName, newStore)
}
// Config the AWS DynamoDB configuration.
type Config struct {
Bucket string
}
func newStore(ctx context.Context, endpoints []string, options valkeyrie.Config) (store.Store, error) {
cfg, ok := options.(*Config)
if !ok && options != nil {
return nil, &store.InvalidConfigurationError{Store: StoreName, Config: options}
}
return New(ctx, endpoints, cfg)
}
// Store implements the store.Store interface.
type Store struct {
dynamoSvc dynamodbiface.DynamoDBAPI
tableName string
}
// New creates a new AWS DynamoDB client.
func New(_ context.Context, endpoints []string, options *Config) (*Store, error) {
if len(endpoints) > 1 {
return nil, ErrMultipleEndpointsUnsupported
}
if options == nil || options.Bucket == "" {
return nil, ErrBucketOptionMissing
}
var config *aws.Config
if len(endpoints) == 1 {
config = &aws.Config{
Endpoint: aws.String(endpoints[0]),
}
}
ddb := &Store{
dynamoSvc: dynamodb.New(session.Must(session.NewSession(config))),
tableName: options.Bucket,
}
return ddb, nil
}
// Put a value at the specified key.
func (ddb *Store) Put(ctx context.Context, key string, value []byte, opts *store.WriteOptions) error {
keyAttr := make(map[string]*dynamodb.AttributeValue)
keyAttr[partitionKey] = &dynamodb.AttributeValue{S: aws.String(key)}
exAttr := map[string]*dynamodb.AttributeValue{
":incr": {N: aws.String("1")},
}
var setList []string
// if a value was provided append it to the update expression.
if len(value) > 0 {
encodedValue := base64.StdEncoding.EncodeToString(value)
exAttr[":encv"] = &dynamodb.AttributeValue{S: aws.String(encodedValue)}
setList = append(setList, fmt.Sprintf("%s = :encv", encodedValueAttribute))
}
// if a ttl was provided validate it and append it to the update expression.
if opts != nil && opts.TTL > 0 {
ttlVal := time.Now().Add(opts.TTL).Unix()
exAttr[":ttl"] = &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(ttlVal, 10))}
setList = append(setList, fmt.Sprintf("%s = :ttl", ttlAttribute))
}
updateExp := fmt.Sprintf("ADD %s :incr", revisionAttribute)
if len(setList) > 0 {
updateExp = fmt.Sprintf("%s SET %s", updateExp, strings.Join(setList, ","))
}
_, err := ddb.dynamoSvc.UpdateItemWithContext(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String(ddb.tableName),
Key: keyAttr,
ExpressionAttributeValues: exAttr,
UpdateExpression: aws.String(updateExp),
})
if err != nil {
return err
}
return nil
}
// Get a value given its key.
func (ddb *Store) Get(ctx context.Context, key string, opts *store.ReadOptions) (*store.KVPair, error) {
if opts == nil {
opts = &store.ReadOptions{
Consistent: true, // default to enabling read consistency.
}
}
res, err := ddb.getKey(ctx, key, opts)
if err != nil {
return nil, err
}
if res.Item == nil {
return nil, store.ErrKeyNotFound
}
// is the item expired?
if isItemExpired(res.Item) {
return nil, store.ErrKeyNotFound
}
return decodeItem(res.Item)
}
func (ddb *Store) getKey(ctx context.Context, key string, options *store.ReadOptions) (*dynamodb.GetItemOutput, error) {
return ddb.dynamoSvc.GetItemWithContext(ctx, &dynamodb.GetItemInput{
TableName: aws.String(ddb.tableName),
ConsistentRead: aws.Bool(options.Consistent),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {S: aws.String(key)},
},
})
}
// Delete the value at the specified key.
func (ddb *Store) Delete(ctx context.Context, key string) error {
_, err := ddb.dynamoSvc.DeleteItemWithContext(ctx, &dynamodb.DeleteItemInput{
TableName: aws.String(ddb.tableName),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {S: aws.String(key)},
},
})
if err != nil {
return err
}
return nil
}
// Exists if a Key exists in the store.
func (ddb *Store) Exists(ctx context.Context, key string, _ *store.ReadOptions) (bool, error) {
res, err := ddb.dynamoSvc.GetItemWithContext(ctx, &dynamodb.GetItemInput{
TableName: aws.String(ddb.tableName),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {
S: aws.String(key),
},
},
})
if err != nil {
return false, err
}
if res.Item == nil {
return false, nil
}
// is the item expired?
if isItemExpired(res.Item) {
return false, nil
}
return true, nil
}
// List the content of a given prefix.
func (ddb *Store) List(ctx context.Context, directory string, opts *store.ReadOptions) ([]*store.KVPair, error) {
if opts == nil {
opts = &store.ReadOptions{
Consistent: true, // default to enabling read consistency.
}
}
expAttr := make(map[string]*dynamodb.AttributeValue)
expAttr[":namePrefix"] = &dynamodb.AttributeValue{S: aws.String(directory)}
filterExp := fmt.Sprintf("begins_with(%s, :namePrefix)", partitionKey)
si := &dynamodb.ScanInput{
TableName: aws.String(ddb.tableName),
FilterExpression: aws.String(filterExp),
ExpressionAttributeValues: expAttr,
ConsistentRead: aws.Bool(opts.Consistent),
}
var items []map[string]*dynamodb.AttributeValue
ctx, cancel := context.WithTimeout(ctx, dynamodbDefaultTimeout)
err := ddb.dynamoSvc.ScanPagesWithContext(ctx, si,
func(page *dynamodb.ScanOutput, lastPage bool) bool {
items = append(items, page.Items...)
if lastPage {
cancel()
return false
}
return true
})
if err != nil {
return nil, err
}
if len(items) == 0 {
return nil, store.ErrKeyNotFound
}
var kvArray []*store.KVPair
var val *store.KVPair
for _, item := range items {
val, err = decodeItem(item)
if err != nil {
return nil, err
}
// skip the records which match the prefix.
if val.Key == directory {
continue
}
// skip records which are expired.
if isItemExpired(item) {
continue
}
kvArray = append(kvArray, val)
}
return kvArray, nil
}
// DeleteTree deletes a range of keys under a given directory.
func (ddb *Store) DeleteTree(ctx context.Context, keyPrefix string) error {
expAttr := make(map[string]*dynamodb.AttributeValue)
expAttr[":namePrefix"] = &dynamodb.AttributeValue{S: aws.String(keyPrefix)}
res, err := ddb.dynamoSvc.ScanWithContext(ctx, &dynamodb.ScanInput{
TableName: aws.String(ddb.tableName),
FilterExpression: aws.String(fmt.Sprintf("begins_with(%s, :namePrefix)", partitionKey)),
ExpressionAttributeValues: expAttr,
})
if err != nil {
return err
}
if len(res.Items) == 0 {
return nil
}
items := make(map[string][]*dynamodb.WriteRequest)
items[ddb.tableName] = make([]*dynamodb.WriteRequest, len(res.Items))
for n, item := range res.Items {
items[ddb.tableName][n] = &dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Key: map[string]*dynamodb.AttributeValue{
partitionKey: item[partitionKey],
},
},
}
}
return ddb.retryDeleteTree(ctx, items)
}
// AtomicPut Atomic CAS operation on a single value.
func (ddb *Store) AtomicPut(ctx context.Context, key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error) {
getRes, err := ddb.getKey(ctx, key, &store.ReadOptions{
Consistent: true, // enable the read consistent flag.
})
if err != nil {
return false, nil, err
}
// AtomicPut is equivalent to Put if previous is nil and the Key exist in the DB or is not expired.
if previous == nil && getRes.Item != nil && !isItemExpired(getRes.Item) {
return false, nil, store.ErrKeyExists
}
keyAttr := make(map[string]*dynamodb.AttributeValue)
keyAttr[partitionKey] = &dynamodb.AttributeValue{S: aws.String(key)}
exAttr := make(map[string]*dynamodb.AttributeValue)
exAttr[":incr"] = &dynamodb.AttributeValue{N: aws.String("1")}
var setList []string
// if a value was provided append it to the update expression.
if len(value) > 0 {
encodedValue := base64.StdEncoding.EncodeToString(value)
exAttr[":encv"] = &dynamodb.AttributeValue{S: aws.String(encodedValue)}
setList = append(setList, fmt.Sprintf("%s = :encv", encodedValueAttribute))
}
// if a ttl was provided validate it and append it to the update expression.
if opts != nil && opts.TTL > 0 {
ttlVal := time.Now().Add(opts.TTL).Unix()
exAttr[":ttl"] = &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(ttlVal, 10))}
setList = append(setList, fmt.Sprintf("%s = :ttl", ttlAttribute))
}
updateExp := fmt.Sprintf("ADD %s :incr", revisionAttribute)
if len(setList) > 0 {
updateExp = fmt.Sprintf("%s SET %s", updateExp, strings.Join(setList, ","))
}
var condExp *string
if previous != nil {
exAttr[":lastRevision"] = &dynamodb.AttributeValue{N: aws.String(strconv.FormatUint(previous.LastIndex, 10))}
exAttr[":timeNow"] = &dynamodb.AttributeValue{N: aws.String(strconv.FormatInt(time.Now().Unix(), 10))}
// the previous kv is in the DB and is at the expected revision, also if it has a TTL set it is NOT expired.
condExp = aws.String(fmt.Sprintf("%s = :lastRevision AND (attribute_not_exists(%s) OR (attribute_exists(%s) AND %s > :timeNow))",
revisionAttribute, ttlAttribute, ttlAttribute, ttlAttribute))
}
res, err := ddb.dynamoSvc.UpdateItemWithContext(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String(ddb.tableName),
Key: keyAttr,
ExpressionAttributeValues: exAttr,
UpdateExpression: aws.String(updateExp),
ConditionExpression: condExp,
ReturnValues: aws.String(dynamodb.ReturnValueAllNew),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return false, nil, store.ErrKeyModified
}
}
return false, nil, err
}
item, err := decodeItem(res.Attributes)
if err != nil {
return false, nil, err
}
return true, item, nil
}
// AtomicDelete delete of a single value.
func (ddb *Store) AtomicDelete(ctx context.Context, key string, previous *store.KVPair) (bool, error) {
getRes, err := ddb.getKey(ctx, key, &store.ReadOptions{
Consistent: true, // enable the read consistent flag.
})
if err != nil {
return false, err
}
if previous == nil && getRes.Item != nil && !isItemExpired(getRes.Item) {
return false, store.ErrKeyExists
}
expAttr := make(map[string]*dynamodb.AttributeValue)
if previous != nil {
expAttr[":lastRevision"] = &dynamodb.AttributeValue{N: aws.String(strconv.FormatUint(previous.LastIndex, 10))}
}
req := &dynamodb.DeleteItemInput{
TableName: aws.String(ddb.tableName),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {S: aws.String(key)},
},
ConditionExpression: aws.String(fmt.Sprintf("%s = :lastRevision", revisionAttribute)),
ExpressionAttributeValues: expAttr,
}
_, err = ddb.dynamoSvc.DeleteItemWithContext(ctx, req)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return false, store.ErrKeyNotFound
}
}
return false, err
}
return true, nil
}
// Close nothing to see here.
func (ddb *Store) Close() error { return nil }
// NewLock has to implemented at the library level since it's not supported by DynamoDB.
func (ddb *Store) NewLock(_ context.Context, key string, opts *store.LockOptions) (store.Locker, error) {
ttl := defaultLockTTL
var value []byte
renewCh := make(chan struct{})
if opts != nil {
if opts.TTL != 0 {
ttl = opts.TTL
}
if len(opts.Value) != 0 {
value = opts.Value
}
if opts.RenewLock != nil {
renewCh = opts.RenewLock
}
}
return &dynamodbLock{
ddb: ddb,
last: nil,
key: key,
value: value,
ttl: ttl,
renewCh: renewCh,
unlockCh: make(chan struct{}),
}, nil
}
// Watch has to implemented at the library level since it's not supported by DynamoDB.
func (ddb *Store) Watch(_ context.Context, _ string, _ *store.ReadOptions) (<-chan *store.KVPair, error) {
return nil, store.ErrCallNotSupported
}
// WatchTree has to implemented at the library level since it's not supported by DynamoDB.
func (ddb *Store) WatchTree(_ context.Context, _ string, _ *store.ReadOptions) (<-chan []*store.KVPair, error) {
return nil, store.ErrCallNotSupported
}
func (ddb *Store) createTable() error {
_, err := ddb.dynamoSvc.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String(partitionKey),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String(partitionKey),
KeyType: aws.String(dynamodb.KeyTypeHash),
},
},
// enable encryption of data by default.
SSESpecification: &dynamodb.SSESpecification{
Enabled: aws.Bool(true),
SSEType: aws.String(dynamodb.SSETypeAes256),
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(DefaultReadCapacityUnits),
WriteCapacityUnits: aws.Int64(DefaultWriteCapacityUnits),
},
TableName: aws.String(ddb.tableName),
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == dynamodb.ErrCodeResourceInUseException {
return nil
}
}
return err
}
err = ddb.dynamoSvc.WaitUntilTableExists(&dynamodb.DescribeTableInput{
TableName: aws.String(ddb.tableName),
})
if err != nil {
return err
}
return nil
}
func (ddb *Store) retryDeleteTree(ctx context.Context, items map[string][]*dynamodb.WriteRequest) error {
batchResult, err := ddb.dynamoSvc.BatchWriteItemWithContext(ctx, &dynamodb.BatchWriteItemInput{
RequestItems: items,
})
if err != nil {
return err
}
if len(batchResult.UnprocessedItems) == 0 {
return nil
}
timeout := make(chan bool, 1)
go func() {
time.Sleep(DeleteTreeTimeoutSeconds * time.Second)
timeout <- true
}()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
// Poll once a second for table status,
// until the table is either active or the timeout deadline has been reached.
for {
select {
case <-ticker.C:
batchResult, err = ddb.dynamoSvc.BatchWriteItemWithContext(ctx, &dynamodb.BatchWriteItemInput{
RequestItems: batchResult.UnprocessedItems,
})
if err != nil {
return err
}
if len(batchResult.UnprocessedItems) == 0 {
return nil
}
case <-timeout:
// polling for table status has taken more than the timeout.
return ErrDeleteTreeTimeout
}
}
}
type dynamodbLock struct {
ddb *Store
last *store.KVPair
renewCh chan struct{}
unlockCh chan struct{}
key string
value []byte
ttl time.Duration
}
func (l *dynamodbLock) Lock(ctx context.Context) (<-chan struct{}, error) {
lockHeld := make(chan struct{})
success, err := l.tryLock(ctx, lockHeld)
if err != nil {
return nil, err
}
if success {
return lockHeld, nil
}
// TODO: This really needs a jitter for backoff.
ticker := time.NewTicker(3 * time.Second)
for {
select {
case <-ticker.C:
success, err := l.tryLock(ctx, lockHeld)
if err != nil {
return nil, err
}
if success {
return lockHeld, nil
}
case <-ctx.Done():
return nil, ErrLockAcquireCancelled
}
}
}
func (l *dynamodbLock) Unlock(ctx context.Context) error {
l.unlockCh <- struct{}{}
_, err := l.ddb.AtomicDelete(ctx, l.key, l.last)
if err != nil {
return err
}
l.last = nil
return nil
}
func (l *dynamodbLock) tryLock(ctx context.Context, lockHeld chan struct{}) (bool, error) {
success, item, err := l.ddb.AtomicPut(ctx, l.key, l.value, l.last, &store.WriteOptions{TTL: l.ttl})
if err != nil {
if errors.Is(err, store.ErrKeyNotFound) || errors.Is(err, store.ErrKeyModified) || errors.Is(err, store.ErrKeyExists) {
return false, nil
}
return false, err
}
if success {
l.last = item
// keep holding.
go l.holdLock(ctx, lockHeld)
return true, nil
}
return false, err
}
func (l *dynamodbLock) holdLock(ctx context.Context, lockHeld chan struct{}) {
defer close(lockHeld)
hold := func() error {
_, item, err := l.ddb.AtomicPut(ctx, l.key, l.value, l.last, &store.WriteOptions{TTL: l.ttl})
if err != nil {
return err
}
l.last = item
return nil
}
// may need a floor of 1 second set.
heartbeat := time.NewTicker(l.ttl / 3)
defer heartbeat.Stop()
for {
select {
case <-heartbeat.C:
if err := hold(); err != nil {
return
}
case <-l.renewCh:
return
case <-l.unlockCh:
return
case <-ctx.Done():
return
}
}
}
func isItemExpired(item map[string]*dynamodb.AttributeValue) bool {
v, ok := item[ttlAttribute]
if !ok {
return false
}
ttl, _ := strconv.ParseInt(aws.StringValue(v.N), 10, 64)
return time.Unix(ttl, 0).Before(time.Now())
}
func decodeItem(item map[string]*dynamodb.AttributeValue) (*store.KVPair, error) {
var key string
if v, ok := item[partitionKey]; ok {
key = aws.StringValue(v.S)
}
var revision int64
if v, ok := item[revisionAttribute]; ok {
var err error
revision, err = strconv.ParseInt(aws.StringValue(v.N), 10, 64)
if err != nil {
return nil, err
}
}
var encodedValue string
if v, ok := item[encodedValueAttribute]; ok {
encodedValue = aws.StringValue(v.S)
}
rawValue, err := base64.StdEncoding.DecodeString(encodedValue)
if err != nil {
return nil, err
}
return &store.KVPair{
Key: key,
Value: rawValue,
LastIndex: uint64(revision),
}, nil
}