Merge "Avoid spamming the duplicate key fetch log in BagOStuff::merge()"
[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 * The callback function returns the new value given the current value
257 * (which will be false if not present), and takes the arguments:
258 * (this BagOStuff, cache key, current value).
259 *
260 * @param string $key
261 * @param callable $callback Callback method to be executed
262 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
263 * @param int $attempts The amount of times to attempt a merge in case of failure
264 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
265 * @return bool Success
266 * @throws InvalidArgumentException
267 */
268 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
269 if ( !is_callable( $callback ) ) {
270 throw new InvalidArgumentException( "Got invalid callback." );
271 }
272
273 return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
274 }
275
276 /**
277 * @see BagOStuff::merge()
278 *
279 * @param string $key
280 * @param callable $callback Callback method to be executed
281 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
282 * @param int $attempts The amount of times to attempt a merge in case of failure
283 * @return bool Success
284 */
285 protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
286 do {
287 $this->clearLastError();
288 $reportDupes = $this->reportDupes;
289 $this->reportDupes = false;
290 $casToken = null; // passed by reference
291 $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
292 $this->reportDupes = $reportDupes;
293
294 if ( $this->getLastError() ) {
295 return false; // don't spam retries (retry only on races)
296 }
297
298 // Derive the new value from the old value
299 $value = call_user_func( $callback, $this, $key, $currentValue );
300
301 $this->clearLastError();
302 if ( $value === false ) {
303 $success = true; // do nothing
304 } elseif ( $currentValue === false ) {
305 // Try to create the key, failing if it gets created in the meantime
306 $success = $this->add( $key, $value, $exptime );
307 } else {
308 // Try to update the key, failing if it gets changed in the meantime
309 $success = $this->cas( $casToken, $key, $value, $exptime );
310 }
311 if ( $this->getLastError() ) {
312 return false; // IO error; don't spam retries
313 }
314 } while ( !$success && --$attempts );
315
316 return $success;
317 }
318
319 /**
320 * Check and set an item
321 *
322 * @param mixed $casToken
323 * @param string $key
324 * @param mixed $value
325 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
326 * @return bool Success
327 * @throws Exception
328 */
329 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
330 throw new Exception( "CAS is not implemented in " . __CLASS__ );
331 }
332
333 /**
334 * @see BagOStuff::merge()
335 *
336 * @param string $key
337 * @param callable $callback Callback method to be executed
338 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
339 * @param int $attempts The amount of times to attempt a merge in case of failure
340 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
341 * @return bool Success
342 */
343 protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
344 if ( !$this->lock( $key, 6 ) ) {
345 return false;
346 }
347
348 $this->clearLastError();
349 $reportDupes = $this->reportDupes;
350 $this->reportDupes = false;
351 $currentValue = $this->get( $key, self::READ_LATEST );
352 $this->reportDupes = $reportDupes;
353
354 if ( $this->getLastError() ) {
355 $success = false;
356 } else {
357 // Derive the new value from the old value
358 $value = call_user_func( $callback, $this, $key, $currentValue );
359 if ( $value === false ) {
360 $success = true; // do nothing
361 } else {
362 $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
363 }
364 }
365
366 if ( !$this->unlock( $key ) ) {
367 // this should never happen
368 trigger_error( "Could not release lock for key '$key'." );
369 }
370
371 return $success;
372 }
373
374 /**
375 * Acquire an advisory lock on a key string
376 *
377 * Note that if reentry is enabled, duplicate calls ignore $expiry
378 *
379 * @param string $key
380 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
381 * @param int $expiry Lock expiry [optional]; 1 day maximum
382 * @param string $rclass Allow reentry if set and the current lock used this value
383 * @return bool Success
384 */
385 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
386 // Avoid deadlocks and allow lock reentry if specified
387 if ( isset( $this->locks[$key] ) ) {
388 if ( $rclass != '' && $this->locks[$key]['class'] === $rclass ) {
389 ++$this->locks[$key]['depth'];
390 return true;
391 } else {
392 return false;
393 }
394 }
395
396 $expiry = min( $expiry ?: INF, self::TTL_DAY );
397
398 $this->clearLastError();
399 $timestamp = microtime( true ); // starting UNIX timestamp
400 if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
401 $locked = true;
402 } elseif ( $this->getLastError() || $timeout <= 0 ) {
403 $locked = false; // network partition or non-blocking
404 } else {
405 // Estimate the RTT (us); use 1ms minimum for sanity
406 $uRTT = max( 1e3, ceil( 1e6 * ( microtime( true ) - $timestamp ) ) );
407 $sleep = 2 * $uRTT; // rough time to do get()+set()
408
409 $attempts = 0; // failed attempts
410 do {
411 if ( ++$attempts >= 3 && $sleep <= 5e5 ) {
412 // Exponentially back off after failed attempts to avoid network spam.
413 // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
414 $sleep *= 2;
415 }
416 usleep( $sleep ); // back off
417 $this->clearLastError();
418 $locked = $this->add( "{$key}:lock", 1, $expiry );
419 if ( $this->getLastError() ) {
420 $locked = false; // network partition
421 break;
422 }
423 } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
424 }
425
426 if ( $locked ) {
427 $this->locks[$key] = [ 'class' => $rclass, 'depth' => 1 ];
428 }
429
430 return $locked;
431 }
432
433 /**
434 * Release an advisory lock on a key string
435 *
436 * @param string $key
437 * @return bool Success
438 */
439 public function unlock( $key ) {
440 if ( isset( $this->locks[$key] ) && --$this->locks[$key]['depth'] <= 0 ) {
441 unset( $this->locks[$key] );
442
443 return $this->delete( "{$key}:lock" );
444 }
445
446 return true;
447 }
448
449 /**
450 * Get a lightweight exclusive self-unlocking lock
451 *
452 * Note that the same lock cannot be acquired twice.
453 *
454 * This is useful for task de-duplication or to avoid obtrusive
455 * (though non-corrupting) DB errors like INSERT key conflicts
456 * or deadlocks when using LOCK IN SHARE MODE.
457 *
458 * @param string $key
459 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
460 * @param int $expiry Lock expiry [optional]; 1 day maximum
461 * @param string $rclass Allow reentry if set and the current lock used this value
462 * @return ScopedCallback|null Returns null on failure
463 * @since 1.26
464 */
465 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
466 $expiry = min( $expiry ?: INF, self::TTL_DAY );
467
468 if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
469 return null;
470 }
471
472 $lSince = microtime( true ); // lock timestamp
473
474 return new ScopedCallback( function() use ( $key, $lSince, $expiry ) {
475 $latency = .050; // latency skew (err towards keeping lock present)
476 $age = ( microtime( true ) - $lSince + $latency );
477 if ( ( $age + $latency ) >= $expiry ) {
478 $this->logger->warning( "Lock for $key held too long ($age sec)." );
479 return; // expired; it's not "safe" to delete the key
480 }
481 $this->unlock( $key );
482 } );
483 }
484
485 /**
486 * Delete all objects expiring before a certain date.
487 * @param string $date The reference date in MW format
488 * @param callable|bool $progressCallback Optional, a function which will be called
489 * regularly during long-running operations with the percentage progress
490 * as the first parameter.
491 *
492 * @return bool Success, false if unimplemented
493 */
494 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
495 // stub
496 return false;
497 }
498
499 /**
500 * Get an associative array containing the item for each of the keys that have items.
501 * @param array $keys List of strings
502 * @param integer $flags Bitfield; supports READ_LATEST [optional]
503 * @return array
504 */
505 public function getMulti( array $keys, $flags = 0 ) {
506 $res = [];
507 foreach ( $keys as $key ) {
508 $val = $this->get( $key );
509 if ( $val !== false ) {
510 $res[$key] = $val;
511 }
512 }
513 return $res;
514 }
515
516 /**
517 * Batch insertion
518 * @param array $data $key => $value assoc array
519 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
520 * @return bool Success
521 * @since 1.24
522 */
523 public function setMulti( array $data, $exptime = 0 ) {
524 $res = true;
525 foreach ( $data as $key => $value ) {
526 if ( !$this->set( $key, $value, $exptime ) ) {
527 $res = false;
528 }
529 }
530 return $res;
531 }
532
533 /**
534 * @param string $key
535 * @param mixed $value
536 * @param int $exptime
537 * @return bool Success
538 */
539 public function add( $key, $value, $exptime = 0 ) {
540 if ( $this->get( $key ) === false ) {
541 return $this->set( $key, $value, $exptime );
542 }
543 return false; // key already set
544 }
545
546 /**
547 * Increase stored value of $key by $value while preserving its TTL
548 * @param string $key Key to increase
549 * @param int $value Value to add to $key (Default 1)
550 * @return int|bool New value or false on failure
551 */
552 public function incr( $key, $value = 1 ) {
553 if ( !$this->lock( $key ) ) {
554 return false;
555 }
556 $n = $this->get( $key );
557 if ( $this->isInteger( $n ) ) { // key exists?
558 $n += intval( $value );
559 $this->set( $key, max( 0, $n ) ); // exptime?
560 } else {
561 $n = false;
562 }
563 $this->unlock( $key );
564
565 return $n;
566 }
567
568 /**
569 * Decrease stored value of $key by $value while preserving its TTL
570 * @param string $key
571 * @param int $value
572 * @return int|bool New value or false on failure
573 */
574 public function decr( $key, $value = 1 ) {
575 return $this->incr( $key, - $value );
576 }
577
578 /**
579 * Increase stored value of $key by $value while preserving its TTL
580 *
581 * This will create the key with value $init and TTL $ttl instead if not present
582 *
583 * @param string $key
584 * @param int $ttl
585 * @param int $value
586 * @param int $init
587 * @return int|bool New value or false on failure
588 * @since 1.24
589 */
590 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
591 $newValue = $this->incr( $key, $value );
592 if ( $newValue === false ) {
593 // No key set; initialize
594 $newValue = $this->add( $key, (int)$init, $ttl ) ? $init : false;
595 }
596 if ( $newValue === false ) {
597 // Raced out initializing; increment
598 $newValue = $this->incr( $key, $value );
599 }
600
601 return $newValue;
602 }
603
604 /**
605 * Get the "last error" registered; clearLastError() should be called manually
606 * @return int ERR_* constant for the "last error" registry
607 * @since 1.23
608 */
609 public function getLastError() {
610 return $this->lastError;
611 }
612
613 /**
614 * Clear the "last error" registry
615 * @since 1.23
616 */
617 public function clearLastError() {
618 $this->lastError = self::ERR_NONE;
619 }
620
621 /**
622 * Set the "last error" registry
623 * @param int $err ERR_* constant
624 * @since 1.23
625 */
626 protected function setLastError( $err ) {
627 $this->lastError = $err;
628 }
629
630 /**
631 * Modify a cache update operation array for EventRelayer::notify()
632 *
633 * This is used for relayed writes, e.g. for broadcasting a change
634 * to multiple data-centers. If the array contains a 'val' field
635 * then the command involves setting a key to that value. Note that
636 * for simplicity, 'val' is always a simple scalar value. This method
637 * is used to possibly serialize the value and add any cache-specific
638 * key/values needed for the relayer daemon (e.g. memcached flags).
639 *
640 * @param array $event
641 * @return array
642 * @since 1.26
643 */
644 public function modifySimpleRelayEvent( array $event ) {
645 return $event;
646 }
647
648 /**
649 * @param string $text
650 */
651 protected function debug( $text ) {
652 if ( $this->debugMode ) {
653 $this->logger->debug( "{class} debug: $text", [
654 'class' => get_class( $this ),
655 ] );
656 }
657 }
658
659 /**
660 * Convert an optionally relative time to an absolute time
661 * @param int $exptime
662 * @return int
663 */
664 protected function convertExpiry( $exptime ) {
665 if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
666 return time() + $exptime;
667 } else {
668 return $exptime;
669 }
670 }
671
672 /**
673 * Convert an optionally absolute expiry time to a relative time. If an
674 * absolute time is specified which is in the past, use a short expiry time.
675 *
676 * @param int $exptime
677 * @return int
678 */
679 protected function convertToRelative( $exptime ) {
680 if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
681 $exptime -= time();
682 if ( $exptime <= 0 ) {
683 $exptime = 1;
684 }
685 return $exptime;
686 } else {
687 return $exptime;
688 }
689 }
690
691 /**
692 * Check if a value is an integer
693 *
694 * @param mixed $value
695 * @return bool
696 */
697 protected function isInteger( $value ) {
698 return ( is_int( $value ) || ctype_digit( $value ) );
699 }
700
701 /**
702 * Construct a cache key.
703 *
704 * @since 1.27
705 * @param string $keyspace
706 * @param array $args
707 * @return string
708 */
709 public function makeKeyInternal( $keyspace, $args ) {
710 $key = $keyspace;
711 foreach ( $args as $arg ) {
712 $arg = str_replace( ':', '%3A', $arg );
713 $key = $key . ':' . $arg;
714 }
715 return strtr( $key, ' ', '_' );
716 }
717
718 /**
719 * Make a global cache key.
720 *
721 * @since 1.27
722 * @param string ... Key component (variadic)
723 * @return string
724 */
725 public function makeGlobalKey() {
726 return $this->makeKeyInternal( 'global', func_get_args() );
727 }
728
729 /**
730 * Make a cache key, scoped to this instance's keyspace.
731 *
732 * @since 1.27
733 * @param string ... Key component (variadic)
734 * @return string
735 */
736 public function makeKey() {
737 return $this->makeKeyInternal( $this->keyspace, func_get_args() );
738 }
739 }