MediaWiki  1.32.5
MultiWriteBagOStuff.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\ObjectFactory;
24 
34  protected $caches;
36  protected $asyncWrites = false;
38  protected $cacheIndexes = [];
39 
40  const UPGRADE_TTL = 3600; // TTL when a key is copied to a higher cache tier
41 
62  public function __construct( $params ) {
63  parent::__construct( $params );
64 
65  if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
66  throw new InvalidArgumentException(
67  __METHOD__ . ': "caches" parameter must be an array of caches'
68  );
69  }
70 
71  $this->caches = [];
72  foreach ( $params['caches'] as $cacheInfo ) {
73  if ( $cacheInfo instanceof BagOStuff ) {
74  $this->caches[] = $cacheInfo;
75  } else {
76  if ( !isset( $cacheInfo['args'] ) ) {
77  // B/C for when $cacheInfo was for ObjectCache::newFromParams().
78  // Callers intenting this to be for ObjectFactory::getObjectFromSpec
79  // should have set "args" per the docs above. Doings so avoids extra
80  // (likely harmless) params (factory/class/calls) ending up in "args".
81  $cacheInfo['args'] = [ $cacheInfo ];
82  }
83  $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
84  }
85  }
86  $this->mergeFlagMaps( $this->caches );
87 
88  $this->asyncWrites = (
89  isset( $params['replication'] ) &&
90  $params['replication'] === 'async' &&
91  is_callable( $this->asyncHandler )
92  );
93 
94  $this->cacheIndexes = array_keys( $this->caches );
95  }
96 
97  public function setDebug( $debug ) {
98  foreach ( $this->caches as $cache ) {
99  $cache->setDebug( $debug );
100  }
101  }
102 
103  protected function doGet( $key, $flags = 0 ) {
104  if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
105  // If the latest write was a delete(), we do NOT want to fallback
106  // to the other tiers and possibly see the old value. Also, this
107  // is used by mergeViaLock(), which only needs to hit the primary.
108  return $this->caches[0]->get( $key, $flags );
109  }
110 
111  $value = false;
112  $missIndexes = []; // backends checked
113  foreach ( $this->caches as $i => $cache ) {
114  $value = $cache->get( $key, $flags );
115  if ( $value !== false ) {
116  break;
117  }
118  $missIndexes[] = $i;
119  }
120 
121  if ( $value !== false
122  && $missIndexes
123  && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
124  ) {
125  // Backfill the value to the higher (and often faster/smaller) cache tiers
126  $this->doWrite(
127  $missIndexes, $this->asyncWrites, 'set', $key, $value, self::UPGRADE_TTL
128  );
129  }
130 
131  return $value;
132  }
133 
134  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
135  $asyncWrites = ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC )
136  ? false
138 
139  return $this->doWrite( $this->cacheIndexes, $asyncWrites, 'set', $key, $value, $exptime );
140  }
141 
142  public function delete( $key ) {
143  return $this->doWrite( $this->cacheIndexes, $this->asyncWrites, 'delete', $key );
144  }
145 
146  public function add( $key, $value, $exptime = 0 ) {
147  // Try the write to the top-tier cache
148  $ok = $this->doWrite( [ 0 ], $this->asyncWrites, 'add', $key, $value, $exptime );
149  if ( $ok ) {
150  // Relay the add() using set() if it succeeded. This is meant to handle certain
151  // migration scenarios where the same store might get written to twice for certain
152  // keys. In that case, it does not make sense to return false due to "self-conflicts".
153  return $this->doWrite(
154  array_slice( $this->cacheIndexes, 1 ),
155  $this->asyncWrites,
156  'set',
157  $key,
158  $value,
159  $exptime
160  );
161  }
162 
163  return false;
164  }
165 
166  public function incr( $key, $value = 1 ) {
167  return $this->doWrite( $this->cacheIndexes, $this->asyncWrites, 'incr', $key, $value );
168  }
169 
170  public function decr( $key, $value = 1 ) {
171  return $this->doWrite( $this->cacheIndexes, $this->asyncWrites, 'decr', $key, $value );
172  }
173 
174  public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
175  $asyncWrites = ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC )
176  ? false
178 
179  return $this->doWrite(
180  $this->cacheIndexes,
181  $asyncWrites,
182  'merge',
183  $key,
184  $callback,
185  $exptime,
186  $attempts,
187  $flags
188  );
189  }
190 
191  public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
192  // Only need to lock the first cache; also avoids deadlocks
193  return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
194  }
195 
196  public function unlock( $key ) {
197  // Only the first cache is locked
198  return $this->caches[0]->unlock( $key );
199  }
200 
201  public function getLastError() {
202  return $this->caches[0]->getLastError();
203  }
204 
205  public function clearLastError() {
206  $this->caches[0]->clearLastError();
207  }
208 
218  protected function doWrite( $indexes, $asyncWrites, $method /*, ... */ ) {
219  $ret = true;
220  $args = array_slice( func_get_args(), 3 );
221 
222  if ( array_diff( $indexes, [ 0 ] ) && $asyncWrites && $method !== 'merge' ) {
223  // Deep-clone $args to prevent misbehavior when something writes an
224  // object to the BagOStuff then modifies it afterwards, e.g. T168040.
226  }
227 
228  foreach ( $indexes as $i ) {
229  $cache = $this->caches[$i];
230  if ( $i == 0 || !$asyncWrites ) {
231  // First store or in sync mode: write now and get result
232  if ( !$cache->$method( ...$args ) ) {
233  $ret = false;
234  }
235  } else {
236  // Secondary write in async mode: do not block this HTTP request
239  function () use ( $cache, $method, $args, $logger ) {
240  if ( !$cache->$method( ...$args ) ) {
241  $logger->warning( "Async $method op failed" );
242  }
243  }
244  );
245  }
246  }
247 
248  return $ret;
249  }
250 
259  public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
260  $ret = false;
261  foreach ( $this->caches as $cache ) {
262  if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
263  $ret = true;
264  }
265  }
266 
267  return $ret;
268  }
269 
270  public function makeKeyInternal( $keyspace, $args ) {
271  return $this->caches[0]->makeKeyInternal( ...func_get_args() );
272  }
273 
274  public function makeKey( $class, $component = null ) {
275  return $this->caches[0]->makeKey( ...func_get_args() );
276  }
277 
278  public function makeGlobalKey( $class, $component = null ) {
279  return $this->caches[0]->makeGlobalKey( ...func_get_args() );
280  }
281 }
MultiWriteBagOStuff\$cacheIndexes
int[] $cacheIndexes
List of all backing cache indexes.
Definition: MultiWriteBagOStuff.php:38
MultiWriteBagOStuff\lock
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
Definition: MultiWriteBagOStuff.php:191
BagOStuff\$asyncHandler
callback null $asyncHandler
Definition: BagOStuff.php:68
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
BagOStuff\WRITE_SYNC
const WRITE_SYNC
Bitfield constants for set()/merge()
Definition: BagOStuff.php:100
MultiWriteBagOStuff\getLastError
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
Definition: MultiWriteBagOStuff.php:201
MultiWriteBagOStuff\$asyncWrites
bool $asyncWrites
Use async secondary writes.
Definition: MultiWriteBagOStuff.php:36
MultiWriteBagOStuff\setDebug
setDebug( $debug)
Definition: MultiWriteBagOStuff.php:97
MultiWriteBagOStuff\clearLastError
clearLastError()
Clear the "last error" registry.
Definition: MultiWriteBagOStuff.php:205
MultiWriteBagOStuff\makeGlobalKey
makeGlobalKey( $class, $component=null)
Make a global cache key.
Definition: MultiWriteBagOStuff.php:278
MultiWriteBagOStuff\merge
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
Definition: MultiWriteBagOStuff.php:174
$params
$params
Definition: styleTest.css.php:44
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:58
serialize
serialize()
Definition: ApiMessageTrait.php:131
BagOStuff\$logger
LoggerInterface $logger
Definition: BagOStuff.php:66
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$debug
$debug
Definition: mcc.php:31
MultiWriteBagOStuff\doGet
doGet( $key, $flags=0)
Definition: MultiWriteBagOStuff.php:103
MultiWriteBagOStuff\incr
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
Definition: MultiWriteBagOStuff.php:166
MultiWriteBagOStuff\unlock
unlock( $key)
Release an advisory lock on a key string.
Definition: MultiWriteBagOStuff.php:196
MultiWriteBagOStuff\add
add( $key, $value, $exptime=0)
Definition: MultiWriteBagOStuff.php:146
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MultiWriteBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $args)
Construct a cache key.
Definition: MultiWriteBagOStuff.php:270
MultiWriteBagOStuff\deleteObjectsExpiringBefore
deleteObjectsExpiringBefore( $date, $progressCallback=false)
Delete objects expiring before a certain date.
Definition: MultiWriteBagOStuff.php:259
MultiWriteBagOStuff\decr
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
Definition: MultiWriteBagOStuff.php:170
MultiWriteBagOStuff\__construct
__construct( $params)
$params include:
Definition: MultiWriteBagOStuff.php:62
BagOStuff\mergeFlagMaps
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
Definition: BagOStuff.php:862
$value
$value
Definition: styleTest.css.php:49
MultiWriteBagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: MultiWriteBagOStuff.php:274
MultiWriteBagOStuff
A cache class that replicates all writes to multiple child caches.
Definition: MultiWriteBagOStuff.php:32
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:2044
MultiWriteBagOStuff\$caches
BagOStuff[] $caches
Definition: MultiWriteBagOStuff.php:34
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:139
$args
if( $line===false) $args
Definition: cdb.php:64
$cache
$cache
Definition: mcc.php:33
MultiWriteBagOStuff\UPGRADE_TTL
const UPGRADE_TTL
Definition: MultiWriteBagOStuff.php:40
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
BagOStuff\$keyspace
string $keyspace
Definition: BagOStuff.php:64
MultiWriteBagOStuff\doWrite
doWrite( $indexes, $asyncWrites, $method)
Apply a write method to the backing caches specified by $indexes (in order)
Definition: MultiWriteBagOStuff.php:218