MediaWiki master
LockManager.php
Go to the documentation of this file.
1<?php
7use Psr\Log\LoggerInterface;
8use Psr\Log\NullLogger;
9use Wikimedia\RequestTimeout\RequestTimeout;
10use Wikimedia\WaitConditionLoop;
11
33abstract class LockManager {
35 protected $logger;
36
38 protected $lockTypeMap = [
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
41 self::LOCK_EX => self::LOCK_EX
42 ];
43
45 protected $locksHeld = [];
46
48 protected $domain;
50 protected $lockTTL;
51
53 protected $session;
54
56 public const LOCK_SH = 1; // shared lock (for reads)
57 public const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
58 public const LOCK_EX = 3; // exclusive lock (for writes)
59
61 protected const MAX_LOCK_TTL = 2 * 3600; // 2 hours
62
64 protected const MIN_LOCK_TTL = 5; // seconds
65
79 public function __construct( array $config ) {
80 $this->domain = $config['domain'] ?? 'global';
81 if ( isset( $config['lockTTL'] ) ) {
82 $this->lockTTL = max( self::MIN_LOCK_TTL, $config['lockTTL'] );
83 } elseif ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
84 $this->lockTTL = 3600; // 1 hour
85 } else {
86 $ttl = 2 * ceil( RequestTimeout::singleton()->getWallTimeLimit() );
87 $minMaxTTL = 5 * 60; // 5 minutes
88 $this->lockTTL = ( $ttl === INF || $ttl < $minMaxTTL ) ? $minMaxTTL : $ttl;
89 }
90
91 $this->lockTTL = min( $this->lockTTL, self::MAX_LOCK_TTL );
92
93 $random = [];
94 for ( $i = 1; $i <= 5; ++$i ) {
95 $random[] = mt_rand( 0, 0xFFFFFFF );
96 }
97 $this->session = md5( implode( '-', $random ) );
98
99 $this->logger = $config['logger'] ?? new NullLogger();
100 }
101
110 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
111 return $this->lockByType( [ $type => $paths ], $timeout );
112 }
113
122 final public function lockByType( array $pathsByType, $timeout = 0 ) {
123 $pathsByType = $this->normalizePathsByType( $pathsByType );
124
125 $status = null;
126 $loop = new WaitConditionLoop(
127 function () use ( &$status, $pathsByType ) {
128 $status = $this->doLockByType( $pathsByType );
129
130 return $status->isOK() ?: WaitConditionLoop::CONDITION_CONTINUE;
131 },
132 $timeout
133 );
134 $loop->invoke();
135
136 // @phan-suppress-next-line PhanTypeMismatchReturn WaitConditionLoop throws or status is set
137 return $status;
138 }
139
147 final public function unlock( array $paths, $type = self::LOCK_EX ) {
148 return $this->unlockByType( [ $type => $paths ] );
149 }
150
158 final public function unlockByType( array $pathsByType ) {
159 $pathsByType = $this->normalizePathsByType( $pathsByType );
160 $status = $this->doUnlockByType( $pathsByType );
161
162 return $status;
163 }
164
173 final protected function sha1Base36Absolute( $path ) {
174 return Wikimedia\base_convert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
175 }
176
185 final protected function normalizePathsByType( array $pathsByType ) {
186 $res = [];
187 foreach ( $pathsByType as $type => $paths ) {
188 foreach ( $paths as $path ) {
189 if ( (string)$path === '' ) {
190 throw new InvalidArgumentException( __METHOD__ . ": got empty path." );
191 }
192 }
193 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
194 }
195
196 return $res;
197 }
198
206 protected function doLockByType( array $pathsByType ) {
207 $status = StatusValue::newGood();
208 $lockedByType = []; // map of (type => paths)
209 foreach ( $pathsByType as $type => $paths ) {
210 $status->merge( $this->doLock( $paths, $type ) );
211 if ( $status->isOK() ) {
212 $lockedByType[$type] = $paths;
213 } else {
214 // Release the subset of locks that were acquired
215 foreach ( $lockedByType as $lType => $lPaths ) {
216 $status->merge( $this->doUnlock( $lPaths, $lType ) );
217 }
218 break;
219 }
220 }
221
222 return $status;
223 }
224
232 abstract protected function doLock( array $paths, $type );
233
241 protected function doUnlockByType( array $pathsByType ) {
242 $status = StatusValue::newGood();
243 foreach ( $pathsByType as $type => $paths ) {
244 $status->merge( $this->doUnlock( $paths, $type ) );
245 }
246
247 return $status;
248 }
249
257 abstract protected function doUnlock( array $paths, $type );
258}
Resource locking handling.
doUnlockByType(array $pathsByType)
string $domain
domain (usually wiki ID)
const MIN_LOCK_TTL
Minimum lock TTL.
array $lockTypeMap
Mapping of lock types to the type actually used.
lock(array $paths, $type=self::LOCK_EX, $timeout=0)
Lock the resources at the given abstract paths.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.
doLock(array $paths, $type)
Lock resources with the given keys and lock type.
string $session
Random 32-char hex number.
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
const LOCK_SH
Lock types; stronger locks have higher values.
doLockByType(array $pathsByType)
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
LoggerInterface $logger
const MAX_LOCK_TTL
Max expected lock expiry in any context.
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
unlock(array $paths, $type=self::LOCK_EX)
Unlock the resources at the given abstract paths.
array $locksHeld
Map of (resource path => lock type => count)
unlockByType(array $pathsByType)
Unlock the resources at the given abstract paths.
int $lockTTL
maximum time locks can be held
__construct(array $config)
Construct a new instance from configuration.