'RedirectSpecialArticle' => __DIR__ . '/includes/specialpage/RedirectSpecialPage.php',
'RedirectSpecialPage' => __DIR__ . '/includes/specialpage/RedirectSpecialPage.php',
'RedisBagOStuff' => __DIR__ . '/includes/objectcache/RedisBagOStuff.php',
- 'RedisConnRef' => __DIR__ . '/includes/clientpool/RedisConnectionPool.php',
- 'RedisConnectionPool' => __DIR__ . '/includes/clientpool/RedisConnectionPool.php',
- 'RedisLockManager' => __DIR__ . '/includes/filebackend/lockmanager/RedisLockManager.php',
+ 'RedisConnRef' => __DIR__ . '/includes/libs/redis/RedisConnRef.php',
+ 'RedisConnectionPool' => __DIR__ . '/includes/libs/redis/RedisConnectionPool.php',
+ 'RedisLockManager' => __DIR__ . '/includes/libs/lockmanager/RedisLockManager.php',
'RedisPubSubFeedEngine' => __DIR__ . '/includes/rcfeed/RedisPubSubFeedEngine.php',
'RefreshFileHeaders' => __DIR__ . '/maintenance/refreshFileHeaders.php',
'RefreshImageMetadata' => __DIR__ . '/maintenance/refreshImageMetadata.php',
+++ /dev/null
-<?php
-/**
- * Redis client connection pooling manager.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @defgroup Redis Redis
- * @author Aaron Schulz
- */
-
-use MediaWiki\Logger\LoggerFactory;
-use Psr\Log\LoggerAwareInterface;
-use Psr\Log\LoggerInterface;
-
-/**
- * Helper class to manage Redis connections.
- *
- * This can be used to get handle wrappers that free the handle when the wrapper
- * leaves scope. The maximum number of free handles (connections) is configurable.
- * This provides an easy way to cache connection handles that may also have state,
- * such as a handle does between multi() and exec(), and without hoarding connections.
- * The wrappers use PHP magic methods so that calling functions on them calls the
- * function of the actual Redis object handle.
- *
- * @ingroup Redis
- * @since 1.21
- */
-class RedisConnectionPool implements LoggerAwareInterface {
- /**
- * @name Pool settings.
- * Settings there are shared for any connection made in this pool.
- * See the singleton() method documentation for more details.
- * @{
- */
- /** @var string Connection timeout in seconds */
- protected $connectTimeout;
- /** @var string Read timeout in seconds */
- protected $readTimeout;
- /** @var string Plaintext auth password */
- protected $password;
- /** @var bool Whether connections persist */
- protected $persistent;
- /** @var int Serializer to use (Redis::SERIALIZER_*) */
- protected $serializer;
- /** @} */
-
- /** @var int Current idle pool size */
- protected $idlePoolSize = 0;
-
- /** @var array (server name => ((connection info array),...) */
- protected $connections = [];
- /** @var array (server name => UNIX timestamp) */
- protected $downServers = [];
-
- /** @var array (pool ID => RedisConnectionPool) */
- protected static $instances = [];
-
- /** integer; seconds to cache servers as "down". */
- const SERVER_DOWN_TTL = 30;
-
- /**
- * @var LoggerInterface
- */
- protected $logger;
-
- /**
- * @param array $options
- * @throws Exception
- */
- protected function __construct( array $options ) {
- if ( !class_exists( 'Redis' ) ) {
- throw new Exception( __CLASS__ . ' requires a Redis client library. ' .
- 'See https://www.mediawiki.org/wiki/Redis#Setup' );
- }
- if ( isset( $options['logger'] ) ) {
- $this->setLogger( $options['logger'] );
- } else {
- $this->setLogger( LoggerFactory::getInstance( 'redis' ) );
- }
- $this->connectTimeout = $options['connectTimeout'];
- $this->readTimeout = $options['readTimeout'];
- $this->persistent = $options['persistent'];
- $this->password = $options['password'];
- if ( !isset( $options['serializer'] ) || $options['serializer'] === 'php' ) {
- $this->serializer = Redis::SERIALIZER_PHP;
- } elseif ( $options['serializer'] === 'igbinary' ) {
- $this->serializer = Redis::SERIALIZER_IGBINARY;
- } elseif ( $options['serializer'] === 'none' ) {
- $this->serializer = Redis::SERIALIZER_NONE;
- } else {
- throw new InvalidArgumentException( "Invalid serializer specified." );
- }
- }
-
- /**
- * @param LoggerInterface $logger
- * @return null
- */
- public function setLogger( LoggerInterface $logger ) {
- $this->logger = $logger;
- }
-
- /**
- * @param array $options
- * @return array
- */
- protected static function applyDefaultConfig( array $options ) {
- if ( !isset( $options['connectTimeout'] ) ) {
- $options['connectTimeout'] = 1;
- }
- if ( !isset( $options['readTimeout'] ) ) {
- $options['readTimeout'] = 1;
- }
- if ( !isset( $options['persistent'] ) ) {
- $options['persistent'] = false;
- }
- if ( !isset( $options['password'] ) ) {
- $options['password'] = null;
- }
-
- return $options;
- }
-
- /**
- * @param array $options
- * $options include:
- * - connectTimeout : The timeout for new connections, in seconds.
- * Optional, default is 1 second.
- * - readTimeout : The timeout for operation reads, in seconds.
- * Commands like BLPOP can fail if told to wait longer than this.
- * Optional, default is 1 second.
- * - persistent : Set this to true to allow connections to persist across
- * multiple web requests. False by default.
- * - password : The authentication password, will be sent to Redis in clear text.
- * Optional, if it is unspecified, no AUTH command will be sent.
- * - serializer : Set to "php", "igbinary", or "none". Default is "php".
- * @return RedisConnectionPool
- */
- public static function singleton( array $options ) {
- $options = self::applyDefaultConfig( $options );
- // Map the options to a unique hash...
- ksort( $options ); // normalize to avoid pool fragmentation
- $id = sha1( serialize( $options ) );
- // Initialize the object at the hash as needed...
- if ( !isset( self::$instances[$id] ) ) {
- self::$instances[$id] = new self( $options );
- LoggerFactory::getInstance( 'redis' )->debug(
- "Creating a new " . __CLASS__ . " instance with id $id."
- );
- }
-
- return self::$instances[$id];
- }
-
- /**
- * Destroy all singleton() instances
- * @since 1.27
- */
- public static function destroySingletons() {
- self::$instances = [];
- }
-
- /**
- * Get a connection to a redis server. Based on code in RedisBagOStuff.php.
- *
- * @param string $server A hostname/port combination or the absolute path of a UNIX socket.
- * If a hostname is specified but no port, port 6379 will be used.
- * @return RedisConnRef|bool Returns false on failure
- * @throws MWException
- */
- public function getConnection( $server ) {
- // Check the listing "dead" servers which have had a connection errors.
- // Servers are marked dead for a limited period of time, to
- // avoid excessive overhead from repeated connection timeouts.
- if ( isset( $this->downServers[$server] ) ) {
- $now = time();
- if ( $now > $this->downServers[$server] ) {
- // Dead time expired
- unset( $this->downServers[$server] );
- } else {
- // Server is dead
- $this->logger->debug(
- 'Server "{redis_server}" is marked down for another ' .
- ( $this->downServers[$server] - $now ) . 'seconds',
- [ 'redis_server' => $server ]
- );
-
- return false;
- }
- }
-
- // Check if a connection is already free for use
- if ( isset( $this->connections[$server] ) ) {
- foreach ( $this->connections[$server] as &$connection ) {
- if ( $connection['free'] ) {
- $connection['free'] = false;
- --$this->idlePoolSize;
-
- return new RedisConnRef(
- $this, $server, $connection['conn'], $this->logger
- );
- }
- }
- }
-
- if ( substr( $server, 0, 1 ) === '/' ) {
- // UNIX domain socket
- // These are required by the redis extension to start with a slash, but
- // we still need to set the port to a special value to make it work.
- $host = $server;
- $port = 0;
- } else {
- // TCP connection
- $hostPort = IP::splitHostAndPort( $server );
- if ( !$server || !$hostPort ) {
- throw new InvalidArgumentException(
- __CLASS__ . ": invalid configured server \"$server\""
- );
- }
- list( $host, $port ) = $hostPort;
- if ( $port === false ) {
- $port = 6379;
- }
- }
-
- $conn = new Redis();
- try {
- if ( $this->persistent ) {
- $result = $conn->pconnect( $host, $port, $this->connectTimeout );
- } else {
- $result = $conn->connect( $host, $port, $this->connectTimeout );
- }
- if ( !$result ) {
- $this->logger->error(
- 'Could not connect to server "{redis_server}"',
- [ 'redis_server' => $server ]
- );
- // Mark server down for some time to avoid further timeouts
- $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
-
- return false;
- }
- if ( $this->password !== null ) {
- if ( !$conn->auth( $this->password ) ) {
- $this->logger->error(
- 'Authentication error connecting to "{redis_server}"',
- [ 'redis_server' => $server ]
- );
- }
- }
- } catch ( RedisException $e ) {
- $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
- $this->logger->error(
- 'Redis exception connecting to "{redis_server}"',
- [
- 'redis_server' => $server,
- 'exception' => $e,
- ]
- );
-
- return false;
- }
-
- if ( $conn ) {
- $conn->setOption( Redis::OPT_READ_TIMEOUT, $this->readTimeout );
- $conn->setOption( Redis::OPT_SERIALIZER, $this->serializer );
- $this->connections[$server][] = [ 'conn' => $conn, 'free' => false ];
-
- return new RedisConnRef( $this, $server, $conn, $this->logger );
- } else {
- return false;
- }
- }
-
- /**
- * Mark a connection to a server as free to return to the pool
- *
- * @param string $server
- * @param Redis $conn
- * @return bool
- */
- public function freeConnection( $server, Redis $conn ) {
- $found = false;
-
- foreach ( $this->connections[$server] as &$connection ) {
- if ( $connection['conn'] === $conn && !$connection['free'] ) {
- $connection['free'] = true;
- ++$this->idlePoolSize;
- break;
- }
- }
-
- $this->closeExcessIdleConections();
-
- return $found;
- }
-
- /**
- * Close any extra idle connections if there are more than the limit
- */
- protected function closeExcessIdleConections() {
- if ( $this->idlePoolSize <= count( $this->connections ) ) {
- return; // nothing to do (no more connections than servers)
- }
-
- foreach ( $this->connections as &$serverConnections ) {
- foreach ( $serverConnections as $key => &$connection ) {
- if ( $connection['free'] ) {
- unset( $serverConnections[$key] );
- if ( --$this->idlePoolSize <= count( $this->connections ) ) {
- return; // done (no more connections than servers)
- }
- }
- }
- }
- }
-
- /**
- * The redis extension throws an exception in response to various read, write
- * and protocol errors. Sometimes it also closes the connection, sometimes
- * not. The safest response for us is to explicitly destroy the connection
- * object and let it be reopened during the next request.
- *
- * @param string $server
- * @param RedisConnRef $cref
- * @param RedisException $e
- * @deprecated since 1.23
- */
- public function handleException( $server, RedisConnRef $cref, RedisException $e ) {
- $this->handleError( $cref, $e );
- }
-
- /**
- * The redis extension throws an exception in response to various read, write
- * and protocol errors. Sometimes it also closes the connection, sometimes
- * not. The safest response for us is to explicitly destroy the connection
- * object and let it be reopened during the next request.
- *
- * @param RedisConnRef $cref
- * @param RedisException $e
- */
- public function handleError( RedisConnRef $cref, RedisException $e ) {
- $server = $cref->getServer();
- $this->logger->error(
- 'Redis exception on server "{redis_server}"',
- [
- 'redis_server' => $server,
- 'exception' => $e,
- ]
- );
- foreach ( $this->connections[$server] as $key => $connection ) {
- if ( $cref->isConnIdentical( $connection['conn'] ) ) {
- $this->idlePoolSize -= $connection['free'] ? 1 : 0;
- unset( $this->connections[$server][$key] );
- break;
- }
- }
- }
-
- /**
- * Re-send an AUTH request to the redis server (useful after disconnects).
- *
- * This works around an upstream bug in phpredis. phpredis hides disconnects by transparently
- * reconnecting, but it neglects to re-authenticate the new connection. To the user of the
- * phpredis client API this manifests as a seemingly random tendency of connections to lose
- * their authentication status.
- *
- * This method is for internal use only.
- *
- * @see https://github.com/nicolasff/phpredis/issues/403
- *
- * @param string $server
- * @param Redis $conn
- * @return bool Success
- */
- public function reauthenticateConnection( $server, Redis $conn ) {
- if ( $this->password !== null ) {
- if ( !$conn->auth( $this->password ) ) {
- $this->logger->error(
- 'Authentication error connecting to "{redis_server}"',
- [ 'redis_server' => $server ]
- );
-
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Adjust or reset the connection handle read timeout value
- *
- * @param Redis $conn
- * @param int $timeout Optional
- */
- public function resetTimeout( Redis $conn, $timeout = null ) {
- $conn->setOption( Redis::OPT_READ_TIMEOUT, $timeout ?: $this->readTimeout );
- }
-
- /**
- * Make sure connections are closed for sanity
- */
- function __destruct() {
- foreach ( $this->connections as $server => &$serverConnections ) {
- foreach ( $serverConnections as $key => &$connection ) {
- $connection['conn']->close();
- }
- }
- }
-}
-
-/**
- * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
- *
- * This class simply wraps the Redis class and can be used the same way
- *
- * @ingroup Redis
- * @since 1.21
- */
-class RedisConnRef {
- /** @var RedisConnectionPool */
- protected $pool;
- /** @var Redis */
- protected $conn;
-
- protected $server; // string
- protected $lastError; // string
-
- /**
- * @var LoggerInterface
- */
- protected $logger;
-
- /**
- * @param RedisConnectionPool $pool
- * @param string $server
- * @param Redis $conn
- * @param LoggerInterface $logger
- */
- public function __construct(
- RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
- ) {
- $this->pool = $pool;
- $this->server = $server;
- $this->conn = $conn;
- $this->logger = $logger;
- }
-
- /**
- * @return string
- * @since 1.23
- */
- public function getServer() {
- return $this->server;
- }
-
- public function getLastError() {
- return $this->lastError;
- }
-
- public function clearLastError() {
- $this->lastError = null;
- }
-
- public function __call( $name, $arguments ) {
- $conn = $this->conn; // convenience
-
- // Work around https://github.com/nicolasff/phpredis/issues/70
- $lname = strtolower( $name );
- if ( ( $lname === 'blpop' || $lname == 'brpop' )
- && is_array( $arguments[0] ) && isset( $arguments[1] )
- ) {
- $this->pool->resetTimeout( $conn, $arguments[1] + 1 );
- } elseif ( $lname === 'brpoplpush' && isset( $arguments[2] ) ) {
- $this->pool->resetTimeout( $conn, $arguments[2] + 1 );
- }
-
- $conn->clearLastError();
- try {
- $res = call_user_func_array( [ $conn, $name ], $arguments );
- if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
- $this->pool->reauthenticateConnection( $this->server, $conn );
- $conn->clearLastError();
- $res = call_user_func_array( [ $conn, $name ], $arguments );
- $this->logger->info(
- "Used automatic re-authentication for method '$name'.",
- [ 'redis_server' => $this->server ]
- );
- }
- } catch ( RedisException $e ) {
- $this->pool->resetTimeout( $conn ); // restore
- throw $e;
- }
-
- $this->lastError = $conn->getLastError() ?: $this->lastError;
-
- $this->pool->resetTimeout( $conn ); // restore
-
- return $res;
- }
-
- /**
- * @param string $script
- * @param array $params
- * @param int $numKeys
- * @return mixed
- * @throws RedisException
- */
- public function luaEval( $script, array $params, $numKeys ) {
- $sha1 = sha1( $script ); // 40 char hex
- $conn = $this->conn; // convenience
- $server = $this->server; // convenience
-
- // Try to run the server-side cached copy of the script
- $conn->clearLastError();
- $res = $conn->evalSha( $sha1, $params, $numKeys );
- // If we got a permission error reply that means that (a) we are not in
- // multi()/pipeline() and (b) some connection problem likely occurred. If
- // the password the client gave was just wrong, an exception should have
- // been thrown back in getConnection() previously.
- if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
- $this->pool->reauthenticateConnection( $server, $conn );
- $conn->clearLastError();
- $res = $conn->eval( $script, $params, $numKeys );
- $this->logger->info(
- "Used automatic re-authentication for Lua script '$sha1'.",
- [ 'redis_server' => $server ]
- );
- }
- // If the script is not in cache, use eval() to retry and cache it
- if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
- $conn->clearLastError();
- $res = $conn->eval( $script, $params, $numKeys );
- $this->logger->info(
- "Used eval() for Lua script '$sha1'.",
- [ 'redis_server' => $server ]
- );
- }
-
- if ( $conn->getLastError() ) { // script bug?
- $this->logger->error(
- 'Lua script error on server "{redis_server}": {lua_error}',
- [
- 'redis_server' => $server,
- 'lua_error' => $conn->getLastError()
- ]
- );
- }
-
- $this->lastError = $conn->getLastError() ?: $this->lastError;
-
- return $res;
- }
-
- /**
- * @param Redis $conn
- * @return bool
- */
- public function isConnIdentical( Redis $conn ) {
- return $this->conn === $conn;
- }
-
- function __destruct() {
- $this->pool->freeConnection( $this->server, $this->conn );
- }
-}
* @ingroup LockManager
*/
use MediaWiki\MediaWikiServices;
+use MediaWiki\Logger\LoggerFactory;
/**
* Class to handle file lock manager registration
$config['dbServers']['localDBMaster'] = $dbw;
$config['srvCache'] = ObjectCache::getLocalServerInstance( 'hash' );
}
+ $config['logger'] = LoggerFactory::getInstance( 'LockManager' );
+
$this->managers[$name]['instance'] = new $class( $config );
}
self::LOCK_EX => self::LOCK_EX
];
+ public function __construct( array $config ) {
+ parent::__construct( $config );
+
+ $this->session = substr( $this->session, 0, 31 ); // fit to field
+ }
+
protected function initConnection( $lockDb, IDatabase $db ) {
# Let this transaction see lock rows from other transactions
$db->query( "SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;" );
+++ /dev/null
-<?php
-/**
- * Version of LockManager based on using redis servers.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @ingroup LockManager
- */
-
-/**
- * Manage locks using redis servers.
- *
- * Version of LockManager based on using redis servers.
- * This is meant for multi-wiki systems that may share files.
- * All locks are non-blocking, which avoids deadlocks.
- *
- * All lock requests for a resource, identified by a hash string, will map to one
- * bucket. Each bucket maps to one or several peer servers, each running redis.
- * A majority of peers must agree for a lock to be acquired.
- *
- * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
- *
- * @ingroup LockManager
- * @since 1.22
- */
-class RedisLockManager extends QuorumLockManager {
- /** @var array Mapping of lock types to the type actually used */
- protected $lockTypeMap = [
- self::LOCK_SH => 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 = [];
-
- /** @var string Random UUID */
- protected $session = '';
-
- /**
- * Construct a new instance from configuration.
- *
- * @param array $config Parameters include:
- * - lockServers : Associative array of server names to "<IP>:<port>" 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'] );
-
- $this->session = wfRandomString( 32 );
- }
-
- protected function getLocksOnServer( $lockSrv, array $pathsByType ) {
- $status = StatusValue::newGood();
-
- $pathList = call_user_func_array( 'array_merge', array_values( $pathsByType ) );
-
- $server = $this->lockServers[$lockSrv];
- $conn = $this->redisPool->getConnection( $server );
- 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 =
-<<<LUA
- local failed = {}
- -- Load input params (e.g. session, ttl, time of request)
- local rSession, rTTL, rTime = unpack(ARGV)
- -- Check that all the locks can be acquired
- for i,requestKey in ipairs(KEYS) do
- local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
- local keyIsFree = true
- local currentLocks = redis.call('hKeys',resourceKey)
- for i,lockKey in ipairs(currentLocks) do
- -- Get the type and session of this lock
- local _, _, type, session = string.find(lockKey,"(%w+):(%w+)")
- -- Check any locks that are not owned by this session
- if session ~= rSession then
- local lockExpiry = redis.call('hGet',resourceKey,lockKey)
- if 1*lockExpiry < 1*rTime then
- -- Lock is stale, so just prune it out
- redis.call('hDel',resourceKey,lockKey)
- elseif rType == 'EX' or type == 'EX' then
- keyIsFree = false
- break
- end
- end
- end
- if not keyIsFree then
- failed[#failed+1] = requestKey
- end
- end
- -- If all locks could be acquired, then do so
- if #failed == 0 then
- for i,requestKey in ipairs(KEYS) do
- local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
- redis.call('hSet',resourceKey,rType .. ':' .. rSession,rTime + rTTL)
- -- In addition to invalidation logic, be sure to garbage collect
- redis.call('expire',resourceKey,rTTL)
- end
- end
- return failed
-LUA;
- $res = $conn->luaEval( $script,
- array_merge(
- array_keys( $pathsByKey ), // KEYS[0], KEYS[1],...,KEYS[N]
- [
- $this->session, // ARGV[1]
- $this->lockTTL, // ARGV[2]
- time() // ARGV[3]
- ]
- ),
- 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 = call_user_func_array( 'array_merge', array_values( $pathsByType ) );
-
- $server = $this->lockServers[$lockSrv];
- $conn = $this->redisPool->getConnection( $server );
- 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 =
-<<<LUA
- local failed = {}
- -- Load input params (e.g. session)
- local rSession = unpack(ARGV)
- for i,requestKey in ipairs(KEYS) do
- local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
- local released = redis.call('hDel',resourceKey,rType .. ':' .. rSession)
- if released > 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 ) {
- return (bool)$this->redisPool->getConnection( $this->lockServers[$lockSrv] );
- }
-
- /**
- * @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
- */
- function __destruct() {
- while ( count( $this->locksHeld ) ) {
- $pathsByType = [];
- foreach ( $this->locksHeld as $path => $locks ) {
- foreach ( $locks as $type => $count ) {
- $pathsByType[$type][] = $path;
- }
- }
- $this->unlockByType( $pathsByType );
- }
- }
-}
* @file
* @author Aaron Schulz
*/
+use Psr\Log\LoggerInterface;
/**
* Class to handle job queues stored in Redis
class JobQueueRedis extends JobQueue {
/** @var RedisConnectionPool */
protected $redisPool;
+ /** @var LoggerInterface */
+ protected $logger;
/** @var string Server address */
protected $server;
"Non-daemonized mode is no longer supported. Please install the " .
"mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
}
+ $this->logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'redis' );
}
protected function supportedOrders() {
* @throws JobQueueConnectionError
*/
protected function getConnection() {
- $conn = $this->redisPool->getConnection( $this->server );
+ $conn = $this->redisPool->getConnection( $this->server, $this->logger );
if ( !$conn ) {
throw new JobQueueConnectionError(
"Unable to connect to redis server {$this->server}." );
* @file
* @author Aaron Schulz
*/
+use Psr\Log\LoggerInterface;
/**
* Class to handle tracking information about all queues using PhpRedis
class JobQueueAggregatorRedis extends JobQueueAggregator {
/** @var RedisConnectionPool */
protected $redisPool;
+ /** @var LoggerInterface */
+ protected $logger;
/** @var array List of Redis server addresses */
protected $servers;
: [ $params['redisServer'] ]; // b/c
$params['redisConfig']['serializer'] = 'none';
$this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
+ $this->logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'redis' );
}
protected function doNotifyQueueEmpty( $wiki, $type ) {
protected function getConnection() {
$conn = false;
foreach ( $this->servers as $server ) {
- $conn = $this->redisPool->getConnection( $server );
+ $conn = $this->redisPool->getConnection( $server, $this->logger );
if ( $conn ) {
break;
}
protected $lockExpiry; // integer number of seconds
protected $safeDelay; // integer number of seconds
-
- protected $session = 0; // random integer
/** @var IDatabase[] Map Database connections (DB name => Database) */
protected $conns = [];
$this->statusCache = isset( $config['srvCache'] )
? $config['srvCache']
: new HashBagOStuff();
-
- $random = [];
- for ( $i = 1; $i <= 5; ++$i ) {
- $random[] = mt_rand( 0, 0xFFFFFFF );
- }
- $this->session = substr( md5( implode( '-', $random ) ), 0, 31 );
}
/**
* @defgroup LockManager Lock management
* @ingroup FileBackend
*/
+use Psr\Log\LoggerInterface;
/**
* Resource locking handling.
* @since 1.19
*/
abstract class LockManager {
+ /** @var LoggerInterface */
+ protected $logger;
+
/** @var array Mapping of lock types to the type actually used */
protected $lockTypeMap = [
self::LOCK_SH => self::LOCK_SH,
protected $domain; // string; domain (usually wiki ID)
protected $lockTTL; // integer; maximum time locks can be held
+ /** @var string Random 32-char hex number */
+ protected $session;
+
/** Lock types; stronger locks have higher values */
const LOCK_SH = 1; // shared lock (for reads)
const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
$met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
$this->lockTTL = max( 5 * 60, 2 * (int)$met );
}
+
+ $random = [];
+ for ( $i = 1; $i <= 5; ++$i ) {
+ $random[] = mt_rand( 0, 0xFFFFFFF );
+ }
+ $this->session = md5( implode( '-', $random ) );
+
+ $this->logger = isset( $config['logger'] ) ? $config['logger'] : new \Psr\Log\NullLogger();
}
/**
--- /dev/null
+<?php
+/**
+ * Version of LockManager based on using redis servers.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup LockManager
+ */
+
+/**
+ * Manage locks using redis servers.
+ *
+ * Version of LockManager based on using redis servers.
+ * This is meant for multi-wiki systems that may share files.
+ * All locks are non-blocking, which avoids deadlocks.
+ *
+ * All lock requests for a resource, identified by a hash string, will map to one
+ * bucket. Each bucket maps to one or several peer servers, each running redis.
+ * A majority of peers must agree for a lock to be acquired.
+ *
+ * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
+ *
+ * @ingroup LockManager
+ * @since 1.22
+ */
+class RedisLockManager extends QuorumLockManager {
+ /** @var array Mapping of lock types to the type actually used */
+ protected $lockTypeMap = [
+ self::LOCK_SH => 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 "<IP>:<port>" 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 = call_user_func_array( '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 =
+<<<LUA
+ local failed = {}
+ -- Load input params (e.g. session, ttl, time of request)
+ local rSession, rTTL, rTime = unpack(ARGV)
+ -- Check that all the locks can be acquired
+ for i,requestKey in ipairs(KEYS) do
+ local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
+ local keyIsFree = true
+ local currentLocks = redis.call('hKeys',resourceKey)
+ for i,lockKey in ipairs(currentLocks) do
+ -- Get the type and session of this lock
+ local _, _, type, session = string.find(lockKey,"(%w+):(%w+)")
+ -- Check any locks that are not owned by this session
+ if session ~= rSession then
+ local lockExpiry = redis.call('hGet',resourceKey,lockKey)
+ if 1*lockExpiry < 1*rTime then
+ -- Lock is stale, so just prune it out
+ redis.call('hDel',resourceKey,lockKey)
+ elseif rType == 'EX' or type == 'EX' then
+ keyIsFree = false
+ break
+ end
+ end
+ end
+ if not keyIsFree then
+ failed[#failed+1] = requestKey
+ end
+ end
+ -- If all locks could be acquired, then do so
+ if #failed == 0 then
+ for i,requestKey in ipairs(KEYS) do
+ local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
+ redis.call('hSet',resourceKey,rType .. ':' .. rSession,rTime + rTTL)
+ -- In addition to invalidation logic, be sure to garbage collect
+ redis.call('expire',resourceKey,rTTL)
+ end
+ end
+ return failed
+LUA;
+ $res = $conn->luaEval( $script,
+ array_merge(
+ array_keys( $pathsByKey ), // KEYS[0], KEYS[1],...,KEYS[N]
+ [
+ $this->session, // ARGV[1]
+ $this->lockTTL, // ARGV[2]
+ time() // ARGV[3]
+ ]
+ ),
+ 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 = call_user_func_array( '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 =
+<<<LUA
+ local failed = {}
+ -- Load input params (e.g. session)
+ local rSession = unpack(ARGV)
+ for i,requestKey in ipairs(KEYS) do
+ local _, _, rType, resourceKey = string.find(requestKey,"(%w+):(%w+)$")
+ local released = redis.call('hDel',resourceKey,rType .. ':' .. rSession)
+ if released > 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
+ */
+ function __destruct() {
+ while ( count( $this->locksHeld ) ) {
+ $pathsByType = [];
+ foreach ( $this->locksHeld as $path => $locks ) {
+ foreach ( $locks as $type => $count ) {
+ $pathsByType[$type][] = $path;
+ }
+ }
+ $this->unlockByType( $pathsByType );
+ }
+ }
+}
--- /dev/null
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Aaron Schulz
+ */
+use Psr\Log\LoggerInterface;
+use Psr\Log\LoggerAwareInterface;
+
+/**
+ * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
+ *
+ * This class simply wraps the Redis class and can be used the same way
+ *
+ * @ingroup Redis
+ * @since 1.21
+ */
+class RedisConnRef implements LoggerAwareInterface {
+ /** @var RedisConnectionPool */
+ protected $pool;
+ /** @var Redis */
+ protected $conn;
+
+ protected $server; // string
+ protected $lastError; // string
+
+ /**
+ * @var LoggerInterface
+ */
+ protected $logger;
+
+ /**
+ * @param RedisConnectionPool $pool
+ * @param string $server
+ * @param Redis $conn
+ * @param LoggerInterface $logger
+ */
+ public function __construct(
+ RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
+ ) {
+ $this->pool = $pool;
+ $this->server = $server;
+ $this->conn = $conn;
+ $this->logger = $logger;
+ }
+
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
+ /**
+ * @return string
+ * @since 1.23
+ */
+ public function getServer() {
+ return $this->server;
+ }
+
+ public function getLastError() {
+ return $this->lastError;
+ }
+
+ public function clearLastError() {
+ $this->lastError = null;
+ }
+
+ public function __call( $name, $arguments ) {
+ $conn = $this->conn; // convenience
+
+ // Work around https://github.com/nicolasff/phpredis/issues/70
+ $lname = strtolower( $name );
+ if ( ( $lname === 'blpop' || $lname == 'brpop' )
+ && is_array( $arguments[0] ) && isset( $arguments[1] )
+ ) {
+ $this->pool->resetTimeout( $conn, $arguments[1] + 1 );
+ } elseif ( $lname === 'brpoplpush' && isset( $arguments[2] ) ) {
+ $this->pool->resetTimeout( $conn, $arguments[2] + 1 );
+ }
+
+ $conn->clearLastError();
+ try {
+ $res = call_user_func_array( [ $conn, $name ], $arguments );
+ if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
+ $this->pool->reauthenticateConnection( $this->server, $conn );
+ $conn->clearLastError();
+ $res = call_user_func_array( [ $conn, $name ], $arguments );
+ $this->logger->info(
+ "Used automatic re-authentication for method '$name'.",
+ [ 'redis_server' => $this->server ]
+ );
+ }
+ } catch ( RedisException $e ) {
+ $this->pool->resetTimeout( $conn ); // restore
+ throw $e;
+ }
+
+ $this->lastError = $conn->getLastError() ?: $this->lastError;
+
+ $this->pool->resetTimeout( $conn ); // restore
+
+ return $res;
+ }
+
+ /**
+ * @param string $script
+ * @param array $params
+ * @param int $numKeys
+ * @return mixed
+ * @throws RedisException
+ */
+ public function luaEval( $script, array $params, $numKeys ) {
+ $sha1 = sha1( $script ); // 40 char hex
+ $conn = $this->conn; // convenience
+ $server = $this->server; // convenience
+
+ // Try to run the server-side cached copy of the script
+ $conn->clearLastError();
+ $res = $conn->evalSha( $sha1, $params, $numKeys );
+ // If we got a permission error reply that means that (a) we are not in
+ // multi()/pipeline() and (b) some connection problem likely occurred. If
+ // the password the client gave was just wrong, an exception should have
+ // been thrown back in getConnection() previously.
+ if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
+ $this->pool->reauthenticateConnection( $server, $conn );
+ $conn->clearLastError();
+ $res = $conn->eval( $script, $params, $numKeys );
+ $this->logger->info(
+ "Used automatic re-authentication for Lua script '$sha1'.",
+ [ 'redis_server' => $server ]
+ );
+ }
+ // If the script is not in cache, use eval() to retry and cache it
+ if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
+ $conn->clearLastError();
+ $res = $conn->eval( $script, $params, $numKeys );
+ $this->logger->info(
+ "Used eval() for Lua script '$sha1'.",
+ [ 'redis_server' => $server ]
+ );
+ }
+
+ if ( $conn->getLastError() ) { // script bug?
+ $this->logger->error(
+ 'Lua script error on server "{redis_server}": {lua_error}',
+ [
+ 'redis_server' => $server,
+ 'lua_error' => $conn->getLastError()
+ ]
+ );
+ }
+
+ $this->lastError = $conn->getLastError() ?: $this->lastError;
+
+ return $res;
+ }
+
+ /**
+ * @param Redis $conn
+ * @return bool
+ */
+ public function isConnIdentical( Redis $conn ) {
+ return $this->conn === $conn;
+ }
+
+ function __destruct() {
+ $this->pool->freeConnection( $this->server, $this->conn );
+ }
+}
--- /dev/null
+<?php
+/**
+ * Redis client connection pooling manager.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @defgroup Redis Redis
+ * @author Aaron Schulz
+ */
+
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Helper class to manage Redis connections.
+ *
+ * This can be used to get handle wrappers that free the handle when the wrapper
+ * leaves scope. The maximum number of free handles (connections) is configurable.
+ * This provides an easy way to cache connection handles that may also have state,
+ * such as a handle does between multi() and exec(), and without hoarding connections.
+ * The wrappers use PHP magic methods so that calling functions on them calls the
+ * function of the actual Redis object handle.
+ *
+ * @ingroup Redis
+ * @since 1.21
+ */
+class RedisConnectionPool implements LoggerAwareInterface {
+ /** @var string Connection timeout in seconds */
+ protected $connectTimeout;
+ /** @var string Read timeout in seconds */
+ protected $readTimeout;
+ /** @var string Plaintext auth password */
+ protected $password;
+ /** @var bool Whether connections persist */
+ protected $persistent;
+ /** @var int Serializer to use (Redis::SERIALIZER_*) */
+ protected $serializer;
+
+ /** @var int Current idle pool size */
+ protected $idlePoolSize = 0;
+
+ /** @var array (server name => ((connection info array),...) */
+ protected $connections = [];
+ /** @var array (server name => UNIX timestamp) */
+ protected $downServers = [];
+
+ /** @var array (pool ID => RedisConnectionPool) */
+ protected static $instances = [];
+
+ /** integer; seconds to cache servers as "down". */
+ const SERVER_DOWN_TTL = 30;
+
+ /**
+ * @var LoggerInterface
+ */
+ protected $logger;
+
+ /**
+ * @param array $options
+ * @throws Exception
+ */
+ protected function __construct( array $options ) {
+ if ( !class_exists( 'Redis' ) ) {
+ throw new RuntimeException(
+ __CLASS__ . ' requires a Redis client library. ' .
+ 'See https://www.mediawiki.org/wiki/Redis#Setup' );
+ }
+ $this->logger = isset( $options['logger'] )
+ ? $options['logger']
+ : new \Psr\Log\NullLogger();
+ $this->connectTimeout = $options['connectTimeout'];
+ $this->readTimeout = $options['readTimeout'];
+ $this->persistent = $options['persistent'];
+ $this->password = $options['password'];
+ if ( !isset( $options['serializer'] ) || $options['serializer'] === 'php' ) {
+ $this->serializer = Redis::SERIALIZER_PHP;
+ } elseif ( $options['serializer'] === 'igbinary' ) {
+ $this->serializer = Redis::SERIALIZER_IGBINARY;
+ } elseif ( $options['serializer'] === 'none' ) {
+ $this->serializer = Redis::SERIALIZER_NONE;
+ } else {
+ throw new InvalidArgumentException( "Invalid serializer specified." );
+ }
+ }
+
+ /**
+ * @param LoggerInterface $logger
+ * @return null
+ */
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
+ /**
+ * @param array $options
+ * @return array
+ */
+ protected static function applyDefaultConfig( array $options ) {
+ if ( !isset( $options['connectTimeout'] ) ) {
+ $options['connectTimeout'] = 1;
+ }
+ if ( !isset( $options['readTimeout'] ) ) {
+ $options['readTimeout'] = 1;
+ }
+ if ( !isset( $options['persistent'] ) ) {
+ $options['persistent'] = false;
+ }
+ if ( !isset( $options['password'] ) ) {
+ $options['password'] = null;
+ }
+
+ return $options;
+ }
+
+ /**
+ * @param array $options
+ * $options include:
+ * - connectTimeout : The timeout for new connections, in seconds.
+ * Optional, default is 1 second.
+ * - readTimeout : The timeout for operation reads, in seconds.
+ * Commands like BLPOP can fail if told to wait longer than this.
+ * Optional, default is 1 second.
+ * - persistent : Set this to true to allow connections to persist across
+ * multiple web requests. False by default.
+ * - password : The authentication password, will be sent to Redis in clear text.
+ * Optional, if it is unspecified, no AUTH command will be sent.
+ * - serializer : Set to "php", "igbinary", or "none". Default is "php".
+ * @return RedisConnectionPool
+ */
+ public static function singleton( array $options ) {
+ $options = self::applyDefaultConfig( $options );
+ // Map the options to a unique hash...
+ ksort( $options ); // normalize to avoid pool fragmentation
+ $id = sha1( serialize( $options ) );
+ // Initialize the object at the hash as needed...
+ if ( !isset( self::$instances[$id] ) ) {
+ self::$instances[$id] = new self( $options );
+ }
+
+ return self::$instances[$id];
+ }
+
+ /**
+ * Destroy all singleton() instances
+ * @since 1.27
+ */
+ public static function destroySingletons() {
+ self::$instances = [];
+ }
+
+ /**
+ * Get a connection to a redis server. Based on code in RedisBagOStuff.php.
+ *
+ * @param string $server A hostname/port combination or the absolute path of a UNIX socket.
+ * If a hostname is specified but no port, port 6379 will be used.
+ * @param LoggerInterface $logger PSR-3 logger intance. [optional]
+ * @return RedisConnRef|bool Returns false on failure
+ * @throws MWException
+ */
+ public function getConnection( $server, LoggerInterface $logger = null ) {
+ $logger = $logger ?: $this->logger;
+ // Check the listing "dead" servers which have had a connection errors.
+ // Servers are marked dead for a limited period of time, to
+ // avoid excessive overhead from repeated connection timeouts.
+ if ( isset( $this->downServers[$server] ) ) {
+ $now = time();
+ if ( $now > $this->downServers[$server] ) {
+ // Dead time expired
+ unset( $this->downServers[$server] );
+ } else {
+ // Server is dead
+ $logger->debug(
+ 'Server "{redis_server}" is marked down for another ' .
+ ( $this->downServers[$server] - $now ) . 'seconds',
+ [ 'redis_server' => $server ]
+ );
+
+ return false;
+ }
+ }
+
+ // Check if a connection is already free for use
+ if ( isset( $this->connections[$server] ) ) {
+ foreach ( $this->connections[$server] as &$connection ) {
+ if ( $connection['free'] ) {
+ $connection['free'] = false;
+ --$this->idlePoolSize;
+
+ return new RedisConnRef(
+ $this, $server, $connection['conn'], $logger
+ );
+ }
+ }
+ }
+
+ if ( !$server ) {
+ throw new InvalidArgumentException(
+ __CLASS__ . ": invalid configured server \"$server\"" );
+ } elseif ( substr( $server, 0, 1 ) === '/' ) {
+ // UNIX domain socket
+ // These are required by the redis extension to start with a slash, but
+ // we still need to set the port to a special value to make it work.
+ $host = $server;
+ $port = 0;
+ } else {
+ // TCP connection
+ if ( preg_match( '/^\[(.+)\]:(\d+)$/', $server, $m ) ) {
+ list( $host, $port ) = [ $m[1], (int)$m[2] ]; // (ip, port)
+ } elseif ( preg_match( '/^([^:]+):(\d+)$/', $server, $m ) ) {
+ list( $host, $port ) = [ $m[1], (int)$m[2] ]; // (ip or path, port)
+ } else {
+ list( $host, $port ) = [ $server, 6379 ]; // (ip or path, port)
+ }
+ }
+
+ $conn = new Redis();
+ try {
+ if ( $this->persistent ) {
+ $result = $conn->pconnect( $host, $port, $this->connectTimeout );
+ } else {
+ $result = $conn->connect( $host, $port, $this->connectTimeout );
+ }
+ if ( !$result ) {
+ $logger->error(
+ 'Could not connect to server "{redis_server}"',
+ [ 'redis_server' => $server ]
+ );
+ // Mark server down for some time to avoid further timeouts
+ $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
+
+ return false;
+ }
+ if ( $this->password !== null ) {
+ if ( !$conn->auth( $this->password ) ) {
+ $logger->error(
+ 'Authentication error connecting to "{redis_server}"',
+ [ 'redis_server' => $server ]
+ );
+ }
+ }
+ } catch ( RedisException $e ) {
+ $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
+ $logger->error(
+ 'Redis exception connecting to "{redis_server}"',
+ [
+ 'redis_server' => $server,
+ 'exception' => $e,
+ ]
+ );
+
+ return false;
+ }
+
+ if ( $conn ) {
+ $conn->setOption( Redis::OPT_READ_TIMEOUT, $this->readTimeout );
+ $conn->setOption( Redis::OPT_SERIALIZER, $this->serializer );
+ $this->connections[$server][] = [ 'conn' => $conn, 'free' => false ];
+
+ return new RedisConnRef( $this, $server, $conn, $logger );
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Mark a connection to a server as free to return to the pool
+ *
+ * @param string $server
+ * @param Redis $conn
+ * @return bool
+ */
+ public function freeConnection( $server, Redis $conn ) {
+ $found = false;
+
+ foreach ( $this->connections[$server] as &$connection ) {
+ if ( $connection['conn'] === $conn && !$connection['free'] ) {
+ $connection['free'] = true;
+ ++$this->idlePoolSize;
+ break;
+ }
+ }
+
+ $this->closeExcessIdleConections();
+
+ return $found;
+ }
+
+ /**
+ * Close any extra idle connections if there are more than the limit
+ */
+ protected function closeExcessIdleConections() {
+ if ( $this->idlePoolSize <= count( $this->connections ) ) {
+ return; // nothing to do (no more connections than servers)
+ }
+
+ foreach ( $this->connections as &$serverConnections ) {
+ foreach ( $serverConnections as $key => &$connection ) {
+ if ( $connection['free'] ) {
+ unset( $serverConnections[$key] );
+ if ( --$this->idlePoolSize <= count( $this->connections ) ) {
+ return; // done (no more connections than servers)
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * The redis extension throws an exception in response to various read, write
+ * and protocol errors. Sometimes it also closes the connection, sometimes
+ * not. The safest response for us is to explicitly destroy the connection
+ * object and let it be reopened during the next request.
+ *
+ * @param string $server
+ * @param RedisConnRef $cref
+ * @param RedisException $e
+ * @deprecated since 1.23
+ */
+ public function handleException( $server, RedisConnRef $cref, RedisException $e ) {
+ $this->handleError( $cref, $e );
+ }
+
+ /**
+ * The redis extension throws an exception in response to various read, write
+ * and protocol errors. Sometimes it also closes the connection, sometimes
+ * not. The safest response for us is to explicitly destroy the connection
+ * object and let it be reopened during the next request.
+ *
+ * @param RedisConnRef $cref
+ * @param RedisException $e
+ */
+ public function handleError( RedisConnRef $cref, RedisException $e ) {
+ $server = $cref->getServer();
+ $this->logger->error(
+ 'Redis exception on server "{redis_server}"',
+ [
+ 'redis_server' => $server,
+ 'exception' => $e,
+ ]
+ );
+ foreach ( $this->connections[$server] as $key => $connection ) {
+ if ( $cref->isConnIdentical( $connection['conn'] ) ) {
+ $this->idlePoolSize -= $connection['free'] ? 1 : 0;
+ unset( $this->connections[$server][$key] );
+ break;
+ }
+ }
+ }
+
+ /**
+ * Re-send an AUTH request to the redis server (useful after disconnects).
+ *
+ * This works around an upstream bug in phpredis. phpredis hides disconnects by transparently
+ * reconnecting, but it neglects to re-authenticate the new connection. To the user of the
+ * phpredis client API this manifests as a seemingly random tendency of connections to lose
+ * their authentication status.
+ *
+ * This method is for internal use only.
+ *
+ * @see https://github.com/nicolasff/phpredis/issues/403
+ *
+ * @param string $server
+ * @param Redis $conn
+ * @return bool Success
+ */
+ public function reauthenticateConnection( $server, Redis $conn ) {
+ if ( $this->password !== null ) {
+ if ( !$conn->auth( $this->password ) ) {
+ $this->logger->error(
+ 'Authentication error connecting to "{redis_server}"',
+ [ 'redis_server' => $server ]
+ );
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Adjust or reset the connection handle read timeout value
+ *
+ * @param Redis $conn
+ * @param int $timeout Optional
+ */
+ public function resetTimeout( Redis $conn, $timeout = null ) {
+ $conn->setOption( Redis::OPT_READ_TIMEOUT, $timeout ?: $this->readTimeout );
+ }
+
+ /**
+ * Make sure connections are closed for sanity
+ */
+ function __destruct() {
+ foreach ( $this->connections as $server => &$serverConnections ) {
+ foreach ( $serverConnections as $key => &$connection ) {
+ /** @var Redis $conn */
+ $conn = $connection['conn'];
+ $conn->close();
+ }
+ }
+ }
+}
while ( ( $tag = array_shift( $candidates ) ) !== null ) {
$server = $this->serverTagMap[$tag];
- $conn = $this->redisPool->getConnection( $server );
+ $conn = $this->redisPool->getConnection( $server, $this->logger );
if ( !$conn ) {
continue;
}
* @file
* @author Aaron Schulz
*/
+use Psr\Log\LoggerInterface;
/**
* Version of PoolCounter that uses Redis
protected $ring;
/** @var RedisConnectionPool */
protected $pool;
+ /** @var LoggerInterface */
+ protected $logger;
/** @var array (server label => host) map */
protected $serversByLabel;
/** @var string SHA-1 of the key */
$conf['redisConfig']['serializer'] = 'none'; // for use with Lua
$this->pool = RedisConnectionPool::singleton( $conf['redisConfig'] );
+ $this->logger = \MediaWiki\Logger\LoggerFactory::getInstance( 'redis' );
$this->keySha1 = sha1( $this->key );
$met = ini_get( 'max_execution_time' ); // usually 0 in CLI mode
$servers = $this->ring->getLocations( $this->key, 3 );
ArrayUtils::consistentHashSort( $servers, $this->key );
foreach ( $servers as $server ) {
- $conn = $this->pool->getConnection( $this->serversByLabel[$server] );
+ $conn = $this->pool->getConnection( $this->serversByLabel[$server], $this->logger );
if ( $conn ) {
break;
}
if ( preg_match( '/^namespace=(\d+)$/', $bit, $m ) ) {
$opts['namespace'] = $m[1];
}
+ if ( preg_match( '/^tagfilter=(.*)$/', $bit, $m ) ) {
+ $opts['tagfilter'] = $m[1];
+ }
}
}