Merge "Add the ability to know if fullscreen is available"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index c88ad05..e35c4c4 100644 (file)
@@ -144,10 +144,10 @@ class LoadBalancer {
                foreach ( $lags as $i => $lag ) {
                        if ( $i != 0 ) {
                                if ( $lag === false ) {
-                                       wfDebugLog( 'replication', "Server #$i is not replicating\n" );
+                                       wfDebugLog( 'replication', "Server #$i is not replicating" );
                                        unset( $loads[$i] );
                                } elseif ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
-                                       wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)\n" );
+                                       wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
                                        unset( $loads[$i] );
                                }
                        }
@@ -188,7 +188,7 @@ class LoadBalancer {
         * @return bool|int|string
         */
        function getReaderIndex( $group = false, $wiki = false ) {
-               global $wgReadOnly, $wgDBClusterTimeout, $wgDBAvgStatusPoll, $wgDBtype;
+               global $wgReadOnly, $wgDBtype;
 
                # @todo FIXME: For now, only go through all this for mysql databases
                if ( $wgDBtype != 'mysql' ) {
@@ -198,17 +198,12 @@ class LoadBalancer {
                if ( count( $this->mServers ) == 1 ) {
                        # Skip the load balancing if there's only one server
                        return 0;
-               } elseif ( $group === false and $this->mReadIndex >= 0 ) {
+               } elseif ( $group === false && $this->mReadIndex >= 0 ) {
                        # Shortcut if generic reader exists already
                        return $this->mReadIndex;
                }
 
-               wfProfileIn( __METHOD__ );
-
-               $totalElapsed = 0;
-
-               # convert from seconds to microseconds
-               $timeout = $wgDBClusterTimeout * 1e6;
+               $section = new ProfileSection( __METHOD__ );
 
                # Find the relevant load array
                if ( $group !== false ) {
@@ -217,7 +212,6 @@ class LoadBalancer {
                        } else {
                                # No loads for this group, return false and the caller can use some other group
                                wfDebug( __METHOD__ . ": no loads for group $group\n" );
-                               wfProfileOut( __METHOD__ );
 
                                return false;
                        }
@@ -225,8 +219,7 @@ class LoadBalancer {
                        $nonErrorLoads = $this->mLoads;
                }
 
-               if ( !$nonErrorLoads ) {
-                       wfProfileOut( __METHOD__ );
+               if ( !count( $nonErrorLoads ) ) {
                        throw new MWException( "Empty server array given to LoadBalancer" );
                }
 
@@ -235,93 +228,59 @@ class LoadBalancer {
 
                $laggedSlaveMode = false;
 
+               # No server found yet
+               $i = false;
                # First try quickly looking through the available servers for a server that
                # meets our criteria
-               do {
-                       $totalThreadsConnected = 0;
-                       $overloadedServers = 0;
-                       $currentLoads = $nonErrorLoads;
-                       while ( count( $currentLoads ) ) {
-                               if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) {
+               $currentLoads = $nonErrorLoads;
+               while ( count( $currentLoads ) ) {
+                       if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) {
+                               $i = ArrayUtils::pickRandom( $currentLoads );
+                       } else {
+                               $i = $this->getRandomNonLagged( $currentLoads, $wiki );
+                               if ( $i === false && count( $currentLoads ) != 0 ) {
+                                       # All slaves lagged. Switch to read-only mode
+                                       wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
+                                       $wgReadOnly = 'The database has been automatically locked ' .
+                                               'while the slave database servers catch up to the master';
                                        $i = ArrayUtils::pickRandom( $currentLoads );
-                               } else {
-                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki );
-                                       if ( $i === false && count( $currentLoads ) != 0 ) {
-                                               # All slaves lagged. Switch to read-only mode
-                                               wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode\n" );
-                                               $wgReadOnly = 'The database has been automatically locked ' .
-                                                       'while the slave database servers catch up to the master';
-                                               $i = ArrayUtils::pickRandom( $currentLoads );
-                                               $laggedSlaveMode = true;
-                                       }
+                                       $laggedSlaveMode = true;
                                }
+                       }
 
-                               if ( $i === false ) {
-                                       # pickRandom() returned false
-                                       # This is permanent and means the configuration or the load monitor
-                                       # wants us to return false.
-                                       wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false\n" );
-                                       wfProfileOut( __METHOD__ );
-
-                                       return false;
-                               }
-
-                               wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: {$this->mServers[$i]['host']}...\n" );
-                               $conn = $this->openConnection( $i, $wiki );
-
-                               if ( !$conn ) {
-                                       wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki\n" );
-                                       unset( $nonErrorLoads[$i] );
-                                       unset( $currentLoads[$i] );
-                                       continue;
-                               }
-
-                               // Perform post-connection backoff
-                               $threshold = isset( $this->mServers[$i]['max threads'] )
-                                       ? $this->mServers[$i]['max threads'] : 0;
-                               $backoff = $this->getLoadMonitor()->postConnectionBackoff( $conn, $threshold );
-
-                               // Decrement reference counter, we are finished with this connection.
-                               // It will be incremented for the caller later.
-                               if ( $wiki !== false ) {
-                                       $this->reuseConnection( $conn );
-                               }
+                       if ( $i === false ) {
+                               # pickRandom() returned false
+                               # This is permanent and means the configuration or the load monitor
+                               # wants us to return false.
+                               wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" );
 
-                               if ( $backoff ) {
-                                       # Post-connection overload, don't use this server for now
-                                       $totalThreadsConnected += $backoff;
-                                       $overloadedServers++;
-                                       unset( $currentLoads[$i] );
-                               } else {
-                                       # Return this server
-                                       break 2;
-                               }
+                               return false;
                        }
 
-                       # No server found yet
-                       $i = false;
+                       wfDebugLog( 'connect', __METHOD__ .
+                               ": Using reader #$i: {$this->mServers[$i]['host']}..." );
 
-                       # If all servers were down, quit now
-                       if ( !count( $nonErrorLoads ) ) {
-                               wfDebugLog( 'connect', "All servers down\n" );
-                               break;
+                       $conn = $this->openConnection( $i, $wiki );
+                       if ( !$conn ) {
+                               wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" );
+                               unset( $nonErrorLoads[$i] );
+                               unset( $currentLoads[$i] );
+                               continue;
                        }
 
-                       # Some servers must have been overloaded
-                       if ( $overloadedServers == 0 ) {
-                               throw new MWException( __METHOD__ . ": unexpectedly found no overloaded servers" );
+                       // Decrement reference counter, we are finished with this connection.
+                       // It will be incremented for the caller later.
+                       if ( $wiki !== false ) {
+                               $this->reuseConnection( $conn );
                        }
-                       # Back off for a while
-                       # Scale the sleep time by the number of connected threads, to produce a
-                       # roughly constant global poll rate
-                       $avgThreads = $totalThreadsConnected / $overloadedServers;
-                       $totalElapsed += $this->sleep( $wgDBAvgStatusPoll * $avgThreads );
-               } while ( $totalElapsed < $timeout );
-
-               if ( $totalElapsed >= $timeout ) {
-                       wfDebugLog( 'connect', "All servers busy\n" );
-                       $this->mErrorConnection = false;
-                       $this->mLastError = 'All servers busy';
+
+                       # Return this server
+                       break;
+               }
+
+               # If all servers were down, quit now
+               if ( !count( $nonErrorLoads ) ) {
+                       wfDebugLog( 'connect', "All servers down" );
                }
 
                if ( $i !== false ) {
@@ -332,11 +291,10 @@ class LoadBalancer {
                                        $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
                                }
                        }
-                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $i !== false ) {
+                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group !== false ) {
                                $this->mReadIndex = $i;
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return $i;
        }