MediaWiki master
MemcLockManager.php
Go to the documentation of this file.
1<?php
24use Wikimedia\WaitConditionLoop;
25
42 protected $lockTypeMap = [
43 self::LOCK_SH => self::LOCK_SH,
44 self::LOCK_UW => self::LOCK_SH,
45 self::LOCK_EX => self::LOCK_EX
46 ];
47
49 protected $cacheServers = [];
51 protected $statusCache;
52
66 public function __construct( array $config ) {
67 parent::__construct( $config );
68
69 if ( isset( $config['srvsByBucket'] ) ) {
70 // Sanitize srvsByBucket config to prevent PHP errors
71 $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
72 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
73 } else {
74 $this->srvsByBucket = [ array_keys( $config['lockServers'] ) ];
75 }
76
77 $memcConfig = $config['memcConfig'] ?? [];
78 $memcConfig += [ 'class' => MemcachedPhpBagOStuff::class ]; // default
79
80 $class = $memcConfig['class'];
81 if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
82 throw new InvalidArgumentException( "$class is not of type MemcachedBagOStuff." );
83 }
84
85 foreach ( $config['lockServers'] as $name => $address ) {
86 $params = [ 'servers' => [ $address ] ] + $memcConfig;
87 $this->cacheServers[$name] = new $class( $params );
88 }
89
90 $this->statusCache = new MapCacheLRU( 100 );
91 }
92
93 protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
94 $status = StatusValue::newGood();
95
96 $memc = $this->getCache( $lockSrv );
97 // List of affected paths
98 $paths = array_merge( ...array_values( $pathsByType ) );
99 $paths = array_unique( $paths );
100 // List of affected lock record keys
101 $keys = array_map( $this->recordKeyForPath( ... ), $paths );
102
103 // Lock all of the active lock record keys...
104 if ( !$this->acquireMutexes( $memc, $keys ) ) {
105 $status->fatal( 'lockmanager-fail-conflict' );
106 return $status;
107 }
108
109 // Fetch all the existing lock records...
110 $lockRecords = $memc->getMulti( $keys );
111
112 $now = time();
113 // Check if the requested locks conflict with existing ones...
114 foreach ( $pathsByType as $type => $paths2 ) {
115 foreach ( $paths2 as $path ) {
116 $locksKey = $this->recordKeyForPath( $path );
117 $locksHeld = isset( $lockRecords[$locksKey] )
118 ? self::sanitizeLockArray( $lockRecords[$locksKey] )
119 : self::newLockArray(); // init
120 foreach ( $locksHeld[self::LOCK_EX] as $session => $expiry ) {
121 if ( $expiry < $now ) { // stale?
122 unset( $locksHeld[self::LOCK_EX][$session] );
123 } elseif ( $session !== $this->session ) {
124 $status->fatal( 'lockmanager-fail-conflict' );
125 }
126 }
127 if ( $type === self::LOCK_EX ) {
128 foreach ( $locksHeld[self::LOCK_SH] as $session => $expiry ) {
129 if ( $expiry < $now ) { // stale?
130 unset( $locksHeld[self::LOCK_SH][$session] );
131 } elseif ( $session !== $this->session ) {
132 $status->fatal( 'lockmanager-fail-conflict' );
133 }
134 }
135 }
136 if ( $status->isOK() ) {
137 // Register the session in the lock record array
139 // We will update this record if none of the other locks conflict
140 $lockRecords[$locksKey] = $locksHeld;
141 }
142 }
143 }
144
145 // If there were no lock conflicts, update all the lock records...
146 if ( $status->isOK() ) {
147 foreach ( $paths as $path ) {
148 $locksKey = $this->recordKeyForPath( $path );
149 $locksHeld = $lockRecords[$locksKey];
150 $ok = $memc->set( $locksKey, $locksHeld, self::MAX_LOCK_TTL );
151 if ( !$ok ) {
152 $status->fatal( 'lockmanager-fail-acquirelock', $path );
153 } else {
154 $this->logger->debug( __METHOD__ . ": acquired lock on key $locksKey." );
155 }
156 }
157 }
158
159 // Unlock all of the active lock record keys...
160 $this->releaseMutexes( $memc, $keys );
161
162 return $status;
163 }
164
165 protected function freeLocksOnServer( $lockSrv, array $pathsByType ) {
166 $status = StatusValue::newGood();
167
168 $memc = $this->getCache( $lockSrv );
169 // List of affected paths
170 $paths = array_merge( ...array_values( $pathsByType ) );
171 $paths = array_unique( $paths );
172 // List of affected lock record keys
173 $keys = array_map( $this->recordKeyForPath( ... ), $paths );
174
175 // Lock all of the active lock record keys...
176 if ( !$this->acquireMutexes( $memc, $keys ) ) {
177 foreach ( $paths as $path ) {
178 $status->fatal( 'lockmanager-fail-releaselock', $path );
179 }
180
181 return $status;
182 }
183
184 // Fetch all the existing lock records...
185 $lockRecords = $memc->getMulti( $keys );
186
187 // Remove the requested locks from all records...
188 foreach ( $pathsByType as $type => $paths2 ) {
189 foreach ( $paths2 as $path ) {
190 $locksKey = $this->recordKeyForPath( $path ); // lock record
191 if ( !isset( $lockRecords[$locksKey] ) ) {
192 $status->warning( 'lockmanager-fail-releaselock', $path );
193 continue; // nothing to do
194 }
195 $locksHeld = $this->sanitizeLockArray( $lockRecords[$locksKey] );
196 if ( isset( $locksHeld[$type][$this->session] ) ) {
197 unset( $locksHeld[$type][$this->session] ); // unregister this session
198 $lockRecords[$locksKey] = $locksHeld;
199 } else {
200 $status->warning( 'lockmanager-fail-releaselock', $path );
201 }
202 }
203 }
204
205 // Persist the new lock record values...
206 foreach ( $paths as $path ) {
207 $locksKey = $this->recordKeyForPath( $path );
208 if ( !isset( $lockRecords[$locksKey] ) ) {
209 continue; // nothing to do
210 }
211 $locksHeld = $lockRecords[$locksKey];
212 if ( $locksHeld === $this->newLockArray() ) {
213 $ok = $memc->delete( $locksKey );
214 } else {
215 $ok = $memc->set( $locksKey, $locksHeld, self::MAX_LOCK_TTL );
216 }
217 if ( $ok ) {
218 $this->logger->debug( __METHOD__ . ": released lock on key $locksKey." );
219 } else {
220 $status->fatal( 'lockmanager-fail-releaselock', $path );
221 }
222 }
223
224 // Unlock all of the active lock record keys...
225 $this->releaseMutexes( $memc, $keys );
226
227 return $status;
228 }
229
234 protected function releaseAllLocks() {
235 return StatusValue::newGood(); // not supported
236 }
237
243 protected function isServerUp( $lockSrv ) {
244 return (bool)$this->getCache( $lockSrv );
245 }
246
253 protected function getCache( $lockSrv ) {
254 if ( !isset( $this->cacheServers[$lockSrv] ) ) {
255 throw new InvalidArgumentException( "Invalid cache server '$lockSrv'." );
256 }
257
258 $online = $this->statusCache->get( "online:$lockSrv", 30 );
259 if ( $online === null ) {
260 $online = $this->cacheServers[$lockSrv]->set( __CLASS__ . ':ping', 1, 1 );
261 if ( !$online ) { // server down?
262 $this->logger->warning( __METHOD__ . ": Could not contact $lockSrv." );
263 }
264 $this->statusCache->set( "online:$lockSrv", (int)$online );
265 }
266
267 return $online ? $this->cacheServers[$lockSrv] : null;
268 }
269
274 protected function recordKeyForPath( $path ) {
275 return implode( ':', [ __CLASS__, 'locks', $this->sha1Base36Absolute( $path ) ] );
276 }
277
281 protected function newLockArray() {
282 return [ self::LOCK_SH => [], self::LOCK_EX => [] ];
283 }
284
289 protected function sanitizeLockArray( $a ) {
290 if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) {
291 return $a;
292 }
293
294 $this->logger->error( __METHOD__ . ": reset invalid lock array." );
295
296 return $this->newLockArray();
297 }
298
304 protected function acquireMutexes( MemcachedBagOStuff $memc, array $keys ) {
305 $lockedKeys = [];
306
307 // Acquire the keys in lexicographical order, to avoid deadlock problems.
308 // If P1 is waiting to acquire a key P2 has, P2 can't also be waiting for a key P1 has.
309 sort( $keys );
310
311 // Try to quickly loop to acquire the keys, but back off after a few rounds.
312 // This reduces memcached spam, especially in the rare case where a server acquires
313 // some lock keys and dies without releasing them. Lock keys expire after a few minutes.
314 $loop = new WaitConditionLoop(
315 static function () use ( $memc, $keys, &$lockedKeys ) {
316 foreach ( array_diff( $keys, $lockedKeys ) as $key ) {
317 if ( $memc->add( "$key:mutex", 1, 180 ) ) { // lock record
318 $lockedKeys[] = $key;
319 }
320 }
321
322 return array_diff( $keys, $lockedKeys )
323 ? WaitConditionLoop::CONDITION_CONTINUE
324 : true;
325 },
326 3.0 // timeout
327 );
328 $loop->invoke();
329
330 if ( count( $lockedKeys ) != count( $keys ) ) {
331 $this->releaseMutexes( $memc, $lockedKeys ); // failed; release what was locked
332 return false;
333 }
334
335 return true;
336 }
337
342 protected function releaseMutexes( MemcachedBagOStuff $memc, array $keys ) {
343 foreach ( $keys as $key ) {
344 $memc->delete( "$key:mutex" );
345 }
346 }
347
351 public function __destruct() {
352 $pathsByType = [];
353 foreach ( $this->locksHeld as $path => $locks ) {
354 foreach ( $locks as $type => $count ) {
355 $pathsByType[$type][] = $path;
356 }
357 }
358 if ( $pathsByType ) {
359 $this->unlockByType( $pathsByType );
360 }
361 }
362}
string $session
Random 32-char hex number.
const LOCK_SH
Lock types; stronger locks have higher values.
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
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
Manage locks using memcached servers.
MapCacheLRU $statusCache
Server status cache.
getLocksOnServer( $lockSrv, array $pathsByType)
Get a connection to a lock server and acquire locks.
getCache( $lockSrv)
Get the MemcachedBagOStuff object for a $lockSrv.
MemcachedBagOStuff[] $cacheServers
Map of (server name => MemcachedBagOStuff)
__construct(array $config)
Construct a new instance from configuration.
freeLocksOnServer( $lockSrv, array $pathsByType)
Get a connection to a lock server and release locks on $paths.
__destruct()
Make sure remaining locks get cleared.
releaseMutexes(MemcachedBagOStuff $memc, array $keys)
array $lockTypeMap
Mapping of lock types to the type actually used.
acquireMutexes(MemcachedBagOStuff $memc, array $keys)
Base class for lock managers that use a quorum of peer servers for locks.
Store key-value entries in a size-limited in-memory LRU cache.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
delete( $key, $flags=0)
Delete an item if it exists.
Store data in a memcached server or memcached cluster.
Store data on memcached servers(s) via a pure-PHP memcached client.