Set "run when DBs idle" DeferredUpdates logic in Maintenance
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 1 Sep 2016 16:43:01 +0000 (09:43 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 1 Sep 2016 22:09:21 +0000 (22:09 +0000)
* This is a more robust version of the one inside
  DeferredUpdates (checking all DBs), which will be
  replaced in a later commit.
* Make sure the listener is restored when the LB gets
  torn down in tests. Also, it should respect the
  $wgCommandLineMode value as tests can change it.

Change-Id: I1e3faa5a058df44e2d27ab5ac185930867eb68ac

maintenance/Maintenance.php
tests/phpunit/MediaWikiTestCase.php

index 343687e..698fd9a 100644 (file)
@@ -554,11 +554,38 @@ abstract class Maintenance {
         * @since 1.28
         */
        public function setTriggers() {
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               self::setLBFactoryTriggers( $lbFactory );
+       }
+
+       /**
+        * @param LBFactory $LBFactory
+        * @since 1.28
+        */
+       public static function setLBFactoryTriggers( LBFactory $LBFactory ) {
                // Hook into period lag checks which often happen in long-running scripts
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $lbFactory->setWaitForReplicationListener(
                        __METHOD__,
-                       [ 'DeferredUpdates', 'tryOpportunisticExecute' ]
+                       function () {
+                               global $wgCommandLineMode;
+                               // Check config in case of JobRunner and unit tests
+                               if ( $wgCommandLineMode ) {
+                                       DeferredUpdates::tryOpportunisticExecute( 'run' );
+                               }
+                       }
+               );
+               // Check for other windows to run them. A script may read or do a few writes
+               // to the master but mostly be writing to something else, like a file store.
+               $lbFactory->getMainLB()->setTransactionListener(
+                       __METHOD__,
+                       function ( $trigger ) {
+                               global $wgCommandLineMode;
+                               // Check config in case of JobRunner and unit tests
+                               if ( $wgCommandLineMode && $trigger === IDatabase::TRIGGER_COMMIT ) {
+                                       DeferredUpdates::tryOpportunisticExecute( 'run' );
+                               }
+                       }
                );
        }
 
index 27f1454..541ac11 100644 (file)
@@ -482,6 +482,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                DeferredUpdates::clearPendingUpdates();
                ObjectCache::getMainWANInstance()->clearProcessCache();
 
+               // XXX: reset maintenance triggers
+               // Hook into period lag checks which often happen in long-running scripts
+               $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               Maintenance::setLBFactoryTriggers( $lbFactory );
+
                ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' );
        }