MediaWiki  master
DeferredUpdates.php
Go to the documentation of this file.
1 <?php
26 
58  private static $preSendUpdates = [];
60  private static $postSendUpdates = [];
61 
62  const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
63  const PRESEND = 1; // for updates that should run before flushing output buffer
64  const POSTSEND = 2; // for updates that should run after flushing output buffer
65 
66  const BIG_QUEUE_SIZE = 100;
67 
69  private static $executeContext;
70 
79  public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
80  global $wgCommandLineMode;
81 
82  if (
83  self::$executeContext &&
84  self::$executeContext['stage'] >= $stage &&
85  !( $update instanceof MergeableUpdate )
86  ) {
87  // This is a sub-DeferredUpdate; run it right after its parent update.
88  // Also, while post-send updates are running, push any "pre-send" jobs to the
89  // active post-send queue to make sure they get run this round (or at all).
90  self::$executeContext['subqueue'][] = $update;
91 
92  return;
93  }
94 
95  if ( $stage === self::PRESEND ) {
96  self::push( self::$preSendUpdates, $update );
97  } else {
98  self::push( self::$postSendUpdates, $update );
99  }
100 
101  // Try to run the updates now if in CLI mode and no transaction is active.
102  // This covers scripts that don't/barely use the DB but make updates to other stores.
103  if ( $wgCommandLineMode ) {
104  self::tryOpportunisticExecute( 'run' );
105  }
106  }
107 
118  public static function addCallableUpdate(
119  $callable, $stage = self::POSTSEND, $dbw = null
120  ) {
121  self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
122  }
123 
130  public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
131  $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
132  // For ALL mode, make sure that any PRESEND updates added along the way get run.
133  // Normally, these use the subqueue, but that isn't true for MergeableUpdate items.
134  do {
135  if ( $stage === self::ALL || $stage === self::PRESEND ) {
136  self::execute( self::$preSendUpdates, $mode, $stageEffective );
137  }
138 
139  if ( $stage === self::ALL || $stage == self::POSTSEND ) {
140  self::execute( self::$postSendUpdates, $mode, $stageEffective );
141  }
142  } while ( $stage === self::ALL && self::$preSendUpdates );
143  }
144 
149  private static function push( array &$queue, DeferrableUpdate $update ) {
150  if ( $update instanceof MergeableUpdate ) {
151  $class = get_class( $update ); // fully-qualified class
152  if ( isset( $queue[$class] ) ) {
154  $existingUpdate = $queue[$class];
155  $existingUpdate->merge( $update );
156  // Move the update to the end to handle things like mergeable purge
157  // updates that might depend on the prior updates in the queue running
158  unset( $queue[$class] );
159  $queue[$class] = $existingUpdate;
160  } else {
161  $queue[$class] = $update;
162  }
163  } else {
164  $queue[] = $update;
165  }
166  }
167 
177  protected static function execute( array &$queue, $mode, $stage ) {
178  $services = MediaWikiServices::getInstance();
179  $stats = $services->getStatsdDataFactory();
180  $lbFactory = $services->getDBLoadBalancerFactory();
181  $method = RequestContext::getMain()->getRequest()->getMethod();
182 
183  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
184 
186  $reportableError = null;
188  $updates = $queue;
189 
190  // Keep doing rounds of updates until none get enqueued...
191  while ( $updates ) {
192  $queue = []; // clear the queue
193 
194  // Order will be DataUpdate followed by generic DeferrableUpdate tasks
195  $updatesByType = [ 'data' => [], 'generic' => [] ];
196  foreach ( $updates as $du ) {
197  if ( $du instanceof DataUpdate ) {
198  $du->setTransactionTicket( $ticket );
199  $updatesByType['data'][] = $du;
200  } else {
201  $updatesByType['generic'][] = $du;
202  }
203 
204  $name = ( $du instanceof DeferrableCallback )
205  ? get_class( $du ) . '-' . $du->getOrigin()
206  : get_class( $du );
207  $stats->increment( 'deferred_updates.' . $method . '.' . $name );
208  }
209 
210  // Execute all remaining tasks...
211  foreach ( $updatesByType as $updatesForType ) {
212  foreach ( $updatesForType as $update ) {
213  self::$executeContext = [ 'stage' => $stage, 'subqueue' => [] ];
214  try {
216  $guiError = self::runUpdate( $update, $lbFactory, $mode, $stage );
217  $reportableError = $reportableError ?: $guiError;
218  // Do the subqueue updates for $update until there are none
219  while ( self::$executeContext['subqueue'] ) {
220  $subUpdate = reset( self::$executeContext['subqueue'] );
221  $firstKey = key( self::$executeContext['subqueue'] );
222  unset( self::$executeContext['subqueue'][$firstKey] );
223 
224  if ( $subUpdate instanceof DataUpdate ) {
225  $subUpdate->setTransactionTicket( $ticket );
226  }
227 
228  $guiError = self::runUpdate( $subUpdate, $lbFactory, $mode, $stage );
229  $reportableError = $reportableError ?: $guiError;
230  }
231  } finally {
232  // Make sure we always clean up the context.
233  // Losing updates while rewinding the stack is acceptable,
234  // losing updates that are added later is not.
235  self::$executeContext = null;
236  }
237  }
238  }
239 
240  $updates = $queue; // new snapshot of queue (check for new entries)
241  }
242 
243  if ( $reportableError ) {
244  throw $reportableError; // throw the first of any GUI errors
245  }
246  }
247 
255  private static function runUpdate(
256  DeferrableUpdate $update, LBFactory $lbFactory, $mode, $stage
257  ) {
258  $guiError = null;
259  try {
260  if ( $mode === 'enqueue' && $update instanceof EnqueueableDataUpdate ) {
261  // Run only the job enqueue logic to complete the update later
262  $spec = $update->getAsJobSpecification();
263  JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
264  } elseif ( $update instanceof TransactionRoundDefiningUpdate ) {
265  $update->doUpdate();
266  } else {
267  // Run the bulk of the update now
268  $fnameTrxOwner = get_class( $update ) . '::doUpdate';
269  $lbFactory->beginMasterChanges( $fnameTrxOwner );
270  $update->doUpdate();
271  $lbFactory->commitMasterChanges( $fnameTrxOwner );
272  }
273  } catch ( Exception $e ) {
274  // Reporting GUI exceptions does not work post-send
275  if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) {
276  $guiError = $e;
277  }
279 
280  // VW-style hack to work around T190178, so we can make sure
281  // PageMetaDataUpdater doesn't throw exceptions.
282  if ( defined( 'MW_PHPUNIT_TEST' ) ) {
283  throw $e;
284  }
285  }
286 
287  return $guiError;
288  }
289 
301  public static function tryOpportunisticExecute( $mode = 'run' ) {
302  // execute() loop is already running
303  if ( self::$executeContext ) {
304  return false;
305  }
306 
307  // Avoiding running updates without them having outer scope
308  if ( !self::areDatabaseTransactionsActive() ) {
309  self::doUpdates( $mode );
310  return true;
311  }
312 
313  if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
314  // If we cannot run the updates with outer transaction context, try to
315  // at least enqueue all the updates that support queueing to job queue
316  self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
317  self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
318  }
319 
320  return !self::pendingUpdatesCount();
321  }
322 
329  private static function enqueueUpdates( array $updates ) {
330  $remaining = [];
331 
332  foreach ( $updates as $update ) {
333  if ( $update instanceof EnqueueableDataUpdate ) {
334  $spec = $update->getAsJobSpecification();
335  JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
336  } else {
337  $remaining[] = $update;
338  }
339  }
340 
341  return $remaining;
342  }
343 
348  public static function pendingUpdatesCount() {
349  return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
350  }
351 
357  public static function getPendingUpdates( $stage = self::ALL ) {
358  $updates = [];
359  if ( $stage === self::ALL || $stage === self::PRESEND ) {
360  $updates = array_merge( $updates, self::$preSendUpdates );
361  }
362  if ( $stage === self::ALL || $stage === self::POSTSEND ) {
363  $updates = array_merge( $updates, self::$postSendUpdates );
364  }
365  return $updates;
366  }
367 
372  public static function clearPendingUpdates() {
373  self::$preSendUpdates = [];
374  self::$postSendUpdates = [];
375  }
376 
380  private static function areDatabaseTransactionsActive() {
381  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
382  if ( $lbFactory->hasTransactionRound() || !$lbFactory->isReadyForRoundOperations() ) {
383  return true;
384  }
385 
386  $connsBusy = false;
387  $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
388  $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
389  if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
390  $connsBusy = true;
391  }
392  } );
393  } );
394 
395  return $connsBusy;
396  }
397 }
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
static enqueueUpdates(array $updates)
Enqueue a job for each EnqueueableDataUpdate item and return the other items.
static DeferrableUpdate [] $postSendUpdates
Updates to be deferred until after request end.
static clearPendingUpdates()
Clear all pending updates without performing them.
$batch execute()
An interface for generating database load balancers.
Definition: LBFactory.php:39
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2174
Interface that marks a DataUpdate as enqueuable via the JobQueue.
static runUpdate(DeferrableUpdate $update, LBFactory $lbFactory, $mode, $stage)
Interface that deferrable updates can implement to signal that updates can be combined.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message key
Definition: hooks.txt:2166
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 MediaWikiServices
Definition: injection.txt:23
static array null $executeContext
Information about the current execute() call or null if not running.
static rollbackMasterChangesAndLog( $e)
Roll back any open database transactions and log the stack trace of the exception.
static areDatabaseTransactionsActive()
commitMasterChanges( $fname=__METHOD__, array $options=[])
Commit changes and clear view snapshots on all master connections.
Definition: LBFactory.php:250
writesOrCallbacksPending()
Whether there is a transaction open with either possible write queries or unresolved pre-commit/commi...
static pendingUpdatesCount()
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add a callable update.
static getMain()
Get the RequestContext object associated with the main request.
Deferrable update that must run outside of any explicit LBFactory transaction round.
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place or wrap services the preferred way to define a new service is the $wgServiceWiringFiles array $services
Definition: hooks.txt:2222
An error page which can definitely be safely rendered using the OutputPage.
Callback wrapper that has an originating method.
forEachOpenMasterConnection( $callback, array $params=[])
Call a function with each open connection object to a master.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
static getPendingUpdates( $stage=self::ALL)
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
static tryOpportunisticExecute( $mode='run')
Run all deferred updates immediately if there are no DB writes active.
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
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
static execute(array &$queue, $mode, $stage)
Immediately run/queue a list of updates.
global $wgCommandLineMode
static push(array &$queue, DeferrableUpdate $update)
doUpdate()
Perform the actual work.
static singleton( $domain=false)
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:276
beginMasterChanges( $fname=__METHOD__)
Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
Definition: LBFactory.php:235
Database connection, tracking, load balancing, and transaction manager for a cluster.
static DeferrableUpdate [] $preSendUpdates
Updates to be deferred until before request end.