(bug 13140) Show parent categories in category namespace
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index cbee03e..0cdadd1 100644 (file)
@@ -1,25 +1,13 @@
 <?php
 /**
  *
- * @package MediaWiki
  */
 
-/**
- * Depends on the database object
- */
-require_once( 'Database.php' );
-
-
-# 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;
@@ -28,7 +16,7 @@ class LoadBalancer {
        /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
        /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
 
-       function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
+       function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
        {
                $this->mServers = $servers;
                $this->mFailFunction = $failFunction;
@@ -36,7 +24,7 @@ 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;
@@ -101,7 +89,9 @@ 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] );
                        }
                }
@@ -137,7 +127,7 @@ class LoadBalancer {
         * Side effect: opens connections to databases
         */
        function getReaderIndex() {
-               global $wgReadOnly, $wgDBClusterTimeout;
+               global $wgReadOnly, $wgDBClusterTimeout, $wgDBAvgStatusPoll;
 
                $fname = 'LoadBalancer::getReaderIndex';
                wfProfileIn( $fname );
@@ -145,6 +135,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;
@@ -161,7 +154,7 @@ class LoadBalancer {
                                                $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 );
                                                }
                                        }
@@ -175,20 +168,23 @@ class LoadBalancer {
                                                        unset( $loads[$i] );
                                                        $sleepTime = 0;
                                                } else {
-                                                       $status = $this->mConnections[$i]->getStatus("Thread%");
-                                                       if ( isset( $this->mServers[$i]['max threads'] ) &&
-                                                         $status['Threads_running'] > $this->mServers[$i]['max threads'] )
-                                                       {
+                                                       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 = AVG_STATUS_POLL * $status['Threads_connected'];
+                                                               $sleepTime = $wgDBAvgStatusPoll * $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 {
@@ -322,13 +318,13 @@ class LoadBalancer {
 
                # Query groups
                if ( !is_array( $groups ) ) {
-                       $groupIndex = $this->getGroupIndex( $groups, $i );
+                       $groupIndex = $this->getGroupIndex( $groups );
                        if ( $groupIndex !== false ) {
                                $i = $groupIndex;
                        }
                } else {
                        foreach ( $groups as $group ) {
-                               $groupIndex = $this->getGroupIndex( $group, $i );
+                               $groupIndex = $this->getGroupIndex( $group );
                                if ( $groupIndex !== false ) {
                                        $i = $groupIndex;
                                        break;
@@ -423,9 +419,6 @@ class LoadBalancer {
                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 );
@@ -433,8 +426,7 @@ class LoadBalancer {
                return $db;
        }
 
-       function reportConnectionError( &$conn )
-       {
+       function reportConnectionError( &$conn ) {
                $fname = 'LoadBalancer::reportConnectionError';
                wfProfileIn( $fname );
                # Prevent infinite recursion
@@ -505,8 +497,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] ) ) {
@@ -554,6 +545,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 );
@@ -606,14 +608,12 @@ 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'];
@@ -623,6 +623,9 @@ class LoadBalancer {
                                wfProfileOut( __METHOD__ );
                                return $times;
                        }
+                       wfIncrStats( 'lag_cache_miss_expired' );
+               } else {
+                       wfIncrStats( 'lag_cache_miss_absent' );
                }
 
                # Cache key missing or expired
@@ -638,14 +641,13 @@ class LoadBalancer {
 
                # 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']);
-               wfIncrStats( 'lag_cache_miss' );
                wfProfileOut( __METHOD__ );
                return $times;
        }
 }
 
-?>
+