deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fnameTrxOwner
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 2 Aug 2019 20:09:44 +0000 (16:09 -0400)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 3 Aug 2019 05:48:02 +0000 (05:48 +0000)
This only applies for instances of DeferrableCallback

Bug: T229694
Change-Id: I57fe351041c87342bd5774e333fcde4ae15059df

includes/deferred/DeferrableCallback.php
includes/deferred/DeferredUpdates.php
tests/phpunit/includes/deferred/DeferredUpdatesTest.php

index 2eb0d5d..33961ed 100644 (file)
@@ -9,5 +9,5 @@ interface DeferrableCallback {
        /**
         * @return string Originating method name
         */
-       function getOrigin();
+       public function getOrigin();
 }
index d43ffbc..3380364 100644 (file)
@@ -362,11 +362,16 @@ class DeferredUpdates {
                        $update->setTransactionTicket( $ticket );
                }
 
-               $fnameTrxOwner = get_class( $update ) . '::doUpdate';
+               // 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
                );
+
                // Flush any pending changes left over from an implicit transaction round
                if ( $useExplicitTrxRound ) {
                        $lbFactory->beginMasterChanges( $fnameTrxOwner ); // new explicit round
index b377c63..7e5ff84 100644 (file)
@@ -373,4 +373,27 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
                DeferredUpdates::tryOpportunisticExecute( 'run' );
                $this->assertEquals( [ 'oti', 1, 2 ], $calls );
        }
+
+       /**
+        * @covers DeferredUpdates::attemptUpdate
+        */
+       public function testCallbackUpdateRounds() {
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+
+               $fname = __METHOD__;
+               $called = false;
+               DeferredUpdates::attemptUpdate(
+                       new MWCallableUpdate(
+                               function () use ( $lbFactory, $fname, &$called ) {
+                                       $lbFactory->flushReplicaSnapshots( $fname );
+                                       $lbFactory->commitMasterChanges( $fname );
+                                       $called = true;
+                               },
+                               $fname
+                       ),
+                       $lbFactory
+               );
+
+               $this->assertTrue( $called, "Callback ran" );
+       }
 }