deferred: Introduce TransactionRoundDefiningUpdate
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 12 Oct 2017 20:19:56 +0000 (21:19 +0100)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 13 Oct 2017 21:24:11 +0000 (21:24 +0000)
This allows scheduling of updates that need to start their own
transaction round. Specifically for cases where the ability to
commit early is not enough (which is already possible via LBFactory
getEmptyTransactionTicket and commitAndWaitForReplication).

Change-Id: I0910587b61c8ddf825f91e92c2f93582cc7ebd80

autoload.php
includes/deferred/DeferredUpdates.php
includes/deferred/TransactionRoundDefiningUpdate.php [new file with mode: 0644]
tests/phpunit/includes/deferred/DeferredUpdatesTest.php
tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php [new file with mode: 0644]

index 0a2ecf0..ede520f 100644 (file)
@@ -1498,6 +1498,7 @@ $wgAutoloadLocalClasses = [
        'TrackBlobs' => __DIR__ . '/maintenance/storage/trackBlobs.php',
        'TrackingCategories' => __DIR__ . '/includes/TrackingCategories.php',
        'TraditionalImageGallery' => __DIR__ . '/includes/gallery/TraditionalImageGallery.php',
+       'TransactionRoundDefiningUpdate' => __DIR__ . '/includes/deferred/TransactionRoundDefiningUpdate.php',
        'TransformParameterError' => __DIR__ . '/includes/media/MediaTransformOutput.php',
        'TransformTooBigImageAreaError' => __DIR__ . '/includes/media/MediaTransformOutput.php',
        'TransformationalImageHandler' => __DIR__ . '/includes/media/TransformationalImageHandler.php',
index e8e250b..3c4833c 100644 (file)
@@ -250,6 +250,8 @@ class DeferredUpdates {
                                // Run only the job enqueue logic to complete the update later
                                $spec = $update->getAsJobSpecification();
                                JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
+                       } elseif ( $update instanceof TransactionRoundDefiningUpdate ) {
+                               $update->doUpdate();
                        } else {
                                // Run the bulk of the update now
                                $fnameTrxOwner = get_class( $update ) . '::doUpdate';
diff --git a/includes/deferred/TransactionRoundDefiningUpdate.php b/includes/deferred/TransactionRoundDefiningUpdate.php
new file mode 100644 (file)
index 0000000..65baec5
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Deferrable update for closure/callback updates that need LBFactory and Database
+ * to be outside any active transaction round.
+ *
+ * @since 1.31
+ */
+class TransactionRoundDefiningUpdate implements DeferrableUpdate, DeferrableCallback {
+       /** @var callable|null */
+       private $callback;
+       /** @var string */
+       private $fname;
+
+       /**
+        * @param callable $callback
+        * @param string $fname Calling method
+        */
+       public function __construct( callable $callback, $fname = 'unknown' ) {
+               $this->callback = $callback;
+               $this->fname = $fname;
+       }
+
+       public function doUpdate() {
+               call_user_func( $this->callback );
+       }
+
+       public function getOrigin() {
+               return $this->fname;
+       }
+}
index 999ad03..8332d2c 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 class DeferredUpdatesTest extends MediaWikiTestCase {
 
        /**
@@ -289,4 +291,46 @@ class DeferredUpdatesTest extends MediaWikiTestCase {
                $this->assertTrue( $x, "Outer POSTSEND update ran" );
                $this->assertTrue( $y, "Nested PRESEND update ran" );
        }
+
+       /**
+        * @covers DeferredUpdates::runUpdate
+        */
+       public function testRunUpdateTransactionScope() {
+               $this->setMwGlobals( 'wgCommandLineMode', false );
+
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
+
+               $ran = 0;
+               DeferredUpdates::addCallableUpdate( function () use ( &$ran, $lbFactory ) {
+                       $ran++;
+                       $this->assertTrue( $lbFactory->hasTransactionRound(), 'Has transaction' );
+               } );
+               DeferredUpdates::doUpdates();
+
+               $this->assertSame( 1, $ran, 'Update ran' );
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Final state' );
+       }
+
+       /**
+        * @covers DeferredUpdates::runUpdate
+        * @covers TransactionRoundDefiningUpdate::getOrigin
+        */
+       public function testRunOuterScopeUpdate() {
+               $this->setMwGlobals( 'wgCommandLineMode', false );
+
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' );
+
+               $ran = 0;
+               DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate(
+                       function () use ( &$ran, $lbFactory ) {
+                               $ran++;
+                               $this->assertFalse( $lbFactory->hasTransactionRound(), 'No transaction' );
+                       } )
+               );
+               DeferredUpdates::doUpdates();
+
+               $this->assertSame( 1, $ran, 'Update ran' );
+       }
 }
diff --git a/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php b/tests/phpunit/includes/deferred/TransactionRoundDefiningUpdateTest.php
new file mode 100644 (file)
index 0000000..e6ad072
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @covers TransactionRoundDefiningUpdate
+ */
+class TransactionRoundDefiningUpdateTest extends PHPUnit_Framework_TestCase {
+
+       public function testDoUpdate() {
+               $ran = 0;
+               $update = new TransactionRoundDefiningUpdate( function () use ( &$ran ) {
+                       $ran++;
+               } );
+               $this->assertSame( 0, $ran );
+               $update->doUpdate();
+               $this->assertSame( 1, $ran );
+       }
+}