MediaWiki master
MediumSpecificBagOStuff.php
Go to the documentation of this file.
1<?php
7
8use InvalidArgumentException;
9use JsonSerializable;
10use stdClass;
11use Wikimedia\WaitConditionLoop;
12
22abstract class MediumSpecificBagOStuff extends BagOStuff {
24 protected $locks = [];
29
31 protected $maxLockSendDelay = 0.05;
32
34 private $duplicateKeyLookups = [];
36 private $reportDupes = false;
38 private $dupeTrackScheduled = false;
39
41 private const SEGMENT_COMPONENT = 'segment';
42
44 protected const PASS_BY_REF = -1;
45
46 protected const METRIC_OP_GET = 'get';
47 protected const METRIC_OP_SET = 'set';
48 protected const METRIC_OP_DELETE = 'delete';
49 protected const METRIC_OP_CHANGE_TTL = 'change_ttl';
50 protected const METRIC_OP_ADD = 'add';
51 protected const METRIC_OP_INCR = 'incr';
52 protected const METRIC_OP_DECR = 'decr';
53 protected const METRIC_OP_CAS = 'cas';
54
55 protected const LOCK_RCLASS = 0;
56 protected const LOCK_DEPTH = 1;
57 protected const LOCK_TIME = 2;
58 protected const LOCK_EXPIRY = 3;
59
78 public function __construct( array $params = [] ) {
79 parent::__construct( $params );
80
81 if ( !empty( $params['reportDupes'] ) && $this->asyncHandler ) {
82 $this->reportDupes = true;
83 }
84
85 // Default to 8MiB if segmentationSize is not set
86 $this->segmentationSize = $params['segmentationSize'] ?? 8_388_608;
87 // Default to 64MiB if segmentedValueMaxSize is not set
88 $this->segmentedValueMaxSize = $params['segmentedValueMaxSize'] ?? 67_108_864;
89 }
90
105 public function get( $key, $flags = 0 ) {
106 $this->trackDuplicateKeys( $key );
107
108 return $this->resolveSegments( $key, $this->doGet( $key, $flags ) );
109 }
110
116 private function trackDuplicateKeys( $key ) {
117 if ( !$this->reportDupes ) {
118 return;
119 }
120
121 if ( !isset( $this->duplicateKeyLookups[$key] ) ) {
122 // Track that we have seen this key. This N-1 counting style allows
123 // easy filtering with array_filter() later.
124 $this->duplicateKeyLookups[$key] = 0;
125 } else {
126 $this->duplicateKeyLookups[$key]++;
127
128 if ( $this->dupeTrackScheduled === false ) {
129 $this->dupeTrackScheduled = true;
130 // Schedule a callback that logs keys processed more than once by get().
131 ( $this->asyncHandler )( function () {
132 $dups = array_filter( $this->duplicateKeyLookups );
133 foreach ( $dups as $key => $count ) {
134 $this->logger->warning(
135 'Duplicate get(): "{key}" fetched {count} times',
136 // Count is N-1 of the actual lookup count
137 [ 'key' => $key, 'count' => $count + 1, ]
138 );
139 }
140 } );
141 }
142 }
143 }
144
156 abstract protected function doGet( $key, $flags = 0, &$casToken = null );
157
159 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
160 $entry = $this->makeValueOrSegmentList( $key, $value, $exptime, $flags, $ok );
161
162 // Only when all segments (if any) are stored should the main key be changed
163 return $ok && $this->doSet( $key, $entry, $exptime, $flags );
164 }
165
176 abstract protected function doSet( $key, $value, $exptime = 0, $flags = 0 );
177
179 public function delete( $key, $flags = 0 ) {
180 if ( !$this->fieldHasFlags( $flags, self::WRITE_ALLOW_SEGMENTS ) ) {
181 return $this->doDelete( $key, $flags );
182 }
183
184 $mainValue = $this->doGet( $key, self::READ_LATEST );
185 if ( !$this->doDelete( $key, $flags ) ) {
186 return false;
187 }
188
189 if ( !SerializedValueContainer::isSegmented( $mainValue ) ) {
190 // no segments to delete
191 return true;
192 }
193
194 $orderedKeys = array_map(
195 function ( $segmentHash ) use ( $key ) {
196 return $this->makeGlobalKey( self::SEGMENT_COMPONENT, $key, $segmentHash );
197 },
199 );
200
201 return $this->deleteMulti( $orderedKeys, $flags & ~self::WRITE_ALLOW_SEGMENTS );
202 }
203
212 abstract protected function doDelete( $key, $flags = 0 );
213
215 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
216 $entry = $this->makeValueOrSegmentList( $key, $value, $exptime, $flags, $ok );
217
218 // Only when all segments (if any) are stored should the main key be changed
219 return $ok && $this->doAdd( $key, $entry, $exptime, $flags );
220 }
221
232 abstract protected function doAdd( $key, $value, $exptime = 0, $flags = 0 );
233
251 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
252 return $this->mergeViaCas( $key, $callback, $exptime, $attempts, $flags );
253 }
254
265 final protected function mergeViaCas( $key, callable $callback, $exptime, $attempts, $flags ) {
266 $attemptsLeft = $attempts;
267 do {
268 $token = self::PASS_BY_REF;
269 // Get the old value and CAS token from cache
270 $watchPoint = $this->watchErrors();
271 $currentValue = $this->resolveSegments(
272 $key,
273 $this->doGet( $key, $flags, $token )
274 );
275 if ( $this->getLastError( $watchPoint ) ) {
276 // Don't spam slow retries due to network problems (retry only on races)
277 $this->logger->warning(
278 __METHOD__ . ' failed due to read I/O error on get() for {key}.', [ 'key' => $key ]
279 );
280 $success = false;
281 break;
282 }
283
284 // Derive the new value from the old value
285 $value = $callback( $this, $key, $currentValue, $exptime );
286 $keyWasNonexistent = ( $currentValue === false );
287 $valueMatchesOldValue = ( $value === $currentValue );
288 // free RAM in case the value is large
289 unset( $currentValue );
290
291 $watchPoint = $this->watchErrors();
292 if ( $value === false || $exptime < 0 ) {
293 // do nothing
294 $success = true;
295 } elseif ( $valueMatchesOldValue && $attemptsLeft !== $attempts ) {
296 // recently set by another thread to the same value
297 $success = true;
298 } elseif ( $keyWasNonexistent ) {
299 // Try to create the key, failing if it gets created in the meantime
300 $success = $this->add( $key, $value, $exptime, $flags );
301 } else {
302 // Try to update the key, failing if it gets changed in the meantime
303 $success = $this->cas( $token, $key, $value, $exptime, $flags );
304 }
305 if ( $this->getLastError( $watchPoint ) ) {
306 // Don't spam slow retries due to network problems (retry only on races)
307 $this->logger->warning(
308 __METHOD__ . ' failed due to write I/O error for {key}.',
309 [ 'key' => $key ]
310 );
311 $success = false;
312 break;
313 }
314
315 } while ( !$success && --$attemptsLeft );
316
317 return $success;
318 }
319
331 protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
332 if ( $casToken === null ) {
333 $this->logger->warning(
334 __METHOD__ . ' got empty CAS token for {key}.',
335 [ 'key' => $key ]
336 );
337
338 // caller may have meant to use add()?
339 return false;
340 }
341
342 $entry = $this->makeValueOrSegmentList( $key, $value, $exptime, $flags, $ok );
343
344 // Only when all segments (if any) are stored should the main key be changed
345 return $ok && $this->doCas( $casToken, $key, $entry, $exptime, $flags );
346 }
347
359 protected function doCas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
360 // @TODO: the use of lock() assumes that all other relevant sets() use a lock
361 if ( !$this->lock( $key, 0 ) ) {
362 // non-blocking
363 return false;
364 }
365
366 $curCasToken = self::PASS_BY_REF;
367 $watchPoint = $this->watchErrors();
368 $exists = ( $this->doGet( $key, self::READ_LATEST, $curCasToken ) !== false );
369 if ( $this->getLastError( $watchPoint ) ) {
370 // Fail if the old CAS token could not be read
371 $success = false;
372 $this->logger->warning(
373 __METHOD__ . ' failed due to write I/O error for {key}.',
374 [ 'key' => $key ]
375 );
376 } elseif ( $exists && $this->tokensMatch( $casToken, $curCasToken ) ) {
377 $success = $this->doSet( $key, $value, $exptime, $flags );
378 } else {
379 // mismatched or failed
380 $success = false;
381 $this->logger->info(
382 __METHOD__ . ' failed due to race condition for {key}.',
383 [ 'key' => $key, 'key_exists' => $exists ]
384 );
385 }
386
387 $this->unlock( $key );
388
389 return $success;
390 }
391
398 final protected function tokensMatch( $value, $otherValue ) {
399 $type = gettype( $value );
400 // Ideally, tokens are counters, timestamps, hashes, or serialized PHP values.
401 // However, some classes might use the PHP values themselves.
402 if ( $type !== gettype( $otherValue ) ) {
403 return false;
404 }
405 // Serialize both tokens to strictly compare objects or arrays (which might objects
406 // nested inside). Note that this will not apply if integer/string CAS tokens are used.
407 if ( $type === 'array' || $type === 'object' ) {
408 return ( serialize( $value ) === serialize( $otherValue ) );
409 }
410
411 // For string/integer tokens, use a simple comparison
412 return ( $value === $otherValue );
413 }
414
433 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
434 return $this->doChangeTTL( $key, $exptime, $flags );
435 }
436
444 protected function doChangeTTL( $key, $exptime, $flags ) {
445 // @TODO: the use of lock() assumes that all other relevant sets() use a lock
446 if ( !$this->lock( $key, 0 ) ) {
447 return false;
448 }
449
450 $expiry = $this->getExpirationAsTimestamp( $exptime );
451 $delete = ( $expiry != self::TTL_INDEFINITE && $expiry < $this->getCurrentTime() );
452
453 // Use doGet() to avoid having to trigger resolveSegments()
454 $blob = $this->doGet( $key, self::READ_LATEST );
455 if ( $blob ) {
456 if ( $delete ) {
457 $ok = $this->doDelete( $key, $flags );
458 } else {
459 $ok = $this->doSet( $key, $blob, $exptime, $flags );
460 }
461 } else {
462 $ok = false;
463 }
464
465 $this->unlock( $key );
466
467 return $ok;
468 }
469
471 public function incrWithInit( $key, $exptime, $step = 1, $init = null, $flags = 0 ) {
472 $step = (int)$step;
473 $init = is_int( $init ) ? $init : $step;
474
475 return $this->doIncrWithInit( $key, $exptime, $step, $init, $flags );
476 }
477
487 abstract protected function doIncrWithInit( $key, $exptime, $step, $init, $flags );
488
497 public function lock( $key, $timeout = 6, $exptime = 6, $rclass = '' ) {
498 $exptime = min( $exptime ?: INF, self::TTL_DAY );
499
500 $acquired = false;
501
502 if ( isset( $this->locks[$key] ) ) {
503 // Already locked; avoid deadlocks and allow lock reentry if specified
504 if ( $rclass != '' && $this->locks[$key][self::LOCK_RCLASS] === $rclass ) {
505 ++$this->locks[$key][self::LOCK_DEPTH];
506 $acquired = true;
507 }
508 } else {
509 // Not already locked; acquire a lock on the backend
510 $lockTsUnix = $this->doLock( $key, $timeout, $exptime );
511 if ( $lockTsUnix !== null ) {
512 $this->locks[$key] = [
513 self::LOCK_RCLASS => $rclass,
514 self::LOCK_DEPTH => 1,
515 self::LOCK_TIME => $lockTsUnix,
516 self::LOCK_EXPIRY => $lockTsUnix + $exptime
517 ];
518 $acquired = true;
519 }
520 }
521
522 return $acquired;
523 }
524
534 protected function doLock( $key, $timeout, $exptime ) {
535 $lockTsUnix = null;
536
537 $fname = __METHOD__;
538 $loop = new WaitConditionLoop(
539 function () use ( $key, $exptime, $fname, &$lockTsUnix ) {
540 $watchPoint = $this->watchErrors();
541 if ( $this->add( $this->makeLockKey( $key ), 1, $exptime ) ) {
542 $lockTsUnix = microtime( true );
543
544 return WaitConditionLoop::CONDITION_REACHED;
545 } elseif ( $this->getLastError( $watchPoint ) ) {
546 $this->logger->warning(
547 "$fname failed due to I/O error for {key}.",
548 [ 'key' => $key ]
549 );
550
551 return WaitConditionLoop::CONDITION_ABORTED;
552 }
553
554 return WaitConditionLoop::CONDITION_CONTINUE;
555 },
556 $timeout
557 );
558 $code = $loop->invoke();
559
560 if ( $code === $loop::CONDITION_TIMED_OUT ) {
561 $this->logger->warning(
562 "$fname failed due to timeout for {key}.",
563 [ 'key' => $key, 'timeout' => $timeout ]
564 );
565 }
566
567 return $lockTsUnix;
568 }
569
577 public function unlock( $key ) {
578 $released = false;
579
580 if ( isset( $this->locks[$key] ) ) {
581 if ( --$this->locks[$key][self::LOCK_DEPTH] > 0 ) {
582 $released = true;
583 } else {
584 $released = $this->doUnlock( $key );
585 unset( $this->locks[$key] );
586 if ( !$released ) {
587 $this->logger->warning(
588 __METHOD__ . ' failed to release lock for {key}.',
589 [ 'key' => $key ]
590 );
591 }
592 }
593 } else {
594 $this->logger->warning(
595 __METHOD__ . ' no lock to release for {key}.',
596 [ 'key' => $key ]
597 );
598 }
599
600 return $released;
601 }
602
610 protected function doUnlock( $key ) {
611 $released = false;
612
613 // Estimate the remaining TTL of the lock key
614 $curTTL = $this->locks[$key][self::LOCK_EXPIRY] - $this->getCurrentTime();
615
616 // Check the risk of race conditions for key deletion
617 if ( $this->getQoS( self::ATTR_DURABILITY ) <= self::QOS_DURABILITY_SCRIPT ) {
618 // Lock (and data) keys use memory specific to this request (e.g. HashBagOStuff)
619 $isSafe = true;
620 } else {
621 // It is unsafe to delete the lock key if there is a serious risk of the key already
622 // being claimed by another thread before the delete operation reaches the backend
623 $isSafe = ( $curTTL > $this->maxLockSendDelay );
624 }
625
626 if ( $isSafe ) {
627 $released = $this->doDelete( $this->makeLockKey( $key ) );
628 } else {
629 $this->logger->warning(
630 "Lock for {key} held too long ({age} sec).",
631 [ 'key' => $key, 'curTTL' => $curTTL ]
632 );
633 }
634
635 return $released;
636 }
637
643 protected function makeLockKey( $key ) {
644 return "$key:lock";
645 }
646
649 $timestamp,
650 ?callable $progress = null,
651 $limit = INF,
652 ?string $tag = null
653 ) {
654 return false;
655 }
656
665 public function getMulti( array $keys, $flags = 0 ) {
666 $foundByKey = $this->doGetMulti( $keys, $flags );
667
668 $res = [];
669 foreach ( $keys as $key ) {
670 // Resolve one blob at a time (avoids too much I/O at once)
671 if ( array_key_exists( $key, $foundByKey ) ) {
672 // A value should not appear in the key if a segment is missing
673 $value = $this->resolveSegments( $key, $foundByKey[$key] );
674 if ( $value !== false ) {
675 $res[$key] = $value;
676 }
677 }
678 }
679
680 return $res;
681 }
682
691 protected function doGetMulti( array $keys, $flags = 0 ) {
692 $res = [];
693 foreach ( $keys as $key ) {
694 $val = $this->doGet( $key, $flags );
695 if ( $val !== false ) {
696 $res[$key] = $val;
697 }
698 }
699
700 return $res;
701 }
702
715 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
716 if ( $this->fieldHasFlags( $flags, self::WRITE_ALLOW_SEGMENTS ) ) {
717 throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' );
718 }
719
720 return $this->doSetMulti( $valueByKey, $exptime, $flags );
721 }
722
730 protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
731 $res = true;
732 foreach ( $data as $key => $value ) {
733 $res = $this->doSet( $key, $value, $exptime, $flags ) && $res;
734 }
735
736 return $res;
737 }
738
740 public function deleteMulti( array $keys, $flags = 0 ) {
741 if ( $this->fieldHasFlags( $flags, self::WRITE_ALLOW_SEGMENTS ) ) {
742 throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' );
743 }
744
745 return $this->doDeleteMulti( $keys, $flags );
746 }
747
754 protected function doDeleteMulti( array $keys, $flags = 0 ) {
755 $res = true;
756 foreach ( $keys as $key ) {
757 $res = $this->doDelete( $key, $flags ) && $res;
758 }
759
760 return $res;
761 }
762
774 public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
775 return $this->doChangeTTLMulti( $keys, $exptime, $flags );
776 }
777
785 protected function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) {
786 $res = true;
787 foreach ( $keys as $key ) {
788 $res = $this->doChangeTTL( $key, $exptime, $flags ) && $res;
789 }
790
791 return $res;
792 }
793
802 final protected function resolveSegments( $key, $mainValue ) {
803 if ( SerializedValueContainer::isSegmented( $mainValue ) ) {
804 $orderedKeys = array_map(
805 function ( $segmentHash ) use ( $key ) {
806 return $this->makeGlobalKey( self::SEGMENT_COMPONENT, $key, $segmentHash );
807 },
809 );
810
811 $segmentsByKey = $this->doGetMulti( $orderedKeys );
812
813 $parts = [];
814 foreach ( $orderedKeys as $segmentKey ) {
815 if ( isset( $segmentsByKey[$segmentKey] ) ) {
816 $parts[] = $segmentsByKey[$segmentKey];
817 } else {
818 // missing segment
819 return false;
820 }
821 }
822
823 return $this->unserialize( implode( '', $parts ) );
824 }
825
826 return $mainValue;
827 }
828
842 private function useSegmentationWrapper( $value, $flags ) {
843 if (
844 $this->segmentationSize === INF ||
845 !$this->fieldHasFlags( $flags, self::WRITE_ALLOW_SEGMENTS )
846 ) {
847 return false;
848 }
849
850 if ( is_string( $value ) ) {
851 return ( strlen( $value ) >= $this->segmentationSize );
852 }
853
854 if ( is_array( $value ) ) {
855 // Expect that the contained value will be one of the first array entries
856 foreach ( array_slice( $value, 0, 4 ) as $v ) {
857 if ( is_string( $v ) && strlen( $v ) >= $this->segmentationSize ) {
858 return true;
859 }
860 }
861 }
862
863 // Avoid breaking functions for incrementing/decrementing integer key values
864 return false;
865 }
866
879 final protected function makeValueOrSegmentList( $key, $value, $exptime, $flags, &$ok ) {
880 $entry = $value;
881 $ok = true;
882
883 if ( $this->useSegmentationWrapper( $value, $flags ) ) {
884 $segmentSize = $this->segmentationSize;
885 $maxTotalSize = $this->segmentedValueMaxSize;
886 $serialized = $this->getSerialized( $value, $key );
887 $size = strlen( $serialized );
888 if ( $size > $maxTotalSize ) {
889 $this->logger->warning(
890 "Value for {key} exceeds $maxTotalSize bytes; cannot segment.",
891 [ 'key' => $key ]
892 );
893 } else {
894 // Split the serialized value into chunks and store them at different keys
895 $chunksByKey = [];
896 $segmentHashes = [];
897 $count = intdiv( $size, $segmentSize ) + ( ( $size % $segmentSize ) ? 1 : 0 );
898 for ( $i = 0; $i < $count; ++$i ) {
899 $segment = substr( $serialized, $i * $segmentSize, $segmentSize );
900 $hash = sha1( $segment );
901 $chunkKey = $this->makeGlobalKey( self::SEGMENT_COMPONENT, $key, $hash );
902 $chunksByKey[$chunkKey] = $segment;
903 $segmentHashes[] = $hash;
904 }
905 $flags &= ~self::WRITE_ALLOW_SEGMENTS;
906 $ok = $this->setMulti( $chunksByKey, $exptime, $flags );
907 $entry = SerializedValueContainer::newSegmented( $segmentHashes );
908 }
909 }
910
911 return $entry;
912 }
913
920 final protected function isRelativeExpiration( $exptime ) {
921 return ( $exptime !== self::TTL_INDEFINITE && $exptime < ( 10 * self::TTL_YEAR ) );
922 }
923
938 final protected function getExpirationAsTimestamp( $exptime ) {
939 if ( $exptime == self::TTL_INDEFINITE ) {
940 return $exptime;
941 }
942
943 return $this->isRelativeExpiration( $exptime )
944 ? intval( $this->getCurrentTime() + $exptime )
945 : $exptime;
946 }
947
963 final protected function getExpirationAsTTL( $exptime ) {
964 if ( $exptime == self::TTL_INDEFINITE ) {
965 return $exptime;
966 }
967
968 return $this->isRelativeExpiration( $exptime )
969 ? $exptime
970 : (int)max( $exptime - $this->getCurrentTime(), 1 );
971 }
972
980 final protected function isInteger( $value ) {
981 if ( is_int( $value ) ) {
982 return true;
983 } elseif ( !is_string( $value ) ) {
984 return false;
985 }
986
987 $integer = (int)$value;
988
989 return ( $value === (string)$integer );
990 }
991
993 public function getQoS( $flag ) {
994 return $this->attrMap[$flag] ?? self::QOS_UNKNOWN;
995 }
996
1000 public function getSegmentationSize() {
1001 wfDeprecated( __METHOD__, '1.43' );
1002
1003 return $this->segmentationSize;
1004 }
1005
1009 public function getSegmentedValueMaxSize() {
1010 wfDeprecated( __METHOD__, '1.43' );
1011
1012 return $this->segmentedValueMaxSize;
1013 }
1014
1024 protected function getSerialized( $value, $key ) {
1025 $this->checkValueSerializability( $value, $key );
1026
1027 return $this->serialize( $value );
1028 }
1029
1050 private function checkValueSerializability( $value, $key ) {
1051 if ( is_array( $value ) ) {
1052 $this->checkIterableMapSerializability( $value, $key );
1053 } elseif ( is_object( $value ) ) {
1054 // Note that Closure instances count as objects
1055 if ( $value instanceof stdClass ) {
1056 $this->checkIterableMapSerializability( $value, $key );
1057 } elseif ( !( $value instanceof JsonSerializable ) ) {
1058 $this->logger->warning(
1059 "{class} value for '{cachekey}'; serialization is suspect.",
1060 [ 'cachekey' => $key, 'class' => get_class( $value ) ]
1061 );
1062 }
1063 }
1064 }
1065
1070 private function checkIterableMapSerializability( $value, $key ) {
1071 foreach ( $value as $index => $entry ) {
1072 if ( is_object( $entry ) ) {
1073 // Note that Closure instances count as objects
1074 if (
1075 !( $entry instanceof \stdClass ) &&
1076 !( $entry instanceof \JsonSerializable )
1077 ) {
1078 $this->logger->warning(
1079 "{class} value for '{cachekey}' at '$index'; serialization is suspect.",
1080 [ 'cachekey' => $key, 'class' => get_class( $entry ) ]
1081 );
1082
1083 return;
1084 }
1085 }
1086 }
1087 }
1088
1095 protected function serialize( $value ) {
1096 return is_int( $value ) ? $value : serialize( $value );
1097 }
1098
1105 protected function unserialize( $value ) {
1106 return $this->isInteger( $value ) ? (int)$value : unserialize( $value );
1107 }
1108
1112 protected function debug( $text ) {
1113 $this->logger->debug( "{class} debug: $text", [ 'class' => static::class ] );
1114 }
1115
1121 private function determinekeyGroupForStats( $key ): string {
1122 // Key came directly from BagOStuff::makeKey() or BagOStuff::makeGlobalKey()
1123 // and thus has the format of "<scope>:<collection>[:<constant or variable>]..."
1124 $components = explode( ':', $key, 3 );
1125 // Handle legacy callers that fail to use the key building methods
1126 $keygroup = $components[1] ?? 'UNKNOWN';
1127
1128 return strtr( $keygroup, '.', '_' );
1129 }
1130
1138 protected function updateOpStats( string $op, array $keyInfo ) {
1139 $deltasByMetric = [];
1140
1141 foreach ( $keyInfo as $indexOrKey => $keyOrSizes ) {
1142 if ( is_array( $keyOrSizes ) ) {
1143 $key = $indexOrKey;
1144 [ $sPayloadSize, $rPayloadSize ] = $keyOrSizes;
1145 } else {
1146 $key = $keyOrSizes;
1147 $sPayloadSize = 0;
1148 $rPayloadSize = 0;
1149 }
1150
1151 // Metric prefix for the cache wrapper and key collection name
1152 $keygroup = $this->determinekeyGroupForStats( $key );
1153
1154 if ( $op === self::METRIC_OP_GET ) {
1155 // This operation was either a "hit" or "miss" for this key
1156 if ( $rPayloadSize === false ) {
1157 $statsName = "bagostuff_miss_total";
1158 } else {
1159 $statsName = "bagostuff_hit_total";
1160 }
1161 } else {
1162 // There is no concept of "hit" or "miss" for this operation
1163 $statsName = "bagostuff_call_total";
1164 }
1165 $deltasByMetric[$statsName][$keygroup] = ( $deltasByMetric[$statsName][$keygroup] ?? 0 ) + 1;
1166
1167 if ( $sPayloadSize > 0 ) {
1168 $statsName = "bagostuff_bytes_sent_total";
1169 $deltasByMetric[$statsName][$keygroup] =
1170 ( $deltasByMetric[$statsName][$keygroup] ?? 0 ) + $sPayloadSize;
1171 }
1172
1173 if ( $rPayloadSize > 0 ) {
1174 $statsName = "bagostuff_bytes_read_total";
1175 $deltasByMetric[$statsName][$keygroup] =
1176 ( $deltasByMetric[$statsName][$keygroup] ?? 0 ) + $rPayloadSize;
1177 }
1178 }
1179
1180 foreach ( $deltasByMetric as $statsName => $deltaByKeygroup ) {
1181 $stats = $this->stats->getCounter( $statsName );
1182 foreach ( $deltaByKeygroup as $keygroup => $delta ) {
1183 $stats->setLabel( 'keygroup', $keygroup )
1184 ->setLabel( 'operation', $op )
1185 ->incrementBy( $delta );
1186 }
1187 }
1188 }
1189}
1190
1192class_alias( MediumSpecificBagOStuff::class, 'MediumSpecificBagOStuff' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
makeGlobalKey( $keygroup,... $components)
Make a cache key from the given components, in the "global" keyspace.
Helper classs that implements most of BagOStuff for a backend.
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.bool Success (item created)
unlock( $key)
Release an advisory lock on a key string.
getSerialized( $value, $key)
Get the serialized form a value, logging a warning if it involves custom classes.
doGet( $key, $flags=0, &$casToken=null)
Get an item.
cas( $casToken, $key, $value, $exptime=0, $flags=0)
Set an item if the current CAS token matches the provided CAS token.
getQoS( $flag)
int BagOStuff:QOS_* constant 1.28
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
array< string, array > $locks
Map of (key => (class LOCK_* constant => value)
deleteObjectsExpiringBefore( $timestamp, ?callable $progress=null, $limit=INF, ?string $tag=null)
Delete all objects expiring before a certain date.bool Success; false if unimplemented
doDelete( $key, $flags=0)
Delete an item.
lock( $key, $timeout=6, $exptime=6, $rclass='')
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
isInteger( $value)
Check if a value is an integer.
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
doIncrWithInit( $key, $exptime, $step, $init, $flags)
float $maxLockSendDelay
Seconds; maximum expected seconds for a lock ping to reach the backend.
int $segmentationSize
Bytes; chunk size of segmented cache values.
makeValueOrSegmentList( $key, $value, $exptime, $flags, &$ok)
Make the entry to store at a key (inline or segment list), storing any segments.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
int $segmentedValueMaxSize
Bytes; maximum total size of a segmented cache value.
doCas( $casToken, $key, $value, $exptime=0, $flags=0)
Set an item if the current CAS token matches the provided CAS token.
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.
setMulti(array $valueByKey, $exptime=0, $flags=0)
Batch insertion/replace.
mergeViaCas( $key, callable $callback, $exptime, $attempts, $flags)
incrWithInit( $key, $exptime, $step=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise....
resolveSegments( $key, $mainValue)
Get and reassemble the chunks of blob at the given key.
const PASS_BY_REF
Idiom for doGet() to return extra information by reference.
deleteMulti(array $keys, $flags=0)
Delete a batch of items.This does not support WRITE_ALLOW_SEGMENTS to avoid excessive read I/OWRITE_B...
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.