MediaWiki  1.29.1
RedisConnRef.php
Go to the documentation of this file.
1 <?php
21 use Psr\Log\LoggerInterface;
22 use Psr\Log\LoggerAwareInterface;
23 
32 class RedisConnRef implements LoggerAwareInterface {
34  protected $pool;
36  protected $conn;
37 
38  protected $server; // string
39  protected $lastError; // string
40 
44  protected $logger;
45 
52  public function __construct(
53  RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
54  ) {
55  $this->pool = $pool;
56  $this->server = $server;
57  $this->conn = $conn;
58  $this->logger = $logger;
59  }
60 
61  public function setLogger( LoggerInterface $logger ) {
62  $this->logger = $logger;
63  }
64 
69  public function getServer() {
70  return $this->server;
71  }
72 
73  public function getLastError() {
74  return $this->lastError;
75  }
76 
77  public function clearLastError() {
78  $this->lastError = null;
79  }
80 
81  public function __call( $name, $arguments ) {
82  $conn = $this->conn; // convenience
83 
84  // Work around https://github.com/nicolasff/phpredis/issues/70
85  $lname = strtolower( $name );
86  if ( ( $lname === 'blpop' || $lname == 'brpop' )
87  && is_array( $arguments[0] ) && isset( $arguments[1] )
88  ) {
89  $this->pool->resetTimeout( $conn, $arguments[1] + 1 );
90  } elseif ( $lname === 'brpoplpush' && isset( $arguments[2] ) ) {
91  $this->pool->resetTimeout( $conn, $arguments[2] + 1 );
92  }
93 
94  $conn->clearLastError();
95  try {
96  $res = call_user_func_array( [ $conn, $name ], $arguments );
97  if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
98  $this->pool->reauthenticateConnection( $this->server, $conn );
99  $conn->clearLastError();
100  $res = call_user_func_array( [ $conn, $name ], $arguments );
101  $this->logger->info(
102  "Used automatic re-authentication for method '$name'.",
103  [ 'redis_server' => $this->server ]
104  );
105  }
106  } catch ( RedisException $e ) {
107  $this->pool->resetTimeout( $conn ); // restore
108  throw $e;
109  }
110 
111  $this->lastError = $conn->getLastError() ?: $this->lastError;
112 
113  $this->pool->resetTimeout( $conn ); // restore
114 
115  return $res;
116  }
117 
125  public function luaEval( $script, array $params, $numKeys ) {
126  $sha1 = sha1( $script ); // 40 char hex
127  $conn = $this->conn; // convenience
128  $server = $this->server; // convenience
129 
130  // Try to run the server-side cached copy of the script
131  $conn->clearLastError();
132  $res = $conn->evalSha( $sha1, $params, $numKeys );
133  // If we got a permission error reply that means that (a) we are not in
134  // multi()/pipeline() and (b) some connection problem likely occurred. If
135  // the password the client gave was just wrong, an exception should have
136  // been thrown back in getConnection() previously.
137  if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
138  $this->pool->reauthenticateConnection( $server, $conn );
139  $conn->clearLastError();
140  $res = $conn->eval( $script, $params, $numKeys );
141  $this->logger->info(
142  "Used automatic re-authentication for Lua script '$sha1'.",
143  [ 'redis_server' => $server ]
144  );
145  }
146  // If the script is not in cache, use eval() to retry and cache it
147  if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
148  $conn->clearLastError();
149  $res = $conn->eval( $script, $params, $numKeys );
150  $this->logger->info(
151  "Used eval() for Lua script '$sha1'.",
152  [ 'redis_server' => $server ]
153  );
154  }
155 
156  if ( $conn->getLastError() ) { // script bug?
157  $this->logger->error(
158  'Lua script error on server "{redis_server}": {lua_error}',
159  [
160  'redis_server' => $server,
161  'lua_error' => $conn->getLastError()
162  ]
163  );
164  }
165 
166  $this->lastError = $conn->getLastError() ?: $this->lastError;
167 
168  return $res;
169  }
170 
175  public function isConnIdentical( Redis $conn ) {
176  return $this->conn === $conn;
177  }
178 
179  function __destruct() {
180  $this->pool->freeConnection( $this->server, $this->conn );
181  }
182 }
RedisConnRef\__destruct
__destruct()
Definition: RedisConnRef.php:179
RedisConnRef\$logger
LoggerInterface $logger
Definition: RedisConnRef.php:44
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
$params
$params
Definition: styleTest.css.php:40
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
RedisConnRef\__call
__call( $name, $arguments)
Definition: RedisConnRef.php:81
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:175
RedisConnRef\$conn
Redis $conn
Definition: RedisConnRef.php:36
RedisConnRef\getLastError
getLastError()
Definition: RedisConnRef.php:73
RedisConnRef\$server
$server
Definition: RedisConnRef.php:38
RedisConnRef\$pool
RedisConnectionPool $pool
Definition: RedisConnRef.php:34
RedisConnRef\$lastError
$lastError
Definition: RedisConnRef.php:39
RedisConnectionPool
Helper class to manage Redis connections.
Definition: RedisConnectionPool.php:41
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
RedisConnRef\luaEval
luaEval( $script, array $params, $numKeys)
Definition: RedisConnRef.php:125
RedisConnRef\clearLastError
clearLastError()
Definition: RedisConnRef.php:77
RedisConnRef
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
Definition: RedisConnRef.php:32
RedisConnRef\setLogger
setLogger(LoggerInterface $logger)
Definition: RedisConnRef.php:61
RedisConnRef\__construct
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
Definition: RedisConnRef.php:52
RedisConnRef\getServer
getServer()
Definition: RedisConnRef.php:69
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.