2011-09-10 06:50:30 +00:00
|
|
|
<?php
|
2012-05-20 15:56:43 +00:00
|
|
|
/**
|
|
|
|
|
* Interface and manager for deferred updates.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2016-08-28 16:06:57 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2012-05-20 15:56:43 +00:00
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
/**
|
2015-05-18 21:20:35 +00:00
|
|
|
* Class for managing the deferred updates
|
|
|
|
|
*
|
2015-11-30 22:02:53 +00:00
|
|
|
* In web request mode, deferred updates can be run at the end of the request, either before or
|
|
|
|
|
* after the HTTP response has been sent. In either case, they run after the DB commit step. If
|
|
|
|
|
* an update runs after the response is sent, it will not block clients. If sent before, it will
|
|
|
|
|
* run synchronously. If such an update works via queueing, it will be more likely to complete by
|
|
|
|
|
* the time the client makes their next request after this one.
|
|
|
|
|
*
|
|
|
|
|
* In CLI mode, updates are only deferred until the current wiki has no DB write transaction
|
|
|
|
|
* active within this request.
|
|
|
|
|
*
|
|
|
|
|
* When updates are deferred, they use a FIFO queue (one for pre-send and one for post-send).
|
2011-09-10 17:16:41 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.19
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
|
|
|
|
class DeferredUpdates {
|
2015-11-30 22:02:53 +00:00
|
|
|
/** @var DeferrableUpdate[] Updates to be deferred until before request end */
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $preSendUpdates = [];
|
2015-11-30 22:02:53 +00:00
|
|
|
/** @var DeferrableUpdate[] Updates to be deferred until after request end */
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $postSendUpdates = [];
|
2015-11-30 22:02:53 +00:00
|
|
|
|
|
|
|
|
const ALL = 0; // all updates
|
|
|
|
|
const PRESEND = 1; // for updates that should run before flushing output buffer
|
|
|
|
|
const POSTSEND = 2; // for updates that should run after flushing output buffer
|
|
|
|
|
|
2016-08-28 16:06:57 +00:00
|
|
|
const BIG_QUEUE_SIZE = 100;
|
|
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
/**
|
2016-09-01 17:03:31 +00:00
|
|
|
* Add an update to the deferred list to be run later by execute()
|
|
|
|
|
*
|
|
|
|
|
* In CLI mode, callback magic will also be used to run updates when safe
|
2015-11-30 22:02:53 +00:00
|
|
|
*
|
2013-11-20 18:43:39 +00:00
|
|
|
* @param DeferrableUpdate $update Some object that implements doUpdate()
|
2015-11-30 22:02:53 +00:00
|
|
|
* @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
2015-11-30 22:02:53 +00:00
|
|
|
public static function addUpdate( DeferrableUpdate $update, $type = self::POSTSEND ) {
|
2016-09-01 17:03:31 +00:00
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
|
2015-11-30 22:02:53 +00:00
|
|
|
if ( $type === self::PRESEND ) {
|
|
|
|
|
self::push( self::$preSendUpdates, $update );
|
|
|
|
|
} else {
|
|
|
|
|
self::push( self::$postSendUpdates, $update );
|
|
|
|
|
}
|
2016-09-01 17:03:31 +00:00
|
|
|
|
|
|
|
|
// Try to run the updates now if in CLI mode and no transaction is active.
|
|
|
|
|
// This covers scripts that don't/barely use the DB but make updates to other stores.
|
|
|
|
|
if ( $wgCommandLineMode ) {
|
|
|
|
|
self::tryOpportunisticExecute( 'run' );
|
|
|
|
|
}
|
2015-11-30 22:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-22 02:32:09 +00:00
|
|
|
* Add a callable update. In a lot of cases, we just need a callback/closure,
|
2015-11-30 22:02:53 +00:00
|
|
|
* defining a new DeferrableUpdate object is not necessary
|
|
|
|
|
*
|
|
|
|
|
* @see MWCallableUpdate::__construct()
|
|
|
|
|
*
|
|
|
|
|
* @param callable $callable
|
|
|
|
|
* @param integer $type DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
|
2016-08-13 19:28:53 +00:00
|
|
|
* @param IDatabase|null $dbw Abort if this DB is rolled back [optional] (since 1.28)
|
2015-11-30 22:02:53 +00:00
|
|
|
*/
|
2016-08-13 19:28:53 +00:00
|
|
|
public static function addCallableUpdate(
|
|
|
|
|
$callable, $type = self::POSTSEND, IDatabase $dbw = null
|
|
|
|
|
) {
|
|
|
|
|
self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $type );
|
2015-11-30 22:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do any deferred updates and clear the list
|
|
|
|
|
*
|
|
|
|
|
* @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
|
|
|
|
|
* @param integer $type DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
|
|
|
|
|
*/
|
|
|
|
|
public static function doUpdates( $mode = 'run', $type = self::ALL ) {
|
|
|
|
|
if ( $type === self::ALL || $type == self::PRESEND ) {
|
|
|
|
|
self::execute( self::$preSendUpdates, $mode );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $type === self::ALL || $type == self::POSTSEND ) {
|
|
|
|
|
self::execute( self::$postSendUpdates, $mode );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function push( array &$queue, DeferrableUpdate $update ) {
|
2015-11-30 23:26:45 +00:00
|
|
|
if ( $update instanceof MergeableUpdate ) {
|
|
|
|
|
$class = get_class( $update ); // fully-qualified class
|
|
|
|
|
if ( isset( $queue[$class] ) ) {
|
|
|
|
|
/** @var $existingUpdate MergeableUpdate */
|
|
|
|
|
$existingUpdate = $queue[$class];
|
|
|
|
|
$existingUpdate->merge( $update );
|
|
|
|
|
} else {
|
|
|
|
|
$queue[$class] = $update;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$queue[] = $update;
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-30 22:02:53 +00:00
|
|
|
public static function execute( array &$queue, $mode ) {
|
2016-06-18 18:24:01 +00:00
|
|
|
$stats = \MediaWiki\MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
|
$method = RequestContext::getMain()->getRequest()->getMethod();
|
2011-09-10 06:50:30 +00:00
|
|
|
|
2016-06-18 18:24:01 +00:00
|
|
|
$updates = $queue; // snapshot of queue
|
2015-11-30 22:02:53 +00:00
|
|
|
// Keep doing rounds of updates until none get enqueued
|
2015-05-18 21:20:35 +00:00
|
|
|
while ( count( $updates ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$queue = []; // clear the queue
|
2015-10-08 06:46:18 +00:00
|
|
|
/** @var DataUpdate[] $dataUpdates */
|
2016-02-17 09:09:32 +00:00
|
|
|
$dataUpdates = [];
|
2015-10-08 06:46:18 +00:00
|
|
|
/** @var DeferrableUpdate[] $otherUpdates */
|
2016-02-17 09:09:32 +00:00
|
|
|
$otherUpdates = [];
|
2014-08-19 22:23:05 +00:00
|
|
|
foreach ( $updates as $update ) {
|
2015-10-08 06:46:18 +00:00
|
|
|
if ( $update instanceof DataUpdate ) {
|
|
|
|
|
$dataUpdates[] = $update;
|
|
|
|
|
} else {
|
|
|
|
|
$otherUpdates[] = $update;
|
|
|
|
|
}
|
2016-07-22 02:32:09 +00:00
|
|
|
|
|
|
|
|
$name = $update instanceof DeferrableCallback
|
|
|
|
|
? get_class( $update ) . '-' . $update->getOrigin()
|
|
|
|
|
: get_class( $update );
|
|
|
|
|
$stats->increment( 'deferred_updates.' . $method . '.' . $name );
|
2015-10-08 06:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delegate DataUpdate execution to the DataUpdate class
|
2016-04-26 21:06:51 +00:00
|
|
|
try {
|
|
|
|
|
DataUpdate::runUpdates( $dataUpdates, $mode );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// Let the other updates occur if these had to rollback
|
|
|
|
|
MWExceptionHandler::logException( $e );
|
|
|
|
|
}
|
2015-10-08 06:46:18 +00:00
|
|
|
// Execute the non-DataUpdate tasks
|
|
|
|
|
foreach ( $otherUpdates as $update ) {
|
2014-08-19 22:23:05 +00:00
|
|
|
try {
|
|
|
|
|
$update->doUpdate();
|
2015-12-23 02:30:20 +00:00
|
|
|
wfGetLBFactory()->commitMasterChanges( __METHOD__ );
|
2015-01-09 23:44:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
2014-08-19 22:23:05 +00:00
|
|
|
// We don't want exceptions thrown during deferred updates to
|
2015-11-03 20:47:05 +00:00
|
|
|
// be reported to the user since the output is already sent
|
2014-08-19 22:23:05 +00:00
|
|
|
if ( !$e instanceof ErrorPageError ) {
|
|
|
|
|
MWExceptionHandler::logException( $e );
|
|
|
|
|
}
|
2015-11-03 20:47:05 +00:00
|
|
|
// Make sure incomplete transactions are not committed and end any
|
|
|
|
|
// open atomic sections so that other DB updates have a chance to run
|
2015-12-23 02:30:20 +00:00
|
|
|
wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
|
2012-07-12 21:07:56 +00:00
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
2015-05-18 21:20:35 +00:00
|
|
|
|
2015-11-30 22:02:53 +00:00
|
|
|
$updates = $queue; // new snapshot of queue (check for new entries)
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 16:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* Run all deferred updates immediately if there are no DB writes active
|
|
|
|
|
*
|
|
|
|
|
* If $mode is 'run' but there are busy databates, EnqueueableDataUpdate
|
|
|
|
|
* tasks will be enqueued anyway for the sake of progress.
|
|
|
|
|
*
|
|
|
|
|
* @param string $mode Use "enqueue" to use the job queue when possible
|
|
|
|
|
* @return bool Whether updates were allowed to run
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function tryOpportunisticExecute( $mode = 'run' ) {
|
2016-09-01 17:01:25 +00:00
|
|
|
if ( !self::getBusyDbConnections() ) {
|
|
|
|
|
self::doUpdates( $mode );
|
|
|
|
|
return true;
|
2016-08-28 16:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-01 17:01:25 +00:00
|
|
|
if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
|
|
|
|
|
// If we cannot run the updates with outer transaction context, try to
|
|
|
|
|
// at least enqueue all the updates that support queueing to job queue
|
|
|
|
|
self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
|
|
|
|
|
self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
|
2016-08-28 16:06:57 +00:00
|
|
|
}
|
2016-09-01 17:01:25 +00:00
|
|
|
|
|
|
|
|
return !self::pendingUpdatesCount();
|
2016-08-28 16:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enqueue a job for each EnqueueableDataUpdate item and return the other items
|
|
|
|
|
*
|
|
|
|
|
* @param DeferrableUpdate[] $updates A list of deferred update instances
|
|
|
|
|
* @return DeferrableUpdate[] Remaining updates that do not support being queued
|
|
|
|
|
*/
|
|
|
|
|
private static function enqueueUpdates( array $updates ) {
|
|
|
|
|
$remaining = [];
|
|
|
|
|
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
if ( $update instanceof EnqueueableDataUpdate ) {
|
|
|
|
|
$spec = $update->getAsJobSpecification();
|
|
|
|
|
JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
|
|
|
|
|
} else {
|
|
|
|
|
$remaining[] = $update;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $remaining;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return integer Number of enqueued updates
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function pendingUpdatesCount() {
|
|
|
|
|
return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-10 06:50:30 +00:00
|
|
|
/**
|
|
|
|
|
* Clear all pending updates without performing them. Generally, you don't
|
|
|
|
|
* want or need to call this. Unit tests need it though.
|
|
|
|
|
*/
|
|
|
|
|
public static function clearPendingUpdates() {
|
2016-02-17 09:09:32 +00:00
|
|
|
self::$preSendUpdates = [];
|
|
|
|
|
self::$postSendUpdates = [];
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
2016-08-28 16:06:57 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the rollback/commit watcher on a DB to trigger update runs when safe
|
|
|
|
|
*
|
|
|
|
|
* @TODO: use this to replace DB logic in push()
|
|
|
|
|
* @param LoadBalancer $lb
|
|
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function installDBListener( LoadBalancer $lb ) {
|
|
|
|
|
static $triggers = [ IDatabase::TRIGGER_COMMIT, IDatabase::TRIGGER_ROLLBACK ];
|
|
|
|
|
// Hook into active master connections to find a moment where no writes are pending
|
|
|
|
|
$lb->setTransactionListener(
|
|
|
|
|
__METHOD__,
|
|
|
|
|
function ( $trigger, IDatabase $conn ) use ( $triggers ) {
|
|
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
|
|
|
|
|
if ( $wgCommandLineMode && in_array( $trigger, $triggers ) ) {
|
|
|
|
|
DeferredUpdates::tryOpportunisticExecute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return IDatabase[] Connection where commit() cannot be called yet
|
|
|
|
|
*/
|
|
|
|
|
private static function getBusyDbConnections() {
|
|
|
|
|
$connsBusy = [];
|
|
|
|
|
|
|
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
|
$lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
|
|
|
|
|
$lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
|
|
|
|
|
if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
|
|
|
|
|
$connsBusy[] = $conn;
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return $connsBusy;
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|