This repository has been archived by the owner on Apr 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Lock.cs
742 lines (670 loc) · 28.5 KB
/
Lock.cs
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
using System;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace Consul
{
#if !(CORECLR || PORTABLE || PORTABLE40)
[Serializable]
#endif
public class LockHeldException : Exception
{
public LockHeldException()
{
}
public LockHeldException(string message)
: base(message)
{
}
public LockHeldException(string message, Exception inner)
: base(message, inner)
{
}
#if !(CORECLR || PORTABLE || PORTABLE40)
protected LockHeldException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
#endif
}
#if !(CORECLR || PORTABLE || PORTABLE40)
[Serializable]
#endif
public class LockNotHeldException : Exception
{
public LockNotHeldException()
{
}
public LockNotHeldException(string message)
: base(message)
{
}
public LockNotHeldException(string message, Exception inner)
: base(message, inner)
{
}
#if !(CORECLR || PORTABLE || PORTABLE40)
protected LockNotHeldException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
#endif
}
#if !(CORECLR || PORTABLE || PORTABLE40)
[Serializable]
#endif
public class LockInUseException : Exception
{
public LockInUseException()
{
}
public LockInUseException(string message)
: base(message)
{
}
public LockInUseException(string message, Exception inner)
: base(message, inner)
{
}
#if !(CORECLR || PORTABLE || PORTABLE40)
protected LockInUseException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
#endif
}
#if !(CORECLR || PORTABLE || PORTABLE40)
[Serializable]
#endif
public class LockConflictException : Exception
{
public LockConflictException()
{
}
public LockConflictException(string message)
: base(message)
{
}
public LockConflictException(string message, Exception inner)
: base(message, inner)
{
}
#if !(CORECLR || PORTABLE || PORTABLE40)
protected LockConflictException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
#endif
}
#if !(CORECLR || PORTABLE || PORTABLE40)
[Serializable]
#endif
public class LockMaxAttemptsReachedException : Exception
{
public LockMaxAttemptsReachedException() { }
public LockMaxAttemptsReachedException(string message) : base(message) { }
public LockMaxAttemptsReachedException(string message, Exception inner) : base(message, inner) { }
#if !(CORECLR || PORTABLE || PORTABLE40)
protected LockMaxAttemptsReachedException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
#endif
}
/// <summary>
/// Lock is used to implement client-side leader election. It is follows the algorithm as described here: https://consul.io/docs/guides/leader-election.html.
/// </summary>
public class Lock : IDistributedLock
{
/// <summary>
/// DefaultLockWaitTime is how long we block for at a time to check if lock acquisition is possible. This affects the minimum time it takes to cancel a Lock acquisition.
/// </summary>
public static readonly TimeSpan DefaultLockWaitTime = TimeSpan.FromSeconds(15);
/// <summary>
/// DefaultLockRetryTime is how long we wait after a failed lock acquisition before attempting
/// to do the lock again. This is so that once a lock-delay is in effect, we do not hot loop
/// retrying the acquisition.
/// </summary>
public static readonly TimeSpan DefaultLockRetryTime = TimeSpan.FromSeconds(5);
/// <summary>
/// DefaultMonitorRetryTime is how long we wait after a failed monitor check
/// of a lock (500 response code). This allows the monitor to ride out brief
/// periods of unavailability, subject to the MonitorRetries setting in the
/// lock options which is by default set to 0, disabling this feature.
/// </summary>
public static readonly TimeSpan DefaultMonitorRetryTime = TimeSpan.FromSeconds(2);
/// <summary>
/// LockFlagValue is a magic flag we set to indicate a key is being used for a lock. It is used to detect a potential conflict with a semaphore.
/// </summary>
private const ulong LockFlagValue = 0x2ddccbc058a50c18;
private readonly AsyncLock _mutex = new AsyncLock();
private bool _isheld;
private int _retries;
private CancellationTokenSource _cts;
private Task _sessionRenewTask;
private Task _monitorTask;
private readonly ConsulClient _client;
internal LockOptions Opts { get; set; }
internal string LockSession { get; set; }
/// <summary>
/// If the lock is held or not.
/// Users of the Lock object should check the IsHeld property before entering the critical section of their code, e.g. in a "while (myLock.IsHeld) {criticalsection}" block.
/// Calls to IsHeld are syncronized across threads using a lock, so multiple threads sharing a single Consul Lock will queue up reading the IsHeld property of the lock.
/// </summary>
public bool IsHeld
{
get
{
return _isheld;
}
private set
{
_isheld = value;
}
}
internal Lock(ConsulClient c)
{
_client = c;
_cts = new CancellationTokenSource();
}
/// <summary>
/// Lock attempts to acquire the lock and blocks while doing so. Not providing a CancellationToken means the thread can block indefinitely until the lock is acquired.
/// There is no notification that the lock has been lost, but it may be closed at any time due to session invalidation, communication errors, operator intervention, etc.
/// It is NOT safe to assume that the lock is held until Unlock() unless the Session is specifically created without any associated health checks.
/// Users of the Lock object should check the IsHeld property before entering the critical section of their code, e.g. in a "while (myLock.IsHeld) {criticalsection}" block.
/// By default Consul sessions prefer liveness over safety and an application must be able to handle the lock being lost.
/// </summary>
public Task<CancellationToken> Acquire()
{
return Acquire(CancellationToken.None);
}
/// <summary>
/// Lock attempts to acquire the lock and blocks while doing so.
/// Providing a CancellationToken can be used to abort the lock attempt.
/// There is no notification that the lock has been lost, but IsHeld may be set to False at any time due to session invalidation, communication errors, operator intervention, etc.
/// It is NOT safe to assume that the lock is held until Unlock() unless the Session is specifically created without any associated health checks.
/// Users of the Lock object should check the IsHeld property before entering the critical section of their code, e.g. in a "while (myLock.IsHeld) {criticalsection}" block.
/// By default Consul sessions prefer liveness over safety and an application must be able to handle the lock being lost.
/// </summary>
/// <param name="ct">The cancellation token to cancel lock acquisition</param>
public async Task<CancellationToken> Acquire(CancellationToken ct)
{
try
{
using (await _mutex.LockAsync().ConfigureAwait(false))
{
if (IsHeld)
{
// Check if we already hold the lock
throw new LockHeldException();
}
// Don't overwrite the CancellationTokenSource until AFTER we've tested for holding,
// since there might be tasks that are currently running for this lock.
DisposeCancellationTokenSource();
_cts = new CancellationTokenSource();
// Check if we need to create a session first
if (string.IsNullOrEmpty(Opts.Session))
{
LockSession = await CreateSession().ConfigureAwait(false);
_sessionRenewTask = _client.Session.RenewPeriodic(Opts.SessionTTL, LockSession,
WriteOptions.Default, _cts.Token);
}
else
{
LockSession = Opts.Session;
}
var qOpts = new QueryOptions()
{
WaitTime = Opts.LockWaitTime
};
var attempts = 0;
var start = DateTime.UtcNow;
while (!ct.IsCancellationRequested)
{
if (attempts > 0 && Opts.LockTryOnce)
{
var elapsed = DateTime.UtcNow.Subtract(start);
if (elapsed > qOpts.WaitTime)
{
DisposeCancellationTokenSource();
throw new LockMaxAttemptsReachedException("LockTryOnce is set and the lock is already held or lock delay is in effect");
}
qOpts.WaitTime -= elapsed;
}
attempts++;
QueryResult<KVPair> pair;
pair = await _client.KV.Get(Opts.Key, qOpts).ConfigureAwait(false);
if (pair.Response != null)
{
if (pair.Response.Flags != LockFlagValue)
{
DisposeCancellationTokenSource();
throw new LockConflictException();
}
// Already locked by this session
if (pair.Response.Session == LockSession)
{
// Don't restart MonitorLock if this session already holds the lock
if (IsHeld)
{
return _cts.Token;
}
IsHeld = true;
_monitorTask = MonitorLock();
return _cts.Token;
}
// If it's not empty, some other session must have the lock
if (!string.IsNullOrEmpty(pair.Response.Session))
{
qOpts.WaitIndex = pair.LastIndex;
continue;
}
}
// If the code executes this far, no other session has the lock, so try to lock it
var kvPair = LockEntry(LockSession);
var locked = (await _client.KV.Acquire(kvPair).ConfigureAwait(false)).Response;
// KV acquisition succeeded, so the session now holds the lock
if (locked)
{
IsHeld = true;
_monitorTask = MonitorLock();
return _cts.Token;
}
// Handle the case of not getting the lock
if (ct.IsCancellationRequested)
{
DisposeCancellationTokenSource();
throw new TaskCanceledException();
}
// Failed to get the lock, determine why by querying for the key again
qOpts.WaitIndex = 0;
pair = await _client.KV.Get(Opts.Key, qOpts).ConfigureAwait(false);
// If the session is not null, this means that a wait can safely happen using a long poll
if (pair.Response != null && pair.Response.Session != null)
{
qOpts.WaitIndex = pair.LastIndex;
continue;
}
// If the session is null and the lock failed to acquire, then it means
// a lock-delay is in effect and a timed wait must be used to avoid a hot loop.
try { await Task.Delay(Opts.LockRetryTime, ct).ConfigureAwait(false); }
catch (TaskCanceledException) { /* Ignore TaskTaskCanceledException */}
}
DisposeCancellationTokenSource();
throw new LockNotHeldException("Unable to acquire the lock with Consul");
}
}
finally
{
if (ct.IsCancellationRequested || (!IsHeld && !string.IsNullOrEmpty(Opts.Session)))
{
DisposeCancellationTokenSource();
if (_sessionRenewTask != null)
{
try
{
await _monitorTask.ConfigureAwait(false);
await _sessionRenewTask.ConfigureAwait(false);
}
catch (AggregateException)
{
// Ignore AggregateExceptions from the tasks during Release, since if the Renew task died, the developer will be Super Confused if they see the exception during Release.
}
}
}
}
}
/// <summary>
/// Unlock released the lock. It is an error to call this if the lock is not currently held.
/// </summary>
public async Task Release(CancellationToken ct = default(CancellationToken))
{
try
{
using (await _mutex.LockAsync().ConfigureAwait(false))
{
if (!IsHeld)
{
throw new LockNotHeldException();
}
IsHeld = false;
DisposeCancellationTokenSource();
var lockEnt = LockEntry(LockSession);
await _client.KV.Release(lockEnt, ct).ConfigureAwait(false);
}
}
finally
{
if (_sessionRenewTask != null)
{
try
{
await _sessionRenewTask.ConfigureAwait(false);
}
catch (Exception)
{
// Ignore Exceptions from the tasks during Release, since if the Renew task died, the developer will be Super Confused if they see the exception during Release.
}
}
}
}
/// <summary>
/// Destroy is used to cleanup the lock entry. It is not necessary to invoke. It will fail if the lock is in use.
/// </summary>
public async Task Destroy(CancellationToken ct = default(CancellationToken))
{
using (await _mutex.LockAsync().ConfigureAwait(false))
{
if (IsHeld)
{
throw new LockHeldException();
}
var pair = (await _client.KV.Get(Opts.Key, ct).ConfigureAwait(false)).Response;
if (pair == null)
{
return;
}
if (pair.Flags != LockFlagValue)
{
throw new LockConflictException();
}
if (!string.IsNullOrEmpty(pair.Session))
{
throw new LockInUseException();
}
var didRemove = (await _client.KV.DeleteCAS(pair, ct).ConfigureAwait(false)).Response;
if (!didRemove)
{
throw new LockInUseException();
}
}
}
private void DisposeCancellationTokenSource()
{
// Make a copy of the reference to the CancellationTokenSource in case it gets removed before we finish.
// It's okay to cancel and dispose of them twice, it doesn't cause exceptions.
var cts = _cts;
if (cts != null)
{
Interlocked.CompareExchange(ref _cts, null, cts);
cts.Cancel();
cts.Dispose();
}
}
/// <summary>
/// MonitorLock is a long running routine to monitor a lock ownership. It sets IsHeld to false if we lose our leadership.
/// </summary>
private Task MonitorLock()
{
return Task.Factory.StartNew(async () => {
// Copy a reference to _cts since we could end up destroying it before this method returns
var cts = _cts;
try
{
var opts = new QueryOptions() { Consistency = ConsistencyMode.Consistent };
_retries = Opts.MonitorRetries;
while (IsHeld && !cts.Token.IsCancellationRequested)
{
try
{
// Check to see if the current session holds the lock
var pair = await _client.KV.Get(Opts.Key, opts).ConfigureAwait(false);
if (pair.Response != null)
{
_retries = Opts.MonitorRetries;
// Lock is no longer held! Shut down everything.
if (pair.Response.Session != LockSession)
{
IsHeld = false;
DisposeCancellationTokenSource();
return;
}
// Lock is still held, start a blocking query
opts.WaitIndex = pair.LastIndex;
continue;
}
else
{
// Failsafe in case the KV store is unavailable
IsHeld = false;
DisposeCancellationTokenSource();
return;
}
}
catch (ConsulRequestException)
{
if (_retries > 0)
{
await Task.Delay(Opts.MonitorRetryTime, cts.Token).ConfigureAwait(false);
_retries--;
opts.WaitIndex = 0;
continue;
}
throw;
}
catch (OperationCanceledException)
{
// Ignore and retry since this could be the underlying HTTPClient being swapped out/disposed of.
}
catch (Exception)
{
throw;
}
}
}
finally
{
IsHeld = false;
DisposeCancellationTokenSource();
}
}, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap();
}
/// <summary>
/// CreateSession is used to create a new managed session
/// </summary>
/// <returns>The session ID</returns>
private async Task<string> CreateSession()
{
var se = new SessionEntry
{
Name = Opts.SessionName,
TTL = Opts.SessionTTL
};
return (await _client.Session.Create(se).ConfigureAwait(false)).Response;
}
/// <summary>
/// LockEntry returns a formatted KVPair for the lock
/// </summary>
/// <param name="session">The session ID</param>
/// <returns>A KVPair with the lock flag set</returns>
private KVPair LockEntry(string session)
{
return new KVPair(Opts.Key)
{
Value = Opts.Value,
Session = session,
Flags = LockFlagValue
};
}
}
/// <summary>
/// LockOptions is used to parameterize the Lock behavior.
/// </summary>
public class LockOptions
{
/// <summary>
/// DefaultLockSessionName is the Session Name we assign if none is provided
/// </summary>
private const string DefaultLockSessionName = "Consul API Lock";
private static readonly TimeSpan LockRetryTimeMin = TimeSpan.FromMilliseconds(500);
/// <summary>
/// DefaultLockSessionTTL is the default session TTL if no Session is provided when creating a new Lock. This is used because we do not have another other check to depend upon.
/// </summary>
private static readonly TimeSpan DefaultLockSessionTTL = TimeSpan.FromSeconds(15);
private TimeSpan _lockRetryTime;
public string Key { get; set; }
public byte[] Value { get; set; }
public string Session { get; set; }
public string SessionName { get; set; }
public TimeSpan SessionTTL { get; set; }
public int MonitorRetries { get; set; }
public TimeSpan LockRetryTime
{
get { return _lockRetryTime; }
set
{
if (value < LockRetryTimeMin)
{
throw new ArgumentOutOfRangeException(nameof(LockRetryTime), $"The retry time must be greater than {LockRetryTimeMin.ToGoDuration()}.");
}
_lockRetryTime = value;
}
}
public TimeSpan LockWaitTime { get; set; }
public TimeSpan MonitorRetryTime { get; set; }
public bool LockTryOnce { get; set; }
public LockOptions(string key)
{
Key = key;
SessionName = DefaultLockSessionName;
SessionTTL = DefaultLockSessionTTL;
MonitorRetryTime = Lock.DefaultMonitorRetryTime;
LockWaitTime = Lock.DefaultLockWaitTime;
LockRetryTime = Lock.DefaultLockRetryTime;
}
}
public partial class ConsulClient : IConsulClient
{
/// <summary>
/// CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public IDistributedLock CreateLock(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
return CreateLock(new LockOptions(key));
}
/// <summary>
/// CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions.
/// </summary>
/// <param name="opts"></param>
/// <returns></returns>
public IDistributedLock CreateLock(LockOptions opts)
{
if (opts == null)
{
throw new ArgumentNullException(nameof(opts));
}
return new Lock(this) { Opts = opts };
}
/// <summary>
/// AcquireLock creates a lock that is already acquired when this call returns.
/// </summary>
/// <param name="key"></param>
/// <param name="ct"></param>
/// <returns></returns>
public Task<IDistributedLock> AcquireLock(string key, CancellationToken ct = default(CancellationToken))
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
return AcquireLock(new LockOptions(key), ct);
}
/// <summary>
/// AcquireLock creates a lock that is already acquired when this call returns.
/// </summary>
/// <param name="opts"></param>
/// <param name="ct"></param>
/// <returns></returns>
public async Task<IDistributedLock> AcquireLock(LockOptions opts, CancellationToken ct = default(CancellationToken))
{
if (opts == null)
{
throw new ArgumentNullException(nameof(opts));
}
var l = CreateLock(opts);
await l.Acquire(ct).ConfigureAwait(false);
return l;
}
/// <summary>
/// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
/// </summary>
/// <param name="key"></param>
/// <param name="action"></param>
/// <returns></returns>
public Task ExecuteLocked(string key, Action action, CancellationToken ct = default(CancellationToken))
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
return ExecuteLocked(new LockOptions(key), action, ct);
}
/// <summary>
/// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
/// </summary>
/// <param name="opts"></param>
/// <param name="action"></param>
/// <returns></returns>
public async Task ExecuteLocked(LockOptions opts, Action action, CancellationToken ct = default(CancellationToken))
{
if (opts == null)
{
throw new ArgumentNullException(nameof(opts));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
var l = await AcquireLock(opts, ct).ConfigureAwait(false);
try
{
if (!l.IsHeld)
{
throw new LockNotHeldException("Could not obtain the lock");
}
action();
}
finally
{
await l.Release().ConfigureAwait(false);
}
}
/// <summary>
/// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
/// </summary>
/// <param name="key"></param>
/// <param name="ct"></param>
/// <param name="action"></param>
/// <returns></returns>
[Obsolete("This method will be removed in 0.8.0. Replace calls with the method signature ExecuteLocked(string, Action, CancellationToken)")]
public Task ExecuteLocked(string key, CancellationToken ct, Action action)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
return ExecuteLocked(new LockOptions(key), action, ct);
}
/// <summary>
/// ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
/// </summary>
/// <param name="opts"></param>
/// <param name="ct"></param>
/// <param name="action"></param>
/// <returns></returns>
[Obsolete("This method will be removed in 0.8.0. Replace calls with the method signature ExecuteLocked(LockOptions, Action, CancellationToken)")]
public Task ExecuteLocked(LockOptions opts, CancellationToken ct, Action action)
{
if (opts == null)
{
throw new ArgumentNullException(nameof(opts));
}
return ExecuteLocked(opts, action, ct);
}
}
}