24use Wikimedia\WaitConditionLoop;
67 parent::__construct( $config );
69 if ( isset( $config[
'srvsByBucket'] ) ) {
71 $this->srvsByBucket = array_filter( $config[
'srvsByBucket'],
'is_array' );
72 $this->srvsByBucket = array_values( $this->srvsByBucket );
74 $this->srvsByBucket = [ array_keys( $config[
'lockServers'] ) ];
77 $memcConfig = $config[
'memcConfig'] ?? [];
78 $memcConfig += [
'class' => MemcachedPhpBagOStuff::class ];
80 $class = $memcConfig[
'class'];
81 if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) {
82 throw new InvalidArgumentException(
"$class is not of type MemcachedBagOStuff." );
85 foreach ( $config[
'lockServers'] as $name => $address ) {
86 $params = [
'servers' => [ $address ] ] + $memcConfig;
87 $this->cacheServers[$name] =
new $class( $params );
94 $status = StatusValue::newGood();
98 $paths = array_merge( ...array_values( $pathsByType ) );
99 $paths = array_unique( $paths );
105 $status->fatal(
'lockmanager-fail-conflict' );
110 $lockRecords = $memc->getMulti( $keys );
114 foreach ( $pathsByType as $type => $paths2 ) {
115 foreach ( $paths2 as
$path ) {
121 if ( $expiry < $now ) {
123 } elseif (
$session !== $this->session ) {
124 $status->fatal(
'lockmanager-fail-conflict' );
127 if ( $type === self::LOCK_EX ) {
129 if ( $expiry < $now ) {
131 } elseif (
$session !== $this->session ) {
132 $status->fatal(
'lockmanager-fail-conflict' );
136 if ( $status->isOK() ) {
146 if ( $status->isOK() ) {
147 foreach ( $paths as
$path ) {
150 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
152 $status->fatal(
'lockmanager-fail-acquirelock',
$path );
154 $this->logger->debug( __METHOD__ .
": acquired lock on key $locksKey." );
166 $status = StatusValue::newGood();
168 $memc = $this->
getCache( $lockSrv );
170 $paths = array_merge( ...array_values( $pathsByType ) );
171 $paths = array_unique( $paths );
177 foreach ( $paths as
$path ) {
178 $status->fatal(
'lockmanager-fail-releaselock',
$path );
185 $lockRecords = $memc->getMulti( $keys );
188 foreach ( $pathsByType as $type => $paths2 ) {
189 foreach ( $paths2 as
$path ) {
191 if ( !isset( $lockRecords[$locksKey] ) ) {
192 $status->warning(
'lockmanager-fail-releaselock',
$path );
196 if ( isset(
$locksHeld[$type][$this->session] ) ) {
200 $status->warning(
'lockmanager-fail-releaselock',
$path );
206 foreach ( $paths as
$path ) {
208 if ( !isset( $lockRecords[$locksKey] ) ) {
213 $ok = $memc->delete( $locksKey );
215 $ok = $memc->set( $locksKey,
$locksHeld, self::MAX_LOCK_TTL );
218 $this->logger->debug( __METHOD__ .
": released lock on key $locksKey." );
220 $status->fatal(
'lockmanager-fail-releaselock',
$path );
235 return StatusValue::newGood();
244 return (
bool)$this->
getCache( $lockSrv );
254 if ( !isset( $this->cacheServers[$lockSrv] ) ) {
255 throw new InvalidArgumentException(
"Invalid cache server '$lockSrv'." );
258 $online = $this->statusCache->get(
"online:$lockSrv", 30 );
259 if ( $online ===
null ) {
260 $online = $this->cacheServers[$lockSrv]->set( __CLASS__ .
':ping', 1, 1 );
262 $this->logger->warning( __METHOD__ .
": Could not contact $lockSrv." );
264 $this->statusCache->set(
"online:$lockSrv", (
int)$online );
267 return $online ? $this->cacheServers[$lockSrv] :
null;
282 return [ self::LOCK_SH => [], self::LOCK_EX => [] ];
290 if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) {
294 $this->logger->error( __METHOD__ .
": reset invalid lock array." );
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 ) ) {
318 $lockedKeys[] = $key;
322 return array_diff( $keys, $lockedKeys )
323 ? WaitConditionLoop::CONDITION_CONTINUE
330 if ( count( $lockedKeys ) != count( $keys ) ) {
343 foreach ( $keys as $key ) {
344 $memc->
delete(
"$key:mutex" );
353 foreach ( $this->locksHeld as
$path => $locks ) {
354 foreach ( $locks as $type => $count ) {
355 $pathsByType[$type][] =
$path;
358 if ( $pathsByType ) {
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.