Merge "[LockManager] Factored QuorumLockManager class out of LSLockManager."
[lhc/web/wiklou.git] / includes / filerepo / backend / lockmanager / LockManager.php
1 <?php
2 /**
3 * @defgroup LockManager Lock management
4 * @ingroup FileBackend
5 */
6
7 /**
8 * Resource locking handling.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup LockManager
27 * @author Aaron Schulz
28 */
29
30 /**
31 * @brief Class for handling resource locking.
32 *
33 * Locks on resource keys can either be shared or exclusive.
34 *
35 * Implementations must keep track of what is locked by this proccess
36 * in-memory and support nested locking calls (using reference counting).
37 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
38 * Locks should either be non-blocking or have low wait timeouts.
39 *
40 * Subclasses should avoid throwing exceptions at all costs.
41 *
42 * @ingroup LockManager
43 * @since 1.19
44 */
45 abstract class LockManager {
46 /** @var Array Mapping of lock types to the type actually used */
47 protected $lockTypeMap = array(
48 self::LOCK_SH => self::LOCK_SH,
49 self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
50 self::LOCK_EX => self::LOCK_EX
51 );
52
53 /** @var Array Map of (resource path => lock type => count) */
54 protected $locksHeld = array();
55
56 /* Lock types; stronger locks have higher values */
57 const LOCK_SH = 1; // shared lock (for reads)
58 const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
59 const LOCK_EX = 3; // exclusive lock (for writes)
60
61 /**
62 * Construct a new instance from configuration
63 *
64 * @param $config Array
65 */
66 public function __construct( array $config ) {}
67
68 /**
69 * Lock the resources at the given abstract paths
70 *
71 * @param $paths Array List of resource names
72 * @param $type integer LockManager::LOCK_* constant
73 * @return Status
74 */
75 final public function lock( array $paths, $type = self::LOCK_EX ) {
76 wfProfileIn( __METHOD__ );
77 $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] );
78 wfProfileOut( __METHOD__ );
79 return $status;
80 }
81
82 /**
83 * Unlock the resources at the given abstract paths
84 *
85 * @param $paths Array List of storage paths
86 * @param $type integer LockManager::LOCK_* constant
87 * @return Status
88 */
89 final public function unlock( array $paths, $type = self::LOCK_EX ) {
90 wfProfileIn( __METHOD__ );
91 $status = $this->doUnlock( array_unique( $paths ), $this->lockTypeMap[$type] );
92 wfProfileOut( __METHOD__ );
93 return $status;
94 }
95
96 /**
97 * Get the base 36 SHA-1 of a string, padded to 31 digits
98 *
99 * @param $path string
100 * @return string
101 */
102 final protected static function sha1Base36( $path ) {
103 return wfBaseConvert( sha1( $path ), 16, 36, 31 );
104 }
105
106 /**
107 * Lock resources with the given keys and lock type
108 *
109 * @param $paths Array List of storage paths
110 * @param $type integer LockManager::LOCK_* constant
111 * @return string
112 */
113 abstract protected function doLock( array $paths, $type );
114
115 /**
116 * Unlock resources with the given keys and lock type
117 *
118 * @param $paths Array List of storage paths
119 * @param $type integer LockManager::LOCK_* constant
120 * @return string
121 */
122 abstract protected function doUnlock( array $paths, $type );
123 }
124
125 /**
126 * Self-releasing locks
127 *
128 * LockManager helper class to handle scoped locks, which
129 * release when an object is destroyed or goes out of scope.
130 *
131 * @ingroup LockManager
132 * @since 1.19
133 */
134 class ScopedLock {
135 /** @var LockManager */
136 protected $manager;
137 /** @var Status */
138 protected $status;
139 /** @var Array List of resource paths*/
140 protected $paths;
141
142 protected $type; // integer lock type
143
144 /**
145 * @param $manager LockManager
146 * @param $paths Array List of storage paths
147 * @param $type integer LockManager::LOCK_* constant
148 * @param $status Status
149 */
150 protected function __construct(
151 LockManager $manager, array $paths, $type, Status $status
152 ) {
153 $this->manager = $manager;
154 $this->paths = $paths;
155 $this->status = $status;
156 $this->type = $type;
157 }
158
159 /**
160 * Get a ScopedLock object representing a lock on resource paths.
161 * Any locks are released once this object goes out of scope.
162 * The status object is updated with any errors or warnings.
163 *
164 * @param $manager LockManager
165 * @param $paths Array List of storage paths
166 * @param $type integer LockManager::LOCK_* constant
167 * @param $status Status
168 * @return ScopedLock|null Returns null on failure
169 */
170 public static function factory(
171 LockManager $manager, array $paths, $type, Status $status
172 ) {
173 $lockStatus = $manager->lock( $paths, $type );
174 $status->merge( $lockStatus );
175 if ( $lockStatus->isOK() ) {
176 return new self( $manager, $paths, $type, $status );
177 }
178 return null;
179 }
180
181 function __destruct() {
182 $wasOk = $this->status->isOK();
183 $this->status->merge( $this->manager->unlock( $this->paths, $this->type ) );
184 if ( $wasOk ) {
185 // Make sure status is OK, despite any unlockFiles() fatals
186 $this->status->setResult( true, $this->status->value );
187 }
188 }
189 }
190
191 /**
192 * Version of LockManager that uses a quorum from peer servers for locks.
193 * The resource space can also be sharded into separate peer groups.
194 *
195 * @ingroup LockManager
196 * @since 1.20
197 */
198 abstract class QuorumLockManager extends LockManager {
199 /** @var Array Map of bucket indexes to peer server lists */
200 protected $srvsByBucket = array(); // (bucket index => (lsrv1, lsrv2, ...))
201
202 /**
203 * @see LockManager::doLock()
204 * @param $paths array
205 * @param $type int
206 * @return Status
207 */
208 final protected function doLock( array $paths, $type ) {
209 $status = Status::newGood();
210
211 $pathsToLock = array(); // (bucket => paths)
212 // Get locks that need to be acquired (buckets => locks)...
213 foreach ( $paths as $path ) {
214 if ( isset( $this->locksHeld[$path][$type] ) ) {
215 ++$this->locksHeld[$path][$type];
216 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
217 $this->locksHeld[$path][$type] = 1;
218 } else {
219 $bucket = $this->getBucketFromKey( $path );
220 $pathsToLock[$bucket][] = $path;
221 }
222 }
223
224 $lockedPaths = array(); // files locked in this attempt
225 // Attempt to acquire these locks...
226 foreach ( $pathsToLock as $bucket => $paths ) {
227 // Try to acquire the locks for this bucket
228 $status->merge( $this->doLockingRequestBucket( $bucket, $paths, $type ) );
229 if ( !$status->isOK() ) {
230 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
231 return $status;
232 }
233 // Record these locks as active
234 foreach ( $paths as $path ) {
235 $this->locksHeld[$path][$type] = 1; // locked
236 }
237 // Keep track of what locks were made in this attempt
238 $lockedPaths = array_merge( $lockedPaths, $paths );
239 }
240
241 return $status;
242 }
243
244 /**
245 * @see LockManager::doUnlock()
246 * @param $paths array
247 * @param $type int
248 * @return Status
249 */
250 final protected function doUnlock( array $paths, $type ) {
251 $status = Status::newGood();
252
253 $pathsToUnlock = array();
254 foreach ( $paths as $path ) {
255 if ( !isset( $this->locksHeld[$path][$type] ) ) {
256 $status->warning( 'lockmanager-notlocked', $path );
257 } else {
258 --$this->locksHeld[$path][$type];
259 // Reference count the locks held and release locks when zero
260 if ( $this->locksHeld[$path][$type] <= 0 ) {
261 unset( $this->locksHeld[$path][$type] );
262 $bucket = $this->getBucketFromKey( $path );
263 $pathsToUnlock[$bucket][] = $path;
264 }
265 if ( !count( $this->locksHeld[$path] ) ) {
266 unset( $this->locksHeld[$path] ); // no SH or EX locks left for key
267 }
268 }
269 }
270
271 // Remove these single locks if the medium supports it
272 foreach ( $pathsToUnlock as $bucket => $paths ) {
273 $status->merge( $this->doUnlockingRequestBucket( $bucket, $paths, $type ) );
274 }
275
276 // Reference count the locks held and release locks when zero
277 if ( !count( $this->locksHeld ) ) {
278 $status->merge( $this->releaseAllLocks() );
279 }
280
281 return $status;
282 }
283
284 /**
285 * Attempt to acquire locks with the peers for a bucket.
286 * This is all or nothing; if any key is locked then this totally fails.
287 *
288 * @param $bucket integer
289 * @param $paths Array List of resource keys to lock
290 * @param $type integer LockManager::LOCK_EX or LockManager::LOCK_SH
291 * @return Status
292 */
293 final protected function doLockingRequestBucket( $bucket, array $paths, $type ) {
294 $status = Status::newGood();
295
296 $yesVotes = 0; // locks made on trustable servers
297 $votesLeft = count( $this->srvsByBucket[$bucket] ); // remaining peers
298 $quorum = floor( $votesLeft/2 + 1 ); // simple majority
299 // Get votes for each peer, in order, until we have enough...
300 foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) {
301 if ( !$this->isServerUp( $lockSrv ) ) {
302 --$votesLeft;
303 $status->warning( 'lockmanager-fail-svr-acquire', $lockSrv );
304 continue; // server down?
305 }
306 // Attempt to acquire the lock on this peer
307 $status->merge( $this->getLocksOnServer( $lockSrv, $paths, $type ) );
308 if ( !$status->isOK() ) {
309 return $status; // vetoed; resource locked
310 }
311 ++$yesVotes; // success for this peer
312 if ( $yesVotes >= $quorum ) {
313 return $status; // lock obtained
314 }
315 --$votesLeft;
316 $votesNeeded = $quorum - $yesVotes;
317 if ( $votesNeeded > $votesLeft ) {
318 break; // short-circuit
319 }
320 }
321 // At this point, we must not have met the quorum
322 $status->setResult( false );
323
324 return $status;
325 }
326
327 /**
328 * Attempt to release locks with the peers for a bucket
329 *
330 * @param $bucket integer
331 * @param $paths Array List of resource keys to lock
332 * @param $type integer LockManager::LOCK_EX or LockManager::LOCK_SH
333 * @return Status
334 */
335 final protected function doUnlockingRequestBucket( $bucket, array $paths, $type ) {
336 $status = Status::newGood();
337
338 foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) {
339 if ( !$this->isServerUp( $lockSrv ) ) {
340 $status->fatal( 'lockmanager-fail-svr-release', $lockSrv );
341 // Attempt to release the lock on this peer
342 } else {
343 $status->merge( $this->freeLocksOnServer( $lockSrv, $paths, $type ) );
344 }
345 }
346
347 return $status;
348 }
349
350 /**
351 * Get the bucket for resource path.
352 * This should avoid throwing any exceptions.
353 *
354 * @param $path string
355 * @return integer
356 */
357 protected function getBucketFromKey( $path ) {
358 $prefix = substr( sha1( $path ), 0, 2 ); // first 2 hex chars (8 bits)
359 return (int)base_convert( $prefix, 16, 10 ) % count( $this->srvsByBucket );
360 }
361
362 /**
363 * Check if a lock server is up
364 *
365 * @param $lockSrv string
366 * @return bool
367 */
368 abstract protected function isServerUp( $lockSrv );
369
370 /**
371 * Get a connection to a lock server and acquire locks on $paths
372 *
373 * @param $lockSrv string
374 * @param $paths array
375 * @param $type integer
376 * @return Status
377 */
378 abstract protected function getLocksOnServer( $lockSrv, array $paths, $type );
379
380 /**
381 * Get a connection to a lock server and release locks on $paths
382 *
383 * @param $lockSrv string
384 * @param $paths array
385 * @param $type integer
386 * @return Status
387 */
388 abstract protected function freeLocksOnServer( $lockSrv, array $paths, $type );
389
390 /**
391 * Release all locks that this session is holding
392 *
393 * @return Status
394 */
395 abstract protected function releaseAllLocks();
396 }
397
398 /**
399 * Simple version of LockManager that does nothing
400 * @since 1.19
401 */
402 class NullLockManager extends LockManager {
403 /**
404 * @see LockManager::doLock()
405 * @param $paths array
406 * @param $type int
407 * @return Status
408 */
409 protected function doLock( array $paths, $type ) {
410 return Status::newGood();
411 }
412
413 /**
414 * @see LockManager::doUnlock()
415 * @param $paths array
416 * @param $type int
417 * @return Status
418 */
419 protected function doUnlock( array $paths, $type ) {
420 return Status::newGood();
421 }
422 }