23 use 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 );
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 );
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" );
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 );
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 ) {