-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.go
494 lines (427 loc) · 12.8 KB
/
manager.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
package lease
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
const (
// Table schema
LeaseKeyKey = "leaseKey"
LeaseOwnerKey = "leaseOwner"
LeaseCounterKey = "leaseCounter"
// AWS exception
AlreadyExist = "ResourceInUseException"
ConditionalFailed = "ConditionalCheckFailedException"
// Max number of retries
maxScanRetries = 3
maxCreateRetries = 3
maxUpdateRetries = 2
maxDeleteRetries = 2
// Maximum duration to wait until the table in active state
maxDurationTableStatus = time.Minute * 5
durationBetweenPolls = time.Second * 10
)
// Manager wrap the basic operations for leases.
type Manager interface {
// Creates the table that will store leases if it's not already exists.
CreateLeaseTable() error
// List all leases(objects) in table.
ListLeases() ([]*Lease, error)
// Renew a lease
RenewLease(*Lease) error
// Take a lease
TakeLease(*Lease) error
// Evict a lease
EvictLease(*Lease) error
// Delete a lease
DeleteLease(*Lease) error
// Create a lease
CreateLease(*Lease) (*Lease, error)
// Update a lease
UpdateLease(*Lease) (*Lease, error)
}
// LeaseManager is the default implemntation of Manager
// that uses DynamoDB.
type LeaseManager struct {
*Config
Serializer Serializer
}
// CreateLeaseTable creates the table that will store the leases. succeeds
// if it's already exists.
func (l *LeaseManager) CreateLeaseTable() (err error) {
for l.Backoff.Attempt() < maxCreateRetries {
_, err = l.Client.CreateTable(&dynamodb.CreateTableInput{
TableName: aws.String(l.LeaseTable),
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String(LeaseKeyKey),
AttributeType: aws.String(dynamodb.ScalarAttributeTypeS),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String(LeaseKeyKey),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(int64(l.LeaseTableReadCap)),
WriteCapacityUnits: aws.Int64(int64(l.LeaseTableWriteCap)),
},
})
// if the operation finished successfully, we need to "wait" until
// the lease table exists and active.
if err == nil {
l.Logger.WithField("table name", l.LeaseTable).Debugf("Worker %s creates the lease table and "+
"wait maximum %s until it will be %q",
l.WorkerId,
maxDurationTableStatus,
dynamodb.TableStatusActive)
duration := maxDurationTableStatus
for {
success := false
if status, ok := l.tableStatus(); ok && status == dynamodb.TableStatusActive {
success = true
}
if success || duration == 0 {
l.Logger.WithFields(logrus.Fields{
"success": success,
"table name": l.LeaseTable,
"time taken": maxDurationTableStatus - duration,
}).Debugf("Worker %s stop waiting for table creation", l.WorkerId)
break
}
time.Sleep(durationBetweenPolls)
duration -= durationBetweenPolls
}
break
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == AlreadyExist {
err = nil
break
}
backoff := l.Backoff.Duration()
l.Logger.WithFields(logrus.Fields{
"backoff": backoff,
"attempt": int(l.Backoff.Attempt()),
}).Warnf("Worker %s failed to create table", l.WorkerId)
time.Sleep(backoff)
}
l.Backoff.Reset()
return
}
// tableStatus returns the "status" of the table, and boolean
// that indicates if the operation success.
//
// The status could be: "CREATING", "UPDATING", "DELETING" or "ACTIVE"
func (l *LeaseManager) tableStatus() (string, bool) {
resp, err := l.Client.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(l.LeaseTable),
})
if err != nil {
return "", false
}
return *resp.Table.TableStatus, true
}
// Renew a lease by incrementing the lease counter.
// Conditional on the leaseCounter in DynamoDB matching the leaseCounter of the input
// Mutates the leaseCounter of the passed-in lease object after updating the record in DynamoDB.
func (l *LeaseManager) RenewLease(lease *Lease) (err error) {
clease := *lease
clease.Counter++
if err = l.condUpdate(clease, *lease); err == nil {
lease.Counter = clease.Counter
}
return
}
// Evict the current owner of lease by setting owner to null
// Conditional on the owner in DynamoDB matching the owner of the input.
// Mutates the lease owner of the passed-in lease object after updating the record in DynamoDB.
func (l *LeaseManager) EvictLease(lease *Lease) (err error) {
clease := *lease
clease.Owner = "NULL"
if err = l.condUpdate(clease, *lease); err == nil {
lease.Owner = clease.Owner
}
return
}
// Take a lease by incrementing its leaseCounter and setting its owner field.
// Conditional on the leaseCounter in DynamoDB matching the leaseCounter of the input
// Mutates the lease counter and owner of the passed-in lease object after updating the record in DynamoDB.
func (l *LeaseManager) TakeLease(lease *Lease) (err error) {
clease := *lease
clease.Counter++
clease.Owner = l.WorkerId
if err = l.condUpdate(clease, *lease); err == nil {
lease.Owner = clease.Owner
lease.Counter = clease.Counter
}
return
}
// ListLeasses returns all the lease units stored in the table.
func (l *LeaseManager) ListLeases() (list []*Lease, err error) {
var res *dynamodb.ScanOutput
for l.Backoff.Attempt() < maxScanRetries {
res, err = l.Client.Scan(&dynamodb.ScanInput{
TableName: aws.String(l.LeaseTable),
})
if err != nil {
backoff := l.Backoff.Duration()
l.Logger.WithFields(logrus.Fields{
"backoff": backoff,
"attempt": int(l.Backoff.Attempt()),
}).Warnf("Worker %s failed to scan leases table", l.WorkerId)
time.Sleep(backoff)
continue
}
for _, item := range res.Items {
if lease, err := l.Serializer.Decode(item); err != nil {
l.Logger.WithError(err).Error("decode lease")
} else {
list = append(list, lease)
}
}
break
}
l.Backoff.Reset()
return
}
// Delete the given lease from DynamoDB. does nothing when passed a
// lease that does not exist in DynamoDB.
func (l *LeaseManager) DeleteLease(lease *Lease) (err error) {
for l.Backoff.Attempt() < maxDeleteRetries {
_, err = l.Client.DeleteItem(&dynamodb.DeleteItemInput{
TableName: aws.String(l.LeaseTable),
Key: map[string]*dynamodb.AttributeValue{
LeaseKeyKey: {
S: aws.String(lease.Key),
},
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":condOwner": {
S: aws.String(lease.Owner),
},
},
ExpressionAttributeNames: map[string]*string{
"#owner": aws.String(LeaseOwnerKey),
"#key": aws.String(LeaseKeyKey),
},
ConditionExpression: aws.String("attribute_not_exists(#key) OR #owner = :condOwner"),
})
if err == nil {
break
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == ConditionalFailed {
break
}
backoff := l.Backoff.Duration()
l.Logger.WithFields(logrus.Fields{
"backoff": backoff,
"attempt": int(l.Backoff.Attempt()),
}).Warnf("Worker %s failed to delete lease", l.WorkerId)
time.Sleep(backoff)
}
l.Backoff.Reset()
return
}
// Create a new lease. conditional on a lease not already existing with different
// owner and counter.
func (l *LeaseManager) CreateLease(lease *Lease) (*Lease, error) {
if lease.Owner == "" {
lease.Owner = l.WorkerId
}
if lease.Counter == 0 {
lease.Counter++
}
item, err := l.Serializer.Encode(lease)
if err != nil {
return lease, err
}
for l.Backoff.Attempt() < maxCreateRetries {
_, err = l.Client.PutItem(&dynamodb.PutItemInput{
TableName: aws.String(l.LeaseTable),
Item: item,
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":condOwner": {
S: aws.String(lease.Owner),
},
":condCounter": {
N: aws.String(strconv.Itoa(lease.Counter)),
},
},
ExpressionAttributeNames: map[string]*string{
"#counter": aws.String(LeaseCounterKey),
"#owner": aws.String(LeaseOwnerKey),
"#key": aws.String(LeaseKeyKey),
},
ConditionExpression: aws.String("attribute_not_exists(#key) OR #counter = :condCounter AND #owner = :condOwner"),
})
if err == nil {
break
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == ConditionalFailed {
break
}
backoff := l.Backoff.Duration()
l.Logger.WithFields(logrus.Fields{
"backoff": backoff,
"attempt": int(l.Backoff.Attempt()),
}).Warnf("Worker %s failed to create lease", l.WorkerId)
time.Sleep(backoff)
}
l.Backoff.Reset()
if err != nil {
return nil, err
}
// the ReturnValues argument can only be ALL_OLD or NONE, it means that
// our lease object is the most updated.
return lease, nil
}
// UpdateLease used to update only the extra fields on the Lease object.
// With this method you will be able to update the task status, or any
// other fields.
// for example: {"status": "done", "last_update": "unix seconds"}
// To add extra fields on a Lease, use Lease.Set(key, val)
func (l *LeaseManager) UpdateLease(lease *Lease) (*Lease, error) {
var (
attExp string
attVal map[string]*dynamodb.AttributeValue
isReserved = func(w string) bool { return w == LeaseKeyKey || w == LeaseOwnerKey || w == LeaseCounterKey }
)
// set fields
if len(lease.extrafields) > 0 || len(lease.explicitfields) > 0 {
item, err := l.Serializer.Encode(lease)
if err != nil {
return lease, err
}
setExp := make([]string, 0)
for k, v := range item {
if !isReserved(k) {
// if it's the first time we add entry to the map
if attVal == nil {
attVal = make(map[string]*dynamodb.AttributeValue)
}
setExp = append(setExp, fmt.Sprintf("%s = :%s", k, k))
attVal[":"+k] = v
}
}
if len(setExp) > 0 {
attExp += "SET " + strings.Join(setExp, ", ")
}
}
// remove fields
if len(lease.removedfields) > 0 {
rmExp := make([]string, 0)
for _, f := range lease.removedfields {
if !isReserved(f) {
rmExp = append(rmExp, f)
}
}
if len(rmExp) > 0 {
attExp += " REMOVE " + strings.Join(rmExp, ", ")
}
}
// if there's nothing to update
if attExp == "" {
return lease, nil
}
return l.updateLease(&dynamodb.UpdateItemInput{
TableName: aws.String(l.LeaseTable),
Key: map[string]*dynamodb.AttributeValue{
LeaseKeyKey: {
S: aws.String(lease.Key),
},
},
UpdateExpression: aws.String(attExp),
ExpressionAttributeValues: attVal,
ReturnValues: aws.String(dynamodb.ReturnValueAllNew),
})
}
// condLease gets a 2 Lease objects. the first one is for the update attributes
// and the second used to construct the condition expression.
func (l *LeaseManager) condUpdate(updateLease, condLease Lease) (err error) {
updateInput := &dynamodb.UpdateItemInput{
TableName: aws.String(l.LeaseTable),
Key: map[string]*dynamodb.AttributeValue{
LeaseKeyKey: {
S: aws.String(updateLease.Key),
},
},
ReturnValues: aws.String(dynamodb.ReturnValueAllNew),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":owner": {
S: aws.String(updateLease.Owner),
},
":count": {
N: aws.String(strconv.Itoa(updateLease.Counter)),
},
},
UpdateExpression: aws.String(fmt.Sprintf(
"SET %s = :owner, %s = :count",
LeaseOwnerKey,
LeaseCounterKey,
)),
}
// add conditions only to veteran leases
var (
condExp string
attrExp = make(map[string]*string)
)
if condLease.Counter > 0 {
updateInput.ExpressionAttributeValues[":condCounter"] = &dynamodb.AttributeValue{
N: aws.String(strconv.Itoa(condLease.Counter)),
}
attrExp["#counter"] = aws.String(LeaseCounterKey)
condExp = ":condCounter = #counter"
}
if condLease.Owner != "" {
updateInput.ExpressionAttributeValues[":condOwner"] = &dynamodb.AttributeValue{
S: aws.String(condLease.Owner),
}
attrExp["#owner"] = aws.String(LeaseOwnerKey)
if condExp != "" {
condExp += " AND "
}
condExp += ":condOwner = #owner"
}
if condExp != "" {
updateInput.ExpressionAttributeNames = attrExp
updateInput.ConditionExpression = aws.String(condExp)
}
_, err = l.updateLease(updateInput)
return
}
// updateLease gets updateInput and call Client.Update with the retries logic.
// use this method to reduce duplicate code.
// if the operation success we serialize the response and return the result.
func (l *LeaseManager) updateLease(input *dynamodb.UpdateItemInput) (*Lease, error) {
var (
err error
out *dynamodb.UpdateItemOutput
)
for l.Backoff.Attempt() < maxUpdateRetries {
out, err = l.Client.UpdateItem(input)
if err == nil {
break
}
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == ConditionalFailed {
break
}
backoff := l.Backoff.Duration()
l.Logger.WithFields(logrus.Fields{
"backoff": backoff,
"attempt": int(l.Backoff.Attempt()),
}).Warnf("Worker %s failed to update lease", l.WorkerId)
time.Sleep(backoff)
}
l.Backoff.Reset()
if err != nil {
return nil, err
}
return l.Serializer.Decode(out.Attributes)
}