23use Wikimedia\WaitConditionLoop;
66 parent::__construct( $config );
68 if ( isset( $config[
'srvsByBucket'] ) ) {
70 $this->srvsByBucket = array_filter( $config[
'srvsByBucket'],
'is_array' );
71 $this->srvsByBucket = array_values( $this->srvsByBucket );
73 $this->srvsByBucket = [ array_keys( $config[
'lockServers'] ) ];
76 $memcConfig = $config[
'memcConfig'] ?? [];
77 $memcConfig += [
'class' => MemcachedPhpBagOStuff::class ];
79 $class = $memcConfig[
'class'];
80 if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
81 throw new InvalidArgumentException(
"$class is not of type MemcachedBagOStuff." );
84 foreach ( $config[
'lockServers'] as $name => $address ) {
85 $params = [
'servers' => [ $address ] ] + $memcConfig;
86 $this->cacheServers[$name] =
new $class( $params );
93 $status = StatusValue::newGood();
97 $paths = array_merge( ...array_values( $pathsByType ) );
98 $paths = array_unique( $paths );
100 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
104 $status->fatal(
'lockmanager-fail-conflict' );
109 $lockRecords = $memc->getMulti(
$keys );
113 foreach ( $pathsByType as
$type => $paths ) {
114 foreach ( $paths as
$path ) {
120 if ( $expiry < $now ) {
122 } elseif (
$session !== $this->session ) {
123 $status->fatal(
'lockmanager-fail-conflict' );
126 if (
$type === self::LOCK_EX ) {
128 if ( $expiry < $now ) {
130 } elseif (
$session !== $this->session ) {
131 $status->fatal(
'lockmanager-fail-conflict' );
135 if ( $status->isOK() ) {
145 if ( $status->isOK() ) {
146 foreach ( $paths as
$path ) {
149 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
151 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
153 $this->logger->debug( __METHOD__ .
": acquired lock on key $locksKey." );
165 $status = StatusValue::newGood();
167 $memc = $this->
getCache( $lockSrv );
169 $paths = array_merge( ...array_values( $pathsByType ) );
170 $paths = array_unique( $paths );
172 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
176 foreach ( $paths as
$path ) {
177 $status->fatal(
'lockmanager-fail-releaselock',
$path );
184 $lockRecords = $memc->getMulti(
$keys );
187 foreach ( $pathsByType as
$type => $paths ) {
188 foreach ( $paths as
$path ) {
190 if ( !isset( $lockRecords[$locksKey] ) ) {
191 $status->warning(
'lockmanager-fail-releaselock',
$path );
199 $status->warning(
'lockmanager-fail-releaselock',
$path );
205 foreach ( $paths as
$path ) {
207 if ( !isset( $lockRecords[$locksKey] ) ) {
212 $ok = $memc->delete( $locksKey );
214 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
217 $this->logger->debug( __METHOD__ .
": released lock on key $locksKey." );
219 $status->fatal(
'lockmanager-fail-releaselock',
$path );
234 return StatusValue::newGood();
243 return (
bool)$this->
getCache( $lockSrv );
253 if ( !isset( $this->cacheServers[$lockSrv] ) ) {
254 throw new InvalidArgumentException(
"Invalid cache server '$lockSrv'." );
257 $online = $this->statusCache->get(
"online:$lockSrv", 30 );
258 if ( $online ===
null ) {
259 $online = $this->cacheServers[$lockSrv]->set( __CLASS__ .
':ping', 1, 1 );
261 $this->logger->warning( __METHOD__ .
": Could not contact $lockSrv." );
263 $this->statusCache->set(
"online:$lockSrv", (
int)$online );
266 return $online ? $this->cacheServers[$lockSrv] :
null;
281 return [ self::LOCK_SH => [], self::LOCK_EX => [] ];
289 if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) {
293 $this->logger->error( __METHOD__ .
": reset invalid lock array." );
313 $loop =
new WaitConditionLoop(
314 static function () use ( $memc,
$keys, &$lockedKeys ) {
315 foreach ( array_diff(
$keys, $lockedKeys ) as $key ) {
316 if ( $memc->
add(
"$key:mutex", 1, 180 ) ) {
317 $lockedKeys[] = $key;
321 return array_diff(
$keys, $lockedKeys )
322 ? WaitConditionLoop::CONDITION_CONTINUE
329 if ( count( $lockedKeys ) != count(
$keys ) ) {
342 foreach (
$keys as $key ) {
343 $memc->
delete(
"$key:mutex" );
351 while ( count( $this->locksHeld ) ) {
352 foreach ( $this->locksHeld as
$path => $locks ) {
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)
Handles a simple LRU key/value map with a maximum number of entries.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
delete( $key, $flags=0)
Delete an item.
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 memcached clients.
Version of LockManager that uses a quorum from peer servers for locks.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.