-
Notifications
You must be signed in to change notification settings - Fork 3
/
drift.go
673 lines (632 loc) · 19.7 KB
/
drift.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
package drift
import (
"context"
"fmt"
"io/ioutil"
"log"
"sort"
"strconv"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/dollarshaveclub/jobmanager"
)
// RawDynamoItem models an item from DynamoDB as returned by the API
type RawDynamoItem map[string]*dynamodb.AttributeValue
// DynamoMigrationFunction is a callback run for each item in the DynamoDB table
// item is the raw item
// action is the DrifterAction object used to mutate/add/remove items
type DynamoMigrationFunction func(item RawDynamoItem, action *DrifterAction) error
// DynamoDrifterMigration models an individual migration
type DynamoDrifterMigration struct {
Number uint `dynamodbav:"Number" json:"number"` // Monotonic number of the migration (ascending)
TableName string `dynamodbav:"TableName" json:"tablename"` // DynamoDB table the migration applies to
Description string `dynamodbav:"Description" json:"description"` // Free-form description of what the migration does
Callback DynamoMigrationFunction `dynamodbav:"-" json:"-"` // Callback for each item in the table
}
// DynamoDrifter is the object that manages and performs migrations
type DynamoDrifter struct {
MetaTableName string // Table to store migration tracking metadata
DynamoDB *dynamodb.DynamoDB // Fully initialized and authenticated DynamoDB client
q actionQueue
}
func (dd *DynamoDrifter) createMetaTable(pwrite, pread uint, metatable string) error {
cti := &dynamodb.CreateTableInput{
TableName: aws.String(metatable),
AttributeDefinitions: []*dynamodb.AttributeDefinition{
&dynamodb.AttributeDefinition{
AttributeName: aws.String("Number"),
AttributeType: aws.String("N"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
&dynamodb.KeySchemaElement{
AttributeName: aws.String("Number"),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(int64(pread)),
WriteCapacityUnits: aws.Int64(int64(pwrite)),
},
}
_, err := dd.DynamoDB.CreateTable(cti)
return err
}
func (dd *DynamoDrifter) findTable(table string) (bool, error) {
var err error
var lto *dynamodb.ListTablesOutput
lti := &dynamodb.ListTablesInput{}
for {
lto, err = dd.DynamoDB.ListTables(lti)
if err != nil {
return false, fmt.Errorf("error listing tables: %v", err)
}
for _, tn := range lto.TableNames {
if tn != nil && *tn == table {
return true, nil
}
}
if lto.LastEvaluatedTableName == nil {
return false, nil
}
lti.ExclusiveStartTableName = lto.LastEvaluatedTableName
}
}
// Init creates the metadata table if necessary. It is safe to run Init multiple times (it's a noop if metadata table already exists).
// pread and pwrite are the provisioned read and write values to use with table creation, if necessary
func (dd *DynamoDrifter) Init(pwrite, pread uint) error {
if dd.DynamoDB == nil {
return fmt.Errorf("DynamoDB client is required")
}
extant, err := dd.findTable(dd.MetaTableName)
if err != nil {
return fmt.Errorf("error checking if meta table exists: %v", err)
}
if !extant {
err = dd.createMetaTable(pwrite, pread, dd.MetaTableName)
if err != nil {
return fmt.Errorf("error creating meta table: %v", err)
}
}
return nil
}
// Applied returns all applied migrations as tracked in metadata table in ascending order
func (dd *DynamoDrifter) Applied() ([]DynamoDrifterMigration, error) {
if dd.DynamoDB == nil {
return nil, fmt.Errorf("DynamoDB client is required")
}
in := &dynamodb.ScanInput{
TableName: &dd.MetaTableName,
}
ms := []DynamoDrifterMigration{}
var consumeErr error
consumePage := func(resp *dynamodb.ScanOutput, last bool) bool {
for _, v := range resp.Items {
m := DynamoDrifterMigration{}
consumeErr = dynamodbattribute.UnmarshalMap(v, &m)
if consumeErr != nil {
return false // stop paging
}
ms = append(ms, m)
}
return true
}
err := dd.DynamoDB.ScanPages(in, consumePage)
if err != nil {
return nil, err
}
if consumeErr != nil {
return nil, consumeErr
}
// sort by Number
sort.Slice(ms, func(i, j int) bool { return ms[i].Number < ms[j].Number })
return ms, nil
}
func (dd *DynamoDrifter) doCallback(ctx context.Context, params ...interface{}) error {
if len(params) != 3 {
return fmt.Errorf("bad parameter count: %v (want 3)", len(params))
}
callback, ok := params[0].(DynamoMigrationFunction)
if !ok {
return fmt.Errorf("bad type for DynamoMigrationFunction: %T", params[0])
}
item, ok := params[1].(map[string]*dynamodb.AttributeValue)
if !ok {
return fmt.Errorf("bad type for RawDynamoItem: %T", params[1])
}
da, ok := params[2].(*DrifterAction)
if !ok {
return fmt.Errorf("bad type for *DrifterAction: %T", params[2])
}
return callback(item, da)
}
type errorCollector struct {
sync.Mutex
errs []error
}
func (ec *errorCollector) clear() {
ec.Lock()
ec.errs = []error{}
ec.Unlock()
}
func (ec *errorCollector) HandleError(err error) error {
ec.Lock()
ec.errs = append(ec.errs, err)
ec.Unlock()
return nil
}
func (dd *DynamoDrifter) progressMsg(cp, ae uint, cerrs, aerrs []error, progressChan chan *MigrationProgress) {
if progressChan != nil {
select {
case progressChan <- &MigrationProgress{
CallbacksProcessed: cp,
ActionsExecuted: ae,
CallbackErrors: cerrs,
ActionErrors: aerrs,
}:
return
default:
return
}
}
}
// runCallbacks gets items from the target table in batches of size concurrency, populates a JobManager with them and then executes all jobs in parallel
func (dd *DynamoDrifter) runCallbacks(ctx context.Context, migration *DynamoDrifterMigration, concurrency uint, scanLimit uint, failOnFirstError bool, progressChan chan *MigrationProgress) (*DrifterAction, []error) {
errs := []error{}
ec := errorCollector{}
da := &DrifterAction{}
var jm *jobmanager.JobManager
getnewjm := func() {
jm = jobmanager.New()
jm.ErrorHandler = &ec
jm.Concurrency = concurrency
jm.Identifier = "migration-callbacks"
jm.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
getnewjm()
si := &dynamodb.ScanInput{
ConsistentRead: aws.Bool(true),
TableName: &migration.TableName,
Limit: aws.Int64(int64(scanLimit)),
}
var cp uint
for {
so, err := dd.DynamoDB.Scan(si)
if err != nil {
return nil, []error{fmt.Errorf("error scanning migration table: %v", err)}
}
j := &jobmanager.Job{
Job: dd.doCallback,
}
for _, item := range so.Items {
jm.AddJob(j, migration.Callback, item, da)
}
jm.Run(ctx)
if len(ec.errs) != 0 && failOnFirstError {
return nil, ec.errs
}
getnewjm()
cp += uint(len(so.Items))
dd.progressMsg(cp, 0, ec.errs, nil, progressChan)
errs = append(errs, ec.errs...)
ec.clear()
if len(so.LastEvaluatedKey) == 0 {
return da, errs
}
si.ExclusiveStartKey = so.LastEvaluatedKey
}
}
func (dd *DynamoDrifter) doAction(ctx context.Context, params ...interface{}) error {
if len(params) != 2 {
return fmt.Errorf("bad parameter length: %v (want 2)", len(params))
}
action, ok := params[0].(*action)
if !ok {
return fmt.Errorf("bad type for *action: %T", params[0])
}
tn, ok := params[1].(string)
if !ok {
return fmt.Errorf("bad type for tablename: %T", params[1])
}
if action.tableName != "" {
tn = action.tableName
}
switch action.atype {
case updateAction:
uii := &dynamodb.UpdateItemInput{
TableName: &tn,
Key: action.keys,
UpdateExpression: aws.String(action.updExpr),
ExpressionAttributeValues: action.values,
ExpressionAttributeNames: action.expAttrNames,
}
_, err := dd.DynamoDB.UpdateItem(uii)
if err != nil {
return fmt.Errorf("error updating item: %v", err)
}
return nil
case insertAction:
pii := &dynamodb.PutItemInput{
TableName: &tn,
Item: action.item,
}
_, err := dd.DynamoDB.PutItem(pii)
if err != nil {
return fmt.Errorf("error inserting item: %v", err)
}
return nil
case deleteAction:
dii := &dynamodb.DeleteItemInput{
TableName: &tn,
Key: action.keys,
}
_, err := dd.DynamoDB.DeleteItem(dii)
if err != nil {
return fmt.Errorf("error deleting item: %v", err)
}
return nil
default:
return fmt.Errorf("unknown action type: %v", action.atype)
}
}
func (dd *DynamoDrifter) executeActions(ctx context.Context, migration *DynamoDrifterMigration, da *DrifterAction, concurrency uint, failonFirstError bool, progressChan chan *MigrationProgress) []error {
ec := errorCollector{}
var jm *jobmanager.JobManager
getnewjm := func() { // you can only Run() a JobManager once
jm = jobmanager.New()
jm.ErrorHandler = &ec
jm.Concurrency = concurrency
jm.Identifier = "migration-actions"
jm.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
getnewjm()
var i int
for i = range da.aq.q {
j := &jobmanager.Job{
Job: dd.doAction,
}
jm.AddJob(j, &(da.aq.q[i]), migration.TableName)
if i != 0 && i%(100*int(concurrency)) == 0 {
jm.Run(ctx)
if len(ec.errs) != 0 && failonFirstError {
return ec.errs
}
getnewjm()
dd.progressMsg(0, uint(i+1), nil, ec.errs, progressChan)
} else {
}
}
if i != 0 {
jm.Run(ctx)
dd.progressMsg(0, uint(i), nil, ec.errs, progressChan)
}
return ec.errs
}
func (dd *DynamoDrifter) insertMetaItem(m *DynamoDrifterMigration) error {
mi, err := dynamodbattribute.MarshalMap(m)
if err != nil {
return fmt.Errorf("error marshaling migration: %v", err)
}
pi := &dynamodb.PutItemInput{
TableName: &dd.MetaTableName,
Item: mi,
}
_, err = dd.DynamoDB.PutItem(pi)
if err != nil {
return fmt.Errorf("error inserting migration item into meta table: %v", err)
}
return nil
}
func (dd *DynamoDrifter) deleteMetaItem(m *DynamoDrifterMigration) error {
di := &dynamodb.DeleteItemInput{
TableName: &dd.MetaTableName,
Key: map[string]*dynamodb.AttributeValue{
"Number": &dynamodb.AttributeValue{
N: aws.String(strconv.Itoa(int(m.Number))),
},
},
}
_, err := dd.DynamoDB.DeleteItem(di)
if err != nil {
return fmt.Errorf("error deleting item from meta table: %v", err)
}
return nil
}
func (dd *DynamoDrifter) run(ctx context.Context, migration *DynamoDrifterMigration, concurrency uint, failOnFirstError bool, progressChan chan *MigrationProgress) []error {
if migration == nil || migration.Callback == nil {
return []error{fmt.Errorf("migration is required")}
}
if concurrency == 0 {
concurrency = 1
}
if migration.TableName == "" {
return []error{fmt.Errorf("TableName is required")}
}
extant, err := dd.findTable(migration.TableName)
if err != nil {
return []error{fmt.Errorf("error finding migration table: %v", err)}
}
if !extant {
return []error{fmt.Errorf("table %v not found", migration.TableName)}
}
defaultScanLimit := concurrency * 100
da, errs := dd.runCallbacks(ctx, migration, concurrency, defaultScanLimit, failOnFirstError, progressChan)
if len(errs) != 0 {
return errs
}
errs = dd.executeActions(ctx, migration, da, concurrency, failOnFirstError, progressChan)
if len(errs) != 0 {
return errs
}
return []error{}
}
// MigrationProgress models periodic progress information communicated back to the caller
type MigrationProgress struct {
CallbacksProcessed uint
ActionsExecuted uint
CallbackErrors []error
ActionErrors []error
}
// Run runs an individual migration at the specified concurrency and blocks until finished.
// concurrency controls the number of table items processed concurrently (value of one will guarantee order of migration actions).
// failOnFirstError causes Run to abort on first error, otherwise the errors will be queued and reported only after all items have been processed.
// progressChan is an optional channel on which periodic MigrationProgress messages will be sent
func (dd *DynamoDrifter) Run(ctx context.Context, migration *DynamoDrifterMigration, concurrency uint, failOnFirstError bool, progressChan chan *MigrationProgress) []error {
if dd.DynamoDB == nil {
return []error{fmt.Errorf("DynamoDB client is required")}
}
if progressChan != nil {
defer close(progressChan)
}
errs := dd.run(ctx, migration, concurrency, failOnFirstError, progressChan)
if len(errs) != 0 {
return errs
}
err := dd.insertMetaItem(migration)
if err != nil {
return []error{err}
}
return []error{}
}
// Undo "undoes" a migration by running the supplied migration but deletes the corresponding metadata record if successful
func (dd *DynamoDrifter) Undo(ctx context.Context, undoMigration *DynamoDrifterMigration, concurrency uint, failOnFirstError bool, progressChan chan *MigrationProgress) []error {
if dd.DynamoDB == nil {
return []error{fmt.Errorf("DynamoDB client is required")}
}
errs := dd.run(ctx, undoMigration, concurrency, failOnFirstError, progressChan)
if len(errs) != 0 {
return errs
}
err := dd.deleteMetaItem(undoMigration)
if err != nil {
return []error{err}
}
return []error{}
}
type actionType int
const (
updateAction actionType = iota
insertAction
deleteAction
)
type action struct {
atype actionType
keys RawDynamoItem
values RawDynamoItem
item RawDynamoItem
updExpr string
expAttrNames map[string]*string
tableName string
}
type actionQueue struct {
sync.Mutex
q []action
}
// DrifterAction is an object useful for performing actions within the migration callback. All actions performed by methods on DrifterAction are queued and performed *after* all existing items have been iterated over and callbacks performed.
// DrifterAction can be used in multiple goroutines by the callback, but must not be retained after the callback returns.
// If concurrency > 1, order of queued operations cannot be guaranteed.
type DrifterAction struct {
dyn *dynamodb.DynamoDB
aq actionQueue
}
// Update mutates the given keys using fields and updateExpression.
// keys and values are arbitrary structs with "dynamodbav" annotations. IMPORTANT: annotation names must match the names used in updateExpression.
// updateExpression is the native DynamoDB update expression. Ex: "SET foo = :bar" (in this example keys must have a field annotated "foo" and values must have a field annotated ":bar").
// expressionAttributeNames is optional but used if item attribute names are reserved keywords. For example: "SET #n = :name", expressionAttributeNames: map[string]string{"#n":"Name"}.
//
// Required: keys, values, updateExpression
//
// Optional: expressionAttributeNames, tableName (defaults to migration table)
func (da *DrifterAction) Update(keys interface{}, values interface{}, updateExpression string, expressionAttributeNames map[string]string, tableName string) error {
var err error
var mkeys, mvals map[string]*dynamodb.AttributeValue
switch v := keys.(type) {
case map[string]*dynamodb.AttributeValue:
mkeys = v
case RawDynamoItem:
mkeys = v
default:
mkeys, err = dynamodbattribute.MarshalMap(keys)
if err != nil {
return fmt.Errorf("error marshaling keys: %v", err)
}
}
switch v := values.(type) {
case map[string]*dynamodb.AttributeValue:
mvals = v
case RawDynamoItem:
mvals = v
default:
mvals, err = dynamodbattribute.MarshalMap(values)
if err != nil {
return fmt.Errorf("error marshaling values: %v", err)
}
}
if updateExpression == "" {
return fmt.Errorf("updateExpression is required")
}
var ean map[string]*string
if expressionAttributeNames != nil && len(expressionAttributeNames) > 0 {
ean = map[string]*string{}
for k, v := range expressionAttributeNames {
ean[k] = &v
}
}
ua := action{
atype: updateAction,
keys: mkeys,
values: mvals,
updExpr: updateExpression,
expAttrNames: ean,
tableName: tableName,
}
da.aq.Lock()
da.aq.q = append(da.aq.q, ua)
da.aq.Unlock()
return nil
}
// Insert inserts item into the specified table.
// item is an arbitrary struct with "dynamodbav" annotations or a RawDynamoItem
// tableName is optional (defaults to migration table).
func (da *DrifterAction) Insert(item interface{}, tableName string) error {
var err error
var mitem map[string]*dynamodb.AttributeValue
switch v := item.(type) {
case RawDynamoItem:
mitem = v
case map[string]*dynamodb.AttributeValue:
mitem = v
default:
mitem, err = dynamodbattribute.MarshalMap(item)
if err != nil {
return fmt.Errorf("error marshaling item: %v", err)
}
}
ia := action{
atype: insertAction,
item: mitem,
tableName: tableName,
}
da.aq.Lock()
da.aq.q = append(da.aq.q, ia)
da.aq.Unlock()
return nil
}
// Delete deletes the specified item(s).
// keys is an arbitrary struct with "dynamodbav" annotations.
// tableName is optional (defaults to migration table).
func (da *DrifterAction) Delete(keys interface{}, tableName string) error {
var err error
var mkeys map[string]*dynamodb.AttributeValue
switch v := keys.(type) {
case map[string]*dynamodb.AttributeValue:
mkeys = v
case RawDynamoItem:
mkeys = v
default:
mkeys, err = dynamodbattribute.MarshalMap(keys)
if err != nil {
return fmt.Errorf("error marshaling keys: %v", err)
}
}
dla := action{
atype: deleteAction,
keys: mkeys,
tableName: tableName,
}
da.aq.Lock()
da.aq.q = append(da.aq.q, dla)
da.aq.Unlock()
return nil
}
// DynamoDB returns the DynamoDB client object
func (da *DrifterAction) DynamoDB() *dynamodb.DynamoDB {
return da.dyn
}
// getAttribute returns an attribute from a raw item
func getAttribute(item RawDynamoItem, attr string, t interface{}) (interface{}, error) {
a, ok := item[attr]
if !ok {
return nil, fmt.Errorf("key not found: %v", attr)
}
var ap interface{}
switch t.(type) {
case string:
ap = a.S
case int:
ap = a.N
case bool:
ap = a.BOOL
case []byte:
ap = a.B
default:
return nil, fmt.Errorf("bad/unsupported type for t: %T", t)
}
return ap, nil
}
// GetStringAttribute returns a string attribute from the raw item, or error if not found or wrong type
func GetStringAttribute(item RawDynamoItem, attr string) (string, error) {
si, err := getAttribute(item, attr, "")
if err != nil {
return "", fmt.Errorf("error getting attribute: %v", err)
}
switch v := si.(type) {
case *string:
if v == nil {
return "", fmt.Errorf("attribute is not a string")
}
return *v, nil
default:
return "", fmt.Errorf("unexpected type for value: %T", si)
}
}
// GetNumberAttribute returns a number attribute (as string) from the raw item, or error if not found or wrong type
func GetNumberAttribute(item RawDynamoItem, attr string) (string, error) {
si, err := getAttribute(item, attr, int(0))
if err != nil {
return "", fmt.Errorf("error getting attribute: %v", err)
}
switch v := si.(type) {
case *string:
if v == nil {
return "", fmt.Errorf("attribute is not a number")
}
return *v, nil
default:
return "", fmt.Errorf("unexpected type for value: %T", si)
}
}
// GetBoolAttribute returns a string attribute from the raw item, or error if not found or wrong type
func GetBoolAttribute(item RawDynamoItem, attr string) (bool, error) {
si, err := getAttribute(item, attr, true)
if err != nil {
return false, fmt.Errorf("error getting attribute: %v", err)
}
switch v := si.(type) {
case *bool:
if v == nil {
return false, fmt.Errorf("attribute is not a bool")
}
return *v, nil
default:
return false, fmt.Errorf("unexpected type for value: %T", si)
}
}
// GetByteSliceAttribute returns a string attribute from the raw item, or error if not found or wrong type
func GetByteSliceAttribute(item RawDynamoItem, attr string) ([]byte, error) {
si, err := getAttribute(item, attr, []byte{})
if err != nil {
return nil, fmt.Errorf("error getting attribute: %v", err)
}
switch v := si.(type) {
case []byte:
if v == nil {
return nil, fmt.Errorf("attribute is not a byte slice")
}
return v, nil
default:
return nil, fmt.Errorf("unexpected type for value: %T", si)
}
}