forked from coinbase/step
/
lock.go
99 lines (84 loc) · 2.71 KB
/
lock.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
package dynamodb
import (
"fmt"
"time"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
stepaws "github.com/cleardataeng/step/aws"
)
var (
columnKey = "key"
columnId = "id"
columnTime = "time"
)
type DynamoDBLocker struct {
client stepaws.DynamoDBAPI
}
func NewDynamoDBLocker(client stepaws.DynamoDBAPI) *DynamoDBLocker {
return &DynamoDBLocker{client}
}
func (l *DynamoDBLocker) GrabLock(namespace string, lockPath string, uuid string, reason string) (bool, error) {
// Construct a conditional expression such that we only allow a new lock
// to be created if there is not already one for the same key.
condExp := expression.Name(columnKey).AttributeNotExists()
condExp = condExp.Or(expression.Name(columnId).Equal(expression.Value(uuid)))
expr, err := expression.NewBuilder().WithCondition(condExp).Build()
if err != nil {
return false, err
}
// Attempt to create a lock
_, err = l.client.PutItem(&dynamodb.PutItemInput{
TableName: awssdk.String(namespace),
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
Item: map[string]*dynamodb.AttributeValue{
columnKey: {
S: awssdk.String(lockPath),
},
columnId: {
S: awssdk.String(uuid),
},
columnTime: {
S: awssdk.String(time.Now().Format(time.RFC3339)),
},
},
})
if err != nil {
awsErr, ok := err.(awserr.Error)
// A lock already exists for the same key.
if ok && awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
return false, nil
}
return false, err
}
return true, nil
}
func (l *DynamoDBLocker) ReleaseLock(namespace string, lockPath string, uuid string) error {
// Construct a condition expression such that we only allow a lock
// to be deleted if the key, and the UUID aligns.
condExp := expression.Name(columnId).Equal(expression.Value(uuid))
expr, err := expression.NewBuilder().WithCondition(condExp).Build()
if err != nil {
return err
}
// Attempt to delete lock
_, err = l.client.DeleteItem(&dynamodb.DeleteItemInput{
TableName: awssdk.String(namespace),
ConditionExpression: expr.Condition(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
Key: map[string]*dynamodb.AttributeValue{
columnKey: {
S: awssdk.String(lockPath),
},
},
})
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == dynamodb.ErrCodeConditionalCheckFailedException {
// A lock already exists, but with a different UUID.
return fmt.Errorf("Lock was stolen for release with UUID(%v)", uuid)
}
return err
}