MediaWiki  1.33.0
RedisConnRef.php
Go to the documentation of this file.
1 <?php
20 use Psr\Log\LoggerInterface;
21 use Psr\Log\LoggerAwareInterface;
22 
31 class RedisConnRef implements LoggerAwareInterface {
33  protected $pool;
35  protected $conn;
37  protected $server;
39  protected $lastError;
40 
44  protected $logger;
45 
49  const AUTH_NO_ERROR = 200;
50 
54  const AUTH_ERROR_TEMPORARY = 201;
55 
59  const AUTH_ERROR_PERMANENT = 202;
60 
67  public function __construct(
68  RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
69  ) {
70  $this->pool = $pool;
71  $this->server = $server;
72  $this->conn = $conn;
73  $this->logger = $logger;
74  }
75 
76  public function setLogger( LoggerInterface $logger ) {
77  $this->logger = $logger;
78  }
79 
84  public function getServer() {
85  return $this->server;
86  }
87 
88  public function getLastError() {
89  return $this->lastError;
90  }
91 
92  public function clearLastError() {
93  $this->lastError = null;
94  }
95 
104  public function __call( $name, $arguments ) {
105  // Work around https://github.com/nicolasff/phpredis/issues/70
106  $lname = strtolower( $name );
107  if (
108  ( $lname === 'blpop' || $lname === 'brpop' || $lname === 'brpoplpush' )
109  && count( $arguments ) > 1
110  ) {
111  // Get timeout off the end since it is always required and argument length can vary
112  $timeout = end( $arguments );
113  // Only give the additional one second buffer if not requesting an infinite timeout
114  $this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) );
115  }
116 
117  return $this->tryCall( $name, $arguments );
118  }
119 
128  private function tryCall( $method, $arguments ) {
129  $this->conn->clearLastError();
130  try {
131  $res = $this->conn->$method( ...$arguments );
132  $authError = $this->checkAuthentication();
133  if ( $authError === self::AUTH_ERROR_TEMPORARY ) {
134  $res = $this->conn->$method( ...$arguments );
135  }
136  if ( $authError === self::AUTH_ERROR_PERMANENT ) {
137  throw new RedisException( "Failure reauthenticating to Redis." );
138  }
139  } finally {
140  $this->postCallCleanup();
141  }
142 
143  return $res;
144  }
145 
156  public function scan( &$iterator, $pattern = null, $count = null ) {
157  return $this->tryCall( 'scan', [ &$iterator, $pattern, $count ] );
158  }
159 
171  public function sScan( $key, &$iterator, $pattern = null, $count = null ) {
172  return $this->tryCall( 'sScan', [ $key, &$iterator, $pattern, $count ] );
173  }
174 
186  public function hScan( $key, &$iterator, $pattern = null, $count = null ) {
187  return $this->tryCall( 'hScan', [ $key, &$iterator, $pattern, $count ] );
188  }
189 
201  public function zScan( $key, &$iterator, $pattern = null, $count = null ) {
202  return $this->tryCall( 'zScan', [ $key, &$iterator, $pattern, $count ] );
203  }
204 
210  private function checkAuthentication() {
211  if ( preg_match( '/^ERR operation not permitted\b/', $this->conn->getLastError() ) ) {
212  if ( !$this->pool->reauthenticateConnection( $this->server, $this->conn ) ) {
214  }
215  $this->conn->clearLastError();
216  $this->logger->info(
217  "Used automatic re-authentication for Redis.",
218  [ 'redis_server' => $this->server ]
219  );
221  }
222  return self::AUTH_NO_ERROR;
223  }
224 
230  private function postCallCleanup() {
231  $this->lastError = $this->conn->getLastError() ?: $this->lastError;
232 
233  // Restore original timeout in the case of blocking calls.
234  $this->pool->resetTimeout( $this->conn );
235  }
236 
244  public function luaEval( $script, array $params, $numKeys ) {
245  $sha1 = sha1( $script ); // 40 char hex
246  $conn = $this->conn; // convenience
247  $server = $this->server; // convenience
248 
249  // Try to run the server-side cached copy of the script
250  $conn->clearLastError();
251  $res = $conn->evalSha( $sha1, $params, $numKeys );
252  // If we got a permission error reply that means that (a) we are not in
253  // multi()/pipeline() and (b) some connection problem likely occurred. If
254  // the password the client gave was just wrong, an exception should have
255  // been thrown back in getConnection() previously.
256  if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
257  $this->pool->reauthenticateConnection( $server, $conn );
258  $conn->clearLastError();
259  $res = $conn->eval( $script, $params, $numKeys );
260  $this->logger->info(
261  "Used automatic re-authentication for Lua script '$sha1'.",
262  [ 'redis_server' => $server ]
263  );
264  }
265  // If the script is not in cache, use eval() to retry and cache it
266  if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
267  $conn->clearLastError();
268  $res = $conn->eval( $script, $params, $numKeys );
269  $this->logger->info(
270  "Used eval() for Lua script '$sha1'.",
271  [ 'redis_server' => $server ]
272  );
273  }
274 
275  if ( $conn->getLastError() ) { // script bug?
276  $this->logger->error(
277  'Lua script error on server "{redis_server}": {lua_error}',
278  [
279  'redis_server' => $server,
280  'lua_error' => $conn->getLastError()
281  ]
282  );
283  }
284 
285  $this->lastError = $conn->getLastError() ?: $this->lastError;
286 
287  return $res;
288  }
289 
294  public function isConnIdentical( Redis $conn ) {
295  return $this->conn === $conn;
296  }
297 
298  function __destruct() {
299  $this->pool->freeConnection( $this->server, $this->conn );
300  }
301 }
RedisConnRef\tryCall
tryCall( $method, $arguments)
Do the method call in the common try catch handler.
Definition: RedisConnRef.php:128
RedisConnRef\hScan
hScan( $key, &$iterator, $pattern=null, $count=null)
Hash Scan Handle this explicity due to needing the iterator passed by reference.
Definition: RedisConnRef.php:186
RedisConnRef\__destruct
__destruct()
Definition: RedisConnRef.php:298
RedisConnRef\$logger
LoggerInterface $logger
Definition: RedisConnRef.php:44
captcha-old.count
count
Definition: captcha-old.py:249
RedisConnRef\scan
scan(&$iterator, $pattern=null, $count=null)
Key Scan Handle this explicity due to needing the iterator passed by reference.
Definition: RedisConnRef.php:156
$params
$params
Definition: styleTest.css.php:44
RedisConnRef\zScan
zScan( $key, &$iterator, $pattern=null, $count=null)
Sorted Set Scan Handle this explicity due to needing the iterator passed by reference.
Definition: RedisConnRef.php:201
$res
$res
Definition: database.txt:21
RedisConnRef\__call
__call( $name, $arguments)
Magic __call handler for most Redis functions.
Definition: RedisConnRef.php:104
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
RedisConnRef\isConnIdentical
isConnIdentical(Redis $conn)
Definition: RedisConnRef.php:294
RedisConnRef\$server
string $server
Definition: RedisConnRef.php:37
RedisConnRef\$conn
Redis $conn
Definition: RedisConnRef.php:35
RedisConnRef\AUTH_NO_ERROR
const AUTH_NO_ERROR
No authentication errors.
Definition: RedisConnRef.php:49
RedisConnRef\getLastError
getLastError()
Definition: RedisConnRef.php:88
RedisConnRef\postCallCleanup
postCallCleanup()
Post Redis call cleanup.
Definition: RedisConnRef.php:230
RedisConnRef\checkAuthentication
checkAuthentication()
Handle authentication errors and automatically reauthenticate.
Definition: RedisConnRef.php:210
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
RedisConnRef\$pool
RedisConnectionPool $pool
Definition: RedisConnRef.php:33
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
RedisConnectionPool
Helper class to manage Redis connections.
Definition: RedisConnectionPool.php:40
RedisConnRef\luaEval
luaEval( $script, array $params, $numKeys)
Definition: RedisConnRef.php:244
RedisConnRef\$lastError
string null $lastError
Definition: RedisConnRef.php:39
RedisConnRef\clearLastError
clearLastError()
Definition: RedisConnRef.php:92
RedisConnRef
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
Definition: RedisConnRef.php:31
RedisConnRef\setLogger
setLogger(LoggerInterface $logger)
Definition: RedisConnRef.php:76
RedisConnRef\__construct
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
Definition: RedisConnRef.php:67
RedisConnRef\getServer
getServer()
Definition: RedisConnRef.php:84
RedisConnRef\AUTH_ERROR_TEMPORARY
const AUTH_ERROR_TEMPORARY
Temporary authentication error; recovered by reauthenticating.
Definition: RedisConnRef.php:54
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
RedisConnRef\AUTH_ERROR_PERMANENT
const AUTH_ERROR_PERMANENT
Authentication error was permanent and could not be recovered.
Definition: RedisConnRef.php:59
RedisConnRef\sScan
sScan( $key, &$iterator, $pattern=null, $count=null)
Set Scan Handle this explicity due to needing the iterator passed by reference.
Definition: RedisConnRef.php:171