Optimized wfWaitForSlaves() to get the master positions for all LBs first
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Oct 2014 17:56:36 +0000 (10:56 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 22 Oct 2014 17:56:36 +0000 (10:56 -0700)
Change-Id: I4a4aa6a3f68ace85dcbfb73e982a16c62e21e6ed

includes/GlobalFunctions.php

index 4eea662..11388e8 100644 (file)
@@ -3807,8 +3807,11 @@ function wfWaitForSlaves(
                $lbs[] = wfGetLB( $wiki );
        }
 
-       $ok = true;
-       foreach ( $lbs as $lb ) {
+       // Get all the master positions of applicable DBs right now.
+       // This can be faster since waiting on one cluster reduces the
+       // time needed to wait on the next clusters.
+       $masterPositions = array_fill( 0, count( $lbs ), false );
+       foreach ( $lbs as $i => $lb ) {
                // bug 27975 - Don't try to wait for slaves if there are none
                // Prevents permission error when getting master position
                if ( $lb->getServerCount() > 1 ) {
@@ -3819,12 +3822,16 @@ function wfWaitForSlaves(
                        if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
                                continue; // no writes since the last wait
                        }
-                       $pos = $dbw->getMasterPos();
+                       $masterPositions[$i] = $dbw->getMasterPos();
+               }
+       }
+
+       $ok = true;
+       foreach ( $lbs as $i => $lb ) {
+               if ( $masterPositions[$i] ) {
                        // The DBMS may not support getMasterPos() or the whole
                        // load balancer might be fake (e.g. $wgAllDBsAreLocalhost).
-                       if ( $pos !== false ) {
-                               $ok = $lb->waitForAll( $pos, $timeout ) && $ok;
-                       }
+                       $ok = $lb->waitForAll( $masterPositions[$i], $timeout ) && $ok;
                }
        }