MediaWiki  REL1_28
DeferredUpdates.php
Go to the documentation of this file.
1 <?php
23 
52  private static $preSendUpdates = [];
54  private static $postSendUpdates = [];
55 
56  const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
57  const PRESEND = 1; // for updates that should run before flushing output buffer
58  const POSTSEND = 2; // for updates that should run after flushing output buffer
59 
60  const BIG_QUEUE_SIZE = 100;
61 
63  private static $executeContext;
64 
73  public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
75 
76  // This is a sub-DeferredUpdate, run it right after its parent update
77  if ( self::$executeContext && self::$executeContext['stage'] >= $stage ) {
78  self::$executeContext['subqueue'][] = $update;
79  return;
80  }
81 
82  if ( $stage === self::PRESEND ) {
83  self::push( self::$preSendUpdates, $update );
84  } else {
85  self::push( self::$postSendUpdates, $update );
86  }
87 
88  // Try to run the updates now if in CLI mode and no transaction is active.
89  // This covers scripts that don't/barely use the DB but make updates to other stores.
90  if ( $wgCommandLineMode ) {
92  }
93  }
94 
105  public static function addCallableUpdate(
106  $callable, $stage = self::POSTSEND, IDatabase $dbw = null
107  ) {
108  self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
109  }
110 
117  public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
118  $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
119 
120  if ( $stage === self::ALL || $stage === self::PRESEND ) {
121  self::execute( self::$preSendUpdates, $mode, $stageEffective );
122  }
123 
124  if ( $stage === self::ALL || $stage == self::POSTSEND ) {
125  self::execute( self::$postSendUpdates, $mode, $stageEffective );
126  }
127  }
128 
134  public static function setImmediateMode( $value ) {
135  wfDeprecated( __METHOD__, '1.29' );
136  }
137 
142  private static function push( array &$queue, DeferrableUpdate $update ) {
143  if ( $update instanceof MergeableUpdate ) {
144  $class = get_class( $update ); // fully-qualified class
145  if ( isset( $queue[$class] ) ) {
147  $existingUpdate = $queue[$class];
148  $existingUpdate->merge( $update );
149  } else {
150  $queue[$class] = $update;
151  }
152  } else {
153  $queue[] = $update;
154  }
155  }
156 
166  protected static function execute( array &$queue, $mode, $stage ) {
167  $services = MediaWikiServices::getInstance();
168  $stats = $services->getStatsdDataFactory();
169  $lbFactory = $services->getDBLoadBalancerFactory();
170  $method = RequestContext::getMain()->getRequest()->getMethod();
171 
172  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
173 
175  $reportableError = null;
177  $updates = $queue;
178 
179  // Keep doing rounds of updates until none get enqueued...
180  while ( $updates ) {
181  $queue = []; // clear the queue
182 
183  if ( $mode === 'enqueue' ) {
184  try {
185  // Push enqueuable updates to the job queue and get the rest
186  $updates = self::enqueueUpdates( $updates );
187  } catch ( Exception $e ) {
188  // Let other updates have a chance to run if this failed
190  }
191  }
192 
193  // Order will be DataUpdate followed by generic DeferrableUpdate tasks
194  $updatesByType = [ 'data' => [], 'generic' => [] ];
195  foreach ( $updates as $du ) {
196  if ( $du instanceof DataUpdate ) {
197  $du->setTransactionTicket( $ticket );
198  $updatesByType['data'][] = $du;
199  } else {
200  $updatesByType['generic'][] = $du;
201  }
202 
203  $name = ( $du instanceof DeferrableCallback )
204  ? get_class( $du ) . '-' . $du->getOrigin()
205  : get_class( $du );
206  $stats->increment( 'deferred_updates.' . $method . '.' . $name );
207  }
208 
209  // Execute all remaining tasks...
210  foreach ( $updatesByType as $updatesForType ) {
211  foreach ( $updatesForType as $update ) {
212  self::$executeContext = [
213  'update' => $update,
214  'stage' => $stage,
215  'subqueue' => []
216  ];
218  $guiError = self::runUpdate( $update, $lbFactory, $stage );
219  $reportableError = $reportableError ?: $guiError;
220  // Do the subqueue updates for $update until there are none
221  while ( self::$executeContext['subqueue'] ) {
222  $subUpdate = reset( self::$executeContext['subqueue'] );
223  $firstKey = key( self::$executeContext['subqueue'] );
224  unset( self::$executeContext['subqueue'][$firstKey] );
225 
226  if ( $subUpdate instanceof DataUpdate ) {
227  $subUpdate->setTransactionTicket( $ticket );
228  }
229 
230  $guiError = self::runUpdate( $subUpdate, $lbFactory, $stage );
231  $reportableError = $reportableError ?: $guiError;
232  }
233  self::$executeContext = null;
234  }
235  }
236 
237  $updates = $queue; // new snapshot of queue (check for new entries)
238  }
239 
240  if ( $reportableError ) {
241  throw $reportableError; // throw the first of any GUI errors
242  }
243  }
244 
251  private static function runUpdate( DeferrableUpdate $update, LBFactory $lbFactory, $stage ) {
252  $guiError = null;
253  try {
254  $fnameTrxOwner = get_class( $update ) . '::doUpdate';
255  $lbFactory->beginMasterChanges( $fnameTrxOwner );
256  $update->doUpdate();
257  $lbFactory->commitMasterChanges( $fnameTrxOwner );
258  } catch ( Exception $e ) {
259  // Reporting GUI exceptions does not work post-send
260  if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) {
261  $guiError = $e;
262  }
264  }
265 
266  return $guiError;
267  }
268 
279  public static function tryOpportunisticExecute( $mode = 'run' ) {
280  // execute() loop is already running
281  if ( self::$executeContext ) {
282  return false;
283  }
284 
285  // Avoiding running updates without them having outer scope
286  if ( !self::areDatabaseTransactionsActive() ) {
287  self::doUpdates( $mode );
288  return true;
289  }
290 
291  if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
292  // If we cannot run the updates with outer transaction context, try to
293  // at least enqueue all the updates that support queueing to job queue
294  self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
295  self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
296  }
297 
298  return !self::pendingUpdatesCount();
299  }
300 
307  private static function enqueueUpdates( array $updates ) {
308  $remaining = [];
309 
310  foreach ( $updates as $update ) {
311  if ( $update instanceof EnqueueableDataUpdate ) {
312  $spec = $update->getAsJobSpecification();
313  JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
314  } else {
315  $remaining[] = $update;
316  }
317  }
318 
319  return $remaining;
320  }
321 
326  public static function pendingUpdatesCount() {
327  return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
328  }
329 
334  public static function clearPendingUpdates() {
335  self::$preSendUpdates = [];
336  self::$postSendUpdates = [];
337  }
338 
342  private static function areDatabaseTransactionsActive() {
343  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
344  if ( $lbFactory->hasTransactionRound() ) {
345  return true;
346  }
347 
348  $connsBusy = false;
349  $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
350  $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
351  if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
352  $connsBusy = true;
353  }
354  } );
355  } );
356 
357  return $connsBusy;
358  }
359 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:3
DeferredUpdates\ALL
const ALL
Definition: DeferredUpdates.php:56
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MergeableUpdate
Interface that deferrable updates can implement.
Definition: MergeableUpdate.php:9
DeferrableUpdate\doUpdate
doUpdate()
Perform the actual work.
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:73
LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:31
DeferredUpdates\clearPendingUpdates
static clearPendingUpdates()
Clear all pending updates without performing them.
Definition: DeferredUpdates.php:334
$lbFactory
$lbFactory
Definition: doMaintenance.php:117
DeferredUpdates\$postSendUpdates
static DeferrableUpdate[] $postSendUpdates
Updates to be deferred until after request end.
Definition: DeferredUpdates.php:54
DeferrableCallback
Callback wrapper that has an originating method.
Definition: DeferrableCallback.php:8
DataUpdate
Abstract base class for update jobs that do something with some secondary data extracted from article...
Definition: DataUpdate.php:28
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
EnqueueableDataUpdate
Interface that marks a DataUpdate as enqueuable via the JobQueue.
Definition: EnqueueableDataUpdate.php:10
key
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
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, IDatabase $dbw=null)
Add a callable update.
Definition: DeferredUpdates.php:105
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1126
IDatabase\explicitTrxActive
explicitTrxActive()
$wgCommandLineMode
global $wgCommandLineMode
Definition: Setup.php:495
DeferredUpdates
Class for managing the deferred updates.
Definition: DeferredUpdates.php:50
MWExceptionHandler\rollbackMasterChangesAndLog
static rollbackMasterChangesAndLog( $e)
If there are any open database transactions, roll them back and log the stack trace of the exception ...
Definition: MWExceptionHandler.php:89
$queue
$queue
Definition: mergeMessageFileList.php:171
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:58
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$services
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:2162
LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:31
DeferredUpdates\setImmediateMode
static setImmediateMode( $value)
Definition: DeferredUpdates.php:134
LoadBalancer\forEachOpenMasterConnection
forEachOpenMasterConnection( $callback, array $params=[])
Call a function with each open connection object to a master.
Definition: LoadBalancer.php:1383
DeferredUpdates\BIG_QUEUE_SIZE
const BIG_QUEUE_SIZE
Definition: DeferredUpdates.php:60
$value
$value
Definition: styleTest.css.php:45
DeferredUpdates\execute
static execute(array &$queue, $mode, $stage)
Immediately run/queue a list of updates.
Definition: DeferredUpdates.php:166
DeferredUpdates\tryOpportunisticExecute
static tryOpportunisticExecute( $mode='run')
Run all deferred updates immediately if there are no DB writes active.
Definition: DeferredUpdates.php:279
DeferredUpdates\$executeContext
static array null $executeContext
Information about the current execute() call or null if not running.
Definition: DeferredUpdates.php:63
DeferredUpdates\areDatabaseTransactionsActive
static areDatabaseTransactionsActive()
Definition: DeferredUpdates.php:342
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:34
DeferredUpdates\doUpdates
static doUpdates( $mode='run', $stage=self::ALL)
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:117
DeferredUpdates\pendingUpdatesCount
static pendingUpdatesCount()
Definition: DeferredUpdates.php:326
MWCallableUpdate
Deferrable Update for closure/callback.
Definition: MWCallableUpdate.php:6
DeferredUpdates\runUpdate
static runUpdate(DeferrableUpdate $update, LBFactory $lbFactory, $stage)
Definition: DeferredUpdates.php:251
DeferredUpdates\PRESEND
const PRESEND
Definition: DeferredUpdates.php:57
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:71
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
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:304
DeferredUpdates\enqueueUpdates
static enqueueUpdates(array $updates)
Enqueue a job for each EnqueueableDataUpdate item and return the other items.
Definition: DeferredUpdates.php:307
IDatabase\writesOrCallbacksPending
writesOrCallbacksPending()
Returns true if there is a transaction open with possible write queries or transaction pre-commit/idl...
DeferredUpdates\push
static push(array &$queue, DeferrableUpdate $update)
Definition: DeferredUpdates.php:142
DeferrableUpdate
Interface that deferrable updates should implement.
Definition: DeferrableUpdate.php:9
MediaWikiServices
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
DeferredUpdates\$preSendUpdates
static DeferrableUpdate[] $preSendUpdates
Updates to be deferred until before request end.
Definition: DeferredUpdates.php:52
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1573
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2105