Refactored parser output handling slightly, and added a hook function, to allow exten...
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index 465f3b9..cbee03e 100644 (file)
@@ -9,16 +9,6 @@
  */
 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
@@ -38,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;
@@ -71,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'];
@@ -83,6 +56,14 @@ class LoadBalancer {
                                }
                        }
                }
+               if ( $waitForMasterNow ) {
+                       $this->loadMasterPos();
+               }
+       }
+
+       static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
+       {
+               return new LoadBalancer( $servers, $failFunction, $waitTimeout );
        }
 
        /**
@@ -186,11 +167,11 @@ class LoadBalancer {
                                        }
                                        $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 {
@@ -436,7 +417,7 @@ 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 );
@@ -469,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 ) {
@@ -477,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)
         */
@@ -613,6 +608,7 @@ class LoadBalancer {
        function getLagTimes() {
                global $wgDBname;
 
+               wfProfileIn( __METHOD__ );
                $expiry = 5;
                $requestRate = 10;
 
@@ -624,6 +620,7 @@ class LoadBalancer {
                        $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
                        if ( mt_rand( 0, $chance ) != 0 ) {
                                unset( $times['timestamp'] );
+                               wfProfileOut( __METHOD__ );
                                return $times;
                        }
                }
@@ -645,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;
        }
 }