MediaWiki
master
|
00001 <?php 00024 class RedisBagOStuff extends BagOStuff { 00025 protected $connectTimeout, $persistent, $password, $automaticFailover; 00026 00030 protected $servers; 00031 00036 protected $conns = array(); 00037 00045 protected $deadServers = array(); 00046 00073 function __construct( $params ) { 00074 if ( !extension_loaded( 'redis' ) ) { 00075 throw new MWException( __CLASS__. ' requires the phpredis extension: ' . 00076 'https://github.com/nicolasff/phpredis' ); 00077 } 00078 00079 $this->servers = $params['servers']; 00080 $this->connectTimeout = isset( $params['connectTimeout'] ) 00081 ? $params['connectTimeout'] : 1; 00082 $this->persistent = !empty( $params['persistent'] ); 00083 if ( isset( $params['password'] ) ) { 00084 $this->password = $params['password']; 00085 } 00086 if ( isset( $params['automaticFailover'] ) ) { 00087 $this->automaticFailover = $params['automaticFailover']; 00088 } else { 00089 $this->automaticFailover = true; 00090 } 00091 } 00092 00093 public function get( $key ) { 00094 wfProfileIn( __METHOD__ ); 00095 list( $server, $conn ) = $this->getConnection( $key ); 00096 if ( !$conn ) { 00097 wfProfileOut( __METHOD__ ); 00098 return false; 00099 } 00100 try { 00101 $result = $conn->get( $key ); 00102 } catch ( RedisException $e ) { 00103 $result = false; 00104 $this->handleException( $server, $e ); 00105 } 00106 $this->logRequest( 'get', $key, $server, $result ); 00107 wfProfileOut( __METHOD__ ); 00108 return $result; 00109 } 00110 00111 public function set( $key, $value, $expiry = 0 ) { 00112 wfProfileIn( __METHOD__ ); 00113 list( $server, $conn ) = $this->getConnection( $key ); 00114 if ( !$conn ) { 00115 wfProfileOut( __METHOD__ ); 00116 return false; 00117 } 00118 $expiry = $this->convertToRelative( $expiry ); 00119 try { 00120 if ( !$expiry ) { 00121 // No expiry, that is very different from zero expiry in Redis 00122 $result = $conn->set( $key, $value ); 00123 } else { 00124 $result = $conn->setex( $key, $expiry, $value ); 00125 } 00126 } catch ( RedisException $e ) { 00127 $result = false; 00128 $this->handleException( $server, $e ); 00129 } 00130 00131 $this->logRequest( 'set', $key, $server, $result ); 00132 wfProfileOut( __METHOD__ ); 00133 return $result; 00134 } 00135 00136 public function delete( $key, $time = 0 ) { 00137 wfProfileIn( __METHOD__ ); 00138 list( $server, $conn ) = $this->getConnection( $key ); 00139 if ( !$conn ) { 00140 wfProfileOut( __METHOD__ ); 00141 return false; 00142 } 00143 try { 00144 $conn->delete( $key ); 00145 // Return true even if the key didn't exist 00146 $result = true; 00147 } catch ( RedisException $e ) { 00148 $result = false; 00149 $this->handleException( $server, $e ); 00150 } 00151 $this->logRequest( 'delete', $key, $server, $result ); 00152 wfProfileOut( __METHOD__ ); 00153 return $result; 00154 } 00155 00156 public function getMulti( array $keys ) { 00157 wfProfileIn( __METHOD__ ); 00158 $batches = array(); 00159 $conns = array(); 00160 foreach ( $keys as $key ) { 00161 list( $server, $conn ) = $this->getConnection( $key ); 00162 if ( !$conn ) { 00163 continue; 00164 } 00165 $conns[$server] = $conn; 00166 $batches[$server][] = $key; 00167 } 00168 $result = array(); 00169 foreach ( $batches as $server => $batchKeys ) { 00170 $conn = $conns[$server]; 00171 try { 00172 $conn->multi( Redis::PIPELINE ); 00173 foreach ( $batchKeys as $key ) { 00174 $conn->get( $key ); 00175 } 00176 $batchResult = $conn->exec(); 00177 if ( $batchResult === false ) { 00178 $this->debug( "multi request to $server failed" ); 00179 continue; 00180 } 00181 foreach ( $batchResult as $i => $value ) { 00182 if ( $value !== false ) { 00183 $result[$batchKeys[$i]] = $value; 00184 } 00185 } 00186 } catch ( RedisException $e ) { 00187 $this->handleException( $server, $e ); 00188 } 00189 } 00190 00191 $this->debug( "getMulti for " . count( $keys ) . " keys " . 00192 "returned " . count( $result ) . " results" ); 00193 wfProfileOut( __METHOD__ ); 00194 return $result; 00195 } 00196 00197 public function add( $key, $value, $expiry = 0 ) { 00198 wfProfileIn( __METHOD__ ); 00199 list( $server, $conn ) = $this->getConnection( $key ); 00200 if ( !$conn ) { 00201 wfProfileOut( __METHOD__ ); 00202 return false; 00203 } 00204 $expiry = $this->convertToRelative( $expiry ); 00205 try { 00206 $result = $conn->setnx( $key, $value ); 00207 if ( $result && $expiry ) { 00208 $conn->expire( $key, $expiry ); 00209 } 00210 } catch ( RedisException $e ) { 00211 $result = false; 00212 $this->handleException( $server, $e ); 00213 } 00214 $this->logRequest( 'add', $key, $server, $result ); 00215 wfProfileOut( __METHOD__ ); 00216 return $result; 00217 } 00218 00223 public function replace( $key, $value, $expiry = 0 ) { 00224 wfProfileIn( __METHOD__ ); 00225 list( $server, $conn ) = $this->getConnection( $key ); 00226 if ( !$conn ) { 00227 wfProfileOut( __METHOD__ ); 00228 return false; 00229 } 00230 if ( !$conn->exists( $key ) ) { 00231 wfProfileOut( __METHOD__ ); 00232 return false; 00233 } 00234 00235 $expiry = $this->convertToRelative( $expiry ); 00236 try { 00237 if ( !$expiry ) { 00238 $result = $conn->set( $key, $value ); 00239 } else { 00240 $result = $conn->setex( $key, $expiry, $value ); 00241 } 00242 } catch ( RedisException $e ) { 00243 $result = false; 00244 $this->handleException( $server, $e ); 00245 } 00246 00247 $this->logRequest( 'replace', $key, $server, $result ); 00248 wfProfileOut( __METHOD__ ); 00249 return $result; 00250 } 00251 00261 public function incr( $key, $value = 1 ) { 00262 wfProfileIn( __METHOD__ ); 00263 list( $server, $conn ) = $this->getConnection( $key ); 00264 if ( !$conn ) { 00265 wfProfileOut( __METHOD__ ); 00266 return false; 00267 } 00268 if ( !$conn->exists( $key ) ) { 00269 wfProfileOut( __METHOD__ ); 00270 return null; 00271 } 00272 try { 00273 $result = $conn->incrBy( $key, $value ); 00274 } catch ( RedisException $e ) { 00275 $result = false; 00276 $this->handleException( $server, $e ); 00277 } 00278 00279 $this->logRequest( 'incr', $key, $server, $result ); 00280 wfProfileOut( __METHOD__ ); 00281 return $result; 00282 } 00283 00287 protected function getConnection( $key ) { 00288 if ( count( $this->servers ) === 1 ) { 00289 $candidates = $this->servers; 00290 } else { 00291 // Use consistent hashing 00292 // 00293 // Note: Benchmarking on PHP 5.3 and 5.4 indicates that for small 00294 // strings, md5() is only 10% slower than hash('joaat',...) etc., 00295 // since the function call overhead dominates. So there's not much 00296 // justification for breaking compatibility with installations 00297 // compiled with ./configure --disable-hash. 00298 $hashes = array(); 00299 foreach ( $this->servers as $server ) { 00300 $hashes[$server] = md5( $server . '/' . $key ); 00301 } 00302 asort( $hashes ); 00303 if ( !$this->automaticFailover ) { 00304 reset( $hashes ); 00305 $candidates = array( key( $hashes ) ); 00306 } else { 00307 $candidates = array_keys( $hashes ); 00308 } 00309 } 00310 00311 foreach ( $candidates as $server ) { 00312 $conn = $this->getConnectionToServer( $server ); 00313 if ( $conn ) { 00314 return array( $server, $conn ); 00315 } 00316 } 00317 return array( false, false ); 00318 } 00319 00328 protected function getConnectionToServer( $server ) { 00329 if ( isset( $this->deadServers[$server] ) ) { 00330 $now = time(); 00331 if ( $now > $this->deadServers[$server] ) { 00332 // Dead time expired 00333 unset( $this->deadServers[$server] ); 00334 } else { 00335 // Server is dead 00336 $this->debug( "server $server is marked down for another " . 00337 ($this->deadServers[$server] - $now ) . 00338 " seconds, can't get connection" ); 00339 return false; 00340 } 00341 } 00342 00343 if ( isset( $this->conns[$server] ) ) { 00344 return $this->conns[$server]; 00345 } 00346 00347 if ( substr( $server, 0, 1 ) === '/' ) { 00348 // UNIX domain socket 00349 // These are required by the redis extension to start with a slash, but 00350 // we still need to set the port to a special value to make it work. 00351 $host = $server; 00352 $port = 0; 00353 } else { 00354 // TCP connection 00355 $hostPort = IP::splitHostAndPort( $server ); 00356 if ( !$hostPort ) { 00357 throw new MWException( __CLASS__.": invalid configured server \"$server\"" ); 00358 } 00359 list( $host, $port ) = $hostPort; 00360 if ( $port === false ) { 00361 $port = 6379; 00362 } 00363 } 00364 $conn = new Redis; 00365 try { 00366 if ( $this->persistent ) { 00367 $this->debug( "opening persistent connection to $host:$port" ); 00368 $result = $conn->pconnect( $host, $port, $this->connectTimeout ); 00369 } else { 00370 $this->debug( "opening non-persistent connection to $host:$port" ); 00371 $result = $conn->connect( $host, $port, $this->connectTimeout ); 00372 } 00373 if ( !$result ) { 00374 $this->logError( "could not connect to server $server" ); 00375 // Mark server down for 30s to avoid further timeouts 00376 $this->deadServers[$server] = time() + 30; 00377 return false; 00378 } 00379 if ( $this->password !== null ) { 00380 if ( !$conn->auth( $this->password ) ) { 00381 $this->logError( "authentication error connecting to $server" ); 00382 } 00383 } 00384 } catch ( RedisException $e ) { 00385 $this->deadServers[$server] = time() + 30; 00386 wfDebugLog( 'redis', "Redis exception: " . $e->getMessage() . "\n" ); 00387 return false; 00388 } 00389 00390 $conn->setOption( Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP ); 00391 $this->conns[$server] = $conn; 00392 return $conn; 00393 } 00394 00398 protected function logError( $msg ) { 00399 wfDebugLog( 'redis', "Redis error: $msg\n" ); 00400 } 00401 00408 protected function handleException( $server, $e ) { 00409 wfDebugLog( 'redis', "Redis exception on server $server: " . $e->getMessage() . "\n" ); 00410 unset( $this->conns[$server] ); 00411 } 00412 00416 public function logRequest( $method, $key, $server, $result ) { 00417 $this->debug( "$method $key on $server: " . 00418 ( $result === false ? "failure" : "success" ) ); 00419 } 00420 } 00421