MediaWiki  1.28.0
DeferredUpdates.php
Go to the documentation of this file.
1 <?php
23 
52  private static $preSendUpdates = [];
54  private static $postSendUpdates = [];
56  private static $immediateMode = false;
57 
58  const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
59  const PRESEND = 1; // for updates that should run before flushing output buffer
60  const POSTSEND = 2; // for updates that should run after flushing output buffer
61 
62  const BIG_QUEUE_SIZE = 100;
63 
65  private static $executeContext;
66 
75  public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
77 
78  // This is a sub-DeferredUpdate, run it right after its parent update
79  if ( self::$executeContext && self::$executeContext['stage'] >= $stage ) {
80  self::$executeContext['subqueue'][] = $update;
81  return;
82  }
83 
84  if ( $stage === self::PRESEND ) {
85  self::push( self::$preSendUpdates, $update );
86  } else {
87  self::push( self::$postSendUpdates, $update );
88  }
89 
90  if ( self::$immediateMode ) {
91  // No more explicit doUpdates() calls will happen, so run this now
92  self::doUpdates( 'run' );
93  return;
94  }
95 
96  // Try to run the updates now if in CLI mode and no transaction is active.
97  // This covers scripts that don't/barely use the DB but make updates to other stores.
98  if ( $wgCommandLineMode ) {
99  self::tryOpportunisticExecute( 'run' );
100  }
101  }
102 
113  public static function addCallableUpdate(
114  $callable, $stage = self::POSTSEND, IDatabase $dbw = null
115  ) {
116  self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
117  }
118 
125  public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
126  $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
127 
128  if ( $stage === self::ALL || $stage === self::PRESEND ) {
129  self::execute( self::$preSendUpdates, $mode, $stageEffective );
130  }
131 
132  if ( $stage === self::ALL || $stage == self::POSTSEND ) {
133  self::execute( self::$postSendUpdates, $mode, $stageEffective );
134  }
135  }
136 
141  public static function setImmediateMode( $value ) {
142  self::$immediateMode = (bool)$value;
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  } else {
157  $queue[$class] = $update;
158  }
159  } else {
160  $queue[] = $update;
161  }
162  }
163 
173  protected static function execute( array &$queue, $mode, $stage ) {
174  $services = MediaWikiServices::getInstance();
175  $stats = $services->getStatsdDataFactory();
176  $lbFactory = $services->getDBLoadBalancerFactory();
177  $method = RequestContext::getMain()->getRequest()->getMethod();
178 
179  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
180 
182  $reportableError = null;
184  $updates = $queue;
185 
186  // Keep doing rounds of updates until none get enqueued...
187  while ( $updates ) {
188  $queue = []; // clear the queue
189 
190  if ( $mode === 'enqueue' ) {
191  try {
192  // Push enqueuable updates to the job queue and get the rest
193  $updates = self::enqueueUpdates( $updates );
194  } catch ( Exception $e ) {
195  // Let other updates have a chance to run if this failed
197  }
198  }
199 
200  // Order will be DataUpdate followed by generic DeferrableUpdate tasks
201  $updatesByType = [ 'data' => [], 'generic' => [] ];
202  foreach ( $updates as $du ) {
203  if ( $du instanceof DataUpdate ) {
204  $du->setTransactionTicket( $ticket );
205  $updatesByType['data'][] = $du;
206  } else {
207  $updatesByType['generic'][] = $du;
208  }
209 
210  $name = ( $du instanceof DeferrableCallback )
211  ? get_class( $du ) . '-' . $du->getOrigin()
212  : get_class( $du );
213  $stats->increment( 'deferred_updates.' . $method . '.' . $name );
214  }
215 
216  // Execute all remaining tasks...
217  foreach ( $updatesByType as $updatesForType ) {
218  foreach ( $updatesForType as $update ) {
219  self::$executeContext = [
220  'update' => $update,
221  'stage' => $stage,
222  'subqueue' => []
223  ];
225  $guiError = self::runUpdate( $update, $lbFactory, $stage );
226  $reportableError = $reportableError ?: $guiError;
227  // Do the subqueue updates for $update until there are none
228  while ( self::$executeContext['subqueue'] ) {
229  $subUpdate = reset( self::$executeContext['subqueue'] );
230  $firstKey = key( self::$executeContext['subqueue'] );
231  unset( self::$executeContext['subqueue'][$firstKey] );
232 
233  if ( $subUpdate instanceof DataUpdate ) {
234  $subUpdate->setTransactionTicket( $ticket );
235  }
236 
237  $guiError = self::runUpdate( $subUpdate, $lbFactory, $stage );
238  $reportableError = $reportableError ?: $guiError;
239  }
240  self::$executeContext = null;
241  }
242  }
243 
244  $updates = $queue; // new snapshot of queue (check for new entries)
245  }
246 
247  if ( $reportableError ) {
248  throw $reportableError; // throw the first of any GUI errors
249  }
250  }
251 
258  private static function runUpdate( DeferrableUpdate $update, LBFactory $lbFactory, $stage ) {
259  $guiError = null;
260  try {
261  $fnameTrxOwner = get_class( $update ) . '::doUpdate';
262  $lbFactory->beginMasterChanges( $fnameTrxOwner );
263  $update->doUpdate();
264  $lbFactory->commitMasterChanges( $fnameTrxOwner );
265  } catch ( Exception $e ) {
266  // Reporting GUI exceptions does not work post-send
267  if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) {
268  $guiError = $e;
269  }
271  }
272 
273  return $guiError;
274  }
275 
286  public static function tryOpportunisticExecute( $mode = 'run' ) {
287  // execute() loop is already running
288  if ( self::$executeContext ) {
289  return false;
290  }
291 
292  // Avoiding running updates without them having outer scope
293  if ( !self::getBusyDbConnections() ) {
294  self::doUpdates( $mode );
295  return true;
296  }
297 
298  if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
299  // If we cannot run the updates with outer transaction context, try to
300  // at least enqueue all the updates that support queueing to job queue
301  self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
302  self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
303  }
304 
305  return !self::pendingUpdatesCount();
306  }
307 
314  private static function enqueueUpdates( array $updates ) {
315  $remaining = [];
316 
317  foreach ( $updates as $update ) {
318  if ( $update instanceof EnqueueableDataUpdate ) {
319  $spec = $update->getAsJobSpecification();
320  JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
321  } else {
322  $remaining[] = $update;
323  }
324  }
325 
326  return $remaining;
327  }
328 
333  public static function pendingUpdatesCount() {
334  return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
335  }
336 
341  public static function clearPendingUpdates() {
342  self::$preSendUpdates = [];
343  self::$postSendUpdates = [];
344  }
345 
349  private static function getBusyDbConnections() {
350  $connsBusy = [];
351 
352  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
353  $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
354  $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
355  if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
356  $connsBusy[] = $conn;
357  }
358  } );
359  } );
360 
361  return $connsBusy;
362  }
363 }
explicitTrxActive()
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.
beginMasterChanges($fname=__METHOD__)
Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
Definition: LBFactory.php:191
static clearPendingUpdates()
Clear all pending updates without performing them.
the array() calling protocol came about after MediaWiki 1.4rc1.
$batch execute()
Interface that deferrable updates should implement.
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:2102
static doUpdates($mode= 'run', $stage=self::ALL)
Do any deferred updates and clear the list.
Interface that marks a DataUpdate as enqueuable via the JobQueue.
Interface that deferrable updates can implement.
$value
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 tryOpportunisticExecute($mode= 'run')
Run all deferred updates immediately if there are no DB writes active.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static runUpdate(DeferrableUpdate $update, LBFactory $lbFactory, $stage)
Deferrable Update for closure/callback.
commitMasterChanges($fname=__METHOD__, array $options=[])
Commit changes on all master connections.
Definition: LBFactory.php:203
global $wgCommandLineMode
Definition: Setup.php:495
static pendingUpdatesCount()
static getMain()
Static methods.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
An error page which can definitely be safely rendered using the OutputPage.
Callback wrapper that has an originating method.
writesOrCallbacksPending()
Returns true if there is a transaction open with possible write queries or transaction pre-commit/idl...
static setImmediateMode($value)
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title 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:2159
static bool $immediateMode
Whether to just run updates in addUpdate()
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
forEachOpenMasterConnection($callback, array $params=[])
Call a function with each open connection object to a master.
static rollbackMasterChangesAndLog($e)
If there are any open database transactions, roll them back and log the stack trace of the exception ...
static getBusyDbConnections()
static singleton($wiki=false)
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 addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
$lbFactory
Abstract base class for update jobs that do something with some secondary data extracted from article...
Definition: DataUpdate.php:28
static execute(array &$queue, $mode, $stage)
Immediately run/queue a list of updates.
static push(array &$queue, DeferrableUpdate $update)
doUpdate()
Perform the actual work.
static addCallableUpdate($callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
static DeferrableUpdate[] $preSendUpdates
Updates to be deferred until before request end.
wfGetCaller($level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:34
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300