Merge "Defer addAutopromoteOnceGroups to transaction idle"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 38c3d2d..5353288 100644 (file)
@@ -45,7 +45,6 @@ class LoadBalancer {
        /**
         * @param array $params with keys:
         *   servers           Required. Array of server info structures.
-        *   masterWaitTimeout Replication lag wait timeout
         *   loadMonitor       Name of a class used to fetch server lag and load.
         * @throws MWException
         */
@@ -54,12 +53,7 @@ class LoadBalancer {
                        throw new MWException( __CLASS__ . ': missing servers parameter' );
                }
                $this->mServers = $params['servers'];
-
-               if ( isset( $params['waitTimeout'] ) ) {
-                       $this->mWaitTimeout = $params['waitTimeout'];
-               } else {
-                       $this->mWaitTimeout = 10;
-               }
+               $this->mWaitTimeout = 10;
 
                $this->mReadIndex = -1;
                $this->mWriteIndex = -1;
@@ -420,11 +414,7 @@ class LoadBalancer {
        public function &getConnection( $i, $groups = array(), $wiki = false ) {
                wfProfileIn( __METHOD__ );
 
-               if ( $i == DB_LAST ) {
-                       wfProfileOut( __METHOD__ );
-                       throw new MWException( 'Attempt to call ' . __METHOD__ .
-                               ' with deprecated server index DB_LAST' );
-               } elseif ( $i === null || $i === false ) {
+               if ( $i === null || $i === false ) {
                        wfProfileOut( __METHOD__ );
                        throw new MWException( 'Attempt to call ' . __METHOD__ .
                                ' with invalid server index' );
@@ -878,17 +868,6 @@ class LoadBalancer {
                );
        }
 
-       /**
-        * Deprecated function, typo in function name
-        *
-        * @deprecated since 1.18
-        * @param DatabaseBase $conn
-        */
-       function closeConnecton( $conn ) {
-               wfDeprecated( __METHOD__, '1.18' );
-               $this->closeConnection( $conn );
-       }
-
        /**
         * Close a connection
         * Using this function makes sure the LoadBalancer knows the connection is closed.
@@ -1069,8 +1048,22 @@ class LoadBalancer {
                $maxLag = -1;
                $host = '';
                $maxIndex = 0;
-               if ( $this->getServerCount() > 1 ) { // no replication = no lag
+
+               if ( $this->getServerCount() <= 1 ) { // no replication = no lag
+                       return array( $host, $maxLag, $maxIndex );
+               }
+
+               // Try to get the max lag info from the server cache
+               $key = 'loadbalancer:maxlag:cluster:' . $this->mServers[0]['host'];
+               $cache = ObjectCache::newAccelerator( array(), 'hash' );
+               $maxLagInfo = $cache->get( $key ); // (host, lag, index)
+
+               // Fallback to connecting to each slave and getting the lag
+               if ( !$maxLagInfo ) {
                        foreach ( $this->mServers as $i => $conn ) {
+                               if ( $i == $this->getWriterIndex() ) {
+                                       continue; // nothing to check
+                               }
                                $conn = false;
                                if ( $wiki === false ) {
                                        $conn = $this->getAnyOpenConnection( $i );
@@ -1088,9 +1081,11 @@ class LoadBalancer {
                                        $maxIndex = $i;
                                }
                        }
+                       $maxLagInfo = array( $host, $maxLag, $maxIndex );
+                       $cache->set( $key, $maxLagInfo, 5 );
                }
 
-               return array( $host, $maxLag, $maxIndex );
+               return $maxLagInfo;
        }
 
        /**