MediaWiki  1.31.0
MultiWriteBagOStuff.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\ObjectFactory;
24 
34  protected $caches;
36  protected $asyncWrites = false;
37 
39  const ALL = INF;
40 
41  const UPGRADE_TTL = 3600; // TTL when a key is copied to a higher cache tier
42 
63  public function __construct( $params ) {
64  parent::__construct( $params );
65 
66  if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
67  throw new InvalidArgumentException(
68  __METHOD__ . ': "caches" parameter must be an array of caches'
69  );
70  }
71 
72  $this->caches = [];
73  foreach ( $params['caches'] as $cacheInfo ) {
74  if ( $cacheInfo instanceof BagOStuff ) {
75  $this->caches[] = $cacheInfo;
76  } else {
77  if ( !isset( $cacheInfo['args'] ) ) {
78  // B/C for when $cacheInfo was for ObjectCache::newFromParams().
79  // Callers intenting this to be for ObjectFactory::getObjectFromSpec
80  // should have set "args" per the docs above. Doings so avoids extra
81  // (likely harmless) params (factory/class/calls) ending up in "args".
82  $cacheInfo['args'] = [ $cacheInfo ];
83  }
84  $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
85  }
86  }
87  $this->mergeFlagMaps( $this->caches );
88 
89  $this->asyncWrites = (
90  isset( $params['replication'] ) &&
91  $params['replication'] === 'async' &&
92  is_callable( $this->asyncHandler )
93  );
94  }
95 
96  public function setDebug( $debug ) {
97  foreach ( $this->caches as $cache ) {
98  $cache->setDebug( $debug );
99  }
100  }
101 
102  protected function doGet( $key, $flags = 0 ) {
103  if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
104  // If the latest write was a delete(), we do NOT want to fallback
105  // to the other tiers and possibly see the old value. Also, this
106  // is used by mergeViaLock(), which only needs to hit the primary.
107  return $this->caches[0]->get( $key, $flags );
108  }
109 
110  $misses = 0; // number backends checked
111  $value = false;
112  foreach ( $this->caches as $cache ) {
113  $value = $cache->get( $key, $flags );
114  if ( $value !== false ) {
115  break;
116  }
117  ++$misses;
118  }
119 
120  if ( $value !== false
121  && $misses > 0
122  && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
123  ) {
124  $this->doWrite( $misses, $this->asyncWrites, 'set', $key, $value, self::UPGRADE_TTL );
125  }
126 
127  return $value;
128  }
129 
130  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
131  $asyncWrites = ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC )
132  ? false
134 
135  return $this->doWrite( self::ALL, $asyncWrites, 'set', $key, $value, $exptime );
136  }
137 
138  public function delete( $key ) {
139  return $this->doWrite( self::ALL, $this->asyncWrites, 'delete', $key );
140  }
141 
142  public function add( $key, $value, $exptime = 0 ) {
143  return $this->doWrite( self::ALL, $this->asyncWrites, 'add', $key, $value, $exptime );
144  }
145 
146  public function incr( $key, $value = 1 ) {
147  return $this->doWrite( self::ALL, $this->asyncWrites, 'incr', $key, $value );
148  }
149 
150  public function decr( $key, $value = 1 ) {
151  return $this->doWrite( self::ALL, $this->asyncWrites, 'decr', $key, $value );
152  }
153 
154  public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
155  // Only need to lock the first cache; also avoids deadlocks
156  return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
157  }
158 
159  public function unlock( $key ) {
160  // Only the first cache is locked
161  return $this->caches[0]->unlock( $key );
162  }
163 
164  public function getLastError() {
165  return $this->caches[0]->getLastError();
166  }
167 
168  public function clearLastError() {
169  $this->caches[0]->clearLastError();
170  }
171 
181  protected function doWrite( $count, $asyncWrites, $method /*, ... */ ) {
182  $ret = true;
183  $args = array_slice( func_get_args(), 3 );
184 
185  if ( $count > 1 && $asyncWrites ) {
186  // Deep-clone $args to prevent misbehavior when something writes an
187  // object to the BagOStuff then modifies it afterwards, e.g. T168040.
189  }
190 
191  foreach ( $this->caches as $i => $cache ) {
192  if ( $i >= $count ) {
193  break; // ignore the lower tiers
194  }
195 
196  if ( $i == 0 || !$asyncWrites ) {
197  // First store or in sync mode: write now and get result
198  if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
199  $ret = false;
200  }
201  } else {
202  // Secondary write in async mode: do not block this HTTP request
204  call_user_func(
205  $this->asyncHandler,
206  function () use ( $cache, $method, $args, $logger ) {
207  if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
208  $logger->warning( "Async $method op failed" );
209  }
210  }
211  );
212  }
213  }
214 
215  return $ret;
216  }
217 
226  public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
227  $ret = false;
228  foreach ( $this->caches as $cache ) {
229  if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
230  $ret = true;
231  }
232  }
233 
234  return $ret;
235  }
236 
237  public function makeKey( $class, $component = null ) {
238  return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
239  }
240 
241  public function makeGlobalKey( $class, $component = null ) {
242  return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
243  }
244 }
MultiWriteBagOStuff\lock
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
Definition: MultiWriteBagOStuff.php:154
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:86
MultiWriteBagOStuff\getLastError
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
Definition: MultiWriteBagOStuff.php:164
MultiWriteBagOStuff\$asyncWrites
bool $asyncWrites
Use async secondary writes.
Definition: MultiWriteBagOStuff.php:36
MultiWriteBagOStuff\setDebug
setDebug( $debug)
Definition: MultiWriteBagOStuff.php:96
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\clearLastError
clearLastError()
Clear the "last error" registry.
Definition: MultiWriteBagOStuff.php:168
MultiWriteBagOStuff\makeGlobalKey
makeGlobalKey( $class, $component=null)
Make a global cache key.
Definition: MultiWriteBagOStuff.php:241
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:192
$params
$params
Definition: styleTest.css.php:40
serialize
serialize()
Definition: ApiMessage.php:184
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
BagOStuff\$logger
LoggerInterface $logger
Definition: BagOStuff.php:55
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:102
MultiWriteBagOStuff\incr
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
Definition: MultiWriteBagOStuff.php:146
MultiWriteBagOStuff\unlock
unlock( $key)
Release an advisory lock on a key string.
Definition: MultiWriteBagOStuff.php:159
MultiWriteBagOStuff\add
add( $key, $value, $exptime=0)
Definition: MultiWriteBagOStuff.php:142
MultiWriteBagOStuff\deleteObjectsExpiringBefore
deleteObjectsExpiringBefore( $date, $progressCallback=false)
Delete objects expiring before a certain date.
Definition: MultiWriteBagOStuff.php:226
MultiWriteBagOStuff\decr
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
Definition: MultiWriteBagOStuff.php:150
MultiWriteBagOStuff\__construct
__construct( $params)
$params include:
Definition: MultiWriteBagOStuff.php:63
BagOStuff\mergeFlagMaps
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
Definition: BagOStuff.php:785
$value
$value
Definition: styleTest.css.php:45
MultiWriteBagOStuff\makeKey
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
Definition: MultiWriteBagOStuff.php:237
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:1987
MultiWriteBagOStuff\$caches
BagOStuff[] $caches
Definition: MultiWriteBagOStuff.php:34
$args
if( $line===false) $args
Definition: cdb.php:64
$cache
$cache
Definition: mcc.php:33
MultiWriteBagOStuff\UPGRADE_TTL
const UPGRADE_TTL
Definition: MultiWriteBagOStuff.php:41
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
MultiWriteBagOStuff\doWrite
doWrite( $count, $asyncWrites, $method)
Apply a write method to the first $count backing caches.
Definition: MultiWriteBagOStuff.php:181
MultiWriteBagOStuff\ALL
const ALL
Idiom for "write to all backends".
Definition: MultiWriteBagOStuff.php:39