1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RemiSan\Lock\Locker; |
4
|
|
|
|
5
|
|
|
use RemiSan\Lock\LockStore; |
6
|
|
|
use RemiSan\Lock\Exceptions\LockingException; |
7
|
|
|
use RemiSan\Lock\Exceptions\UnlockingException; |
8
|
|
|
use RemiSan\Lock\Lock; |
9
|
|
|
use RemiSan\Lock\Locker; |
10
|
|
|
use RemiSan\Lock\Quorum; |
11
|
|
|
use RemiSan\Lock\TokenGenerator; |
12
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
13
|
|
|
|
14
|
|
|
final class MultipleStoreLocker implements Locker |
15
|
|
|
{ |
16
|
|
|
/** @var LockStore[] */ |
17
|
|
|
private $stores = []; |
18
|
|
|
|
19
|
|
|
/** @var TokenGenerator */ |
20
|
|
|
private $tokenGenerator; |
21
|
|
|
|
22
|
|
|
/** @var Quorum */ |
23
|
|
|
private $quorum; |
24
|
|
|
|
25
|
|
|
/** @var Stopwatch */ |
26
|
|
|
private $stopwatch; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* RedLock constructor. |
30
|
|
|
* |
31
|
|
|
* @param LockStore[] $stores Array of persistence system connections |
32
|
|
|
* @param TokenGenerator $tokenGenerator The token generator |
33
|
|
|
* @param Quorum $quorum The quorum implementation to use |
34
|
|
|
* @param Stopwatch $stopwatch A way to measure time passed |
35
|
|
|
*/ |
36
|
36 |
|
public function __construct( |
37
|
|
|
array $stores, |
38
|
|
|
TokenGenerator $tokenGenerator, |
39
|
|
|
Quorum $quorum, |
40
|
|
|
Stopwatch $stopwatch |
41
|
|
|
) { |
42
|
36 |
|
$this->setStores($stores); |
43
|
36 |
|
$this->setQuorum($quorum); |
44
|
|
|
|
45
|
36 |
|
$this->tokenGenerator = $tokenGenerator; |
46
|
36 |
|
$this->stopwatch = $stopwatch; |
47
|
36 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
12 |
|
public function lock($resource, $ttl = null, $retryDelay = 0, $retryCount = 0) |
53
|
|
|
{ |
54
|
12 |
|
$lock = new Lock((string) $resource, $this->tokenGenerator->generateToken()); |
55
|
|
|
|
56
|
12 |
|
$tried = 0; |
57
|
12 |
|
while (true) { |
58
|
|
|
try { |
59
|
12 |
|
return $this->lockAndCheckOnAllStores($lock, $ttl); |
60
|
6 |
|
} catch (LockingException $e) { |
61
|
6 |
|
$this->resetLock($lock); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
if ($tried++ === $retryCount) { |
65
|
6 |
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$this->waitBeforeRetrying($retryDelay); |
69
|
4 |
|
} |
70
|
|
|
|
71
|
6 |
|
throw new LockingException('Failed locking the resource.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
9 |
|
public function isLocked($resource) |
78
|
|
|
{ |
79
|
9 |
|
foreach ($this->stores as $store) { |
80
|
9 |
|
if ($store->exists((string) $resource)) { |
81
|
7 |
|
return true; |
82
|
|
|
} |
83
|
4 |
|
} |
84
|
|
|
|
85
|
3 |
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
12 |
|
public function unlock(Lock $lock) |
92
|
|
|
{ |
93
|
12 |
|
foreach ($this->stores as $store) { |
94
|
12 |
|
if (!$store->delete($lock)) { |
95
|
9 |
|
if ($store->exists($lock->getResource())) { |
96
|
|
|
// Only throw an exception if the lock is still present |
97
|
8 |
|
throw new UnlockingException('Failed releasing the lock.'); |
98
|
|
|
} |
99
|
2 |
|
} |
100
|
6 |
|
} |
101
|
6 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Try locking resource on all stores. |
105
|
|
|
* |
106
|
|
|
* Measure the time to do it and reject if not enough stores have successfully |
107
|
|
|
* locked the resource or if time to lock on all stores have exceeded the ttl. |
108
|
|
|
* |
109
|
|
|
* @param Lock $lock The lock instance |
110
|
|
|
* @param int $ttl Time to live in milliseconds |
111
|
|
|
* |
112
|
|
|
* @throws LockingException |
113
|
|
|
* |
114
|
|
|
* @return Lock |
115
|
|
|
*/ |
116
|
12 |
|
private function lockAndCheckOnAllStores(Lock $lock, $ttl) |
117
|
|
|
{ |
118
|
12 |
|
$timeMeasure = $this->stopwatch->start($lock->getToken()); |
119
|
12 |
|
$storesLocked = $this->lockOnAllStores($lock, $ttl); |
120
|
12 |
|
$timeMeasure->stop(); |
121
|
|
|
|
122
|
12 |
|
$this->checkQuorum($storesLocked); |
123
|
|
|
|
124
|
9 |
|
if ($ttl) { |
125
|
6 |
|
$this->checkTtl($timeMeasure->getDuration(), $ttl); |
126
|
3 |
|
$lock->setValidityEndTime($timeMeasure->getOrigin() + $ttl); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
6 |
|
return $lock; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Lock resource on all stores and count how many stores did it with success. |
134
|
|
|
* |
135
|
|
|
* @param Lock $lock The lock instance |
136
|
|
|
* @param int $ttl Time to live in milliseconds |
137
|
|
|
* |
138
|
|
|
* @return int The number of stores on which the resource has been locked |
139
|
|
|
*/ |
140
|
12 |
|
private function lockOnAllStores(Lock $lock, $ttl) |
141
|
|
|
{ |
142
|
12 |
|
$storesLocked = 0; |
143
|
|
|
|
144
|
12 |
|
foreach ($this->stores as $store) { |
145
|
12 |
|
if ($store->set($lock, $ttl)) { |
146
|
12 |
|
++$storesLocked; |
147
|
8 |
|
} |
148
|
8 |
|
} |
149
|
|
|
|
150
|
12 |
|
return $storesLocked; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Unlock the resource on all stores. |
155
|
|
|
* |
156
|
|
|
* @param Lock $lock The lock to release |
157
|
|
|
*/ |
158
|
6 |
|
private function resetLock($lock) |
159
|
|
|
{ |
160
|
6 |
|
foreach ($this->stores as $store) { |
161
|
6 |
|
$store->delete($lock); |
162
|
4 |
|
} |
163
|
6 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Init all stores passed to the constructor. |
167
|
|
|
* |
168
|
|
|
* If no store is given, it will return an InvalidArgumentException. |
169
|
|
|
* |
170
|
|
|
* @param LockStore[] $stores The lock stores |
171
|
|
|
* |
172
|
|
|
* @throws \InvalidArgumentException |
173
|
|
|
*/ |
174
|
36 |
|
private function setStores(array $stores) |
175
|
|
|
{ |
176
|
36 |
|
if (count($stores) === 0) { |
177
|
3 |
|
throw new \InvalidArgumentException('You must provide at least one LockStore.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
36 |
|
$this->stores = $stores; |
181
|
36 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the quorum based on the number of stores passed to the constructor. |
185
|
|
|
* |
186
|
|
|
* @param Quorum $quorum The quorum implementation to use |
187
|
|
|
*/ |
188
|
36 |
|
private function setQuorum(Quorum $quorum) |
189
|
|
|
{ |
190
|
36 |
|
$this->quorum = $quorum; |
191
|
36 |
|
$this->quorum->init(count($this->stores)); |
192
|
36 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check if the number of stores where the resource has been locked meet the quorum. |
196
|
|
|
* |
197
|
|
|
* @param int $storesLocked The number of stores on which the resource has been locked |
198
|
|
|
* |
199
|
|
|
* @throws LockingException |
200
|
|
|
*/ |
201
|
12 |
|
private function checkQuorum($storesLocked) |
202
|
|
|
{ |
203
|
12 |
|
if (!$this->quorum->isMet($storesLocked)) { |
204
|
3 |
|
throw new LockingException('Quorum has not been met.'); |
205
|
|
|
} |
206
|
9 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Make the script wait before retrying to lock. |
210
|
|
|
* |
211
|
|
|
* @param int $retryDelay The retry delay in milliseconds |
212
|
|
|
*/ |
213
|
6 |
|
private function waitBeforeRetrying($retryDelay) |
214
|
|
|
{ |
215
|
6 |
|
usleep($retryDelay * 1000); |
216
|
6 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Checks if the elapsed time is inferior to the ttl. |
220
|
|
|
* |
221
|
|
|
* To the elapsed time is added a drift time to have a margin of error. |
222
|
|
|
* If this adjusted time is greater than the ttl, it will throw a LockingException. |
223
|
|
|
* |
224
|
|
|
* @param int $elapsedTime The time elapsed in milliseconds |
225
|
|
|
* @param int $ttl The time to live in milliseconds |
226
|
|
|
* |
227
|
|
|
* @throws LockingException |
228
|
|
|
*/ |
229
|
6 |
|
private function checkTtl($elapsedTime, $ttl) |
230
|
|
|
{ |
231
|
6 |
|
$adjustedElapsedTime = ($elapsedTime + $this->getDrift($ttl)); |
232
|
|
|
|
233
|
6 |
|
if ($adjustedElapsedTime >= $ttl) { |
234
|
3 |
|
throw new LockingException('Time to lock the resource has exceeded the ttl.'); |
235
|
|
|
} |
236
|
3 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get the drift time based on ttl in ms. |
240
|
|
|
* |
241
|
|
|
* Return the max drift time of all stores |
242
|
|
|
* |
243
|
|
|
* @param int $ttl The time to live in milliseconds |
244
|
|
|
* |
245
|
|
|
* @return float |
246
|
|
|
*/ |
247
|
|
|
private function getDrift($ttl) |
248
|
|
|
{ |
249
|
6 |
|
return max(array_map(function (LockStore $store) use ($ttl) { |
250
|
6 |
|
return $store->getDrift($ttl); |
251
|
6 |
|
}, $this->stores)); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|