23use Wikimedia\WaitConditionLoop;
65 parent::__construct( $config );
68 $this->srvsByBucket = array_filter( $config[
'srvsByBucket'],
'is_array' );
69 $this->srvsByBucket = array_values( $this->srvsByBucket );
71 $memcConfig = $config[
'memcConfig'] ?? [];
72 $memcConfig += [
'class' => MemcachedPhpBagOStuff::class ];
74 $class = $memcConfig[
'class'];
75 if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
76 throw new InvalidArgumentException(
"$class is not of type MemcachedBagOStuff." );
79 foreach ( $config[
'lockServers'] as $name => $address ) {
80 $params = [
'servers' => [ $address ] ] + $memcConfig;
81 $this->cacheServers[$name] =
new $class( $params );
88 $status = StatusValue::newGood();
92 $paths = array_merge( ...array_values( $pathsByType ) );
93 $paths = array_unique( $paths );
95 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
99 foreach ( $paths as
$path ) {
100 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
107 $lockRecords = $memc->getMulti(
$keys );
111 foreach ( $pathsByType as
$type => $paths ) {
112 foreach ( $paths as
$path ) {
118 if ( $expiry < $now ) {
120 } elseif (
$session !== $this->session ) {
121 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
124 if (
$type === self::LOCK_EX ) {
126 if ( $expiry < $now ) {
128 } elseif (
$session !== $this->session ) {
129 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
133 if ( $status->isOK() ) {
143 if ( $status->isOK() ) {
144 foreach ( $paths as
$path ) {
147 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
149 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
151 $this->logger->debug( __METHOD__ .
": acquired lock on key $locksKey.\n" );
163 $status = StatusValue::newGood();
165 $memc = $this->
getCache( $lockSrv );
167 $paths = array_merge( ...array_values( $pathsByType ) );
168 $paths = array_unique( $paths );
170 $keys = array_map( [ $this,
'recordKeyForPath' ], $paths );
174 foreach ( $paths as
$path ) {
175 $status->fatal(
'lockmanager-fail-releaselock',
$path );
182 $lockRecords = $memc->getMulti(
$keys );
185 foreach ( $pathsByType as
$type => $paths ) {
186 foreach ( $paths as
$path ) {
188 if ( !isset( $lockRecords[$locksKey] ) ) {
189 $status->warning(
'lockmanager-fail-releaselock',
$path );
197 $status->warning(
'lockmanager-fail-releaselock',
$path );
203 foreach ( $paths as
$path ) {
205 if ( !isset( $lockRecords[$locksKey] ) ) {
210 $ok = $memc->delete( $locksKey );
212 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
215 $this->logger->debug( __METHOD__ .
": released lock on key $locksKey.\n" );
217 $status->fatal(
'lockmanager-fail-releaselock',
$path );
232 return StatusValue::newGood();
241 return (
bool)$this->
getCache( $lockSrv );
251 if ( !isset( $this->cacheServers[$lockSrv] ) ) {
252 throw new InvalidArgumentException(
"Invalid cache server '$lockSrv'." );
255 $online = $this->statusCache->get(
"online:$lockSrv", 30 );
256 if ( $online ===
null ) {
257 $online = $this->cacheServers[$lockSrv]->set( __CLASS__ .
':ping', 1, 1 );
259 $this->logger->warning( __METHOD__ .
": Could not contact $lockSrv." );
261 $this->statusCache->set(
"online:$lockSrv", (
int)$online );
264 return $online ? $this->cacheServers[$lockSrv] :
null;
279 return [ self::LOCK_SH => [], self::LOCK_EX => [] ];
287 if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) {
291 $this->logger->error( __METHOD__ .
": reset invalid lock array." );
311 $loop =
new WaitConditionLoop(
312 function () use ( $memc,
$keys, &$lockedKeys ) {
313 foreach ( array_diff(
$keys, $lockedKeys ) as $key ) {
314 if ( $memc->
add(
"$key:mutex", 1, 180 ) ) {
315 $lockedKeys[] = $key;
319 return array_diff(
$keys, $lockedKeys )
320 ? WaitConditionLoop::CONDITION_CONTINUE
327 if ( count( $lockedKeys ) != count(
$keys ) ) {
340 foreach (
$keys as $key ) {
341 $memc->
delete(
"$key:mutex" );
349 while ( count( $this->locksHeld ) ) {
350 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 for sanity.
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.