self::LOCK_SH, self::LOCK_UW => self::LOCK_SH, self::LOCK_EX => self::LOCK_EX ]; /** @var RedisConnectionPool */ protected $redisPool; /** @var array Map server names to hostname/IP and port numbers */ protected $lockServers = []; /** * Construct a new instance from configuration. * * @param array $config Parameters include: * - lockServers : Associative array of server names to ":" strings. * - srvsByBucket : Array of 1-16 consecutive integer keys, starting from 0, * each having an odd-numbered list of server names (peers) as values. * - redisConfig : Configuration for RedisConnectionPool::__construct(). * @throws Exception */ public function __construct( array $config ) { parent::__construct( $config ); $this->lockServers = $config['lockServers']; // Sanitize srvsByBucket config to prevent PHP errors $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' ); $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive $config['redisConfig']['serializer'] = 'none'; $this->redisPool = RedisConnectionPool::singleton( $config['redisConfig'] ); } protected function getLocksOnServer( $lockSrv, array $pathsByType ) { $status = StatusValue::newGood(); $pathList = array_merge( ...array_values( $pathsByType ) ); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server, $this->logger ); if ( !$conn ) { foreach ( $pathList as $path ) { $status->fatal( 'lockmanager-fail-acquirelock', $path ); } return $status; } $pathsByKey = []; // (type:hash => path) map foreach ( $pathsByType as $type => $paths ) { $typeString = ( $type == LockManager::LOCK_SH ) ? 'SH' : 'EX'; foreach ( $paths as $path ) { $pathsByKey[$this->recordKeyForPath( $path, $typeString )] = $path; } } try { static $script = /** @lang Lua */ <<luaEval( $script, array_merge( array_keys( $pathsByKey ), // KEYS[0], KEYS[1],...,KEYS[N] [ $this->session, // ARGV[1] $this->lockTTL, // ARGV[2] self::MAX_LOCK_TTL, // ARGV[3] time() // ARGV[4] ] ), count( $pathsByKey ) # number of first argument(s) that are keys ); } catch ( RedisException $e ) { $res = false; $this->redisPool->handleError( $conn, $e ); } if ( $res === false ) { foreach ( $pathList as $path ) { $status->fatal( 'lockmanager-fail-acquirelock', $path ); } } else { foreach ( $res as $key ) { $status->fatal( 'lockmanager-fail-acquirelock', $pathsByKey[$key] ); } } return $status; } protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { $status = StatusValue::newGood(); $pathList = array_merge( ...array_values( $pathsByType ) ); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server, $this->logger ); if ( !$conn ) { foreach ( $pathList as $path ) { $status->fatal( 'lockmanager-fail-releaselock', $path ); } return $status; } $pathsByKey = []; // (type:hash => path) map foreach ( $pathsByType as $type => $paths ) { $typeString = ( $type == LockManager::LOCK_SH ) ? 'SH' : 'EX'; foreach ( $paths as $path ) { $pathsByKey[$this->recordKeyForPath( $path, $typeString )] = $path; } } try { static $script = /** @lang Lua */ << 0 then -- Remove the whole structure if it is now empty if redis.call('hLen',resourceKey) == 0 then redis.call('del',resourceKey) end else failed[#failed+1] = requestKey end end return failed LUA; $res = $conn->luaEval( $script, array_merge( array_keys( $pathsByKey ), // KEYS[0], KEYS[1],...,KEYS[N] [ $this->session, // ARGV[1] ] ), count( $pathsByKey ) # number of first argument(s) that are keys ); } catch ( RedisException $e ) { $res = false; $this->redisPool->handleError( $conn, $e ); } if ( $res === false ) { foreach ( $pathList as $path ) { $status->fatal( 'lockmanager-fail-releaselock', $path ); } } else { foreach ( $res as $key ) { $status->fatal( 'lockmanager-fail-releaselock', $pathsByKey[$key] ); } } return $status; } protected function releaseAllLocks() { return StatusValue::newGood(); // not supported } protected function isServerUp( $lockSrv ) { $conn = $this->redisPool->getConnection( $this->lockServers[$lockSrv], $this->logger ); return (bool)$conn; } /** * @param string $path * @param string $type One of (EX,SH) * @return string */ protected function recordKeyForPath( $path, $type ) { return implode( ':', [ __CLASS__, 'locks', "$type:" . $this->sha1Base36Absolute( $path ) ] ); } /** * Make sure remaining locks get cleared for sanity */ public function __destruct() { while ( count( $this->locksHeld ) ) { $pathsByType = []; foreach ( $this->locksHeld as $path => $locks ) { foreach ( $locks as $type => $count ) { $pathsByType[$type][] = $path; } } $this->unlockByType( $pathsByType ); } } }