Switch to LOCK IN SHARE MODE in recordUpload2()
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index ad20c6f..11388e8 100644 (file)
@@ -3776,7 +3776,7 @@ function wfGetNull() {
  * does this check, it makes since to use $ifWritesSince, particularly if
  * cluster is "*", to avoid excess overhead.
  *
- * Never call this method after a big DB write that is still in a transaction.
+ * Never call this function after a big DB write that is still in a transaction.
  * This only makes sense after the possible lag inducing changes were committed.
  *
  * @param float|null $ifWritesSince Only wait if writes were done since this UNIX timestamp
@@ -3807,24 +3807,31 @@ 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 ) {
                        if ( $ifWritesSince && !$lb->hasMasterConnection() ) {
-                               break; // assume no writes done
+                               continue; // assume no writes done
                        }
                        $dbw = $lb->getConnection( DB_MASTER, array(), $wiki );
                        if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
-                               break; // no writes since the last wait
+                               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;
                }
        }