MediaWiki  1.23.6
RedisConnectionPool.php
Go to the documentation of this file.
1 <?php
46  protected $connectTimeout;
48  protected $password;
50  protected $persistent;
52  protected $serializer;
56  protected $idlePoolSize = 0;
57 
59  protected $connections = array();
61  protected $downServers = array();
62 
64  protected static $instances = array();
65 
67  const SERVER_DOWN_TTL = 30;
68 
73  protected function __construct( array $options ) {
74  if ( !class_exists( 'Redis' ) ) {
75  throw new MWException( __CLASS__ . ' requires a Redis client library. ' .
76  'See https://www.mediawiki.org/wiki/Redis#Setup' );
77  }
78  $this->connectTimeout = $options['connectTimeout'];
79  $this->persistent = $options['persistent'];
80  $this->password = $options['password'];
81  if ( !isset( $options['serializer'] ) || $options['serializer'] === 'php' ) {
82  $this->serializer = Redis::SERIALIZER_PHP;
83  } elseif ( $options['serializer'] === 'igbinary' ) {
84  $this->serializer = Redis::SERIALIZER_IGBINARY;
85  } elseif ( $options['serializer'] === 'none' ) {
86  $this->serializer = Redis::SERIALIZER_NONE;
87  } else {
88  throw new MWException( "Invalid serializer specified." );
89  }
90  }
91 
96  protected static function applyDefaultConfig( array $options ) {
97  if ( !isset( $options['connectTimeout'] ) ) {
98  $options['connectTimeout'] = 1;
99  }
100  if ( !isset( $options['persistent'] ) ) {
101  $options['persistent'] = false;
102  }
103  if ( !isset( $options['password'] ) ) {
104  $options['password'] = null;
105  }
106 
107  return $options;
108  }
109 
122  public static function singleton( array $options ) {
124  // Map the options to a unique hash...
125  ksort( $options ); // normalize to avoid pool fragmentation
126  $id = sha1( serialize( $options ) );
127  // Initialize the object at the hash as needed...
128  if ( !isset( self::$instances[$id] ) ) {
129  self::$instances[$id] = new self( $options );
130  wfDebug( "Creating a new " . __CLASS__ . " instance with id $id.\n" );
131  }
132 
133  return self::$instances[$id];
134  }
135 
144  public function getConnection( $server ) {
145  // Check the listing "dead" servers which have had a connection errors.
146  // Servers are marked dead for a limited period of time, to
147  // avoid excessive overhead from repeated connection timeouts.
148  if ( isset( $this->downServers[$server] ) ) {
149  $now = time();
150  if ( $now > $this->downServers[$server] ) {
151  // Dead time expired
152  unset( $this->downServers[$server] );
153  } else {
154  // Server is dead
155  wfDebug( "server $server is marked down for another " .
156  ( $this->downServers[$server] - $now ) . " seconds, can't get connection\n" );
157 
158  return false;
159  }
160  }
161 
162  // Check if a connection is already free for use
163  if ( isset( $this->connections[$server] ) ) {
164  foreach ( $this->connections[$server] as &$connection ) {
165  if ( $connection['free'] ) {
166  $connection['free'] = false;
168 
169  return new RedisConnRef( $this, $server, $connection['conn'] );
170  }
171  }
172  }
173 
174  if ( substr( $server, 0, 1 ) === '/' ) {
175  // UNIX domain socket
176  // These are required by the redis extension to start with a slash, but
177  // we still need to set the port to a special value to make it work.
178  $host = $server;
179  $port = 0;
180  } else {
181  // TCP connection
182  $hostPort = IP::splitHostAndPort( $server );
183  if ( !$hostPort ) {
184  throw new MWException( __CLASS__ . ": invalid configured server \"$server\"" );
185  }
186  list( $host, $port ) = $hostPort;
187  if ( $port === false ) {
188  $port = 6379;
189  }
190  }
191 
192  $conn = new Redis();
193  try {
194  if ( $this->persistent ) {
195  $result = $conn->pconnect( $host, $port, $this->connectTimeout );
196  } else {
197  $result = $conn->connect( $host, $port, $this->connectTimeout );
198  }
199  if ( !$result ) {
200  wfDebugLog( 'redis', "Could not connect to server $server" );
201  // Mark server down for some time to avoid further timeouts
202  $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
203 
204  return false;
205  }
206  if ( $this->password !== null ) {
207  if ( !$conn->auth( $this->password ) ) {
208  wfDebugLog( 'redis', "Authentication error connecting to $server" );
209  }
210  }
211  } catch ( RedisException $e ) {
212  $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
213  wfDebugLog( 'redis', "Redis exception connecting to $server: " . $e->getMessage() );
214 
215  return false;
216  }
217 
218  if ( $conn ) {
219  $conn->setOption( Redis::OPT_SERIALIZER, $this->serializer );
220  $this->connections[$server][] = array( 'conn' => $conn, 'free' => false );
221 
222  return new RedisConnRef( $this, $server, $conn );
223  } else {
224  return false;
225  }
226  }
227 
235  public function freeConnection( $server, Redis $conn ) {
236  $found = false;
237 
238  foreach ( $this->connections[$server] as &$connection ) {
239  if ( $connection['conn'] === $conn && !$connection['free'] ) {
240  $connection['free'] = true;
242  break;
243  }
244  }
245 
246  $this->closeExcessIdleConections();
247 
248  return $found;
249  }
250 
254  protected function closeExcessIdleConections() {
255  if ( $this->idlePoolSize <= count( $this->connections ) ) {
256  return; // nothing to do (no more connections than servers)
257  }
258 
259  foreach ( $this->connections as &$serverConnections ) {
260  foreach ( $serverConnections as $key => &$connection ) {
261  if ( $connection['free'] ) {
262  unset( $serverConnections[$key] );
263  if ( --$this->idlePoolSize <= count( $this->connections ) ) {
264  return; // done (no more connections than servers)
265  }
266  }
267  }
268  }
269  }
270 
282  public function handleException( $server, RedisConnRef $cref, RedisException $e ) {
283  return $this->handleError( $cref, $e );
284  }
285 
295  public function handleError( RedisConnRef $cref, RedisException $e ) {
296  $server = $cref->getServer();
297  wfDebugLog( 'redis', "Redis exception on server $server: " . $e->getMessage() . "\n" );
298  foreach ( $this->connections[$server] as $key => $connection ) {
299  if ( $cref->isConnIdentical( $connection['conn'] ) ) {
300  $this->idlePoolSize -= $connection['free'] ? 1 : 0;
301  unset( $this->connections[$server][$key] );
302  break;
303  }
304  }
305  }
306 
323  public function reauthenticateConnection( $server, Redis $conn ) {
324  if ( $this->password !== null ) {
325  if ( !$conn->auth( $this->password ) ) {
326  wfDebugLog( 'redis', "Authentication error connecting to $server" );
327 
328  return false;
329  }
330  }
331 
332  return true;
333  }
334 
338  function __destruct() {
339  foreach ( $this->connections as $server => &$serverConnections ) {
340  foreach ( $serverConnections as $key => &$connection ) {
341  $connection['conn']->close();
342  }
343  }
344  }
345 }
346 
355 class RedisConnRef {
357  protected $pool;
359  protected $conn;
360 
361  protected $server; // string
362  protected $lastError; // string
363 
369  public function __construct( RedisConnectionPool $pool, $server, Redis $conn ) {
370  $this->pool = $pool;
371  $this->server = $server;
372  $this->conn = $conn;
373  }
374 
379  public function getServer() {
380  return $this->server;
381  }
382 
383  public function getLastError() {
384  return $this->lastError;
385  }
386 
387  public function clearLastError() {
388  $this->lastError = null;
389  }
390 
391  public function __call( $name, $arguments ) {
392  $conn = $this->conn; // convenience
393 
394  $conn->clearLastError();
395  $res = call_user_func_array( array( $conn, $name ), $arguments );
396  if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
397  $this->pool->reauthenticateConnection( $this->server, $conn );
398  $conn->clearLastError();
399  $res = call_user_func_array( array( $conn, $name ), $arguments );
400  wfDebugLog( 'redis', "Used automatic re-authentication for method '$name'." );
401  }
402 
403  $this->lastError = $conn->getLastError() ?: $this->lastError;
404 
405  return $res;
406  }
407 
415  public function luaEval( $script, array $params, $numKeys ) {
416  $sha1 = sha1( $script ); // 40 char hex
417  $conn = $this->conn; // convenience
418  $server = $this->server; // convenience
419 
420  // Try to run the server-side cached copy of the script
421  $conn->clearLastError();
422  $res = $conn->evalSha( $sha1, $params, $numKeys );
423  // If we got a permission error reply that means that (a) we are not in
424  // multi()/pipeline() and (b) some connection problem likely occurred. If
425  // the password the client gave was just wrong, an exception should have
426  // been thrown back in getConnection() previously.
427  if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
428  $this->pool->reauthenticateConnection( $server, $conn );
429  $conn->clearLastError();
430  $res = $conn->eval( $script, $params, $numKeys );
431  wfDebugLog( 'redis', "Used automatic re-authentication for Lua script $sha1." );
432  }
433  // If the script is not in cache, use eval() to retry and cache it
434  if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
435  $conn->clearLastError();
436  $res = $conn->eval( $script, $params, $numKeys );
437  wfDebugLog( 'redis', "Used eval() for Lua script $sha1." );
438  }
439 
440  if ( $conn->getLastError() ) { // script bug?
441  wfDebugLog( 'redis', "Lua script error on server $server: " . $conn->getLastError() );
442  }
443 
444  $this->lastError = $conn->getLastError() ?: $this->lastError;
445 
446  return $res;
447  }
448 
453  public function isConnIdentical( Redis $conn ) {
454  return $this->conn === $conn;
455  }
456 
457  function __destruct() {
458  $this->pool->freeConnection( $this->server, $this->conn );
459  }
460 }
RedisConnectionPool\freeConnection
freeConnection( $server, Redis $conn)
Mark a connection to a server as free to return to the pool.
Definition: RedisConnectionPool.php:228
RedisConnectionPool\SERVER_DOWN_TTL
const SERVER_DOWN_TTL
integer; seconds to cache servers as "down".
Definition: RedisConnectionPool.php:60
RedisConnectionPool\$downServers
array $downServers
(server name => UNIX timestamp) *
Definition: RedisConnectionPool.php:54
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. $reader:XMLReader object $logInfo:Array of information Return false to stop further processing of the tag 'ImportHandlePageXMLTag':When parsing a XML tag in a page. $reader:XMLReader object $pageInfo:Array of information Return false to stop further processing of the tag 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information Return false to stop further processing of the tag 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. $reader:XMLReader object Return false to stop further processing of the tag 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. $reader:XMLReader object $revisionInfo:Array of information Return false to stop further processing of the tag 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. $title:Title object for the current page $request:WebRequest $ignoreRedirect:boolean to skip redirect check $target:Title/string of redirect target $article:Article object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) $article:article(object) being checked 'IsTrustedProxy':Override the result of wfIsTrustedProxy() $ip:IP being check $result:Change this value to override the result of wfIsTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of User::isValidEmailAddr(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetMagic':DEPRECATED, use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetSpecialPageAliases':DEPRECATED, use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Associative array mapping language codes to prefixed links of the form "language:title". & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LinkBegin':Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1528
RedisConnectionPool\singleton
static singleton(array $options)
Definition: RedisConnectionPool.php:115
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
RedisConnRef\__destruct
__destruct()
Definition: RedisConnectionPool.php:448
RedisConnectionPool\__construct
__construct(array $options)
Definition: RedisConnectionPool.php:66
RedisConnectionPool\$connections
array $connections
(server name => ((connection info array),...) *
Definition: RedisConnectionPool.php:53
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
RedisConnectionPool\handleError
handleError(RedisConnRef $cref, RedisException $e)
The redis extension throws an exception in response to various read, write and protocol errors.
Definition: RedisConnectionPool.php:288
$params
$params
Definition: styleTest.css.php:40
RedisConnRef\__call
__call( $name, $arguments)
Definition: RedisConnectionPool.php:382
RedisConnRef\isConnIdentical
isConnIdentical(Redis $conn)
Definition: RedisConnectionPool.php:444
RedisConnectionPool\closeExcessIdleConections
closeExcessIdleConections()
Close any extra idle connections if there are more than the limit.
Definition: RedisConnectionPool.php:247
RedisConnectionPool\handleException
handleException( $server, RedisConnRef $cref, RedisException $e)
The redis extension throws an exception in response to various read, write and protocol errors.
Definition: RedisConnectionPool.php:275
RedisConnRef\$conn
Redis $conn
Definition: RedisConnectionPool.php:350
MWException
MediaWiki exception.
Definition: MWException.php:26
RedisConnectionPool\getConnection
getConnection( $server)
Get a connection to a redis server.
Definition: RedisConnectionPool.php:137
RedisConnectionPool\$password
string $password
Plaintext auth password *.
Definition: RedisConnectionPool.php:46
RedisConnRef\getLastError
getLastError()
Definition: RedisConnectionPool.php:374
RedisConnectionPool\$serializer
int $serializer
Serializer to use (Redis::SERIALIZER_*) *.
Definition: RedisConnectionPool.php:48
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
RedisConnectionPool\applyDefaultConfig
static applyDefaultConfig(array $options)
Definition: RedisConnectionPool.php:89
RedisConnRef\$server
$server
Definition: RedisConnectionPool.php:352
RedisConnRef\$pool
RedisConnectionPool $pool
Definition: RedisConnectionPool.php:349
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
RedisConnectionPool\$instances
static $instances
Definition: RedisConnectionPool.php:57
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
RedisConnRef\__construct
__construct(RedisConnectionPool $pool, $server, Redis $conn)
Definition: RedisConnectionPool.php:360
RedisConnectionPool\$idlePoolSize
int $idlePoolSize
Current idle pool size *.
Definition: RedisConnectionPool.php:51
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
RedisConnRef\$lastError
$lastError
Definition: RedisConnectionPool.php:353
RedisConnectionPool
Helper class to manage Redis connections.
Definition: RedisConnectionPool.php:38
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
IP\splitHostAndPort
static splitHostAndPort( $both)
Given a host/port string, like one might find in the host part of a URL per RFC 2732,...
Definition: IP.php:240
RedisConnectionPool\reauthenticateConnection
reauthenticateConnection( $server, Redis $conn)
Re-send an AUTH request to the redis server (useful after disconnects).
Definition: RedisConnectionPool.php:316
RedisConnRef\luaEval
luaEval( $script, array $params, $numKeys)
Definition: RedisConnectionPool.php:406
RedisConnectionPool\$connectTimeout
string $connectTimeout
Connection timeout in seconds *.
Definition: RedisConnectionPool.php:45
RedisConnRef\clearLastError
clearLastError()
Definition: RedisConnectionPool.php:378
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
RedisConnRef
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
Definition: RedisConnectionPool.php:348
RedisConnectionPool\$persistent
bool $persistent
Whether connections persist *.
Definition: RedisConnectionPool.php:47
RedisConnectionPool\__destruct
__destruct()
Make sure connections are closed for sanity.
Definition: RedisConnectionPool.php:331
RedisConnRef\getServer
getServer()
Definition: RedisConnectionPool.php:370
$e
if( $useReadline) $e
Definition: eval.php:66
$res
$res
Definition: database.txt:21
server
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such and we might be restricted by PHP settings such as safe mode or open_basedir We cannot assume that the software even has read access anywhere useful Many shared hosts run all users web applications under the same so they can t rely on Unix and must forbid reads to even standard directories like tmp lest users read each others files We cannot assume that the user has the ability to install or run any programs not written as web accessible PHP scripts Since anything that works on cheap shared hosting will work if you have shell or root access MediaWiki s design is based around catering to the lowest common denominator Although we support higher end setups as the way many things work by default is tailored toward shared hosting These defaults are unconventional from the point of view of and they certainly aren t ideal for someone who s installing MediaWiki as MediaWiki does not conform to normal Unix filesystem layout Hopefully we ll offer direct support for standard layouts in the but for now *any change to the location of files is unsupported *Moving things and leaving symlinks will *probably *not break but it is *strongly *advised not to try any more intrusive changes to get MediaWiki to conform more closely to your filesystem hierarchy Any such attempt will almost certainly result in unnecessary bugs The standard recommended location to install relative to the web is it should be possible to enable the appropriate rewrite rules by if you can reconfigure the web server
Definition: distributors.txt:53