MediaWiki REL1_32
RedisConnRef.php
Go to the documentation of this file.
1<?php
20use Psr\Log\LoggerInterface;
21use Psr\Log\LoggerAwareInterface;
22
31class RedisConnRef implements LoggerAwareInterface {
33 protected $pool;
35 protected $conn;
36
37 protected $server; // string
38 protected $lastError; // string
39
43 protected $logger;
44
50 const AUTH_NO_ERROR = 200;
51
57 const AUTH_ERROR_TEMPORARY = 201;
58
64 const AUTH_ERROR_PERMANENT = 202;
65
72 public function __construct(
73 RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
74 ) {
75 $this->pool = $pool;
76 $this->server = $server;
77 $this->conn = $conn;
78 $this->logger = $logger;
79 }
80
81 public function setLogger( LoggerInterface $logger ) {
82 $this->logger = $logger;
83 }
84
89 public function getServer() {
90 return $this->server;
91 }
92
93 public function getLastError() {
94 return $this->lastError;
95 }
96
97 public function clearLastError() {
98 $this->lastError = null;
99 }
100
109 public function __call( $name, $arguments ) {
110 // Work around https://github.com/nicolasff/phpredis/issues/70
111 $lname = strtolower( $name );
112 if (
113 ( $lname === 'blpop' || $lname === 'brpop' || $lname === 'brpoplpush' )
114 && count( $arguments ) > 1
115 ) {
116 // Get timeout off the end since it is always required and argument length can vary
117 $timeout = end( $arguments );
118 // Only give the additional one second buffer if not requesting an infinite timeout
119 $this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) );
120 }
121
122 return $this->tryCall( $name, $arguments );
123 }
124
133 private function tryCall( $method, $arguments ) {
134 $this->conn->clearLastError();
135 try {
136 $res = $this->conn->$method( ...$arguments );
137 $authError = $this->checkAuthentication();
138 if ( $authError === self::AUTH_ERROR_TEMPORARY ) {
139 $res = $this->conn->$method( ...$arguments );
140 }
141 if ( $authError === self::AUTH_ERROR_PERMANENT ) {
142 throw new RedisException( "Failure reauthenticating to Redis." );
143 }
144 } finally {
145 $this->postCallCleanup();
146 }
147
148 return $res;
149 }
150
161 public function scan( &$iterator, $pattern = null, $count = null ) {
162 return $this->tryCall( 'scan', [ &$iterator, $pattern, $count ] );
163 }
164
176 public function sScan( $key, &$iterator, $pattern = null, $count = null ) {
177 return $this->tryCall( 'sScan', [ $key, &$iterator, $pattern, $count ] );
178 }
179
191 public function hScan( $key, &$iterator, $pattern = null, $count = null ) {
192 return $this->tryCall( 'hScan', [ $key, &$iterator, $pattern, $count ] );
193 }
194
206 public function zScan( $key, &$iterator, $pattern = null, $count = null ) {
207 return $this->tryCall( 'zScan', [ $key, &$iterator, $pattern, $count ] );
208 }
209
215 private function checkAuthentication() {
216 if ( preg_match( '/^ERR operation not permitted\b/', $this->conn->getLastError() ) ) {
217 if ( !$this->pool->reauthenticateConnection( $this->server, $this->conn ) ) {
218 return self::AUTH_ERROR_PERMANENT;
219 }
220 $this->conn->clearLastError();
221 $this->logger->info(
222 "Used automatic re-authentication for Redis.",
223 [ 'redis_server' => $this->server ]
224 );
225 return self::AUTH_ERROR_TEMPORARY;
226 }
227 return self::AUTH_NO_ERROR;
228 }
229
235 private function postCallCleanup() {
236 $this->lastError = $this->conn->getLastError() ?: $this->lastError;
237
238 // Restore original timeout in the case of blocking calls.
239 $this->pool->resetTimeout( $this->conn );
240 }
241
249 public function luaEval( $script, array $params, $numKeys ) {
250 $sha1 = sha1( $script ); // 40 char hex
251 $conn = $this->conn; // convenience
252 $server = $this->server; // convenience
253
254 // Try to run the server-side cached copy of the script
255 $conn->clearLastError();
256 $res = $conn->evalSha( $sha1, $params, $numKeys );
257 // If we got a permission error reply that means that (a) we are not in
258 // multi()/pipeline() and (b) some connection problem likely occurred. If
259 // the password the client gave was just wrong, an exception should have
260 // been thrown back in getConnection() previously.
261 if ( preg_match( '/^ERR operation not permitted\b/', $conn->getLastError() ) ) {
262 $this->pool->reauthenticateConnection( $server, $conn );
263 $conn->clearLastError();
264 $res = $conn->eval( $script, $params, $numKeys );
265 $this->logger->info(
266 "Used automatic re-authentication for Lua script '$sha1'.",
267 [ 'redis_server' => $server ]
268 );
269 }
270 // If the script is not in cache, use eval() to retry and cache it
271 if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
272 $conn->clearLastError();
273 $res = $conn->eval( $script, $params, $numKeys );
274 $this->logger->info(
275 "Used eval() for Lua script '$sha1'.",
276 [ 'redis_server' => $server ]
277 );
278 }
279
280 if ( $conn->getLastError() ) { // script bug?
281 $this->logger->error(
282 'Lua script error on server "{redis_server}": {lua_error}',
283 [
284 'redis_server' => $server,
285 'lua_error' => $conn->getLastError()
286 ]
287 );
288 }
289
290 $this->lastError = $conn->getLastError() ?: $this->lastError;
291
292 return $res;
293 }
294
299 public function isConnIdentical( Redis $conn ) {
300 return $this->conn === $conn;
301 }
302
303 function __destruct() {
304 $this->pool->freeConnection( $this->server, $this->conn );
305 }
306}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
RedisConnectionPool $pool
LoggerInterface $logger
zScan( $key, &$iterator, $pattern=null, $count=null)
Sorted Set Scan Handle this explicity due to needing the iterator passed by reference.
scan(&$iterator, $pattern=null, $count=null)
Key Scan Handle this explicity due to needing the iterator passed by reference.
tryCall( $method, $arguments)
Do the method call in the common try catch handler.
__call( $name, $arguments)
Magic __call handler for most Redis functions.
luaEval( $script, array $params, $numKeys)
sScan( $key, &$iterator, $pattern=null, $count=null)
Set Scan Handle this explicity due to needing the iterator passed by reference.
hScan( $key, &$iterator, $pattern=null, $count=null)
Hash Scan Handle this explicity due to needing the iterator passed by reference.
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
setLogger(LoggerInterface $logger)
postCallCleanup()
Post Redis call cleanup.
checkAuthentication()
Handle authentication errors and automatically reauthenticate.
isConnIdentical(Redis $conn)
Helper class to manage Redis connections.
$res
Definition database.txt:21
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
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:302
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:37
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))
$params