X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/deferred/DeferredUpdates.php diff --git a/includes/deferred/DeferredUpdates.php b/includes/deferred/DeferredUpdates.php new file mode 100644 index 00000000..e8e250b5 --- /dev/null +++ b/includes/deferred/DeferredUpdates.php @@ -0,0 +1,377 @@ += $stage ) { + // 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). + self::$executeContext['subqueue'][] = $update; + + return; + } + + if ( $stage === self::PRESEND ) { + self::push( self::$preSendUpdates, $update ); + } else { + self::push( self::$postSendUpdates, $update ); + } + + // 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' ); + } + } + + /** + * Add a callable update. In a lot of cases, we just need a callback/closure, + * defining a new DeferrableUpdate object is not necessary + * + * @see MWCallableUpdate::__construct() + * + * @param callable $callable + * @param int $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27) + * @param IDatabase|null $dbw Abort if this DB is rolled back [optional] (since 1.28) + */ + public static function addCallableUpdate( + $callable, $stage = self::POSTSEND, IDatabase $dbw = null + ) { + self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage ); + } + + /** + * Do any deferred updates and clear the list + * + * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"] + * @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27) + */ + public static function doUpdates( $mode = 'run', $stage = self::ALL ) { + $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage; + + if ( $stage === self::ALL || $stage === self::PRESEND ) { + self::execute( self::$preSendUpdates, $mode, $stageEffective ); + } + + if ( $stage === self::ALL || $stage == self::POSTSEND ) { + self::execute( self::$postSendUpdates, $mode, $stageEffective ); + } + } + + /** + * @param bool $value Whether to just immediately run updates in addUpdate() + * @since 1.28 + * @deprecated 1.29 Causes issues in Web-executed jobs - see T165714 and T100085. + */ + public static function setImmediateMode( $value ) { + wfDeprecated( __METHOD__, '1.29' ); + } + + /** + * @param DeferrableUpdate[] $queue + * @param DeferrableUpdate $update + */ + private static function push( array &$queue, DeferrableUpdate $update ) { + if ( $update instanceof MergeableUpdate ) { + $class = get_class( $update ); // fully-qualified class + if ( isset( $queue[$class] ) ) { + /** @var MergeableUpdate $existingUpdate */ + $existingUpdate = $queue[$class]; + $existingUpdate->merge( $update ); + } else { + $queue[$class] = $update; + } + } else { + $queue[] = $update; + } + } + + /** + * Immediately run/queue a list of updates + * + * @param DeferrableUpdate[] &$queue List of DeferrableUpdate objects + * @param string $mode Use "enqueue" to use the job queue when possible + * @param int $stage Class constant (PRESEND, POSTSEND) (since 1.28) + * @throws ErrorPageError Happens on top-level calls + * @throws Exception Happens on second-level calls + */ + protected static function execute( array &$queue, $mode, $stage ) { + $services = MediaWikiServices::getInstance(); + $stats = $services->getStatsdDataFactory(); + $lbFactory = $services->getDBLoadBalancerFactory(); + $method = RequestContext::getMain()->getRequest()->getMethod(); + + $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ ); + + /** @var ErrorPageError $reportableError */ + $reportableError = null; + /** @var DeferrableUpdate[] $updates Snapshot of queue */ + $updates = $queue; + + // Keep doing rounds of updates until none get enqueued... + while ( $updates ) { + $queue = []; // clear the queue + + // Order will be DataUpdate followed by generic DeferrableUpdate tasks + $updatesByType = [ 'data' => [], 'generic' => [] ]; + foreach ( $updates as $du ) { + if ( $du instanceof DataUpdate ) { + $du->setTransactionTicket( $ticket ); + $updatesByType['data'][] = $du; + } else { + $updatesByType['generic'][] = $du; + } + + $name = ( $du instanceof DeferrableCallback ) + ? get_class( $du ) . '-' . $du->getOrigin() + : get_class( $du ); + $stats->increment( 'deferred_updates.' . $method . '.' . $name ); + } + + // Execute all remaining tasks... + foreach ( $updatesByType as $updatesForType ) { + foreach ( $updatesForType as $update ) { + self::$executeContext = [ 'stage' => $stage, 'subqueue' => [] ]; + /** @var DeferrableUpdate $update */ + $guiError = self::runUpdate( $update, $lbFactory, $mode, $stage ); + $reportableError = $reportableError ?: $guiError; + // Do the subqueue updates for $update until there are none + while ( self::$executeContext['subqueue'] ) { + $subUpdate = reset( self::$executeContext['subqueue'] ); + $firstKey = key( self::$executeContext['subqueue'] ); + unset( self::$executeContext['subqueue'][$firstKey] ); + + if ( $subUpdate instanceof DataUpdate ) { + $subUpdate->setTransactionTicket( $ticket ); + } + + $guiError = self::runUpdate( $subUpdate, $lbFactory, $mode, $stage ); + $reportableError = $reportableError ?: $guiError; + } + self::$executeContext = null; + } + } + + $updates = $queue; // new snapshot of queue (check for new entries) + } + + if ( $reportableError ) { + throw $reportableError; // throw the first of any GUI errors + } + } + + /** + * @param DeferrableUpdate $update + * @param LBFactory $lbFactory + * @param string $mode + * @param int $stage + * @return ErrorPageError|null + */ + private static function runUpdate( + DeferrableUpdate $update, LBFactory $lbFactory, $mode, $stage + ) { + $guiError = null; + try { + if ( $mode === 'enqueue' && $update instanceof EnqueueableDataUpdate ) { + // Run only the job enqueue logic to complete the update later + $spec = $update->getAsJobSpecification(); + JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] ); + } else { + // Run the bulk of the update now + $fnameTrxOwner = get_class( $update ) . '::doUpdate'; + $lbFactory->beginMasterChanges( $fnameTrxOwner ); + $update->doUpdate(); + $lbFactory->commitMasterChanges( $fnameTrxOwner ); + } + } catch ( Exception $e ) { + // Reporting GUI exceptions does not work post-send + if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) { + $guiError = $e; + } + MWExceptionHandler::rollbackMasterChangesAndLog( $e ); + } + + return $guiError; + } + + /** + * 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' ) { + // execute() loop is already running + if ( self::$executeContext ) { + return false; + } + + // Avoiding running updates without them having outer scope + if ( !self::areDatabaseTransactionsActive() ) { + self::doUpdates( $mode ); + return true; + } + + 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 ); + } + + return !self::pendingUpdatesCount(); + } + + /** + * 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 int Number of enqueued updates + * @since 1.28 + */ + public static function pendingUpdatesCount() { + return count( self::$preSendUpdates ) + count( self::$postSendUpdates ); + } + + /** + * @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) + * @return DeferrableUpdate[] + * @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; + } + + /** + * 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() { + self::$preSendUpdates = []; + self::$postSendUpdates = []; + } + + /** + * @return bool If a transaction round is active or connection is not ready for commit() + */ + private static function areDatabaseTransactionsActive() { + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + if ( $lbFactory->hasTransactionRound() ) { + return true; + } + + $connsBusy = false; + $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) { + $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) { + if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) { + $connsBusy = true; + } + } ); + } ); + + return $connsBusy; + } +}