20use Wikimedia\WaitConditionLoop;
63 parent::__construct( $config );
65 if ( isset( $config[
'srvsByBucket'] ) ) {
67 $this->srvsByBucket = array_filter( $config[
'srvsByBucket'],
'is_array' );
68 $this->srvsByBucket = array_values( $this->srvsByBucket );
70 $this->srvsByBucket = [ array_keys( $config[
'lockServers'] ) ];
73 $memcConfig = $config[
'memcConfig'] ?? [];
74 $memcConfig += [
'class' => MemcachedPhpBagOStuff::class ];
76 $class = $memcConfig[
'class'];
77 if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
78 throw new InvalidArgumentException(
"$class is not of type MemcachedBagOStuff." );
81 foreach ( $config[
'lockServers'] as $name => $address ) {
82 $params = [
'servers' => [ $address ] ] + $memcConfig;
83 $this->cacheServers[$name] =
new $class( $params );
90 $status = StatusValue::newGood();
94 $paths = array_merge( ...array_values( $pathsByType ) );
95 $paths = array_unique( $paths );
97 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
101 $status->fatal(
'lockmanager-fail-conflict' );
106 $lockRecords = $memc->getMulti(
$keys );
110 foreach ( $pathsByType as
$type => $paths ) {
111 foreach ( $paths as
$path ) {
117 if ( $expiry < $now ) {
119 } elseif (
$session !== $this->session ) {
120 $status->fatal(
'lockmanager-fail-conflict' );
123 if (
$type === self::LOCK_EX ) {
125 if ( $expiry < $now ) {
127 } elseif (
$session !== $this->session ) {
128 $status->fatal(
'lockmanager-fail-conflict' );
132 if ( $status->isOK() ) {
142 if ( $status->isOK() ) {
143 foreach ( $paths as
$path ) {
146 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
148 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
150 $this->logger->debug( __METHOD__ .
": acquired lock on key $locksKey." );
162 $status = StatusValue::newGood();
164 $memc = $this->
getCache( $lockSrv );
166 $paths = array_merge( ...array_values( $pathsByType ) );
167 $paths = array_unique( $paths );
169 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
173 foreach ( $paths as
$path ) {
174 $status->fatal(
'lockmanager-fail-releaselock',
$path );
181 $lockRecords = $memc->getMulti(
$keys );
184 foreach ( $pathsByType as
$type => $paths ) {
185 foreach ( $paths as
$path ) {
187 if ( !isset( $lockRecords[$locksKey] ) ) {
188 $status->warning(
'lockmanager-fail-releaselock',
$path );
196 $status->warning(
'lockmanager-fail-releaselock',
$path );
202 foreach ( $paths as
$path ) {
204 if ( !isset( $lockRecords[$locksKey] ) ) {
209 $ok = $memc->delete( $locksKey );
211 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
214 $this->logger->debug( __METHOD__ .
": released lock on key $locksKey." );
216 $status->fatal(
'lockmanager-fail-releaselock',
$path );
231 return StatusValue::newGood();
240 return (
bool)$this->
getCache( $lockSrv );
250 if ( !isset( $this->cacheServers[$lockSrv] ) ) {
251 throw new InvalidArgumentException(
"Invalid cache server '$lockSrv'." );
254 $online = $this->statusCache->get(
"online:$lockSrv", 30 );
255 if ( $online ===
null ) {
256 $online = $this->cacheServers[$lockSrv]->set( __CLASS__ .
':ping', 1, 1 );
258 $this->logger->warning( __METHOD__ .
": Could not contact $lockSrv." );
260 $this->statusCache->set(
"online:$lockSrv", (
int)$online );
263 return $online ? $this->cacheServers[$lockSrv] :
null;
278 return [ self::LOCK_SH => [], self::LOCK_EX => [] ];
286 if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) {
290 $this->logger->error( __METHOD__ .
": reset invalid lock array." );
310 $loop =
new WaitConditionLoop(
311 static function () use ( $memc,
$keys, &$lockedKeys ) {
312 foreach ( array_diff(
$keys, $lockedKeys ) as $key ) {
313 if ( $memc->
add(
"$key:mutex", 1, 180 ) ) {
314 $lockedKeys[] = $key;
318 return array_diff(
$keys, $lockedKeys )
319 ? WaitConditionLoop::CONDITION_CONTINUE
326 if ( count( $lockedKeys ) != count(
$keys ) ) {
339 foreach (
$keys as $key ) {
340 $memc->
delete(
"$key:mutex" );
348 while ( count( $this->locksHeld ) ) {
349 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.
Base class for lock managers that use a quorum of peer servers for locks.
doUnlock(array $paths, $type)
Unlock resources with the given keys and lock type.