MediaWiki master
RedisConnRef.php
Go to the documentation of this file.
1<?php
21namespace Wikimedia\ObjectCache;
22
23use Psr\Log\LoggerAwareInterface;
24use Psr\Log\LoggerInterface;
25use Redis;
26use RedisException;
27
39class RedisConnRef implements LoggerAwareInterface {
41 protected $pool;
43 protected $conn;
45 protected $server;
47 protected $lastError;
48
52 protected $logger;
53
57 private const AUTH_NO_ERROR = 200;
58
62 private const AUTH_ERROR_TEMPORARY = 201;
63
67 private const AUTH_ERROR_PERMANENT = 202;
68
75 public function __construct(
76 RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger
77 ) {
78 $this->pool = $pool;
79 $this->server = $server;
80 $this->conn = $conn;
81 $this->logger = $logger;
82 }
83
84 public function setLogger( LoggerInterface $logger ) {
85 $this->logger = $logger;
86 }
87
92 public function getServer() {
93 return $this->server;
94 }
95
96 public function getLastError() {
97 return $this->lastError;
98 }
99
100 public function clearLastError() {
101 $this->lastError = null;
102 }
103
112 public function __call( $name, $arguments ) {
113 // Work around https://github.com/nicolasff/phpredis/issues/70
114 $lname = strtolower( $name );
115 if (
116 ( $lname === 'blpop' || $lname === 'brpop' || $lname === 'brpoplpush' )
117 && count( $arguments ) > 1
118 ) {
119 // Get timeout off the end since it is always required and argument length can vary
120 $timeout = end( $arguments );
121 // Only give the additional one second buffer if not requesting an infinite timeout
122 $this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) );
123 }
124
125 return $this->tryCall( $name, $arguments );
126 }
127
136 private function tryCall( $method, $arguments ) {
137 $this->conn->clearLastError();
138 try {
139 $res = $this->conn->$method( ...$arguments );
140 $authError = $this->checkAuthentication();
141 if ( $authError === self::AUTH_ERROR_TEMPORARY ) {
142 $res = $this->conn->$method( ...$arguments );
143 }
144 if ( $authError === self::AUTH_ERROR_PERMANENT ) {
145 throw new RedisException( "Failure reauthenticating to Redis." );
146 }
147 return $res;
148 } finally {
149 $this->postCallCleanup();
150 }
151 }
152
163 public function scan( &$iterator, $pattern = null, $count = null ) {
164 return $this->tryCall( 'scan', [ &$iterator, $pattern, $count ] );
165 }
166
178 public function sScan( $key, &$iterator, $pattern = null, $count = null ) {
179 return $this->tryCall( 'sScan', [ $key, &$iterator, $pattern, $count ] );
180 }
181
193 public function hScan( $key, &$iterator, $pattern = null, $count = null ) {
194 return $this->tryCall( 'hScan', [ $key, &$iterator, $pattern, $count ] );
195 }
196
208 public function zScan( $key, &$iterator, $pattern = null, $count = null ) {
209 return $this->tryCall( 'zScan', [ $key, &$iterator, $pattern, $count ] );
210 }
211
217 private function checkAuthentication() {
218 $lastError = $this->conn->getLastError();
219 if ( $lastError && preg_match( '/^ERR operation not permitted\b/', $lastError ) ) {
220 if ( !$this->pool->reauthenticateConnection( $this->server, $this->conn ) ) {
221 return self::AUTH_ERROR_PERMANENT;
222 }
223 $this->conn->clearLastError();
224 $this->logger->info(
225 "Used automatic re-authentication for Redis.",
226 [ 'redis_server' => $this->server ]
227 );
228 return self::AUTH_ERROR_TEMPORARY;
229 }
230 return self::AUTH_NO_ERROR;
231 }
232
238 private function postCallCleanup() {
239 $this->lastError = $this->conn->getLastError() ?: $this->lastError;
240
241 // Restore original timeout in the case of blocking calls.
242 $this->pool->resetTimeout( $this->conn );
243 }
244
252 public function luaEval( $script, array $params, $numKeys ) {
253 $sha1 = sha1( $script ); // 40 char hex
254 $conn = $this->conn; // convenience
255 $server = $this->server; // convenience
256
257 // Try to run the server-side cached copy of the script
258 $conn->clearLastError();
259 $res = $conn->evalSha( $sha1, $params, $numKeys );
260 // If we got a permission error reply that means that (a) we are not in
261 // multi()/pipeline() and (b) some connection problem likely occurred. If
262 // the password the client gave was just wrong, an exception should have
263 // been thrown back in getConnection() previously.
264 $lastError = $conn->getLastError();
265 if ( $lastError && preg_match( '/^ERR operation not permitted\b/', $lastError ) ) {
266 $this->pool->reauthenticateConnection( $server, $conn );
267 $conn->clearLastError();
268 $res = $conn->eval( $script, $params, $numKeys );
269 $this->logger->info(
270 "Used automatic re-authentication for Lua script '$sha1'.",
271 [ 'redis_server' => $server ]
272 );
273 }
274 // If the script is not in cache, use eval() to retry and cache it
275 $lastError = $conn->getLastError();
276 if ( $lastError && preg_match( '/^NOSCRIPT/', $lastError ) ) {
277 $conn->clearLastError();
278 $res = $conn->eval( $script, $params, $numKeys );
279 $this->logger->info(
280 "Used eval() for Lua script '$sha1'.",
281 [ 'redis_server' => $server ]
282 );
283 }
284
285 $lastError = $conn->getLastError();
286 if ( $lastError ) { // script bug?
287 $this->logger->error(
288 'Lua script error on server "{redis_server}": {lua_error}',
289 [
290 'redis_server' => $server,
291 'lua_error' => $lastError
292 ]
293 );
294 $this->lastError = $lastError;
295 }
296
297 return $res;
298 }
299
304 public function isConnIdentical( Redis $conn ) {
305 return $this->conn === $conn;
306 }
307
308 public function __destruct() {
309 $this->pool->freeConnection( $this->server, $this->conn );
310 }
311}
312
314class_alias( RedisConnRef::class, 'RedisConnRef' );
array $params
The job parameters.
Wrapper class for Redis connections that automatically reuses connections (via RAII pattern)
hScan( $key, &$iterator, $pattern=null, $count=null)
Hash Scan Handle this explicitly due to needing the iterator passed by reference.
setLogger(LoggerInterface $logger)
scan(&$iterator, $pattern=null, $count=null)
Key Scan Handle this explicitly due to needing the iterator passed by reference.
zScan( $key, &$iterator, $pattern=null, $count=null)
Sorted Set Scan Handle this explicitly due to needing the iterator passed by reference.
__call( $name, $arguments)
Magic __call handler for most Redis functions.
sScan( $key, &$iterator, $pattern=null, $count=null)
Set Scan Handle this explicitly due to needing the iterator passed by reference.
__construct(RedisConnectionPool $pool, $server, Redis $conn, LoggerInterface $logger)
luaEval( $script, array $params, $numKeys)
Manage one or more Redis client connection.