MWException: Don't send headers multiple times
[mediawiki.git] / includes / objectcache / RedisBagOStuff.php
blobe770b73886863e415211a9243dc19c29f5c70e06
1 <?php
2 /**
3 * Object caching using Redis (http://redis.io/).
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 class RedisBagOStuff extends BagOStuff {
24 /** @var RedisConnectionPool */
25 protected $redisPool;
26 /** @var array List of server names */
27 protected $servers;
28 /** @var bool */
29 protected $automaticFailover;
31 /**
32 * Construct a RedisBagOStuff object. Parameters are:
34 * - servers: An array of server names. A server name may be a hostname,
35 * a hostname/port combination or the absolute path of a UNIX socket.
36 * If a hostname is specified but no port, the standard port number
37 * 6379 will be used. Required.
39 * - connectTimeout: The timeout for new connections, in seconds. Optional,
40 * default is 1 second.
42 * - persistent: Set this to true to allow connections to persist across
43 * multiple web requests. False by default.
45 * - password: The authentication password, will be sent to Redis in
46 * clear text. Optional, if it is unspecified, no AUTH command will be
47 * sent.
49 * - automaticFailover: If this is false, then each key will be mapped to
50 * a single server, and if that server is down, any requests for that key
51 * will fail. If this is true, a connection failure will cause the client
52 * to immediately try the next server in the list (as determined by a
53 * consistent hashing algorithm). True by default. This has the
54 * potential to create consistency issues if a server is slow enough to
55 * flap, for example if it is in swap death.
56 * @param array $params
58 function __construct( $params ) {
59 $redisConf = array( 'serializer' => 'none' ); // manage that in this class
60 foreach ( array( 'connectTimeout', 'persistent', 'password' ) as $opt ) {
61 if ( isset( $params[$opt] ) ) {
62 $redisConf[$opt] = $params[$opt];
65 $this->redisPool = RedisConnectionPool::singleton( $redisConf );
67 $this->servers = $params['servers'];
68 if ( isset( $params['automaticFailover'] ) ) {
69 $this->automaticFailover = $params['automaticFailover'];
70 } else {
71 $this->automaticFailover = true;
75 public function get( $key, &$casToken = null ) {
76 $section = new ProfileSection( __METHOD__ );
78 list( $server, $conn ) = $this->getConnection( $key );
79 if ( !$conn ) {
80 return false;
82 try {
83 $value = $conn->get( $key );
84 $casToken = $value;
85 $result = $this->unserialize( $value );
86 } catch ( RedisException $e ) {
87 $result = false;
88 $this->handleException( $conn, $e );
91 $this->logRequest( 'get', $key, $server, $result );
92 return $result;
95 public function set( $key, $value, $expiry = 0 ) {
96 $section = new ProfileSection( __METHOD__ );
98 list( $server, $conn ) = $this->getConnection( $key );
99 if ( !$conn ) {
100 return false;
102 $expiry = $this->convertToRelative( $expiry );
103 try {
104 if ( $expiry ) {
105 $result = $conn->setex( $key, $expiry, $this->serialize( $value ) );
106 } else {
107 // No expiry, that is very different from zero expiry in Redis
108 $result = $conn->set( $key, $this->serialize( $value ) );
110 } catch ( RedisException $e ) {
111 $result = false;
112 $this->handleException( $conn, $e );
115 $this->logRequest( 'set', $key, $server, $result );
116 return $result;
119 public function cas( $casToken, $key, $value, $expiry = 0 ) {
120 $section = new ProfileSection( __METHOD__ );
122 list( $server, $conn ) = $this->getConnection( $key );
123 if ( !$conn ) {
124 return false;
126 $expiry = $this->convertToRelative( $expiry );
127 try {
128 $conn->watch( $key );
130 if ( $this->serialize( $this->get( $key ) ) !== $casToken ) {
131 $conn->unwatch();
132 return false;
135 // multi()/exec() will fail atomically if the key changed since watch()
136 $conn->multi();
137 if ( $expiry ) {
138 $conn->setex( $key, $expiry, $this->serialize( $value ) );
139 } else {
140 // No expiry, that is very different from zero expiry in Redis
141 $conn->set( $key, $this->serialize( $value ) );
143 $result = ( $conn->exec() == array( true ) );
144 } catch ( RedisException $e ) {
145 $result = false;
146 $this->handleException( $conn, $e );
149 $this->logRequest( 'cas', $key, $server, $result );
150 return $result;
153 public function delete( $key, $time = 0 ) {
154 $section = new ProfileSection( __METHOD__ );
156 list( $server, $conn ) = $this->getConnection( $key );
157 if ( !$conn ) {
158 return false;
160 try {
161 $conn->delete( $key );
162 // Return true even if the key didn't exist
163 $result = true;
164 } catch ( RedisException $e ) {
165 $result = false;
166 $this->handleException( $conn, $e );
169 $this->logRequest( 'delete', $key, $server, $result );
170 return $result;
173 public function getMulti( array $keys ) {
174 $section = new ProfileSection( __METHOD__ );
176 $batches = array();
177 $conns = array();
178 foreach ( $keys as $key ) {
179 list( $server, $conn ) = $this->getConnection( $key );
180 if ( !$conn ) {
181 continue;
183 $conns[$server] = $conn;
184 $batches[$server][] = $key;
186 $result = array();
187 foreach ( $batches as $server => $batchKeys ) {
188 $conn = $conns[$server];
189 try {
190 $conn->multi( Redis::PIPELINE );
191 foreach ( $batchKeys as $key ) {
192 $conn->get( $key );
194 $batchResult = $conn->exec();
195 if ( $batchResult === false ) {
196 $this->debug( "multi request to $server failed" );
197 continue;
199 foreach ( $batchResult as $i => $value ) {
200 if ( $value !== false ) {
201 $result[$batchKeys[$i]] = $this->unserialize( $value );
204 } catch ( RedisException $e ) {
205 $this->handleException( $conn, $e );
209 $this->debug( "getMulti for " . count( $keys ) . " keys " .
210 "returned " . count( $result ) . " results" );
211 return $result;
215 * @param array $data
216 * @param int $expiry
217 * @return bool
219 public function setMulti( array $data, $expiry = 0 ) {
220 $section = new ProfileSection( __METHOD__ );
222 $batches = array();
223 $conns = array();
224 foreach ( $data as $key => $value ) {
225 list( $server, $conn ) = $this->getConnection( $key );
226 if ( !$conn ) {
227 continue;
229 $conns[$server] = $conn;
230 $batches[$server][] = $key;
233 $expiry = $this->convertToRelative( $expiry );
234 $result = true;
235 foreach ( $batches as $server => $batchKeys ) {
236 $conn = $conns[$server];
237 try {
238 $conn->multi( Redis::PIPELINE );
239 foreach ( $batchKeys as $key ) {
240 if ( $expiry ) {
241 $conn->setex( $key, $expiry, $this->serialize( $data[$key] ) );
242 } else {
243 $conn->set( $key, $this->serialize( $data[$key] ) );
246 $batchResult = $conn->exec();
247 if ( $batchResult === false ) {
248 $this->debug( "setMulti request to $server failed" );
249 continue;
251 foreach ( $batchResult as $value ) {
252 if ( $value === false ) {
253 $result = false;
256 } catch ( RedisException $e ) {
257 $this->handleException( $server, $conn, $e );
258 $result = false;
262 return $result;
267 public function add( $key, $value, $expiry = 0 ) {
268 $section = new ProfileSection( __METHOD__ );
270 list( $server, $conn ) = $this->getConnection( $key );
271 if ( !$conn ) {
272 return false;
274 $expiry = $this->convertToRelative( $expiry );
275 try {
276 if ( $expiry ) {
277 $conn->multi();
278 $conn->setnx( $key, $this->serialize( $value ) );
279 $conn->expire( $key, $expiry );
280 $result = ( $conn->exec() == array( true, true ) );
281 } else {
282 $result = $conn->setnx( $key, $this->serialize( $value ) );
284 } catch ( RedisException $e ) {
285 $result = false;
286 $this->handleException( $conn, $e );
289 $this->logRequest( 'add', $key, $server, $result );
290 return $result;
294 * Non-atomic implementation of incr().
296 * Probably all callers actually want incr() to atomically initialise
297 * values to zero if they don't exist, as provided by the Redis INCR
298 * command. But we are constrained by the memcached-like interface to
299 * return null in that case. Once the key exists, further increments are
300 * atomic.
301 * @param string $key
302 * @param int $value
303 * @param bool|mixed
305 public function incr( $key, $value = 1 ) {
306 $section = new ProfileSection( __METHOD__ );
308 list( $server, $conn ) = $this->getConnection( $key );
309 if ( !$conn ) {
310 return false;
312 if ( !$conn->exists( $key ) ) {
313 return null;
315 try {
316 $result = $this->unserialize( $conn->incrBy( $key, $value ) );
317 } catch ( RedisException $e ) {
318 $result = false;
319 $this->handleException( $conn, $e );
322 $this->logRequest( 'incr', $key, $server, $result );
323 return $result;
327 * @param mixed $data
328 * @return string
330 protected function serialize( $data ) {
331 // Ignore digit strings and ints so INCR/DECR work
332 return ( is_int( $data ) || ctype_digit( $data ) ) ? $data : serialize( $data );
336 * @param string $data
337 * @return mixed
339 protected function unserialize( $data ) {
340 // Ignore digit strings and ints so INCR/DECR work
341 return ( is_int( $data ) || ctype_digit( $data ) ) ? $data : unserialize( $data );
345 * Get a Redis object with a connection suitable for fetching the specified key
346 * @return array (server, RedisConnRef) or (false, false)
348 protected function getConnection( $key ) {
349 if ( count( $this->servers ) === 1 ) {
350 $candidates = $this->servers;
351 } else {
352 $candidates = $this->servers;
353 ArrayUtils::consistentHashSort( $candidates, $key, '/' );
354 if ( !$this->automaticFailover ) {
355 $candidates = array_slice( $candidates, 0, 1 );
359 foreach ( $candidates as $server ) {
360 $conn = $this->redisPool->getConnection( $server );
361 if ( $conn ) {
362 return array( $server, $conn );
365 $this->setLastError( BagOStuff::ERR_UNREACHABLE );
366 return array( false, false );
370 * Log a fatal error
371 * @param string $msg
373 protected function logError( $msg ) {
374 wfDebugLog( 'redis', "Redis error: $msg" );
378 * The redis extension throws an exception in response to various read, write
379 * and protocol errors. Sometimes it also closes the connection, sometimes
380 * not. The safest response for us is to explicitly destroy the connection
381 * object and let it be reopened during the next request.
382 * @param RedisConnRef $conn
383 * @param Exception $e
385 protected function handleException( RedisConnRef $conn, $e ) {
386 $this->setLastError( BagOStuff::ERR_UNEXPECTED );
387 $this->redisPool->handleError( $conn, $e );
391 * Send information about a single request to the debug log
392 * @param string $method
393 * @param string $key
394 * @param string $server
395 * @param bool $result
397 public function logRequest( $method, $key, $server, $result ) {
398 $this->debug( "$method $key on $server: " .
399 ( $result === false ? "failure" : "success" ) );