This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
BlobLeaseManager.cs
163 lines (132 loc) · 4.65 KB
/
BlobLeaseManager.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reactive.Linq;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace Brisebois.WindowsAzure.Storage.Blobs
{
public class BlobLeaseManager : IDisposable
{
private const string FAILED_ATTEMPT_AT_RELEASING_BLOB_LEASE =
@"|> failed attempt at releasing blob lock on {0} using lease id {1}";
private const string FAILED_TO_ACQUIRE_LEASE = @"|> failed to acquire lease {0}";
private const string REMOVED_LEASE_FROM_ACQUIREDLEASES
= @"|> removed lease from acquired leases for blob {0}";
private const string RENEWED_LEASE = @"|> renewed lease for {0}";
private readonly Dictionary<string, Lease> acquiredLeases = new Dictionary<string, Lease>();
public void Dispose()
{
acquiredLeases.ToList().ForEach(pair => pair.Value.KeepAlive.Dispose());
}
public bool HasLease(CloudBlockBlob blob)
{
return acquiredLeases.ContainsKey(blob.Name);
}
public string GetLeaseId(CloudBlockBlob blob)
{
return HasLease(blob) ? acquiredLeases[blob.Name].LeaseId : string.Empty;
}
public void ReleaseLease(CloudBlockBlob blob)
{
if (!HasLease(blob)) return;
string leaseId = GetLeaseId(blob);
ClearLease(blob);
try
{
blob.ReleaseLease(new AccessCondition
{
LeaseId = leaseId
});
}
catch (StorageException)
{
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, FAILED_ATTEMPT_AT_RELEASING_BLOB_LEASE,
blob.Name,
leaseId));
}
}
public bool TryAcquireLease(CloudBlockBlob blob, double leaseTimeInSeconds)
{
if(leaseTimeInSeconds < 15 || leaseTimeInSeconds > 60)
throw new ArgumentException(@"value must be greater than 15 and smaller than 60",
"leaseTimeInSeconds");
try
{
string proposedLeaseId = Guid.NewGuid().ToString();
var leaseTime = TimeSpan.FromSeconds(leaseTimeInSeconds);
string leaseId = blob.AcquireLease(leaseTime,
proposedLeaseId);
UpdateAcquiredLease(blob, leaseId, leaseTimeInSeconds);
return true;
}
catch (StorageException)
{
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, FAILED_TO_ACQUIRE_LEASE, blob.Name));
return false;
}
}
private void UpdateAcquiredLease(CloudBlockBlob blob,
string leaseId,
double lockTimeInSeconds)
{
var name = blob.Name;
if (IsAcquiredLeaseMissMatched(name, leaseId))
ClearLease(blob);
else
acquiredLeases.Add(name, MakeLease(blob, leaseId, lockTimeInSeconds));
}
private bool IsAcquiredLeaseMissMatched(string name, string leaseId)
{
return acquiredLeases.ContainsKey(name) &&
acquiredLeases[name].LeaseId != leaseId;
}
private void ClearLease(CloudBlockBlob blob)
{
var name = blob.Name;
Lease lease = acquiredLeases[name];
lease.KeepAlive.Dispose();
acquiredLeases.Remove(name);
}
private Lease MakeLease(CloudBlockBlob blob,
string leaseId,
double lockTimeInSeconds)
{
TimeSpan interval = TimeSpan.FromSeconds(lockTimeInSeconds - 1);
return new Lease
{
LeaseId = leaseId,
KeepAlive = Observable.Interval(interval)
.Subscribe(l => RenewLease(blob))
};
}
private bool RenewLease(CloudBlockBlob blob)
{
if (!HasLease(blob))
return false;
var name = blob.Name;
try
{
blob.RenewLease(new AccessCondition
{
LeaseId = acquiredLeases[name].LeaseId
});
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, RENEWED_LEASE, name));
return true;
}
catch (StorageException)
{
acquiredLeases.Remove(name);
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, REMOVED_LEASE_FROM_ACQUIREDLEASES, name));
return false;
}
}
private struct Lease
{
internal string LeaseId { get; set; }
internal IDisposable KeepAlive { get; set; }
}
}
}