Make the JobRunner flushReplicaSnapshots() call cover the first job
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 2 Aug 2019 19:28:18 +0000 (15:28 -0400)
committerKrinkle <krinklemail@gmail.com>
Sun, 25 Aug 2019 14:07:24 +0000 (14:07 +0000)
If JobRunner is called when replica transactions exists, the first job
would previously use the stale REPEATABLE-READ snapshot data.

Also clear any master connection snapshots via commitMasterChanges().
This makes the code more similar to DeferredUpdates::attemptUpdate().

Change-Id: I2157a91fb01ea8c233f964b1f3164e8c3b1a07ca

includes/jobqueue/JobRunner.php

index 21d8c7e..adb4221 100644 (file)
@@ -279,16 +279,26 @@ class JobRunner implements LoggerAwareInterface {
                ] );
                $this->debugCallback( $msg );
 
+               // Clear out title cache data from prior snapshots
+               // (e.g. from before JobRunner was invoked in this process)
+               MediaWikiServices::getInstance()->getLinkCache()->clear();
+
                // Run the job...
                $rssStart = $this->getMaxRssKb();
                $jobStartTime = microtime( true );
                try {
                        $fnameTrxOwner = get_class( $job ) . '::run'; // give run() outer scope
-                       if ( !$job->hasExecutionFlag( $job::JOB_NO_EXPLICIT_TRX_ROUND ) ) {
-                               $lbFactory->beginMasterChanges( $fnameTrxOwner );
+                       // Flush any pending changes left over from an implicit transaction round
+                       if ( $job->hasExecutionFlag( $job::JOB_NO_EXPLICIT_TRX_ROUND ) ) {
+                               $lbFactory->commitMasterChanges( $fnameTrxOwner ); // new implicit round
+                       } else {
+                               $lbFactory->beginMasterChanges( $fnameTrxOwner ); // new explicit round
                        }
+                       // Clear any stale REPEATABLE-READ snapshots from replica DB connections
+                       $lbFactory->flushReplicaSnapshots( $fnameTrxOwner );
                        $status = $job->run();
                        $error = $job->getLastError();
+                       // Commit all pending changes from this job
                        $this->commitMasterChanges( $lbFactory, $job, $fnameTrxOwner );
                        // Run any deferred update tasks; doUpdates() manages transactions itself
                        DeferredUpdates::doUpdates();
@@ -304,12 +314,6 @@ class JobRunner implements LoggerAwareInterface {
                        MWExceptionHandler::logException( $e );
                }
 
-               // Commit all outstanding connections that are in a transaction
-               // to get a fresh repeatable read snapshot on every connection.
-               // Note that jobs are still responsible for handling replica DB lag.
-               $lbFactory->flushReplicaSnapshots( __METHOD__ );
-               // Clear out title cache data from prior snapshots
-               MediaWikiServices::getInstance()->getLinkCache()->clear();
                $timeMs = intval( ( microtime( true ) - $jobStartTime ) * 1000 );
                $rssEnd = $this->getMaxRssKb();