Revert r29671, it was based on a misunderstanding of the purpose of the LoggedOut...
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index 40b4613..657bb2c 100644 (file)
@@ -1,62 +1,28 @@
 <?php
 /**
  *
- * @package MediaWiki
  */
 
-/**
- * Depends on the database object
- */
-require_once( 'Database.php' );
-
-# Valid database indexes
-# Operation-based indexes
-define( 'DB_SLAVE', -1 );     # Read from the slave (or only server)
-define( 'DB_MASTER', -2 );    # Write to master (or only server)
-define( 'DB_LAST', -3 );     # Whatever database was used last
-
-# Obsolete aliases
-define( 'DB_READ', -1 );
-define( 'DB_WRITE', -2 );
-
-
-# Scale polling time so that under overload conditions, the database server
-# receives a SHOW STATUS query at an average interval of this many microseconds
-define( 'AVG_STATUS_POLL', 2000 );
-
 
 /**
  * Database load balancing object
  *
  * @todo document
- * @package MediaWiki
  */
 class LoadBalancer {
        /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
        /* private */ var $mFailFunction, $mErrorConnection;
-       /* private */ var $mForce, $mReadIndex, $mLastIndex;
+       /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
        /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
        /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
 
-       function LoadBalancer()
-       {
-               $this->mServers = array();
-               $this->mConnections = array();
-               $this->mFailFunction = false;
-               $this->mReadIndex = -1;
-               $this->mForce = -1;
-               $this->mLastIndex = -1;
-               $this->mErrorConnection = false;
-       }
-
-       function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
-       {
-               $lb = new LoadBalancer;
-               $lb->initialise( $servers, $failFunction, $waitTimeout );
-               return $lb;
-       }
+       /**
+        * Scale polling time so that under overload conditions, the database server
+        * receives a SHOW STATUS query at an average interval of this many microseconds
+        */
+       const AVG_STATUS_POLL = 2000;
 
-       function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
+       function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
        {
                $this->mServers = $servers;
                $this->mFailFunction = $failFunction;
@@ -64,12 +30,14 @@ class LoadBalancer {
                $this->mWriteIndex = -1;
                $this->mForce = -1;
                $this->mConnections = array();
-               $this->mLastIndex = 1;
+               $this->mLastIndex = -1;
                $this->mLoads = array();
                $this->mWaitForFile = false;
                $this->mWaitForPos = false;
                $this->mWaitTimeout = $waitTimeout;
                $this->mLaggedSlaveMode = false;
+               $this->mErrorConnection = false;
+               $this->mAllowLag = false;
 
                foreach( $servers as $i => $server ) {
                        $this->mLoads[$i] = $server['load'];
@@ -82,6 +50,14 @@ class LoadBalancer {
                                }
                        }
                }
+               if ( $waitForMasterNow ) {
+                       $this->loadMasterPos();
+               }
+       }
+
+       static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
+       {
+               return new LoadBalancer( $servers, $failFunction, $waitTimeout );
        }
 
        /**
@@ -94,17 +70,13 @@ class LoadBalancer {
                        return false;
                }
 
-               $sum = 0;
-               foreach ( $weights as $w ) {
-                       $sum += $w;
-               }
-
+               $sum = array_sum( $weights );
                if ( $sum == 0 ) {
                        # No loads on any of them
-                       # Just pick one at random
-                       foreach ( $weights as $i => $w ) {
-                               $weights[$i] = 1;
-                       }
+                       # In previous versions, this triggered an unweighted random selection,
+                       # but this feature has been removed as of April 2006 to allow for strict 
+                       # separation of query groups. 
+                       return false;
                }
                $max = mt_getrandmax();
                $rand = mt_rand(0, $max) / $max * $sum;
@@ -123,12 +95,13 @@ class LoadBalancer {
                # Unset excessively lagged servers
                $lags = $this->getLagTimes();
                foreach ( $lags as $i => $lag ) {
-                       if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
+                       if ( $i != 0 && isset( $this->mServers[$i]['max lag'] ) && 
+                               ( $lag === false || $lag > $this->mServers[$i]['max lag'] ) ) 
+                       {
                                unset( $loads[$i] );
                        }
                }
 
-
                # Find out if all the slaves with non-zero load are lagged
                $sum = 0;
                foreach ( $loads as $load ) {
@@ -139,7 +112,7 @@ class LoadBalancer {
                        # Do NOT use the master
                        # Instead, this function will return false, triggering read-only mode,
                        # and a lagged slave will be used instead.
-                       unset ( $loads[0] );
+                       return false;
                }
 
                if ( count( $loads ) == 0 ) {
@@ -168,6 +141,9 @@ class LoadBalancer {
                $i = false;
                if ( $this->mForce >= 0 ) {
                        $i = $this->mForce;
+               } elseif ( count( $this->mServers ) == 1 )  {
+                       # Skip the load balancing if there's only one server
+                       $i = 0;
                } else {
                        if ( $this->mReadIndex >= 0 ) {
                                $i = $this->mReadIndex;
@@ -178,38 +154,43 @@ class LoadBalancer {
                                $done = false;
                                $totalElapsed = 0;
                                do {
-                                       if ( $wgReadOnly ) {
+                                       if ( $wgReadOnly or $this->mAllowLagged ) {
                                                $i = $this->pickRandom( $loads );
                                        } else {
                                                $i = $this->getRandomNonLagged( $loads );
                                                if ( $i === false && count( $loads ) != 0 )  {
                                                        # All slaves lagged. Switch to read-only mode
-                                                       $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
+                                                       $wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
                                                        $i = $this->pickRandom( $loads );
                                                }
                                        }
                                        $serverIndex = $i;
                                        if ( $i !== false ) {
-                                               wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
+                                               wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
                                                $this->openConnection( $i );
 
                                                if ( !$this->isOpen( $i ) ) {
-                                                       wfDebug( "Failed\n" );
+                                                       wfDebug( "$fname: Failed\n" );
                                                        unset( $loads[$i] );
                                                        $sleepTime = 0;
                                                } else {
-                                                       $status = $this->mConnections[$i]->getStatus();
-                                                       if ( isset( $this->mServers[$i]['max threads'] ) &&
-                                                         $status['Threads_running'] > $this->mServers[$i]['max threads'] )
-                                                       {
-                                                               # Slave is lagged, wait for a while
-                                                               $sleepTime = AVG_STATUS_POLL * $status['Threads_connected'];
+                                                       if ( isset( $this->mServers[$i]['max threads'] ) ) {
+                                                           $status = $this->mConnections[$i]->getStatus("Thread%");
+                                                           if ( $status['Threads_running'] > $this->mServers[$i]['max threads'] ) {
+                                                               # Too much load, back off and wait for a while.
+                                                               # The sleep time is scaled by the number of threads connected,
+                                                               # to produce a roughly constant global poll rate.
+                                                               $sleepTime = self::AVG_STATUS_POLL * $status['Threads_connected'];
 
                                                                # If we reach the timeout and exit the loop, don't use it
                                                                $i = false;
-                                                       } else {
+                                                           } else {
                                                                $done = true;
                                                                $sleepTime = 0;
+                                                           }
+                                                       } else {
+                                                           $done = true;
+                                                           $sleepTime = 0;
                                                        }
                                                }
                                        } else {
@@ -228,7 +209,7 @@ class LoadBalancer {
                                        $this->mErrorConnection = false;
                                        $this->mLastError = 'All servers busy';
                                }
-                               
+
                                if ( $i !== false && $this->isOpen( $i ) ) {
                                        # Wait for the session master pos for a short time
                                        if ( $this->mWaitForFile ) {
@@ -336,21 +317,33 @@ class LoadBalancer {
         */
        function &getConnection( $i, $fail = true, $groups = array() )
        {
+               global $wgDBtype;
                $fname = 'LoadBalancer::getConnection';
                wfProfileIn( $fname );
 
+
                # Query groups
-               $groupIndex = false;
-               foreach ( $groups as $group ) {
-                       $groupIndex = $this->getGroupIndex( $group );
+               if ( !is_array( $groups ) ) {
+                       $groupIndex = $this->getGroupIndex( $groups );
                        if ( $groupIndex !== false ) {
                                $i = $groupIndex;
-                               break;
+                       }
+               } else {
+                       foreach ( $groups as $group ) {
+                               $groupIndex = $this->getGroupIndex( $group );
+                               if ( $groupIndex !== false ) {
+                                       $i = $groupIndex;
+                                       break;
+                               }
                        }
                }
 
+               # For now, only go through all this for mysql databases
+               if ($wgDBtype != 'mysql') {
+                       $i = $this->getWriterIndex();
+               }
                # Operation-based index
-               if ( $i == DB_SLAVE ) {
+               elseif ( $i == DB_SLAVE ) {
                        $i = $this->getReaderIndex();
                } elseif ( $i == DB_MASTER ) {
                        $i = $this->getWriterIndex();
@@ -378,7 +371,7 @@ class LoadBalancer {
         * Open a connection to the server given by the specified index
         * Index must be an actual index into the array
         * Returns success
-        * @private
+        * @access private
         */
        function openConnection( $i, $fail = false ) {
                $fname = 'LoadBalancer::openConnection';
@@ -405,7 +398,7 @@ class LoadBalancer {
 
        /**
         * Test if the specified index represents an open connection
-        * @private
+        * @access private
         */
        function isOpen( $index ) {
                if( !is_integer( $index ) ) {
@@ -422,19 +415,16 @@ class LoadBalancer {
 
        /**
         * Really opens a connection
-        * @private
+        * @access private
         */
        function reallyOpenConnection( &$server ) {
                if( !is_array( $server ) ) {
-                       wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
+                       throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
                }
 
                extract( $server );
                # Get class for this database type
                $class = 'Database' . ucfirst( $type );
-               if ( !class_exists( $class ) ) {
-                       require_once( "$class.php" );
-               }
 
                # Create object
                $db = new $class( $host, $user, $password, $dbname, 1, $flags );
@@ -459,7 +449,7 @@ class LoadBalancer {
                                        $conn->reportConnectionError( $this->mLastError );
                                } else {
                                        // If all servers were busy, mLastError will contain something sensible
-                                       wfEmergencyAbort( $conn, $this->mLastError );
+                                       throw new DBConnectionError( $conn, $this->mLastError );
                                }
                        } else {
                                if ( $this->mFailFunction ) {
@@ -467,28 +457,42 @@ class LoadBalancer {
                                } else {
                                        $conn->failFunction( false );
                                }
-                               $conn->reportConnectionError( "{$this->mLastError} ({$conn->mServer})" );
+                               $server = $conn->getProperty( 'mServer' );
+                               $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
                        }
                        $reporting = false;
                }
                wfProfileOut( $fname );
        }
 
-       function getWriterIndex()
-       {
+       function getWriterIndex() {
                return 0;
        }
 
-       function force( $i )
-       {
+       /**
+        * Force subsequent calls to getConnection(DB_SLAVE) to return the 
+        * given index. Set to -1 to restore the original load balancing
+        * behaviour. I thought this was a good idea when I originally 
+        * wrote this class, but it has never been used.
+        */
+       function force( $i ) {
                $this->mForce = $i;
        }
 
-       function haveIndex( $i )
-       {
+       /**
+        * Returns true if the specified index is a valid server index
+        */
+       function haveIndex( $i ) {
                return array_key_exists( $i, $this->mServers );
        }
 
+       /**
+        * Returns true if the specified index is valid and has non-zero load
+        */
+       function isNonZeroLoad( $i ) {
+               return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
+       }
+
        /**
         * Get the number of defined servers (not the number of open connections)
         */
@@ -500,8 +504,7 @@ class LoadBalancer {
         * Save master pos to the session and to memcached, if the session exists
         */
        function saveMasterPos() {
-               global $wgSessionStarted;
-               if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
+               if ( session_id() != '' && count( $this->mServers ) > 1 ) {
                        # If this entire request was served from a slave without opening a connection to the
                        # master (however unlikely that may be), then we can fetch the position from the slave.
                        if ( empty( $this->mConnections[0] ) ) {
@@ -549,6 +552,17 @@ class LoadBalancer {
                        }
                }
        }
+       
+       /* Issue COMMIT only on master, only if queries were done on connection */
+       function commitMasterChanges() {
+               // Always 0, but who knows.. :)
+               $i = $this->getWriterIndex();
+               if (array_key_exists($i,$this->mConnections)) {
+                       if ($this->mConnections[$i]->lastQuery() != '') {
+                               $this->mConnections[$i]->immediateCommit();
+                       }
+               }
+       }
 
        function waitTimeout( $value = NULL ) {
                return wfSetVar( $this->mWaitTimeout, $value );
@@ -558,6 +572,13 @@ class LoadBalancer {
                return $this->mLaggedSlaveMode;
        }
 
+       /* Disables/enables lag checks */
+       function allowLagged($mode=null) {
+               if ($mode===null)
+                       return $this->mAllowLagged;
+               $this->mAllowLagged=$mode;
+       }
+
        function pingAll() {
                $success = true;
                foreach ( $this->mConnections as $i => $conn ) {
@@ -594,40 +615,46 @@ class LoadBalancer {
         * Results are cached for a short time in memcached
         */
        function getLagTimes() {
-               global $wgDBname;
-               
+               wfProfileIn( __METHOD__ );
                $expiry = 5;
                $requestRate = 10;
 
                global $wgMemc;
-               $times = $wgMemc->get( "$wgDBname:lag_times" );
+               $times = $wgMemc->get( wfMemcKey( 'lag_times' ) );
                if ( $times ) {
                        # Randomly recache with probability rising over $expiry
                        $elapsed = time() - $times['timestamp'];
                        $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
                        if ( mt_rand( 0, $chance ) != 0 ) {
                                unset( $times['timestamp'] );
+                               wfProfileOut( __METHOD__ );
                                return $times;
                        }
+                       wfIncrStats( 'lag_cache_miss_expired' );
+               } else {
+                       wfIncrStats( 'lag_cache_miss_absent' );
                }
 
                # Cache key missing or expired
 
                $times = array();
                foreach ( $this->mServers as $i => $conn ) {
-                       if ( $this->openConnection( $i ) ) {
+                       if ($i==0) { # Master
+                               $times[$i] = 0;
+                       } elseif ( $this->openConnection( $i ) ) {
                                $times[$i] = $this->mConnections[$i]->getLag();
                        }
                }
 
                # Add a timestamp key so we know when it was cached
                $times['timestamp'] = time();
-               $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
+               $wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );
 
                # But don't give the timestamp to the caller
                unset($times['timestamp']);
+               wfProfileOut( __METHOD__ );
                return $times;
        }
 }
 
-?>
+