Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / deferred / DeferredUpdates.php
index c754cff..3716971 100644 (file)
@@ -28,6 +28,7 @@ use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\LBFactory;
 use Wikimedia\Rdbms\ILBFactory;
 use Wikimedia\Rdbms\LoadBalancer;
+use Wikimedia\Rdbms\DBTransactionError;
 
 /**
  * Class for managing the deferred updates
@@ -160,6 +161,7 @@ class DeferredUpdates {
                        if ( isset( $queue[$class] ) ) {
                                /** @var MergeableUpdate $existingUpdate */
                                $existingUpdate = $queue[$class];
+                               '@phan-var MergeableUpdate $existingUpdate';
                                $existingUpdate->merge( $update );
                                // Move the update to the end to handle things like mergeable purge
                                // updates that might depend on the prior updates in the queue running
@@ -352,28 +354,35 @@ class DeferredUpdates {
         * @since 1.34
         */
        public static function attemptUpdate( DeferrableUpdate $update, ILBFactory $lbFactory ) {
+               $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
+               if ( !$ticket || $lbFactory->hasTransactionRound() ) {
+                       throw new DBTransactionError( null, "A database transaction round is pending." );
+               }
+
                if ( $update instanceof DataUpdate ) {
-                       $update->setTransactionTicket( $lbFactory->getEmptyTransactionTicket( __METHOD__ ) );
+                       $update->setTransactionTicket( $ticket );
                }
 
-               if (
+               // 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
+               $useExplicitTrxRound = !(
                        $update instanceof TransactionRoundAwareUpdate &&
                        $update->getTransactionRoundRequirement() == $update::TRX_ROUND_ABSENT
-               ) {
-                       $fnameTrxOwner = null;
-               } else {
-                       $fnameTrxOwner = get_class( $update ) . '::doUpdate';
-               }
+               );
 
-               if ( $fnameTrxOwner !== null ) {
-                       $lbFactory->beginMasterChanges( $fnameTrxOwner );
+               // Flush any pending changes left over from an implicit transaction round
+               if ( $useExplicitTrxRound ) {
+                       $lbFactory->beginMasterChanges( $fnameTrxOwner ); // new explicit round
+               } else {
+                       $lbFactory->commitMasterChanges( $fnameTrxOwner ); // new implicit round
                }
-
+               // Run the update after any stale master view snapshots have been flushed
                $update->doUpdate();
-
-               if ( $fnameTrxOwner !== null ) {
-                       $lbFactory->commitMasterChanges( $fnameTrxOwner );
-               }
+               // Commit any pending changes from the explicit or implicit transaction round
+               $lbFactory->commitMasterChanges( $fnameTrxOwner );
        }
 
        /**