-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb.go
592 lines (524 loc) · 17.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
/*
* Copyright (C) 2023 Asim Ihsan
* SPDX-License-Identifier: AGPL-3.0-only
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>
*/
package aws
import (
"context"
"encoding/json"
"errors"
"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/expression"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
dynamodbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/gofrs/uuid"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog"
"math/rand"
"strconv"
"sync"
"time"
)
var (
LockNotFoundError = errors.New("lock not found")
LockHeartbeatFailedError = errors.New("failed to heartbeat lock")
LockReleaseFailedError = errors.New("failed to release lock")
LockConditionalUpdateFailedError = errors.New("failed to update lock due to condition not being met")
LockAbandonedError = errors.New("lock abandoned")
)
type LockCurrentlyUnavailableError struct {
}
func (e LockCurrentlyUnavailableError) Error() string {
return "lock is currently unavailable"
}
type DynamoDBLockConfig struct {
Owner string
MaxShards int
LeaseDurationSeconds int
HeartbeatIntervalSeconds int
}
type DynamoDBLockClient struct {
Client *dynamodb.Client
TableName string
Config DynamoDBLockConfig
locks map[string]Lock
mu sync.Mutex
stopBackgroundJobs chan struct{}
zlog *zerolog.Logger
}
func NewDynamoDBClient(region string) (*dynamodb.Client, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithRetryMaxAttempts(3),
config.WithDefaultsMode(aws.DefaultsModeAuto),
)
if err != nil {
return nil, err
}
return dynamodb.NewFromConfig(cfg), nil
}
func NewDynamoDBLockClient(
tableName string,
region string,
config DynamoDBLockConfig,
zlog *zerolog.Logger,
) (*DynamoDBLockClient, error) {
client, err := NewDynamoDBClient(region)
if err != nil {
return nil, err
}
d := DynamoDBLockClient{
Client: client,
TableName: tableName,
Config: config,
locks: make(map[string]Lock),
mu: sync.Mutex{},
stopBackgroundJobs: make(chan struct{}),
zlog: zlog,
}
// Start a background job that once a minute heartbeat all locks that we own. There is another
// channel that tells the background job to stop.
go func() {
ticker := time.NewTicker(time.Duration(d.Config.HeartbeatIntervalSeconds) * time.Second)
for {
select {
case <-ticker.C:
// Make a []string of lock IDs to heartbeat
d.mu.Lock()
lockIDs := make([]string, 0, len(d.locks))
for lockID := range d.locks {
lockIDs = append(lockIDs, lockID)
}
d.mu.Unlock()
var wg sync.WaitGroup
var errs multierror.Error
for _, lockID := range lockIDs {
wg.Add(1)
go func(lockID string) {
defer wg.Done()
err := d.Heartbeat(context.TODO(), lockID, nil)
if err != nil {
// if we are abandoning a lock, remove it from the map
if errors.Is(err, LockAbandonedError) {
d.mu.Lock()
delete(d.locks, lockID)
d.mu.Unlock()
}
errs.Errors = append(errs.Errors, err)
}
}(lockID)
}
wg.Wait()
if len(errs.Errors) > 0 {
zlog.Error().Err(errs.ErrorOrNil()).Msg("failed to heartbeat locks")
}
case <-d.stopBackgroundJobs:
zlog.Info().Msg("stopping background heartbeat job")
return
}
}
}()
return &d, nil
}
func (d *DynamoDBLockClient) Close() error {
d.stopBackgroundJobs <- struct{}{}
return nil
}
func (d *DynamoDBLockClient) Owner() string {
return d.Config.Owner
}
func (d *DynamoDBLockClient) Acquire(
ctx context.Context,
id string,
data interface{},
) (*Lock, error) {
zlog := d.zlog.With().Str("id", id).Logger()
nowMilliseconds := time.Now().UnixNano() / int64(time.Millisecond)
existingLock, err := d.getLock(ctx, id)
if err != nil {
zlog.Error().Err(err).Msg("failed to get lock")
return nil, err
}
if existingLock != nil {
zlog.Debug().Interface("existingLock", existingLock).Msg("lock is already acquired")
if !existingLock.IsExpired(nowMilliseconds) {
zlog.Debug().Msg("lock is already acquired and not expired")
return existingLock, LockCurrentlyUnavailableError{}
}
zlog.Debug().Msg("lock is already acquired but expired")
newLock, err := d.updateExistingLock(ctx, *existingLock, data, nowMilliseconds)
if err != nil {
// Lock is acquired, expired, and when we tried to get it we got a conditional error, meaning we lost
// the lease to someone else. We need to evict the lock from our cache and return an error.
if err == LockConditionalUpdateFailedError {
zlog.Debug().Msg("lock is already acquired but expired and conditional check failed")
d.mu.Lock()
delete(d.locks, id)
d.mu.Unlock()
return nil, LockCurrentlyUnavailableError{}
}
zlog.Error().Err(err).Msg("failed to update existing lock")
return nil, err
}
return newLock, nil
}
zlog.Debug().Msg("lock is not acquired")
lock, err := d.putNewLock(ctx, id, data, nowMilliseconds)
if err != nil {
zlog.Error().Err(err).Msg("failed to put new lock")
return nil, err
}
zlog.Info().Interface("lock", lock).Msg("acquired lock")
return lock, nil
}
func (d *DynamoDBLockClient) Heartbeat(
ctx context.Context,
id string,
maybeNewData *interface{},
) error {
zlog := d.zlog.With().Str("id", id).Logger()
zlog.Debug().Msg("heartbeat")
existingLock, ok := d.getLocalLock(id)
if !ok {
zlog.Debug().Msg("lock is not locally acquired")
return LockNotFoundError
}
// if the existing lock was created more than 5 minutes ago, then just leave it alone
const abandonLockAfterMilliseconds = 5 * 60 * 1000
if existingLock.CreatedAtMilliseconds < time.Now().UnixNano()/int64(time.Millisecond)-abandonLockAfterMilliseconds {
zlog.Debug().Msg("lock is more than 1 minute old, abandoning it")
return LockAbandonedError
}
var newData interface{}
if maybeNewData != nil {
newData = *maybeNewData
} else {
newData = existingLock.Data
}
var resultError multierror.Error
nowMilliseconds := time.Now().UnixNano() / int64(time.Millisecond)
_, err := d.updateExistingLock(ctx, existingLock, newData, nowMilliseconds)
if err != nil {
// Lock is acquired, expired, and when we tried to get it we got a conditional error, meaning we lost
// the lease to someone else. We need to evict the lock from our cache and return an error.
if err == LockConditionalUpdateFailedError {
zlog.Debug().Msg("lock is already acquired but expired and conditional check failed")
d.mu.Lock()
delete(d.locks, id)
d.mu.Unlock()
return LockCurrentlyUnavailableError{}
}
zlog.Error().Err(err).Msg("failed to update existing lock")
resultError = *multierror.Append(&resultError, err, LockHeartbeatFailedError)
}
return resultError.ErrorOrNil()
}
func (d *DynamoDBLockClient) Release(ctx context.Context, id string) error {
zlog := d.zlog.With().Str("id", id).Logger()
zlog.Debug().Msg("releasing lock")
existingLock, ok := d.getLocalLock(id)
if !ok {
return LockNotFoundError
}
var resultError multierror.Error
err := d.releaseLock(ctx, existingLock, &zlog)
if err != nil {
d.zlog.Error().Err(err).Msg("failed to delete lock")
resultError = *multierror.Append(&resultError, err, LockReleaseFailedError)
}
return resultError.ErrorOrNil()
}
// getLock returns the lock with the given ID. If the lock is not found, then it returns nil.
func (d *DynamoDBLockClient) getLock(
ctx context.Context,
id string,
) (*Lock, error) {
zlog := d.zlog.With().Str("id", id).Logger()
zlog.Debug().Msg("getting lock")
resp, err := d.Client.GetItem(ctx, &dynamodb.GetItemInput{
TableName: &d.TableName,
Key: map[string]dynamodbtypes.AttributeValue{
"LockID": &dynamodbtypes.AttributeValueMemberS{
Value: id,
},
},
ConsistentRead: aws.Bool(true),
})
if err != nil {
zlog.Error().Err(err).Msg("failed to get lock")
return nil, err
}
zlog.Debug().Interface("resp", resp).Msg("got lock")
if resp.Item == nil {
zlog.Debug().Msg("lock not found")
return nil, nil
}
owner := resp.Item["Owner"].(*dynamodbtypes.AttributeValueMemberS).Value
zlog.Debug().Str("owner", owner).Msg("got owner")
leaseDurationMilliseconds, err := strconv.Atoi(resp.Item["LeaseDurationMilliseconds"].(*dynamodbtypes.AttributeValueMemberN).Value)
if err != nil {
zlog.Error().Err(err).Msg("failed to parse lease duration")
return nil, err
}
zlog.Debug().Int("leaseDurationMilliseconds", leaseDurationMilliseconds).Msg("got lease duration")
lastUpdatedTimeMilliseconds, err := strconv.Atoi(resp.Item["LastUpdatedTimeMilliseconds"].(*dynamodbtypes.AttributeValueMemberN).Value)
if err != nil {
zlog.Error().Err(err).Msg("failed to parse last updated time")
return nil, err
}
zlog.Debug().Int("lastUpdatedTimeMilliseconds", lastUpdatedTimeMilliseconds).Msg("got last updated time")
recordVersionNumber := resp.Item["RecordVersionNumber"].(*dynamodbtypes.AttributeValueMemberS).Value
zlog.Debug().Str("recordVersionNumber", recordVersionNumber).Msg("got record version number")
shard, err := strconv.Atoi(resp.Item["Shard"].(*dynamodbtypes.AttributeValueMemberN).Value)
if err != nil {
zlog.Error().Err(err).Msg("failed to parse shard")
return nil, err
}
ttl, err := strconv.Atoi(resp.Item["TTL"].(*dynamodbtypes.AttributeValueMemberN).Value)
if err != nil {
zlog.Error().Err(err).Msg("failed to parse TTL")
return nil, err
}
zlog.Debug().Int("ttl", ttl).Msg("got TTL")
createdAtMilliseconds, err := strconv.Atoi(resp.Item["CreatedAtMilliseconds"].(*dynamodbtypes.AttributeValueMemberN).Value)
if err != nil {
zlog.Error().Err(err).Msg("failed to parse createdAt")
return nil, err
}
dataSerialized := resp.Item["Data"].(*dynamodbtypes.AttributeValueMemberB).Value
zlog.Debug().Str("dataSerialized", string(dataSerialized)).Msg("got data")
var data interface{}
err = json.Unmarshal(dataSerialized, &data)
if err != nil {
zlog.Error().Err(err).Msg("failed to deserialize data")
return nil, err
}
zlog.Debug().Interface("data", data).Msg("got deserialized data")
newLock := PtrToLock(NewLock(
id,
owner,
int64(leaseDurationMilliseconds),
int64(lastUpdatedTimeMilliseconds),
recordVersionNumber,
int64(shard),
int64(ttl),
int64(createdAtMilliseconds),
data,
))
zlog.Debug().Interface("lock", newLock).Msg("returning new lock")
d.mu.Lock()
defer d.mu.Unlock()
d.locks[id] = *newLock
return newLock, nil
}
func (d *DynamoDBLockClient) updateExistingLock(
ctx context.Context,
existingLock Lock,
newData interface{},
nowMilliseconds int64,
) (*Lock, error) {
zlog := d.zlog.With().Str("id", existingLock.ID).Logger()
leaseDurationMilliseconds := int64(d.Config.LeaseDurationSeconds) * int64(time.Second) / int64(time.Millisecond)
newRecordVersionNumber, err := uuid.NewV7()
if err != nil {
zlog.Error().Err(err).Msg("failed to generate record version number")
return nil, err
}
newTtl := nowMilliseconds/1000 + 10*leaseDurationMilliseconds/1000
newLock := NewLock(
existingLock.ID,
d.Config.Owner,
leaseDurationMilliseconds,
nowMilliseconds,
newRecordVersionNumber.String(),
existingLock.Shard,
newTtl,
existingLock.CreatedAtMilliseconds,
newData,
)
item, err := lockToDynamoDBAttributeValues(newLock)
if err != nil {
zlog.Error().Err(err).Msg("failed to convert lock to dynamodb attribute values")
return nil, err
}
conditionSameRecordVersionNumber := expression.Name("RecordVersionNumber").Equal(expression.Value(existingLock.RecordVersionNumber))
conditionSameOwner := expression.Name("Owner").Equal(expression.Value(existingLock.Owner))
conditionDifferentOwner := expression.Name("Owner").NotEqual(expression.Value(existingLock.Owner))
conditionExpired := expression.Name("LastUpdatedTimeMilliseconds").LessThan(expression.Value(nowMilliseconds - leaseDurationMilliseconds))
condition := conditionSameRecordVersionNumber.And(conditionSameOwner.Or(conditionDifferentOwner.And(conditionExpired)))
builder := expression.NewBuilder()
builder = builder.WithCondition(condition)
expr, err := builder.Build()
if err != nil {
zlog.Error().Err(err).Msg("failed to build expression")
return nil, err
}
_, err = d.Client.PutItem(ctx, &dynamodb.PutItemInput{
TableName: &d.TableName,
Item: item,
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
})
if err != nil {
// If this is a ConditionalCheckFailedException, then the lock was not updated because the condition was not met.
// This is an expected error and means we've lost the lease.
var ccfe *dynamodbtypes.ConditionalCheckFailedException
if errors.As(err, &ccfe) {
zlog.Debug().Err(err).Msg("failed to update lock because condition was not met")
return nil, LockConditionalUpdateFailedError
}
zlog.Error().Err(err).Msg("failed to update lock")
return nil, err
}
d.mu.Lock()
defer d.mu.Unlock()
d.locks[existingLock.ID] = newLock
return &newLock, nil
}
func (d *DynamoDBLockClient) putNewLock(
ctx context.Context,
id string,
data interface{},
nowMilliseconds int64,
) (*Lock, error) {
leaseDurationMilliseconds := int64(d.Config.LeaseDurationSeconds) * int64(time.Second) / int64(time.Millisecond)
recordVersionNumber, err := uuid.NewV7()
if err != nil {
d.zlog.Error().Err(err).Msg("failed to generate record version number")
return nil, err
}
shard := rand.Intn(d.Config.MaxShards)
ttl := nowMilliseconds/1000 + int64(d.Config.LeaseDurationSeconds)*10
lock := NewLock(
id,
d.Config.Owner,
leaseDurationMilliseconds,
nowMilliseconds,
recordVersionNumber.String(),
int64(shard),
ttl,
nowMilliseconds,
data,
)
item, err := lockToDynamoDBAttributeValues(lock)
if err != nil {
d.zlog.Error().Err(err).Msg("failed to convert lock to DynamoDB attribute values")
return nil, err
}
builder := expression.NewBuilder()
builder = builder.WithCondition(expression.Name("LockID").AttributeNotExists())
expr, err := builder.Build()
if err != nil {
d.zlog.Error().Err(err).Msg("failed to build expression")
return nil, err
}
_, err = d.Client.PutItem(ctx, &dynamodb.PutItemInput{
TableName: &d.TableName,
Item: item,
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
})
if err != nil {
d.zlog.Error().Err(err).Msg("failed to put lock")
return nil, err
}
d.mu.Lock()
defer d.mu.Unlock()
d.locks[id] = lock
return PtrToLock(lock), nil
}
func (d *DynamoDBLockClient) releaseLock(
ctx context.Context,
existingLock Lock,
zlog *zerolog.Logger,
) error {
// First release the lock locally. If the remote release fails, the lease will eventually expire and the lock will
// be available again.
d.mu.Lock()
delete(d.locks, existingLock.ID)
d.mu.Unlock()
conditionSameRecordVersionNumber := expression.Name("RecordVersionNumber").Equal(expression.Value(existingLock.RecordVersionNumber))
conditionSameOwner := expression.Name("Owner").Equal(expression.Value(d.Config.Owner))
condition := conditionSameRecordVersionNumber.And(conditionSameOwner)
builder := expression.NewBuilder()
builder = builder.WithCondition(condition)
expr, err := builder.Build()
if err != nil {
d.zlog.Error().Err(err).Msg("failed to build expression")
return err
}
// Delete item from table
_, err = d.Client.DeleteItem(ctx, &dynamodb.DeleteItemInput{
TableName: &d.TableName,
Key: map[string]dynamodbtypes.AttributeValue{
"LockID": &dynamodbtypes.AttributeValueMemberS{Value: existingLock.ID},
},
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
})
if err != nil {
zlog.Error().Err(err).Msg("failed to release lock")
return err
}
return nil
}
func (d *DynamoDBLockClient) getLocalLock(id string) (Lock, bool) {
d.mu.Lock()
defer d.mu.Unlock()
lock, ok := d.locks[id]
d.zlog.Debug().Str("id", id).Interface("lock", lock).Bool("ok", ok).Msg("getLocalLock exit")
return lock, ok
}
func lockToDynamoDBAttributeValues(lock Lock) (map[string]dynamodbtypes.AttributeValue, error) {
serializedData, err := json.Marshal(lock.Data)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return map[string]dynamodbtypes.AttributeValue{
"LockID": &dynamodbtypes.AttributeValueMemberS{
Value: lock.ID,
},
"Owner": &dynamodbtypes.AttributeValueMemberS{
Value: lock.Owner,
},
"LeaseDurationMilliseconds": &dynamodbtypes.AttributeValueMemberN{
Value: strconv.Itoa(int(lock.LeaseDurationMilliseconds)),
},
"LastUpdatedTimeMilliseconds": &dynamodbtypes.AttributeValueMemberN{
Value: strconv.Itoa(int(lock.LastUpdatedTimeMilliseconds)),
},
"RecordVersionNumber": &dynamodbtypes.AttributeValueMemberS{
Value: lock.RecordVersionNumber,
},
"Data": &dynamodbtypes.AttributeValueMemberB{
Value: serializedData,
},
"Shard": &dynamodbtypes.AttributeValueMemberN{
Value: strconv.Itoa(int(lock.Shard)),
},
"TTL": &dynamodbtypes.AttributeValueMemberN{
Value: strconv.Itoa(int(lock.TTLEpochSeconds)),
},
"CreatedAtMilliseconds": &dynamodbtypes.AttributeValueMemberN{
Value: strconv.Itoa(int(lock.CreatedAtMilliseconds)),
},
}, nil
}