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 = array(); protected $session = ''; // string; random UUID /** * Construct a new instance from configuration. * * $config paramaters 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(). * * @param Array $config * @throws MWException */ 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'] ); $this->session = wfRandomString( 32 ); } // @TODO: change this code to work in one batch protected function getLocksOnServer( $lockSrv, array $pathsByType ) { $status = Status::newGood(); $lockedPaths = array(); foreach ( $pathsByType as $type => $paths ) { $status->merge( $this->doGetLocksOnServer( $lockSrv, $paths, $type ) ); if ( $status->isOK() ) { $lockedPaths[$type] = isset( $lockedPaths[$type] ) ? array_merge( $lockedPaths[$type], $paths ) : $paths; } else { foreach ( $lockedPaths as $type => $paths ) { $status->merge( $this->doFreeLocksOnServer( $lockSrv, $paths, $type ) ); } break; } } return $status; } // @TODO: change this code to work in one batch protected function freeLocksOnServer( $lockSrv, array $pathsByType ) { $status = Status::newGood(); foreach ( $pathsByType as $type => $paths ) { $status->merge( $this->doFreeLocksOnServer( $lockSrv, $paths, $type ) ); } return $status; } protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) { $status = Status::newGood(); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server ); if ( !$conn ) { foreach ( $paths as $path ) { $status->fatal( 'lockmanager-fail-acquirelock', $path ); } return $status; } $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records try { static $script = <<luaEval( $script, array_merge( $keys, // KEYS[0], KEYS[1],...KEYS[N] array( $type === self::LOCK_SH ? 'SH' : 'EX', // ARGV[1] $this->session, // ARGV[2] $this->lockTTL, // ARGV[3] time() // ARGV[4] ) ), count( $keys ) # number of first argument(s) that are keys ); } catch ( RedisException $e ) { $res = false; $this->redisPool->handleException( $server, $conn, $e ); } if ( $res === false ) { foreach ( $paths as $path ) { $status->fatal( 'lockmanager-fail-acquirelock', $path ); } } else { $pathsByKey = array_combine( $keys, $paths ); foreach ( $res as $key ) { $status->fatal( 'lockmanager-fail-acquirelock', $pathsByKey[$key] ); } } return $status; } protected function doFreeLocksOnServer( $lockSrv, array $paths, $type ) { $status = Status::newGood(); $server = $this->lockServers[$lockSrv]; $conn = $this->redisPool->getConnection( $server ); if ( !$conn ) { foreach ( $paths as $path ) { $status->fatal( 'lockmanager-fail-releaselock', $path ); } return $status; } $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records try { static $script = << 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] = resourceKey end end return failed LUA; $res = $conn->luaEval( $script, array_merge( $keys, // KEYS[0], KEYS[1],...KEYS[N] array( $type === self::LOCK_SH ? 'SH' : 'EX', // ARGV[1] $this->session // ARGV[2] ) ), count( $keys ) # number of first argument(s) that are keys ); } catch ( RedisException $e ) { $res = false; $this->redisPool->handleException( $server, $conn, $e ); } if ( $res === false ) { foreach ( $paths as $path ) { $status->fatal( 'lockmanager-fail-releaselock', $path ); } } else { $pathsByKey = array_combine( $keys, $paths ); foreach ( $res as $key ) { $status->fatal( 'lockmanager-fail-releaselock', $pathsByKey[$key] ); } } return $status; } protected function releaseAllLocks() { return Status::newGood(); // not supported } protected function isServerUp( $lockSrv ) { return (bool)$this->redisPool->getConnection( $this->lockServers[$lockSrv] ); } /** * @param $path string * @return string */ protected function recordKeyForPath( $path ) { return implode( ':', array( __CLASS__, 'locks', $this->sha1Base36Absolute( $path ) ) ); } /** * Make sure remaining locks get cleared for sanity */ function __destruct() { while ( count( $this->locksHeld ) ) { foreach ( $this->locksHeld as $path => $locks ) { $this->doUnlock( array( $path ), self::LOCK_EX ); $this->doUnlock( array( $path ), self::LOCK_SH ); } } } }