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
|
|
|
|
|
*/
|
2019-07-11 19:47:48 +00:00
|
|
|
|
|
|
|
|
use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2016-08-28 16:06:57 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-01-26 18:27:37 +00:00
|
|
|
use Wikimedia\Rdbms\LBFactory;
|
2019-05-21 21:52:57 +00:00
|
|
|
use Wikimedia\Rdbms\ILBFactory;
|
2017-02-18 00:26:47 +00:00
|
|
|
use Wikimedia\Rdbms\LoadBalancer;
|
2019-07-18 19:27:07 +00:00
|
|
|
use Wikimedia\Rdbms\DBTransactionError;
|
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
|
2016-09-05 03:09:30 +00:00
|
|
|
* run synchronously. These two modes are defined via PRESEND and POSTSEND constants, the latter
|
|
|
|
|
* being the default for addUpdate() and addCallableUpdate().
|
|
|
|
|
*
|
|
|
|
|
* Updates that work through this system will be more likely to complete by the time the client
|
|
|
|
|
* makes their next request after this one than with the JobQueue system.
|
2015-11-30 22:02:53 +00:00
|
|
|
*
|
2018-05-02 23:56:07 +00:00
|
|
|
* In CLI mode, deferred updates will run:
|
|
|
|
|
* - a) During DeferredUpdates::addUpdate if no LBFactory DB handles have writes pending
|
|
|
|
|
* - b) On commit of an LBFactory DB handle if no other such handles have writes pending
|
|
|
|
|
* - c) During an LBFactory::waitForReplication call if no LBFactory DBs have writes pending
|
|
|
|
|
* - d) When the queue is large and an LBFactory DB handle commits (EnqueueableDataUpdate only)
|
|
|
|
|
* - e) At the completion of Maintenance::execute()
|
|
|
|
|
*
|
|
|
|
|
* @see Maintenance::setLBFactoryTriggers
|
2015-11-30 22:02:53 +00:00
|
|
|
*
|
2016-09-05 03:09:30 +00:00
|
|
|
* When updates are deferred, they go into one two FIFO "top-queues" (one for pre-send and one
|
|
|
|
|
* for post-send). Updates enqueued *during* doUpdate() of a "top" update go into the "sub-queue"
|
|
|
|
|
* for that update. After that method finishes, the sub-queue is run until drained. This continues
|
|
|
|
|
* for each top-queue job until the entire top queue is drained. This happens for the pre-send
|
|
|
|
|
* top-queue, and later on, the post-send top-queue, in execute().
|
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
|
|
|
|
2016-08-28 16:23:52 +00:00
|
|
|
const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
|
2015-11-30 22:02:53 +00:00
|
|
|
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;
|
|
|
|
|
|
2016-08-28 16:23:52 +00:00
|
|
|
/** @var array|null Information about the current execute() call or null if not running */
|
|
|
|
|
private static $executeContext;
|
|
|
|
|
|
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()
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
|
2011-09-10 06:50:30 +00:00
|
|
|
*/
|
2016-08-28 16:23:52 +00:00
|
|
|
public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
|
2016-09-01 17:03:31 +00:00
|
|
|
global $wgCommandLineMode;
|
|
|
|
|
|
2018-10-11 22:11:50 +00:00
|
|
|
if (
|
2019-10-11 14:06:45 +00:00
|
|
|
self::$executeContext !== null &&
|
2019-10-12 12:30:37 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
|
2018-10-11 22:11:50 +00:00
|
|
|
self::$executeContext['stage'] >= $stage &&
|
|
|
|
|
!( $update instanceof MergeableUpdate )
|
|
|
|
|
) {
|
2017-07-04 02:18:54 +00:00
|
|
|
// This is a sub-DeferredUpdate; run it right after its parent update.
|
|
|
|
|
// Also, while post-send updates are running, push any "pre-send" jobs to the
|
|
|
|
|
// active post-send queue to make sure they get run this round (or at all).
|
2016-08-28 16:23:52 +00:00
|
|
|
self::$executeContext['subqueue'][] = $update;
|
2017-07-04 02:18:54 +00:00
|
|
|
|
2016-08-28 16:23:52 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $stage === self::PRESEND ) {
|
2015-11-30 22:02:53 +00:00
|
|
|
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
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
|
2018-02-13 20:50:49 +00:00
|
|
|
* @param IDatabase|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(
|
2018-02-13 20:50:49 +00:00
|
|
|
$callable, $stage = self::POSTSEND, $dbw = null
|
2016-08-13 19:28:53 +00:00
|
|
|
) {
|
2016-08-28 16:23:52 +00:00
|
|
|
self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
|
2015-11-30 22:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do any deferred updates and clear the list
|
|
|
|
|
*
|
2019-03-06 08:04:12 +00:00
|
|
|
* If $stage is self::ALL then the queue of PRESEND updates will be resolved,
|
|
|
|
|
* followed by the queue of POSTSEND updates
|
|
|
|
|
*
|
2015-11-30 22:02:53 +00:00
|
|
|
* @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
|
2015-11-30 22:02:53 +00:00
|
|
|
*/
|
2016-08-28 16:23:52 +00:00
|
|
|
public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
|
|
|
|
|
$stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
|
2018-10-11 22:11:50 +00:00
|
|
|
// For ALL mode, make sure that any PRESEND updates added along the way get run.
|
|
|
|
|
// Normally, these use the subqueue, but that isn't true for MergeableUpdate items.
|
|
|
|
|
do {
|
|
|
|
|
if ( $stage === self::ALL || $stage === self::PRESEND ) {
|
2019-05-21 21:52:57 +00:00
|
|
|
self::handleUpdateQueue( self::$preSendUpdates, $mode, $stageEffective );
|
2018-10-11 22:11:50 +00:00
|
|
|
}
|
2016-08-28 16:23:52 +00:00
|
|
|
|
2018-10-11 22:11:50 +00:00
|
|
|
if ( $stage === self::ALL || $stage == self::POSTSEND ) {
|
2019-05-21 21:52:57 +00:00
|
|
|
self::handleUpdateQueue( self::$postSendUpdates, $mode, $stageEffective );
|
2018-10-11 22:11:50 +00:00
|
|
|
}
|
|
|
|
|
} while ( $stage === self::ALL && self::$preSendUpdates );
|
2015-11-30 22:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 16:23:52 +00:00
|
|
|
/**
|
|
|
|
|
* @param DeferrableUpdate[] $queue
|
|
|
|
|
* @param DeferrableUpdate $update
|
|
|
|
|
*/
|
2015-11-30 22:02:53 +00:00
|
|
|
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] ) ) {
|
2017-09-04 18:05:26 +00:00
|
|
|
/** @var MergeableUpdate $existingUpdate */
|
2015-11-30 23:26:45 +00:00
|
|
|
$existingUpdate = $queue[$class];
|
2019-08-31 16:14:38 +00:00
|
|
|
'@phan-var MergeableUpdate $existingUpdate';
|
2015-11-30 23:26:45 +00:00
|
|
|
$existingUpdate->merge( $update );
|
2018-10-11 21:51:36 +00:00
|
|
|
// Move the update to the end to handle things like mergeable purge
|
|
|
|
|
// updates that might depend on the prior updates in the queue running
|
|
|
|
|
unset( $queue[$class] );
|
|
|
|
|
$queue[$class] = $existingUpdate;
|
2015-11-30 23:26:45 +00:00
|
|
|
} else {
|
|
|
|
|
$queue[$class] = $update;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$queue[] = $update;
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 16:23:52 +00:00
|
|
|
/**
|
2019-05-21 21:52:57 +00:00
|
|
|
* Immediately run or enqueue a list of updates
|
2016-09-11 12:19:57 +00:00
|
|
|
*
|
2016-08-28 16:23:52 +00:00
|
|
|
* @param DeferrableUpdate[] &$queue List of DeferrableUpdate objects
|
2019-05-21 21:52:57 +00:00
|
|
|
* @param string $mode Either "run" or "enqueue" (to use the job queue when possible)
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $stage Class constant (PRESEND, POSTSEND) (since 1.28)
|
2016-08-28 16:23:52 +00:00
|
|
|
* @throws ErrorPageError Happens on top-level calls
|
|
|
|
|
* @throws Exception Happens on second-level calls
|
2019-10-12 12:30:37 +00:00
|
|
|
* @suppress PhanTypeArraySuspiciousNullable False positives
|
2016-08-28 16:23:52 +00:00
|
|
|
*/
|
2019-05-21 21:52:57 +00:00
|
|
|
protected static function handleUpdateQueue( array &$queue, $mode, $stage ) {
|
2016-08-28 16:23:52 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
|
$stats = $services->getStatsdDataFactory();
|
2019-07-11 19:47:48 +00:00
|
|
|
$lbf = $services->getDBLoadBalancerFactory();
|
|
|
|
|
$logger = LoggerFactory::getInstance( 'DeferredUpdates' );
|
|
|
|
|
$httpMethod = $services->getMainConfig()->get( 'CommandLineMode' )
|
|
|
|
|
? 'cli'
|
|
|
|
|
: strtolower( RequestContext::getMain()->getRequest()->getMethod() );
|
2011-09-10 06:50:30 +00:00
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
/** @var ErrorPageError $guiEx */
|
|
|
|
|
$guiEx = null;
|
2016-08-28 16:23:52 +00:00
|
|
|
/** @var DeferrableUpdate[] $updates Snapshot of queue */
|
|
|
|
|
$updates = $queue;
|
|
|
|
|
|
|
|
|
|
// Keep doing rounds of updates until none get enqueued...
|
|
|
|
|
while ( $updates ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$queue = []; // clear the queue
|
2016-08-28 16:23:52 +00:00
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
// Segregate the queue into one for DataUpdate and one for everything else
|
|
|
|
|
$dataUpdateQueue = [];
|
|
|
|
|
$genericUpdateQueue = [];
|
|
|
|
|
foreach ( $updates as $update ) {
|
|
|
|
|
if ( $update instanceof DataUpdate ) {
|
|
|
|
|
$dataUpdateQueue[] = $update;
|
2016-09-13 04:48:51 +00:00
|
|
|
} else {
|
2019-07-11 19:47:48 +00:00
|
|
|
$genericUpdateQueue[] = $update;
|
2016-09-13 04:48:51 +00:00
|
|
|
}
|
2015-10-08 06:46:18 +00:00
|
|
|
}
|
2019-07-11 19:47:48 +00:00
|
|
|
// Execute all DataUpdate queue followed by the DeferrableUpdate queue...
|
|
|
|
|
foreach ( [ $dataUpdateQueue, $genericUpdateQueue ] as $updateQueue ) {
|
|
|
|
|
foreach ( $updateQueue as $du ) {
|
|
|
|
|
// Enqueue the task into the job queue system instead if applicable
|
|
|
|
|
if ( $mode === 'enqueue' && $du instanceof EnqueueableDataUpdate ) {
|
|
|
|
|
self::jobify( $du, $lbf, $logger, $stats, $httpMethod );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// Otherwise, execute the task and any subtasks that it spawns
|
2017-07-04 02:18:54 +00:00
|
|
|
self::$executeContext = [ 'stage' => $stage, 'subqueue' => [] ];
|
2018-03-29 20:14:55 +00:00
|
|
|
try {
|
2019-07-11 19:47:48 +00:00
|
|
|
$e = self::run( $du, $lbf, $logger, $stats, $httpMethod );
|
|
|
|
|
$guiEx = $guiEx ?: ( $e instanceof ErrorPageError ? $e : null );
|
2018-03-29 20:14:55 +00:00
|
|
|
// Do the subqueue updates for $update until there are none
|
|
|
|
|
while ( self::$executeContext['subqueue'] ) {
|
2019-07-11 19:47:48 +00:00
|
|
|
$duChild = reset( self::$executeContext['subqueue'] );
|
2018-03-29 20:14:55 +00:00
|
|
|
$firstKey = key( self::$executeContext['subqueue'] );
|
|
|
|
|
unset( self::$executeContext['subqueue'][$firstKey] );
|
2016-08-28 16:23:52 +00:00
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
$e = self::run( $duChild, $lbf, $logger, $stats, $httpMethod );
|
|
|
|
|
$guiEx = $guiEx ?: ( $e instanceof ErrorPageError ? $e : null );
|
2018-03-29 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
// Make sure we always clean up the context.
|
|
|
|
|
// Losing updates while rewinding the stack is acceptable,
|
|
|
|
|
// losing updates that are added later is not.
|
|
|
|
|
self::$executeContext = null;
|
2014-08-19 22:23:05 +00:00
|
|
|
}
|
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:23:52 +00:00
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
// Throw the first of any GUI errors as long as the context is HTTP pre-send. However,
|
|
|
|
|
// callers should check permissions *before* enqueueing updates. If the main transaction
|
|
|
|
|
// round actions succeed but some deferred updates fail due to permissions errors then
|
|
|
|
|
// there is a risk that some secondary data was not properly updated.
|
|
|
|
|
if ( $guiEx && $stage === self::PRESEND && !headers_sent() ) {
|
|
|
|
|
throw $guiEx;
|
2016-08-28 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-11 19:47:48 +00:00
|
|
|
* Run a task and catch/log any exceptions
|
2019-05-21 21:52:57 +00:00
|
|
|
*
|
2016-08-28 16:23:52 +00:00
|
|
|
* @param DeferrableUpdate $update
|
|
|
|
|
* @param LBFactory $lbFactory
|
2019-07-11 19:47:48 +00:00
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param StatsdDataFactoryInterface $stats
|
|
|
|
|
* @param string $httpMethod
|
|
|
|
|
* @return Exception|Throwable|null
|
2016-08-28 16:23:52 +00:00
|
|
|
*/
|
2019-07-11 19:47:48 +00:00
|
|
|
private static function run(
|
|
|
|
|
DeferrableUpdate $update,
|
|
|
|
|
LBFactory $lbFactory,
|
|
|
|
|
LoggerInterface $logger,
|
|
|
|
|
StatsdDataFactoryInterface $stats,
|
|
|
|
|
$httpMethod
|
2017-07-04 02:18:54 +00:00
|
|
|
) {
|
2019-07-11 19:47:48 +00:00
|
|
|
$name = get_class( $update );
|
|
|
|
|
$suffix = ( $update instanceof DeferrableCallback ) ? "_{$update->getOrigin()}" : '';
|
|
|
|
|
$stats->increment( "deferred_updates.$httpMethod.{$name}{$suffix}" );
|
|
|
|
|
|
|
|
|
|
$e = null;
|
2016-08-28 16:23:52 +00:00
|
|
|
try {
|
2019-07-11 19:47:48 +00:00
|
|
|
self::attemptUpdate( $update, $lbFactory );
|
2016-08-28 16:23:52 +00:00
|
|
|
} catch ( Exception $e ) {
|
2019-07-11 19:47:48 +00:00
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
|
}
|
2018-03-29 20:14:55 +00:00
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
if ( $e ) {
|
|
|
|
|
$logger->error(
|
|
|
|
|
"Deferred update {type} failed: {message}",
|
|
|
|
|
[
|
|
|
|
|
'type' => $name . $suffix,
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$lbFactory->rollbackMasterChanges( __METHOD__ );
|
2018-03-29 20:14:55 +00:00
|
|
|
// VW-style hack to work around T190178, so we can make sure
|
|
|
|
|
// PageMetaDataUpdater doesn't throw exceptions.
|
|
|
|
|
if ( defined( 'MW_PHPUNIT_TEST' ) ) {
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
2016-08-28 16:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-11 19:47:48 +00:00
|
|
|
return $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Push a task into the job queue system and catch/log any exceptions
|
|
|
|
|
*
|
|
|
|
|
* @param EnqueueableDataUpdate $update
|
|
|
|
|
* @param LBFactory $lbFactory
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param StatsdDataFactoryInterface $stats
|
|
|
|
|
* @param string $httpMethod
|
|
|
|
|
*/
|
|
|
|
|
private static function jobify(
|
|
|
|
|
EnqueueableDataUpdate $update,
|
|
|
|
|
LBFactory $lbFactory,
|
|
|
|
|
LoggerInterface $logger,
|
|
|
|
|
StatsdDataFactoryInterface $stats,
|
|
|
|
|
$httpMethod
|
|
|
|
|
) {
|
|
|
|
|
$stats->increment( "deferred_updates.$httpMethod." . get_class( $update ) );
|
|
|
|
|
|
|
|
|
|
$e = null;
|
|
|
|
|
try {
|
|
|
|
|
$spec = $update->getAsJobSpecification();
|
|
|
|
|
JobQueueGroup::singleton( $spec['domain'] ?? $spec['wiki'] )->push( $spec['job'] );
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $e ) {
|
|
|
|
|
$logger->error(
|
|
|
|
|
"Job insertion of deferred update {type} failed: {message}",
|
|
|
|
|
[
|
|
|
|
|
'type' => get_class( $update ),
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'trace' => $e->getTraceAsString()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
$lbFactory->rollbackMasterChanges( __METHOD__ );
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-21 21:52:57 +00:00
|
|
|
/**
|
|
|
|
|
* Attempt to run an update with the appropriate transaction round state it expects
|
|
|
|
|
*
|
|
|
|
|
* DeferredUpdate classes that wrap the execution of bundles of other DeferredUpdate
|
|
|
|
|
* instances can use this method to run the updates. Any such wrapper class should
|
|
|
|
|
* always use TRX_ROUND_ABSENT itself.
|
|
|
|
|
*
|
|
|
|
|
* @param DeferrableUpdate $update
|
|
|
|
|
* @param ILBFactory $lbFactory
|
|
|
|
|
* @since 1.34
|
|
|
|
|
*/
|
|
|
|
|
public static function attemptUpdate( DeferrableUpdate $update, ILBFactory $lbFactory ) {
|
2019-07-18 19:27:07 +00:00
|
|
|
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
|
|
|
|
|
if ( !$ticket || $lbFactory->hasTransactionRound() ) {
|
|
|
|
|
throw new DBTransactionError( null, "A database transaction round is pending." );
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:07:03 +00:00
|
|
|
if ( $update instanceof DataUpdate ) {
|
2019-07-18 19:27:07 +00:00
|
|
|
$update->setTransactionTicket( $ticket );
|
2019-05-29 21:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-02 20:09:44 +00:00
|
|
|
// Designate $update::doUpdate() as the write round owner
|
|
|
|
|
$fnameTrxOwner = ( $update instanceof DeferrableCallback )
|
|
|
|
|
? $update->getOrigin()
|
|
|
|
|
: get_class( $update ) . '::doUpdate';
|
|
|
|
|
// Determine whether the write round will be explicit or implicit
|
2019-07-18 19:27:07 +00:00
|
|
|
$useExplicitTrxRound = !(
|
2019-05-21 21:52:57 +00:00
|
|
|
$update instanceof TransactionRoundAwareUpdate &&
|
|
|
|
|
$update->getTransactionRoundRequirement() == $update::TRX_ROUND_ABSENT
|
2019-07-18 19:27:07 +00:00
|
|
|
);
|
2019-08-02 20:09:44 +00:00
|
|
|
|
2019-07-18 19:27:07 +00:00
|
|
|
// Flush any pending changes left over from an implicit transaction round
|
|
|
|
|
if ( $useExplicitTrxRound ) {
|
|
|
|
|
$lbFactory->beginMasterChanges( $fnameTrxOwner ); // new explicit round
|
2019-05-21 21:52:57 +00:00
|
|
|
} else {
|
2019-07-18 19:27:07 +00:00
|
|
|
$lbFactory->commitMasterChanges( $fnameTrxOwner ); // new implicit round
|
2019-07-11 19:47:48 +00:00
|
|
|
}
|
2019-07-18 19:27:07 +00:00
|
|
|
// Run the update after any stale master view snapshots have been flushed
|
2019-07-11 19:47:48 +00:00
|
|
|
$update->doUpdate();
|
2019-07-18 19:27:07 +00:00
|
|
|
// Commit any pending changes from the explicit or implicit transaction round
|
|
|
|
|
$lbFactory->commitMasterChanges( $fnameTrxOwner );
|
2019-05-21 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-28 16:06:57 +00:00
|
|
|
/**
|
|
|
|
|
* Run all deferred updates immediately if there are no DB writes active
|
|
|
|
|
*
|
2018-05-02 23:56:07 +00:00
|
|
|
* If there are many deferred updates pending, $mode is 'run', and there
|
|
|
|
|
* are still busy LBFactory database handles, then any EnqueueableDataUpdate
|
|
|
|
|
* tasks might be enqueued as jobs to be executed later.
|
2016-08-28 16:06:57 +00:00
|
|
|
*
|
|
|
|
|
* @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-08-28 16:23:52 +00:00
|
|
|
// execute() loop is already running
|
|
|
|
|
if ( self::$executeContext ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoiding running updates without them having outer scope
|
2017-06-09 02:56:02 +00:00
|
|
|
if ( !self::areDatabaseTransactionsActive() ) {
|
2016-09-01 17:01:25 +00:00
|
|
|
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();
|
2019-03-29 03:47:09 +00:00
|
|
|
$domain = $spec['domain'] ?? $spec['wiki'];
|
|
|
|
|
JobQueueGroup::singleton( $domain )->push( $spec['job'] );
|
2016-08-28 16:06:57 +00:00
|
|
|
} else {
|
|
|
|
|
$remaining[] = $update;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $remaining;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-20 11:20:59 +00:00
|
|
|
* @return int Number of enqueued updates
|
2016-08-28 16:06:57 +00:00
|
|
|
* @since 1.28
|
|
|
|
|
*/
|
|
|
|
|
public static function pendingUpdatesCount() {
|
|
|
|
|
return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 11:49:13 +00:00
|
|
|
/**
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL)
|
2017-06-01 07:11:44 +00:00
|
|
|
* @return DeferrableUpdate[]
|
2017-01-03 11:49:13 +00:00
|
|
|
* @since 1.29
|
|
|
|
|
*/
|
|
|
|
|
public static function getPendingUpdates( $stage = self::ALL ) {
|
|
|
|
|
$updates = [];
|
|
|
|
|
if ( $stage === self::ALL || $stage === self::PRESEND ) {
|
|
|
|
|
$updates = array_merge( $updates, self::$preSendUpdates );
|
|
|
|
|
}
|
|
|
|
|
if ( $stage === self::ALL || $stage === self::POSTSEND ) {
|
|
|
|
|
$updates = array_merge( $updates, self::$postSendUpdates );
|
|
|
|
|
}
|
|
|
|
|
return $updates;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
2017-06-09 02:56:02 +00:00
|
|
|
* @return bool If a transaction round is active or connection is not ready for commit()
|
2016-08-28 16:06:57 +00:00
|
|
|
*/
|
2017-06-09 02:56:02 +00:00
|
|
|
private static function areDatabaseTransactionsActive() {
|
2016-08-28 16:06:57 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
2018-05-02 23:56:07 +00:00
|
|
|
if ( $lbFactory->hasTransactionRound() || !$lbFactory->isReadyForRoundOperations() ) {
|
2017-06-09 02:56:02 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$connsBusy = false;
|
2016-08-28 16:06:57 +00:00
|
|
|
$lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
|
|
|
|
|
$lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
|
|
|
|
|
if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
|
2017-06-09 02:56:02 +00:00
|
|
|
$connsBusy = true;
|
2016-08-28 16:06:57 +00:00
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return $connsBusy;
|
|
|
|
|
}
|
2011-09-10 06:50:30 +00:00
|
|
|
}
|