| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Wikimedia\ObjectCache; |
| 22 |
|
| 23 |
use Psr\Log\LoggerAwareInterface; |
| 24 |
use Psr\Log\LoggerInterface; |
| 25 |
use Redis; |
| 26 |
use RedisException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class RedisConnRef implements LoggerAwareInterface { |
| 40 |
|
| 41 |
protected $pool; |
| 42 |
|
| 43 |
protected $conn; |
| 44 |
|
| 45 |
protected $server; |
| 46 |
|
| 47 |
protected $lastError; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
protected $logger; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private const AUTH_NO_ERROR = 200; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private const AUTH_ERROR_TEMPORARY = 201; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private const AUTH_ERROR_PERMANENT = 202; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function __call( $name, $arguments ) { |
| 113 |
|
| 114 |
$lname = strtolower( $name ); |
| 115 |
if ( |
| 116 |
( $lname === 'blpop' || $lname === 'brpop' || $lname === 'brpoplpush' ) |
| 117 |
&& count( $arguments ) > 1 |
| 118 |
) { |
| 119 |
|
| 120 |
$timeout = end( $arguments ); |
| 121 |
|
| 122 |
$this->pool->resetTimeout( $this->conn, ( $timeout > 0 ? $timeout + 1 : $timeout ) ); |
| 123 |
} |
| 124 |
|
| 125 |
return $this->tryCall( $name, $arguments ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function scan( &$iterator, $pattern = null, $count = null ) { |
| 164 |
return $this->tryCall( 'scan', [ &$iterator, $pattern, $count ] ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function sScan( $key, &$iterator, $pattern = null, $count = null ) { |
| 179 |
return $this->tryCall( 'sScan', [ $key, &$iterator, $pattern, $count ] ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function hScan( $key, &$iterator, $pattern = null, $count = null ) { |
| 194 |
return $this->tryCall( 'hScan', [ $key, &$iterator, $pattern, $count ] ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public function zScan( $key, &$iterator, $pattern = null, $count = null ) { |
| 209 |
return $this->tryCall( 'zScan', [ $key, &$iterator, $pattern, $count ] ); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private function postCallCleanup() { |
| 239 |
$this->lastError = $this->conn->getLastError() ?: $this->lastError; |
| 240 |
|
| 241 |
|
| 242 |
$this->pool->resetTimeout( $this->conn ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function luaEval( $script, array $params, $numKeys ) { |
| 253 |
$sha1 = sha1( $script ); |
| 254 |
$conn = $this->conn; |
| 255 |
$server = $this->server; |
| 256 |
|
| 257 |
|
| 258 |
$conn->clearLastError(); |
| 259 |
$res = $conn->evalSha( $sha1, $params, $numKeys ); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 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 ) { |
| 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 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 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 |
|
| 313 |
|
| 314 |
class_alias( RedisConnRef::class, 'RedisConnRef' ); |