MediaWiki REL1_40
RedisBagOStuff.php
Go to the documentation of this file.
1<?php
35 protected $redisPool;
37 protected $servers;
39 protected $serverTagMap;
42
71 public function __construct( $params ) {
72 parent::__construct( $params );
73 $redisConf = [ 'serializer' => 'none' ]; // manage that in this class
74 foreach ( [ 'connectTimeout', 'persistent', 'password' ] as $opt ) {
75 if ( isset( $params[$opt] ) ) {
76 $redisConf[$opt] = $params[$opt];
77 }
78 }
79 $this->redisPool = RedisConnectionPool::singleton( $redisConf );
80
81 $this->servers = $params['servers'];
82 foreach ( $this->servers as $key => $server ) {
83 $this->serverTagMap[is_int( $key ) ? $server : $key] = $server;
84 }
85
86 $this->automaticFailover = $params['automaticFailover'] ?? true;
87
88 // ...and uses rdb snapshots (redis.conf default)
90 }
91
92 protected function doGet( $key, $flags = 0, &$casToken = null ) {
93 $getToken = ( $casToken === self::PASS_BY_REF );
94 $casToken = null;
95
96 $conn = $this->getConnection( $key );
97 if ( !$conn ) {
98 return false;
99 }
100
101 $e = null;
102 try {
103 $blob = $conn->get( $key );
104 if ( $blob !== false ) {
105 $value = $this->unserialize( $blob );
106 $valueSize = strlen( $blob );
107 } else {
108 $value = false;
109 $valueSize = false;
110 }
111 if ( $getToken && $value !== false ) {
112 $casToken = $blob;
113 }
114 } catch ( RedisException $e ) {
115 $value = false;
116 $valueSize = false;
117 $this->handleException( $conn, $e );
118 }
119
120 $this->logRequest( 'get', $key, $conn->getServer(), $e );
121
122 $this->updateOpStats( self::METRIC_OP_GET, [ $key => [ 0, $valueSize ] ] );
123
124 return $value;
125 }
126
127 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
128 $conn = $this->getConnection( $key );
129 if ( !$conn ) {
130 return false;
131 }
132
133 $ttl = $this->getExpirationAsTTL( $exptime );
134 $serialized = $this->getSerialized( $value, $key );
135 $valueSize = strlen( $serialized );
136
137 $e = null;
138 try {
139 if ( $ttl ) {
140 $result = $conn->setex( $key, $ttl, $serialized );
141 } else {
142 $result = $conn->set( $key, $serialized );
143 }
144 } catch ( RedisException $e ) {
145 $result = false;
146 $this->handleException( $conn, $e );
147 }
148
149 $this->logRequest( 'set', $key, $conn->getServer(), $e );
150
151 $this->updateOpStats( self::METRIC_OP_SET, [ $key => [ $valueSize, 0 ] ] );
152
153 return $result;
154 }
155
156 protected function doDelete( $key, $flags = 0 ) {
157 $conn = $this->getConnection( $key );
158 if ( !$conn ) {
159 return false;
160 }
161
162 $e = null;
163 try {
164 // Note that redis does not return false if the key was not there
165 $result = ( $conn->del( $key ) !== false );
166 } catch ( RedisException $e ) {
167 $result = false;
168 $this->handleException( $conn, $e );
169 }
170
171 $this->logRequest( 'delete', $key, $conn->getServer(), $e );
172
173 $this->updateOpStats( self::METRIC_OP_DELETE, [ $key ] );
174
175 return $result;
176 }
177
178 protected function doGetMulti( array $keys, $flags = 0 ) {
180 $conns = [];
181 $batches = [];
182 foreach ( $keys as $key ) {
183 $conn = $this->getConnection( $key );
184 if ( $conn ) {
185 $server = $conn->getServer();
186 $conns[$server] = $conn;
187 $batches[$server][] = $key;
188 }
189 }
190
191 $blobsFound = [];
192 foreach ( $batches as $server => $batchKeys ) {
193 $conn = $conns[$server];
194
195 $e = null;
196 try {
197 // Avoid mget() to reduce CPU hogging from a single request
198 $conn->multi( Redis::PIPELINE );
199 foreach ( $batchKeys as $key ) {
200 $conn->get( $key );
201 }
202 $batchResult = $conn->exec();
203 if ( $batchResult === false ) {
204 $this->logRequest( 'get', implode( ',', $batchKeys ), $server, true );
205 continue;
206 }
207
208 foreach ( $batchResult as $i => $blob ) {
209 if ( $blob !== false ) {
210 $blobsFound[$batchKeys[$i]] = $blob;
211 }
212 }
213 } catch ( RedisException $e ) {
214 $this->handleException( $conn, $e );
215 }
216
217 $this->logRequest( 'get', implode( ',', $batchKeys ), $server, $e );
218 }
219
220 // Preserve the order of $keys
221 $result = [];
222 $valueSizesByKey = [];
223 foreach ( $keys as $key ) {
224 if ( array_key_exists( $key, $blobsFound ) ) {
225 $blob = $blobsFound[$key];
226 $value = $this->unserialize( $blob );
227 if ( $value !== false ) {
228 $result[$key] = $value;
229 }
230 $valueSize = strlen( $blob );
231 } else {
232 $valueSize = false;
233 }
234 $valueSizesByKey[$key] = [ 0, $valueSize ];
235 }
236
237 $this->updateOpStats( self::METRIC_OP_GET, $valueSizesByKey );
238
239 return $result;
240 }
241
242 protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
243 $result = true;
244
246 $conns = [];
247 $batches = [];
248 foreach ( $data as $key => $value ) {
249 $conn = $this->getConnection( $key );
250 if ( $conn ) {
251 $server = $conn->getServer();
252 $conns[$server] = $conn;
253 $batches[$server][] = $key;
254 } else {
255 $result = false;
256 }
257 }
258
259 $ttl = $this->getExpirationAsTTL( $exptime );
260 $op = $ttl ? 'setex' : 'set';
261
262 $valueSizesByKey = [];
263 foreach ( $batches as $server => $batchKeys ) {
264 $conn = $conns[$server];
265
266 $e = null;
267 try {
268 // Avoid mset() to reduce CPU hogging from a single request
269 $conn->multi( Redis::PIPELINE );
270 foreach ( $batchKeys as $key ) {
271 $serialized = $this->getSerialized( $data[$key], $key );
272 if ( $ttl ) {
273 $conn->setex( $key, $ttl, $serialized );
274 } else {
275 $conn->set( $key, $serialized );
276 }
277 $valueSizesByKey[$key] = [ strlen( $serialized ), 0 ];
278 }
279 $batchResult = $conn->exec();
280 if ( $batchResult === false ) {
281 $result = false;
282 $this->logRequest( $op, implode( ',', $batchKeys ), $server, true );
283 continue;
284 }
285
286 $result = $result && !in_array( false, $batchResult, true );
287 } catch ( RedisException $e ) {
288 $this->handleException( $conn, $e );
289 $result = false;
290 }
291
292 $this->logRequest( $op, implode( ',', $batchKeys ), $server, $e );
293 }
294
295 $this->updateOpStats( self::METRIC_OP_SET, $valueSizesByKey );
296
297 return $result;
298 }
299
300 protected function doDeleteMulti( array $keys, $flags = 0 ) {
301 $result = true;
302
304 $conns = [];
305 $batches = [];
306 foreach ( $keys as $key ) {
307 $conn = $this->getConnection( $key );
308 if ( $conn ) {
309 $server = $conn->getServer();
310 $conns[$server] = $conn;
311 $batches[$server][] = $key;
312 } else {
313 $result = false;
314 }
315 }
316
317 foreach ( $batches as $server => $batchKeys ) {
318 $conn = $conns[$server];
319
320 $e = null;
321 try {
322 // Avoid delete() with array to reduce CPU hogging from a single request
323 $conn->multi( Redis::PIPELINE );
324 foreach ( $batchKeys as $key ) {
325 $conn->del( $key );
326 }
327 $batchResult = $conn->exec();
328 if ( $batchResult === false ) {
329 $result = false;
330 $this->logRequest( 'delete', implode( ',', $batchKeys ), $server, true );
331 continue;
332 }
333 // Note that redis does not return false if the key was not there
334 $result = $result && !in_array( false, $batchResult, true );
335 } catch ( RedisException $e ) {
336 $this->handleException( $conn, $e );
337 $result = false;
338 }
339
340 $this->logRequest( 'delete', implode( ',', $batchKeys ), $server, $e );
341 }
342
343 $this->updateOpStats( self::METRIC_OP_DELETE, array_values( $keys ) );
344
345 return $result;
346 }
347
348 public function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) {
349 $result = true;
350
352 $conns = [];
353 $batches = [];
354 foreach ( $keys as $key ) {
355 $conn = $this->getConnection( $key );
356 if ( $conn ) {
357 $server = $conn->getServer();
358 $conns[$server] = $conn;
359 $batches[$server][] = $key;
360 } else {
361 $result = false;
362 }
363 }
364
365 $relative = $this->isRelativeExpiration( $exptime );
366 $op = ( $exptime == self::TTL_INDEFINITE )
367 ? 'persist'
368 : ( $relative ? 'expire' : 'expireAt' );
369
370 foreach ( $batches as $server => $batchKeys ) {
371 $conn = $conns[$server];
372
373 $e = null;
374 try {
375 $conn->multi( Redis::PIPELINE );
376 foreach ( $batchKeys as $key ) {
377 if ( $exptime == self::TTL_INDEFINITE ) {
378 $conn->persist( $key );
379 } elseif ( $relative ) {
380 $conn->expire( $key, $this->getExpirationAsTTL( $exptime ) );
381 } else {
382 $conn->expireAt( $key, $this->getExpirationAsTimestamp( $exptime ) );
383 }
384 }
385 $batchResult = $conn->exec();
386 if ( $batchResult === false ) {
387 $result = false;
388 $this->logRequest( $op, implode( ',', $batchKeys ), $server, true );
389 continue;
390 }
391 $result = in_array( false, $batchResult, true ) ? false : $result;
392 } catch ( RedisException $e ) {
393 $this->handleException( $conn, $e );
394 $result = false;
395 }
396
397 $this->logRequest( $op, implode( ',', $batchKeys ), $server, $e );
398 }
399
400 $this->updateOpStats( self::METRIC_OP_CHANGE_TTL, array_values( $keys ) );
401
402 return $result;
403 }
404
405 protected function doAdd( $key, $value, $exptime = 0, $flags = 0 ) {
406 $conn = $this->getConnection( $key );
407 if ( !$conn ) {
408 return false;
409 }
410
411 $ttl = $this->getExpirationAsTTL( $exptime );
412 $serialized = $this->getSerialized( $value, $key );
413 $valueSize = strlen( $serialized );
414
415 try {
416 $result = $conn->set(
417 $key,
419 $ttl ? [ 'nx', 'ex' => $ttl ] : [ 'nx' ]
420 );
421 } catch ( RedisException $e ) {
422 $result = false;
423 $this->handleException( $conn, $e );
424 }
425
426 $this->logRequest( 'add', $key, $conn->getServer(), $result );
427
428 $this->updateOpStats( self::METRIC_OP_ADD, [ $key => [ $valueSize, 0 ] ] );
429
430 return $result;
431 }
432
433 protected function doIncrWithInit( $key, $exptime, $step, $init, $flags ) {
434 $conn = $this->getConnection( $key );
435 if ( !$conn ) {
436 return false;
437 }
438
439 $ttl = $this->getExpirationAsTTL( $exptime );
440
441 try {
442 if ( $init === $step && $exptime == self::TTL_INDEFINITE ) {
443 $newValue = $conn->incrBy( $key, $step );
444 } else {
445 $conn->multi( Redis::PIPELINE );
446 $conn->set(
447 $key,
448 (string)( $init - $step ),
449 $ttl ? [ 'nx', 'ex' => $ttl ] : [ 'nx' ]
450 );
451 $conn->incrBy( $key, $step );
452 $batchResult = $conn->exec();
453 $newValue = ( $batchResult === false ) ? false : $batchResult[1];
454 $this->logRequest( 'incrWithInit', $key, $conn->getServer(), $newValue === false );
455 }
456 } catch ( RedisException $e ) {
457 $newValue = false;
458 $this->handleException( $conn, $e );
459 }
460
461 return $newValue;
462 }
463
464 protected function doChangeTTL( $key, $exptime, $flags ) {
465 $conn = $this->getConnection( $key );
466 if ( !$conn ) {
467 return false;
468 }
469
470 $relative = $this->isRelativeExpiration( $exptime );
471 try {
472 if ( $exptime == self::TTL_INDEFINITE ) {
473 $result = $conn->persist( $key );
474 $this->logRequest( 'persist', $key, $conn->getServer(), $result );
475 } elseif ( $relative ) {
476 $result = $conn->expire( $key, $this->getExpirationAsTTL( $exptime ) );
477 $this->logRequest( 'expire', $key, $conn->getServer(), $result );
478 } else {
479 $result = $conn->expireAt( $key, $this->getExpirationAsTimestamp( $exptime ) );
480 $this->logRequest( 'expireAt', $key, $conn->getServer(), $result );
481 }
482 } catch ( RedisException $e ) {
483 $result = false;
484 $this->handleException( $conn, $e );
485 }
486
487 $this->updateOpStats( self::METRIC_OP_CHANGE_TTL, [ $key ] );
488
489 return $result;
490 }
491
496 protected function getConnection( $key ) {
497 $candidates = array_keys( $this->serverTagMap );
498
499 if ( count( $this->servers ) > 1 ) {
500 ArrayUtils::consistentHashSort( $candidates, $key, '/' );
501 if ( !$this->automaticFailover ) {
502 $candidates = array_slice( $candidates, 0, 1 );
503 }
504 }
505
506 while ( ( $tag = array_shift( $candidates ) ) !== null ) {
507 $server = $this->serverTagMap[$tag];
508 $conn = $this->redisPool->getConnection( $server, $this->logger );
509 if ( !$conn ) {
510 continue;
511 }
512
513 // If automatic failover is enabled, check that the server's link
514 // to its master (if any) is up -- but only if there are other
515 // viable candidates left to consider. Also, getMasterLinkStatus()
516 // does not work with twemproxy, though $candidates will be empty
517 // by now in such cases.
518 if ( $this->automaticFailover && $candidates ) {
519 try {
521 $info = $conn->info();
522 if ( ( $info['master_link_status'] ?? null ) === 'down' ) {
523 // If the master cannot be reached, fail-over to the next server.
524 // If masters are in data-center A, and replica DBs in data-center B,
525 // this helps avoid the case were fail-over happens in A but not
526 // to the corresponding server in B (e.g. read/write mismatch).
527 continue;
528 }
529 } catch ( RedisException $e ) {
530 // Server is not accepting commands
531 $this->redisPool->handleError( $conn, $e );
532 continue;
533 }
534 }
535
536 return $conn;
537 }
538
540
541 return null;
542 }
543
548 protected function logError( $msg ) {
549 $this->logger->error( "Redis error: $msg" );
550 }
551
560 protected function handleException( RedisConnRef $conn, RedisException $e ) {
562 $this->redisPool->handleError( $conn, $e );
563 }
564
572 public function logRequest( $op, $keys, $server, $e = null ) {
573 $this->debug( "$op($keys) on $server: " . ( $e ? "failure" : "success" ) );
574 }
575
576 protected function makeKeyInternal( $keyspace, $components ) {
577 return $this->genericKeyFromComponents( $keyspace, ...$components );
578 }
579
580 protected function convertGenericKey( $key ) {
581 // short-circuit; already uses "generic" keys
582 return $key;
583 }
584}
setLastError( $error)
Set the "last error" registry due to a problem encountered during an attempted operation.
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
Storage medium specific cache for storing items (e.g.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
getSerialized( $value, $key)
Get the serialized form a value, logging a warning if it involves custom classes.
updateOpStats(string $op, array $keyInfo)
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
Redis-based caching module for redis server >= 2.6.12 and phpredis >= 2.2.4.
RedisConnectionPool $redisPool
handleException(RedisConnRef $conn, RedisException $e)
The redis extension throws an exception in response to various read, write and protocol errors.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
array $servers
List of server names.
doSetMulti(array $data, $exptime=0, $flags=0)
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
doChangeTTL( $key, $exptime, $flags)
logRequest( $op, $keys, $server, $e=null)
Send information about a single request to the debug log.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
doDelete( $key, $flags=0)
Delete an item.
logError( $msg)
Log a fatal error.
__construct( $params)
Construct a RedisBagOStuff object.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
array $serverTagMap
Map of (tag => server name)
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
doChangeTTLMulti(array $keys, $exptime, $flags=0)
doAdd( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doDeleteMulti(array $keys, $flags=0)
Helper class to handle automatically marking connections as reusable (via RAII pattern)
Helper class to manage Redis connections.
const ERR_UNREACHABLE
Storage medium could not be reached to establish a connection.
const ATTR_DURABILITY
Durability of writes; see QOS_DURABILITY_* (higher means stronger)
const ERR_UNEXPECTED
Storage medium operation failed due to usage limitations or an I/O error.
const QOS_DURABILITY_DISK
Data is saved to disk and writes do not usually block on fsync()
foreach( $res as $row) $serialized