Revert r29671, it was based on a misunderstanding of the purpose of the LoggedOut...
[lhc/web/wiklou.git] / includes / LoadBalancer.php
index 3690b43..657bb2c 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 /**
  *
- * @package MediaWiki
  */
 
 
@@ -9,7 +8,6 @@
  * Database load balancing object
  *
  * @todo document
- * @package MediaWiki
  */
 class LoadBalancer {
        /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
@@ -24,7 +22,7 @@ class LoadBalancer {
         */
        const AVG_STATUS_POLL = 2000;
 
-       function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
+       function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
        {
                $this->mServers = $servers;
                $this->mFailFunction = $failFunction;
@@ -32,7 +30,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;
@@ -97,7 +95,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] );
                        }
                }
@@ -174,10 +174,9 @@ 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.
@@ -185,9 +184,13 @@ class LoadBalancer {
 
                                                                # 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 {
@@ -321,13 +324,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;
@@ -501,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] ) ) {
@@ -550,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 );
@@ -644,4 +657,4 @@ class LoadBalancer {
        }
 }
 
-?>
+