Merge "Move Linker::getLinkColour() into LinkRenderer"
[lhc/web/wiklou.git] / includes / libs / objectcache / BagOStuff.php
1 <?php
2 /**
3 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
4 * https://www.mediawiki.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Cache
23 */
24
25 /**
26 * @defgroup Cache Cache
27 */
28
29 use Psr\Log\LoggerAwareInterface;
30 use Psr\Log\LoggerInterface;
31 use Psr\Log\NullLogger;
32
33 /**
34 * interface is intended to be more or less compatible with
35 * the PHP memcached client.
36 *
37 * backends for local hash array and SQL table included:
38 * @code
39 * $bag = new HashBagOStuff();
40 * $bag = new SqlBagOStuff(); # connect to db first
41 * @endcode
42 *
43 * @ingroup Cache
44 */
45 abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
46 /** @var array[] Lock tracking */
47 protected $locks = [];
48
49 /** @var integer */
50 protected $lastError = self::ERR_NONE;
51
52 /** @var string */
53 protected $keyspace = 'local';
54
55 /** @var LoggerInterface */
56 protected $logger;
57
58 /** @var callback|null */
59 protected $asyncHandler;
60
61 /** @var bool */
62 private $debugMode = false;
63
64 /** @var array */
65 private $duplicateKeyLookups = [];
66
67 /** @var bool */
68 private $reportDupes = false;
69
70 /** @var bool */
71 private $dupeTrackScheduled = false;
72
73 /** Possible values for getLastError() */
74 const ERR_NONE = 0; // no error
75 const ERR_NO_RESPONSE = 1; // no response
76 const ERR_UNREACHABLE = 2; // can't connect
77 const ERR_UNEXPECTED = 3; // response gave some error
78
79 /** Bitfield constants for get()/getMulti() */
80 const READ_LATEST = 1; // use latest data for replicated stores
81 const READ_VERIFIED = 2; // promise that caller can tell when keys are stale
82 /** Bitfield constants for set()/merge() */
83 const WRITE_SYNC = 1; // synchronously write to all locations for replicated stores
84 const WRITE_CACHE_ONLY = 2; // Only change state of the in-memory cache
85
86 /**
87 * $params include:
88 * - logger: Psr\Log\LoggerInterface instance
89 * - keyspace: Default keyspace for $this->makeKey()
90 * - asyncHandler: Callable to use for scheduling tasks after the web request ends.
91 * In CLI mode, it should run the task immediately.
92 * - reportDupes: Whether to emit warning log messages for all keys that were
93 * requested more than once (requires an asyncHandler).
94 * @param array $params
95 */
96 public function __construct( array $params = [] ) {
97 if ( isset( $params['logger'] ) ) {
98 $this->setLogger( $params['logger'] );
99 } else {
100 $this->setLogger( new NullLogger() );
101 }
102
103 if ( isset( $params['keyspace'] ) ) {
104 $this->keyspace = $params['keyspace'];
105 }
106
107 $this->asyncHandler = isset( $params['asyncHandler'] )
108 ? $params['asyncHandler']
109 : null;
110
111 if ( !empty( $params['reportDupes'] ) && is_callable( $this->asyncHandler ) ) {
112 $this->reportDupes = true;
113 }
114 }
115
116 /**
117 * @param LoggerInterface $logger
118 * @return null
119 */
120 public function setLogger( LoggerInterface $logger ) {
121 $this->logger = $logger;
122 }
123
124 /**
125 * @param bool $bool
126 */
127 public function setDebug( $bool ) {
128 $this->debugMode = $bool;
129 }
130
131 /**
132 * Get an item with the given key, regenerating and setting it if not found
133 *
134 * If the callback returns false, then nothing is stored.
135 *
136 * @param string $key
137 * @param int $ttl Time-to-live (seconds)
138 * @param callable $callback Callback that derives the new value
139 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
140 * @return mixed The cached value if found or the result of $callback otherwise
141 * @since 1.27
142 */
143 final public function getWithSetCallback( $key, $ttl, $callback, $flags = 0 ) {
144 $value = $this->get( $key, $flags );
145
146 if ( $value === false ) {
147 if ( !is_callable( $callback ) ) {
148 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
149 }
150 $value = call_user_func( $callback );
151 if ( $value !== false ) {
152 $this->set( $key, $value, $ttl );
153 }
154 }
155
156 return $value;
157 }
158
159 /**
160 * Get an item with the given key
161 *
162 * If the key includes a determistic input hash (e.g. the key can only have
163 * the correct value) or complete staleness checks are handled by the caller
164 * (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set.
165 * This lets tiered backends know they can safely upgrade a cached value to
166 * higher tiers using standard TTLs.
167 *
168 * @param string $key
169 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
170 * @param integer $oldFlags [unused]
171 * @return mixed Returns false on failure and if the item does not exist
172 */
173 public function get( $key, $flags = 0, $oldFlags = null ) {
174 // B/C for ( $key, &$casToken = null, $flags = 0 )
175 $flags = is_int( $oldFlags ) ? $oldFlags : $flags;
176
177 $this->trackDuplicateKeys( $key );
178
179 return $this->doGet( $key, $flags );
180 }
181
182 /**
183 * Track the number of times that a given key has been used.
184 * @param string $key
185 */
186 private function trackDuplicateKeys( $key ) {
187 if ( !$this->reportDupes ) {
188 return;
189 }
190
191 if ( !isset( $this->duplicateKeyLookups[$key] ) ) {
192 // Track that we have seen this key. This N-1 counting style allows
193 // easy filtering with array_filter() later.
194 $this->duplicateKeyLookups[$key] = 0;
195 } else {
196 $this->duplicateKeyLookups[$key] += 1;
197
198 if ( $this->dupeTrackScheduled === false ) {
199 $this->dupeTrackScheduled = true;
200 // Schedule a callback that logs keys processed more than once by get().
201 call_user_func( $this->asyncHandler, function () {
202 $dups = array_filter( $this->duplicateKeyLookups );
203 foreach ( $dups as $key => $count ) {
204 $this->logger->warning(
205 'Duplicate get(): "{key}" fetched {count} times',
206 // Count is N-1 of the actual lookup count
207 [ 'key' => $key, 'count' => $count + 1, ]
208 );
209 }
210 } );
211 }
212 }
213 }
214
215 /**
216 * @param string $key
217 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
218 * @return mixed Returns false on failure and if the item does not exist
219 */
220 abstract protected function doGet( $key, $flags = 0 );
221
222 /**
223 * @note: This method is only needed if merge() uses mergeViaCas()
224 *
225 * @param string $key
226 * @param mixed $casToken
227 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
228 * @return mixed Returns false on failure and if the item does not exist
229 * @throws Exception
230 */
231 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
232 throw new Exception( __METHOD__ . ' not implemented.' );
233 }
234
235 /**
236 * Set an item
237 *
238 * @param string $key
239 * @param mixed $value
240 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
241 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
242 * @return bool Success
243 */
244 abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
245
246 /**
247 * Delete an item
248 *
249 * @param string $key
250 * @return bool True if the item was deleted or not found, false on failure
251 */
252 abstract public function delete( $key );
253
254 /**
255 * Merge changes into the existing cache value (possibly creating a new one)
256 *
257 * The callback function returns the new value given the current value
258 * (which will be false if not present), and takes the arguments:
259 * (this BagOStuff, cache key, current value, TTL).
260 * The TTL parameter is reference set to $exptime. It can be overriden in the callback.
261 *
262 * @param string $key
263 * @param callable $callback Callback method to be executed
264 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
265 * @param int $attempts The amount of times to attempt a merge in case of failure
266 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
267 * @return bool Success
268 * @throws InvalidArgumentException
269 */
270 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
271 return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
272 }
273
274 /**
275 * @see BagOStuff::merge()
276 *
277 * @param string $key
278 * @param callable $callback Callback method to be executed
279 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
280 * @param int $attempts The amount of times to attempt a merge in case of failure
281 * @return bool Success
282 */
283 protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
284 do {
285 $this->clearLastError();
286 $reportDupes = $this->reportDupes;
287 $this->reportDupes = false;
288 $casToken = null; // passed by reference
289 $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
290 $this->reportDupes = $reportDupes;
291
292 if ( $this->getLastError() ) {
293 return false; // don't spam retries (retry only on races)
294 }
295
296 // Derive the new value from the old value
297 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
298
299 $this->clearLastError();
300 if ( $value === false ) {
301 $success = true; // do nothing
302 } elseif ( $currentValue === false ) {
303 // Try to create the key, failing if it gets created in the meantime
304 $success = $this->add( $key, $value, $exptime );
305 } else {
306 // Try to update the key, failing if it gets changed in the meantime
307 $success = $this->cas( $casToken, $key, $value, $exptime );
308 }
309 if ( $this->getLastError() ) {
310 return false; // IO error; don't spam retries
311 }
312 } while ( !$success && --$attempts );
313
314 return $success;
315 }
316
317 /**
318 * Check and set an item
319 *
320 * @param mixed $casToken
321 * @param string $key
322 * @param mixed $value
323 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
324 * @return bool Success
325 * @throws Exception
326 */
327 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
328 throw new Exception( "CAS is not implemented in " . __CLASS__ );
329 }
330
331 /**
332 * @see BagOStuff::merge()
333 *
334 * @param string $key
335 * @param callable $callback Callback method to be executed
336 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
337 * @param int $attempts The amount of times to attempt a merge in case of failure
338 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
339 * @return bool Success
340 */
341 protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
342 if ( !$this->lock( $key, 6 ) ) {
343 return false;
344 }
345
346 $this->clearLastError();
347 $reportDupes = $this->reportDupes;
348 $this->reportDupes = false;
349 $currentValue = $this->get( $key, self::READ_LATEST );
350 $this->reportDupes = $reportDupes;
351
352 if ( $this->getLastError() ) {
353 $success = false;
354 } else {
355 // Derive the new value from the old value
356 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
357 if ( $value === false ) {
358 $success = true; // do nothing
359 } else {
360 $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
361 }
362 }
363
364 if ( !$this->unlock( $key ) ) {
365 // this should never happen
366 trigger_error( "Could not release lock for key '$key'." );
367 }
368
369 return $success;
370 }
371
372 /**
373 * Acquire an advisory lock on a key string
374 *
375 * Note that if reentry is enabled, duplicate calls ignore $expiry
376 *
377 * @param string $key
378 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
379 * @param int $expiry Lock expiry [optional]; 1 day maximum
380 * @param string $rclass Allow reentry if set and the current lock used this value
381 * @return bool Success
382 */
383 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
384 // Avoid deadlocks and allow lock reentry if specified
385 if ( isset( $this->locks[$key] ) ) {
386 if ( $rclass != '' && $this->locks[$key]['class'] === $rclass ) {
387 ++$this->locks[$key]['depth'];
388 return true;
389 } else {
390 return false;
391 }
392 }
393
394 $expiry = min( $expiry ?: INF, self::TTL_DAY );
395
396 $this->clearLastError();
397 $timestamp = microtime( true ); // starting UNIX timestamp
398 if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
399 $locked = true;
400 } elseif ( $this->getLastError() || $timeout <= 0 ) {
401 $locked = false; // network partition or non-blocking
402 } else {
403 // Estimate the RTT (us); use 1ms minimum for sanity
404 $uRTT = max( 1e3, ceil( 1e6 * ( microtime( true ) - $timestamp ) ) );
405 $sleep = 2 * $uRTT; // rough time to do get()+set()
406
407 $attempts = 0; // failed attempts
408 do {
409 if ( ++$attempts >= 3 && $sleep <= 5e5 ) {
410 // Exponentially back off after failed attempts to avoid network spam.
411 // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
412 $sleep *= 2;
413 }
414 usleep( $sleep ); // back off
415 $this->clearLastError();
416 $locked = $this->add( "{$key}:lock", 1, $expiry );
417 if ( $this->getLastError() ) {
418 $locked = false; // network partition
419 break;
420 }
421 } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
422 }
423
424 if ( $locked ) {
425 $this->locks[$key] = [ 'class' => $rclass, 'depth' => 1 ];
426 }
427
428 return $locked;
429 }
430
431 /**
432 * Release an advisory lock on a key string
433 *
434 * @param string $key
435 * @return bool Success
436 */
437 public function unlock( $key ) {
438 if ( isset( $this->locks[$key] ) && --$this->locks[$key]['depth'] <= 0 ) {
439 unset( $this->locks[$key] );
440
441 return $this->delete( "{$key}:lock" );
442 }
443
444 return true;
445 }
446
447 /**
448 * Get a lightweight exclusive self-unlocking lock
449 *
450 * Note that the same lock cannot be acquired twice.
451 *
452 * This is useful for task de-duplication or to avoid obtrusive
453 * (though non-corrupting) DB errors like INSERT key conflicts
454 * or deadlocks when using LOCK IN SHARE MODE.
455 *
456 * @param string $key
457 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
458 * @param int $expiry Lock expiry [optional]; 1 day maximum
459 * @param string $rclass Allow reentry if set and the current lock used this value
460 * @return ScopedCallback|null Returns null on failure
461 * @since 1.26
462 */
463 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
464 $expiry = min( $expiry ?: INF, self::TTL_DAY );
465
466 if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
467 return null;
468 }
469
470 $lSince = microtime( true ); // lock timestamp
471
472 return new ScopedCallback( function() use ( $key, $lSince, $expiry ) {
473 $latency = .050; // latency skew (err towards keeping lock present)
474 $age = ( microtime( true ) - $lSince + $latency );
475 if ( ( $age + $latency ) >= $expiry ) {
476 $this->logger->warning( "Lock for $key held too long ($age sec)." );
477 return; // expired; it's not "safe" to delete the key
478 }
479 $this->unlock( $key );
480 } );
481 }
482
483 /**
484 * Delete all objects expiring before a certain date.
485 * @param string $date The reference date in MW format
486 * @param callable|bool $progressCallback Optional, a function which will be called
487 * regularly during long-running operations with the percentage progress
488 * as the first parameter.
489 *
490 * @return bool Success, false if unimplemented
491 */
492 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
493 // stub
494 return false;
495 }
496
497 /**
498 * Get an associative array containing the item for each of the keys that have items.
499 * @param array $keys List of strings
500 * @param integer $flags Bitfield; supports READ_LATEST [optional]
501 * @return array
502 */
503 public function getMulti( array $keys, $flags = 0 ) {
504 $res = [];
505 foreach ( $keys as $key ) {
506 $val = $this->get( $key );
507 if ( $val !== false ) {
508 $res[$key] = $val;
509 }
510 }
511 return $res;
512 }
513
514 /**
515 * Batch insertion
516 * @param array $data $key => $value assoc array
517 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
518 * @return bool Success
519 * @since 1.24
520 */
521 public function setMulti( array $data, $exptime = 0 ) {
522 $res = true;
523 foreach ( $data as $key => $value ) {
524 if ( !$this->set( $key, $value, $exptime ) ) {
525 $res = false;
526 }
527 }
528 return $res;
529 }
530
531 /**
532 * @param string $key
533 * @param mixed $value
534 * @param int $exptime
535 * @return bool Success
536 */
537 public function add( $key, $value, $exptime = 0 ) {
538 if ( $this->get( $key ) === false ) {
539 return $this->set( $key, $value, $exptime );
540 }
541 return false; // key already set
542 }
543
544 /**
545 * Increase stored value of $key by $value while preserving its TTL
546 * @param string $key Key to increase
547 * @param int $value Value to add to $key (Default 1)
548 * @return int|bool New value or false on failure
549 */
550 public function incr( $key, $value = 1 ) {
551 if ( !$this->lock( $key ) ) {
552 return false;
553 }
554 $n = $this->get( $key );
555 if ( $this->isInteger( $n ) ) { // key exists?
556 $n += intval( $value );
557 $this->set( $key, max( 0, $n ) ); // exptime?
558 } else {
559 $n = false;
560 }
561 $this->unlock( $key );
562
563 return $n;
564 }
565
566 /**
567 * Decrease stored value of $key by $value while preserving its TTL
568 * @param string $key
569 * @param int $value
570 * @return int|bool New value or false on failure
571 */
572 public function decr( $key, $value = 1 ) {
573 return $this->incr( $key, - $value );
574 }
575
576 /**
577 * Increase stored value of $key by $value while preserving its TTL
578 *
579 * This will create the key with value $init and TTL $ttl instead if not present
580 *
581 * @param string $key
582 * @param int $ttl
583 * @param int $value
584 * @param int $init
585 * @return int|bool New value or false on failure
586 * @since 1.24
587 */
588 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
589 $newValue = $this->incr( $key, $value );
590 if ( $newValue === false ) {
591 // No key set; initialize
592 $newValue = $this->add( $key, (int)$init, $ttl ) ? $init : false;
593 }
594 if ( $newValue === false ) {
595 // Raced out initializing; increment
596 $newValue = $this->incr( $key, $value );
597 }
598
599 return $newValue;
600 }
601
602 /**
603 * Get the "last error" registered; clearLastError() should be called manually
604 * @return int ERR_* constant for the "last error" registry
605 * @since 1.23
606 */
607 public function getLastError() {
608 return $this->lastError;
609 }
610
611 /**
612 * Clear the "last error" registry
613 * @since 1.23
614 */
615 public function clearLastError() {
616 $this->lastError = self::ERR_NONE;
617 }
618
619 /**
620 * Set the "last error" registry
621 * @param int $err ERR_* constant
622 * @since 1.23
623 */
624 protected function setLastError( $err ) {
625 $this->lastError = $err;
626 }
627
628 /**
629 * Modify a cache update operation array for EventRelayer::notify()
630 *
631 * This is used for relayed writes, e.g. for broadcasting a change
632 * to multiple data-centers. If the array contains a 'val' field
633 * then the command involves setting a key to that value. Note that
634 * for simplicity, 'val' is always a simple scalar value. This method
635 * is used to possibly serialize the value and add any cache-specific
636 * key/values needed for the relayer daemon (e.g. memcached flags).
637 *
638 * @param array $event
639 * @return array
640 * @since 1.26
641 */
642 public function modifySimpleRelayEvent( array $event ) {
643 return $event;
644 }
645
646 /**
647 * @param string $text
648 */
649 protected function debug( $text ) {
650 if ( $this->debugMode ) {
651 $this->logger->debug( "{class} debug: $text", [
652 'class' => get_class( $this ),
653 ] );
654 }
655 }
656
657 /**
658 * Convert an optionally relative time to an absolute time
659 * @param int $exptime
660 * @return int
661 */
662 protected function convertExpiry( $exptime ) {
663 if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
664 return time() + $exptime;
665 } else {
666 return $exptime;
667 }
668 }
669
670 /**
671 * Convert an optionally absolute expiry time to a relative time. If an
672 * absolute time is specified which is in the past, use a short expiry time.
673 *
674 * @param int $exptime
675 * @return int
676 */
677 protected function convertToRelative( $exptime ) {
678 if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
679 $exptime -= time();
680 if ( $exptime <= 0 ) {
681 $exptime = 1;
682 }
683 return $exptime;
684 } else {
685 return $exptime;
686 }
687 }
688
689 /**
690 * Check if a value is an integer
691 *
692 * @param mixed $value
693 * @return bool
694 */
695 protected function isInteger( $value ) {
696 return ( is_int( $value ) || ctype_digit( $value ) );
697 }
698
699 /**
700 * Construct a cache key.
701 *
702 * @since 1.27
703 * @param string $keyspace
704 * @param array $args
705 * @return string
706 */
707 public function makeKeyInternal( $keyspace, $args ) {
708 $key = $keyspace;
709 foreach ( $args as $arg ) {
710 $arg = str_replace( ':', '%3A', $arg );
711 $key = $key . ':' . $arg;
712 }
713 return strtr( $key, ' ', '_' );
714 }
715
716 /**
717 * Make a global cache key.
718 *
719 * @since 1.27
720 * @param string ... Key component (variadic)
721 * @return string
722 */
723 public function makeGlobalKey() {
724 return $this->makeKeyInternal( 'global', func_get_args() );
725 }
726
727 /**
728 * Make a cache key, scoped to this instance's keyspace.
729 *
730 * @since 1.27
731 * @param string ... Key component (variadic)
732 * @return string
733 */
734 public function makeKey() {
735 return $this->makeKeyInternal( $this->keyspace, func_get_args() );
736 }
737 }