Refactored parser output handling slightly, and added a hook function, to allow exten...
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index 3e6cd7e..cbee03e 100644 (file)
@@ -7,16 +7,7 @@
 /**
  * Depends on the database object
  */
-
-# 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 );
+require_once( 'Database.php' );
 
 
 # Scale polling time so that under overload conditions, the database server
@@ -37,26 +28,7 @@ class LoadBalancer {
        /* 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;
-               $this->mAllowLag = false;
-       }
-
-       function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
-       {
-               $lb = new LoadBalancer;
-               $lb->initialise( $servers, $failFunction, $waitTimeout );
-               return $lb;
-       }
-
-       function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
+       function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
        {
                $this->mServers = $servers;
                $this->mFailFunction = $failFunction;
@@ -70,6 +42,8 @@ class LoadBalancer {
                $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 +56,14 @@ class LoadBalancer {
                                }
                        }
                }
+               if ( $waitForMasterNow ) {
+                       $this->loadMasterPos();
+               }
+       }
+
+       static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
+       {
+               return new LoadBalancer( $servers, $failFunction, $waitTimeout );
        }
 
        /**
@@ -435,18 +417,16 @@ class LoadBalancer {
         */
        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
-               if ($type != 'mysql' ) {
-                       $class = 'Database' . ucfirst( $type );
-               } else {
-                       $class = 'Database';
+               $class = 'Database' . ucfirst( $type );
+               if ( !class_exists( $class ) ) {
+                       require_once( "$class.php" );
                }
-               
+
                # Create object
                $db = new $class( $host, $user, $password, $dbname, 1, $flags );
                $db->setLBInfo( $server );
@@ -470,7 +450,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 ) {
@@ -478,28 +458,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)
         */
@@ -614,6 +608,7 @@ class LoadBalancer {
        function getLagTimes() {
                global $wgDBname;
 
+               wfProfileIn( __METHOD__ );
                $expiry = 5;
                $requestRate = 10;
 
@@ -625,6 +620,7 @@ class LoadBalancer {
                        $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
                        if ( mt_rand( 0, $chance ) != 0 ) {
                                unset( $times['timestamp'] );
+                               wfProfileOut( __METHOD__ );
                                return $times;
                        }
                }
@@ -646,6 +642,8 @@ class LoadBalancer {
 
                # But don't give the timestamp to the caller
                unset($times['timestamp']);
+               wfIncrStats( 'lag_cache_miss' );
+               wfProfileOut( __METHOD__ );
                return $times;
        }
 }