| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use Wikimedia\ObjectCache\MemcachedBagOStuff; |
| 22 |
use Wikimedia\ObjectCache\MemcachedPhpBagOStuff; |
| 23 |
use Wikimedia\WaitConditionLoop; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class MemcLockManager extends QuorumLockManager { |
| 40 |
|
| 41 |
protected $lockTypeMap = [ |
| 42 |
self::LOCK_SH => self::LOCK_SH, |
| 43 |
self::LOCK_UW => self::LOCK_SH, |
| 44 |
self::LOCK_EX => self::LOCK_EX |
| 45 |
]; |
| 46 |
|
| 47 |
|
| 48 |
protected $cacheServers = []; |
| 49 |
|
| 50 |
protected $statusCache; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function __construct( array $config ) { |
| 66 |
parent::__construct( $config ); |
| 67 |
|
| 68 |
if ( isset( $config['srvsByBucket'] ) ) { |
| 69 |
|
| 70 |
$this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' ); |
| 71 |
$this->srvsByBucket = array_values( $this->srvsByBucket ); |
| 72 |
} else { |
| 73 |
$this->srvsByBucket = [ array_keys( $config['lockServers'] ) ]; |
| 74 |
} |
| 75 |
|
| 76 |
$memcConfig = $config['memcConfig'] ?? []; |
| 77 |
$memcConfig += [ 'class' => MemcachedPhpBagOStuff::class ]; |
| 78 |
|
| 79 |
$class = $memcConfig['class']; |
| 80 |
if ( !is_subclass_of( $class, MemcachedBagOStuff::class ) ) { |
| 81 |
throw new InvalidArgumentException( "$class is not of type MemcachedBagOStuff." ); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ( $config['lockServers'] as $name => $address ) { |
| 85 |
$params = [ 'servers' => [ $address ] ] + $memcConfig; |
| 86 |
$this->cacheServers[$name] = new $class( $params ); |
| 87 |
} |
| 88 |
|
| 89 |
$this->statusCache = new MapCacheLRU( 100 ); |
| 90 |
} |
| 91 |
|
| 92 |
protected function getLocksOnServer( $lockSrv, array $pathsByType ) { |
| 93 |
$status = StatusValue::newGood(); |
| 94 |
|
| 95 |
$memc = $this->getCache( $lockSrv ); |
| 96 |
|
| 97 |
$paths = array_merge( ...array_values( $pathsByType ) ); |
| 98 |
$paths = array_unique( $paths ); |
| 99 |
|
| 100 |
$keys = array_map( [ $this, 'recordKeyForPath' ], $paths ); |
| 101 |
|
| 102 |
|
| 103 |
if ( !$this->acquireMutexes( $memc, $keys ) ) { |
| 104 |
$status->fatal( 'lockmanager-fail-conflict' ); |
| 105 |
return $status; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
$lockRecords = $memc->getMulti( $keys ); |
| 110 |
|
| 111 |
$now = time(); |
| 112 |
|
| 113 |
foreach ( $pathsByType as $type => $paths2 ) { |
| 114 |
foreach ( $paths2 as $path ) { |
| 115 |
$locksKey = $this->recordKeyForPath( $path ); |
| 116 |
$locksHeld = isset( $lockRecords[$locksKey] ) |
| 117 |
? self::sanitizeLockArray( $lockRecords[$locksKey] ) |
| 118 |
: self::newLockArray(); |
| 119 |
foreach ( $locksHeld[self::LOCK_EX] as $session => $expiry ) { |
| 120 |
if ( $expiry < $now ) { |
| 121 |
unset( $locksHeld[self::LOCK_EX][$session] ); |
| 122 |
} elseif ( $session !== $this->session ) { |
| 123 |
$status->fatal( 'lockmanager-fail-conflict' ); |
| 124 |
} |
| 125 |
} |
| 126 |
if ( $type === self::LOCK_EX ) { |
| 127 |
foreach ( $locksHeld[self::LOCK_SH] as $session => $expiry ) { |
| 128 |
if ( $expiry < $now ) { |
| 129 |
unset( $locksHeld[self::LOCK_SH][$session] ); |
| 130 |
} elseif ( $session !== $this->session ) { |
| 131 |
$status->fatal( 'lockmanager-fail-conflict' ); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
if ( $status->isOK() ) { |
| 136 |
|
| 137 |
$locksHeld[$type][$this->session] = $now + $this->lockTTL; |
| 138 |
|
| 139 |
$lockRecords[$locksKey] = $locksHeld; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if ( $status->isOK() ) { |
| 146 |
foreach ( $paths as $path ) { |
| 147 |
$locksKey = $this->recordKeyForPath( $path ); |
| 148 |
$locksHeld = $lockRecords[$locksKey]; |
| 149 |
$ok = $memc->set( $locksKey, $locksHeld, self::MAX_LOCK_TTL ); |
| 150 |
if ( !$ok ) { |
| 151 |
$status->fatal( 'lockmanager-fail-acquirelock', $path ); |
| 152 |
} else { |
| 153 |
$this->logger->debug( __METHOD__ . ": acquired lock on key $locksKey." ); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
$this->releaseMutexes( $memc, $keys ); |
| 160 |
|
| 161 |
return $status; |
| 162 |
} |
| 163 |
|
| 164 |
protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { |
| 165 |
$status = StatusValue::newGood(); |
| 166 |
|
| 167 |
$memc = $this->getCache( $lockSrv ); |
| 168 |
|
| 169 |
$paths = array_merge( ...array_values( $pathsByType ) ); |
| 170 |
$paths = array_unique( $paths ); |
| 171 |
|
| 172 |
$keys = array_map( [ $this, 'recordKeyForPath' ], $paths ); |
| 173 |
|
| 174 |
|
| 175 |
if ( !$this->acquireMutexes( $memc, $keys ) ) { |
| 176 |
foreach ( $paths as $path ) { |
| 177 |
$status->fatal( 'lockmanager-fail-releaselock', $path ); |
| 178 |
} |
| 179 |
|
| 180 |
return $status; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
$lockRecords = $memc->getMulti( $keys ); |
| 185 |
|
| 186 |
|
| 187 |
foreach ( $pathsByType as $type => $paths2 ) { |
| 188 |
foreach ( $paths2 as $path ) { |
| 189 |
$locksKey = $this->recordKeyForPath( $path ); |
| 190 |
if ( !isset( $lockRecords[$locksKey] ) ) { |
| 191 |
$status->warning( 'lockmanager-fail-releaselock', $path ); |
| 192 |
continue; |
| 193 |
} |
| 194 |
$locksHeld = $this->sanitizeLockArray( $lockRecords[$locksKey] ); |
| 195 |
if ( isset( $locksHeld[$type][$this->session] ) ) { |
| 196 |
unset( $locksHeld[$type][$this->session] ); |
| 197 |
$lockRecords[$locksKey] = $locksHeld; |
| 198 |
} else { |
| 199 |
$status->warning( 'lockmanager-fail-releaselock', $path ); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
foreach ( $paths as $path ) { |
| 206 |
$locksKey = $this->recordKeyForPath( $path ); |
| 207 |
if ( !isset( $lockRecords[$locksKey] ) ) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
$locksHeld = $lockRecords[$locksKey]; |
| 211 |
if ( $locksHeld === $this->newLockArray() ) { |
| 212 |
$ok = $memc->delete( $locksKey ); |
| 213 |
} else { |
| 214 |
$ok = $memc->set( $locksKey, $locksHeld, self::MAX_LOCK_TTL ); |
| 215 |
} |
| 216 |
if ( $ok ) { |
| 217 |
$this->logger->debug( __METHOD__ . ": released lock on key $locksKey." ); |
| 218 |
} else { |
| 219 |
$status->fatal( 'lockmanager-fail-releaselock', $path ); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
$this->releaseMutexes( $memc, $keys ); |
| 225 |
|
| 226 |
return $status; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected function releaseAllLocks() { |
| 234 |
return StatusValue::newGood(); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
protected function isServerUp( $lockSrv ) { |
| 243 |
return (bool)$this->getCache( $lockSrv ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
protected function getCache( $lockSrv ) { |
| 253 |
if ( !isset( $this->cacheServers[$lockSrv] ) ) { |
| 254 |
throw new InvalidArgumentException( "Invalid cache server '$lockSrv'." ); |
| 255 |
} |
| 256 |
|
| 257 |
$online = $this->statusCache->get( "online:$lockSrv", 30 ); |
| 258 |
if ( $online === null ) { |
| 259 |
$online = $this->cacheServers[$lockSrv]->set( __CLASS__ . ':ping', 1, 1 ); |
| 260 |
if ( !$online ) { |
| 261 |
$this->logger->warning( __METHOD__ . ": Could not contact $lockSrv." ); |
| 262 |
} |
| 263 |
$this->statusCache->set( "online:$lockSrv", (int)$online ); |
| 264 |
} |
| 265 |
|
| 266 |
return $online ? $this->cacheServers[$lockSrv] : null; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
protected function recordKeyForPath( $path ) { |
| 274 |
return implode( ':', [ __CLASS__, 'locks', $this->sha1Base36Absolute( $path ) ] ); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
protected function newLockArray() { |
| 281 |
return [ self::LOCK_SH => [], self::LOCK_EX => [] ]; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected function sanitizeLockArray( $a ) { |
| 289 |
if ( is_array( $a ) && isset( $a[self::LOCK_EX] ) && isset( $a[self::LOCK_SH] ) ) { |
| 290 |
return $a; |
| 291 |
} |
| 292 |
|
| 293 |
$this->logger->error( __METHOD__ . ": reset invalid lock array." ); |
| 294 |
|
| 295 |
return $this->newLockArray(); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
protected function acquireMutexes( MemcachedBagOStuff $memc, array $keys ) { |
| 304 |
$lockedKeys = []; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
sort( $keys ); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 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; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return array_diff( $keys, $lockedKeys ) |
| 322 |
? WaitConditionLoop::CONDITION_CONTINUE |
| 323 |
: true; |
| 324 |
}, |
| 325 |
3.0 |
| 326 |
); |
| 327 |
$loop->invoke(); |
| 328 |
|
| 329 |
if ( count( $lockedKeys ) != count( $keys ) ) { |
| 330 |
$this->releaseMutexes( $memc, $lockedKeys ); |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
return true; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
protected function releaseMutexes( MemcachedBagOStuff $memc, array $keys ) { |
| 342 |
foreach ( $keys as $key ) { |
| 343 |
$memc->delete( "$key:mutex" ); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function __destruct() { |
| 351 |
$pathsByType = []; |
| 352 |
foreach ( $this->locksHeld as $path => $locks ) { |
| 353 |
foreach ( $locks as $type => $count ) { |
| 354 |
$pathsByType[$type][] = $path; |
| 355 |
} |
| 356 |
} |
| 357 |
if ( $pathsByType ) { |
| 358 |
$this->unlockByType( $pathsByType ); |
| 359 |
} |
| 360 |
} |
| 361 |
} |