-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.go
551 lines (502 loc) · 15.9 KB
/
store.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
package stream
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// ErrStateNotFound is returned if the state is not found.
var ErrStateNotFound = errors.New("state not found")
var ErrOptimisticConcurrency = errors.New("state has been updated since it was read, try again")
type StoreOption func(*StoreOptions) error
type StoreOptions struct {
Region string
Client *dynamodb.Client
PersistStateHistory bool
CodecTag string
}
func WithRegion(region string) StoreOption {
return func(o *StoreOptions) error {
o.Region = region
return nil
}
}
func WithPersistStateHistory(do bool) StoreOption {
return func(o *StoreOptions) error {
o.PersistStateHistory = do
return nil
}
}
func WithClient(client *dynamodb.Client) StoreOption {
return func(o *StoreOptions) error {
o.Client = client
return nil
}
}
func WithCodecTag(tag string) StoreOption {
return func(o *StoreOptions) error {
o.CodecTag = tag
return nil
}
}
// NewStore creates a new store using default config.
func NewStore(tableName, namespace string, opts ...StoreOption) (s *DynamoDBStore, err error) {
o := StoreOptions{}
for _, opt := range opts {
err = opt(&o)
if err != nil {
return
}
}
if o.Client == nil {
var cfg aws.Config
cfg, err = config.LoadDefaultConfig(context.Background(), config.WithRegion(o.Region))
if err != nil {
return
}
o.Client = dynamodb.NewFromConfig(cfg)
}
s = &DynamoDBStore{
Client: o.Client,
TableName: aws.String(tableName),
Namespace: namespace,
PersistStateHistory: o.PersistStateHistory,
Encoder: attributevalue.NewEncoder(func(opts *attributevalue.EncoderOptions) {
opts.TagKey = o.CodecTag
}),
Decoder: attributevalue.NewDecoder(func(opts *attributevalue.DecoderOptions) {
opts.TagKey = o.CodecTag
}),
Now: func() time.Time {
return time.Now().UTC()
},
}
return
}
// NewStoreWithConfig creates a new store with custom config.
//
// Deprecated: Use NewStore with the WithRegion option
func NewStoreWithConfig(region, tableName, namespace string, opts ...StoreOption) (s *DynamoDBStore, err error) {
opts = append(opts, WithRegion(region))
return NewStore(tableName, namespace, opts...)
}
// DynamoDBStore is a DynamoDB implementation of the Store interface.
type DynamoDBStore struct {
Client *dynamodb.Client
TableName *string
Namespace string
PersistStateHistory bool
Encoder *attributevalue.Encoder
Decoder *attributevalue.Decoder
Now func() time.Time
}
// Get data using the id and populate the state variable.
func (ddb *DynamoDBStore) Get(id string, state State) (sequence int64, err error) {
if reflect.ValueOf(state).Kind() != reflect.Ptr {
err = errors.New("the state parameter must be a pointer")
return
}
gio, err := ddb.Client.GetItem(context.Background(), &dynamodb.GetItemInput{
TableName: ddb.TableName,
ConsistentRead: aws.Bool(true),
Key: map[string]types.AttributeValue{
"_pk": &types.AttributeValueMemberS{Value: ddb.createPartitionKey(id)},
"_sk": &types.AttributeValueMemberS{Value: ddb.createStateRecordSortKey()},
},
})
if err != nil {
return
}
if len(gio.Item) == 0 {
err = ErrStateNotFound
return
}
err = ddb.unmarshalMap(gio.Item, state)
if err != nil {
return
}
return ddb.getRecordSequenceNumber(gio.Item)
}
// Put the updated state in the database.
func (ddb *DynamoDBStore) Put(id string, atSequence int64, state State, inbound []InboundEvent, outbound []OutboundEvent) error {
items, err := ddb.Prepare(id, atSequence, state, inbound, outbound)
if err != nil {
return err
}
return ddb.Execute(items)
}
// Execute a prepared transaction.
func (ddb *DynamoDBStore) Execute(items []types.TransactWriteItem) error {
_, err := ddb.Client.TransactWriteItems(context.Background(), &dynamodb.TransactWriteItemsInput{
TransactItems: items,
})
if err != nil {
var transactionCanceled *types.TransactionCanceledException
if errors.As(err, &transactionCanceled) {
for _, reason := range transactionCanceled.CancellationReasons {
if *reason.Code == "ConditionalCheckFailed" {
return ErrOptimisticConcurrency
}
}
}
}
return err
}
// Prepare the transaction.
func (ddb *DynamoDBStore) Prepare(id string, atSequence int64, state State, inbound []InboundEvent, outbound []OutboundEvent) (items []types.TransactWriteItem, err error) {
atSequence++
stwi, err := ddb.createStateTransactWriteItems(id, atSequence, state)
if err != nil {
return
}
itwi, err := ddb.createInboundTransactWriteItems(id, atSequence, inbound)
if err != nil {
return
}
otwi, err := ddb.createOutboundTransactWriteItems(id, atSequence, outbound)
if err != nil {
return
}
items = append(items, stwi...)
items = append(items, itwi...)
items = append(items, otwi...)
return
}
func (ddb *DynamoDBStore) createInboundTransactWriteItems(id string, atSequence int64, inbound []InboundEvent) (puts []types.TransactWriteItem, err error) {
puts = make([]types.TransactWriteItem, len(inbound))
for i := 0; i < len(inbound); i++ {
var item map[string]types.AttributeValue
item, err = ddb.createRecord(id,
ddb.createInboundRecordSortKey(inbound[i].EventName(), atSequence, i),
atSequence,
inbound[i],
inbound[i].EventName())
if err != nil {
return
}
puts[i] = ddb.createPut(item)
}
return
}
func (ddb *DynamoDBStore) createOutboundTransactWriteItems(id string, atSequence int64, outbound []OutboundEvent) (puts []types.TransactWriteItem, err error) {
puts = make([]types.TransactWriteItem, len(outbound))
for i := 0; i < len(outbound); i++ {
var item map[string]types.AttributeValue
item, err = ddb.createRecord(id,
ddb.createOutboundRecordSortKey(outbound[i].EventName(), atSequence, i),
atSequence,
outbound[i],
outbound[i].EventName())
if err != nil {
return
}
puts[i] = ddb.createPut(item)
}
return
}
func (ddb *DynamoDBStore) createPut(item map[string]types.AttributeValue) types.TransactWriteItem {
return types.TransactWriteItem{
Put: &types.Put{
TableName: ddb.TableName,
Item: item,
ConditionExpression: aws.String("attribute_not_exists(#_pk)"),
ExpressionAttributeNames: map[string]string{
"#_pk": "_pk",
},
},
}
}
func (ddb *DynamoDBStore) createStateTransactWriteItem(id string, atSequence int64, state State, key string) (twi types.TransactWriteItem, err error) {
item, err := ddb.createRecord(id, key, atSequence, state, ddb.Namespace)
if err != nil {
return
}
twi = types.TransactWriteItem{
Put: &types.Put{
TableName: ddb.TableName,
Item: item,
ConditionExpression: aws.String("attribute_not_exists(#_pk) OR #_seq = :_seq"),
ExpressionAttributeNames: map[string]string{
"#_pk": "_pk",
"#_seq": "_seq",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":_seq": ddb.attributeValueInteger(int64(atSequence - 1)),
},
},
}
return
}
func (ddb *DynamoDBStore) createStateTransactWriteItems(id string, atSequence int64, state State) (twis []types.TransactWriteItem, err error) {
twi, err := ddb.createStateTransactWriteItem(id, atSequence, state, ddb.createStateRecordSortKey())
if err != nil {
return
}
twis = append(twis, twi)
if ddb.PersistStateHistory {
twi, err = ddb.createStateTransactWriteItem(id, atSequence, state, ddb.createVersionedRecordSortKey(atSequence))
if err != nil {
return
}
twis = append(twis, twi)
}
return
}
func (ddb *DynamoDBStore) createPartitionKey(id string) string {
return fmt.Sprintf(`%s/%s`, ddb.Namespace, id)
}
func (ddb *DynamoDBStore) createStateRecordSortKey() string {
return "STATE"
}
func (ddb *DynamoDBStore) createVersionedRecordSortKey(atSequence int64) string {
return fmt.Sprintf("STATE/%d", atSequence)
}
func (ddb *DynamoDBStore) createInboundRecordSortKey(typeName string, sequence int64, index int) string {
return fmt.Sprintf(`INBOUND/%d/%d/%s`, sequence, index, typeName)
}
func (ddb *DynamoDBStore) createOutboundRecordSortKey(typeName string, sequence int64, index int) string {
return fmt.Sprintf(`OUTBOUND/%d/%d/%s`, sequence, index, typeName)
}
func (ddb *DynamoDBStore) attributeValueString(v string) types.AttributeValue {
return &types.AttributeValueMemberS{Value: v}
}
func (ddb *DynamoDBStore) attributeValueInteger(i int64) types.AttributeValue {
v := strconv.FormatInt(i, 10)
return &types.AttributeValueMemberN{Value: v}
}
func (ddb *DynamoDBStore) createRecord(id, sk string, sequence int64, item interface{}, recordName string) (record map[string]types.AttributeValue, err error) {
record, err = ddb.marshalMap(item)
if err != nil {
err = fmt.Errorf("error marshalling item to map: %w", err)
return
}
record["_namespace"] = ddb.attributeValueString(ddb.Namespace)
record["_pk"] = ddb.attributeValueString(ddb.createPartitionKey(id))
record["_seq"] = ddb.attributeValueInteger(int64(sequence))
record["_sk"] = ddb.attributeValueString(sk)
record["_typ"] = ddb.attributeValueString(recordName)
record["_ts"] = ddb.attributeValueInteger(ddb.Now().Unix())
record["_date"] = ddb.attributeValueString(ddb.Now().Format(time.RFC3339))
return
}
func (ddb *DynamoDBStore) getRecordSequenceNumber(r map[string]types.AttributeValue) (sequence int64, err error) {
a, ok := r["_seq"]
if !ok {
return sequence, fmt.Errorf("missing _seq field in record")
}
v, ok := a.(*types.AttributeValueMemberN)
if !ok {
return sequence, fmt.Errorf("null _seq field in record")
}
sequence, err = strconv.ParseInt(v.Value, 10, 64)
if err != nil {
return sequence, fmt.Errorf("invalid _seq field in record: %w", err)
}
return
}
func (ddb *DynamoDBStore) getRecordType(r map[string]types.AttributeValue) (typ string, err error) {
a, ok := r["_typ"]
if !ok {
return "", fmt.Errorf("missing _typ field in record")
}
v, ok := a.(*types.AttributeValueMemberS)
if !ok {
return "", fmt.Errorf("null _typ field in record")
}
return v.Value, nil
}
func NewInboundEventReader() *InboundEventReader {
return &InboundEventReader{
readers: make(map[string]func(item map[string]types.AttributeValue) (InboundEvent, error), 0),
}
}
type InboundEventReader struct {
readers map[string]func(item map[string]types.AttributeValue) (InboundEvent, error)
}
func (r *InboundEventReader) Add(eventName string, f func(item map[string]types.AttributeValue) (InboundEvent, error)) *InboundEventReader {
r.readers[eventName] = f
return r
}
func (r *InboundEventReader) Read(eventName string, item map[string]types.AttributeValue) (e InboundEvent, ok bool, err error) {
f, ok := r.readers[eventName]
if !ok {
return
}
e, err = f(item)
return
}
func NewOutboundEventReader() *OutboundEventReader {
return &OutboundEventReader{
readers: make(map[string]func(item map[string]types.AttributeValue) (OutboundEvent, error), 0),
}
}
type OutboundEventReader struct {
readers map[string]func(item map[string]types.AttributeValue) (OutboundEvent, error)
}
func (r *OutboundEventReader) Add(eventName string, f func(item map[string]types.AttributeValue) (OutboundEvent, error)) *OutboundEventReader {
r.readers[eventName] = f
return r
}
func (r *OutboundEventReader) Read(eventName string, item map[string]types.AttributeValue) (e OutboundEvent, ok bool, err error) {
f, ok := r.readers[eventName]
if !ok {
return
}
e, err = f(item)
return
}
func NewStateHistoryReader(f func(item map[string]types.AttributeValue) (State, error)) *StateHistoryReader {
return &StateHistoryReader{reader: f}
}
type StateHistoryReader struct {
reader func(item map[string]types.AttributeValue) (State, error)
}
func (r *StateHistoryReader) Read(item map[string]types.AttributeValue) (e State, err error) {
e, err = r.reader(item)
return
}
func (ddb *DynamoDBStore) queryPages(qi *dynamodb.QueryInput, pager func(*dynamodb.QueryOutput, bool) bool) (err error) {
pages := dynamodb.NewQueryPaginator(ddb.Client, qi)
for carryOn := pages.HasMorePages(); carryOn && pages.HasMorePages(); {
var page *dynamodb.QueryOutput
page, err = pages.NextPage(context.Background())
if err != nil {
return err
}
carryOn = pager(page, pages.HasMorePages())
}
return
}
func (ddb *DynamoDBStore) Query(id string, state State, inboundEventReader *InboundEventReader, outboundEventReader *OutboundEventReader) (sequence int64, inbound []InboundEvent, outbound []OutboundEvent, err error) {
noopStateHistoryReader := NewStateHistoryReader(func(item map[string]types.AttributeValue) (State, error) { return nil, nil })
sequence, inbound, outbound, _, err = ddb.QueryWithHistory(id, state, inboundEventReader, outboundEventReader, noopStateHistoryReader)
return
}
// Query data for the id.
func (ddb *DynamoDBStore) QueryWithHistory(id string, state State, inboundEventReader *InboundEventReader, outboundEventReader *OutboundEventReader, stateHistoryReader *StateHistoryReader) (sequence int64, inbound []InboundEvent, outbound []OutboundEvent, stateHistory []State, err error) {
if reflect.ValueOf(state).Kind() != reflect.Ptr {
err = errors.New("the state parameter must be a pointer")
return
}
qi := &dynamodb.QueryInput{
TableName: ddb.TableName,
ConsistentRead: aws.Bool(true),
KeyConditionExpression: aws.String("#_pk = :_pk"),
ExpressionAttributeNames: map[string]string{
"#_pk": "_pk",
},
ExpressionAttributeValues: map[string]types.AttributeValue{
":_pk": ddb.attributeValueString(ddb.createPartitionKey(id)),
},
}
var found bool
var pagerError error
pager := func(qo *dynamodb.QueryOutput, _ bool) (carryOn bool) {
for i := 0; i < len(qo.Items); i++ {
r := qo.Items[i]
prefix, suffix := ddb.splitSortKey(r)
switch prefix {
case "STATE":
if suffix == "" {
found = true
pagerError = ddb.unmarshalMap(r, state)
if pagerError != nil {
return false
}
sequence, pagerError = ddb.getRecordSequenceNumber(r)
if pagerError != nil {
return false
}
} else {
transition, err := stateHistoryReader.Read(r)
if err != nil {
pagerError = err
return false
}
stateHistory = append(stateHistory, transition)
}
case "INBOUND":
var typ string
typ, pagerError = ddb.getRecordType(r)
if pagerError != nil {
return false
}
event, ok, err := inboundEventReader.Read(typ, r)
if err != nil {
pagerError = err
return false
}
if !ok {
pagerError = fmt.Errorf("inbound event: no reader for %q", typ)
return false
}
inbound = append(inbound, event)
case "OUTBOUND":
var typ string
typ, pagerError = ddb.getRecordType(r)
if pagerError != nil {
return false
}
event, ok, err := outboundEventReader.Read(typ, r)
if err != nil {
pagerError = err
return false
}
if !ok {
pagerError = fmt.Errorf("outbound event: no reader for %q", typ)
return false
}
outbound = append(outbound, event)
}
}
return true
}
err = ddb.queryPages(qi, pager)
if err != nil {
return
}
if pagerError != nil {
err = pagerError
return
}
if !found {
err = ErrStateNotFound
return
}
return
}
func (ddb *DynamoDBStore) splitSortKey(item map[string]types.AttributeValue) (prefix string, suffix string) {
sk, ok := item["_sk"]
if !ok {
return
}
v, ok := sk.(*types.AttributeValueMemberS)
if ok {
split := strings.SplitN(v.Value, "/", 2)
prefix = split[0]
if len(split) == 2 {
suffix = split[1]
}
}
return
}
func (ddb *DynamoDBStore) unmarshalMap(m map[string]types.AttributeValue, out interface{}) error {
return ddb.Decoder.Decode(&types.AttributeValueMemberM{Value: m}, out)
}
func (ddb *DynamoDBStore) marshalMap(in interface{}) (out map[string]types.AttributeValue, err error) {
av, err := ddb.Encoder.Encode(in)
asMap, ok := av.(*types.AttributeValueMemberM)
if err != nil || av == nil || !ok {
return
}
out = asMap.Value
return
}