Merge "Break some long lines in maintenance, skins, tests"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 306c2d3..0379e86 100644 (file)
@@ -43,7 +43,7 @@ class LoadBalancer {
        private $mLoadMonitorClass, $mLoadMonitor;
 
        /**
-        * @param array $params with keys:
+        * @param array $params Array with keys:
         *   servers           Required. Array of server info structures.
         *   loadMonitor       Name of a class used to fetch server lag and load.
         * @throws MWException
@@ -868,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.
@@ -959,6 +948,14 @@ class LoadBalancer {
                }
        }
 
+       /**
+        * @return bool Whether a master connection is already open
+        * @since 1.24
+        */
+       function hasMasterConnection() {
+               return $this->isOpen( $this->getWriterIndex() );
+       }
+
        /**
         * Determine if there are any pending changes that need to be rolled back
         * or committed.
@@ -1059,8 +1056,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 );
@@ -1078,9 +1089,11 @@ class LoadBalancer {
                                        $maxIndex = $i;
                                }
                        }
+                       $maxLagInfo = array( $host, $maxLag, $maxIndex );
+                       $cache->set( $key, $maxLagInfo, 5 );
                }
 
-               return array( $host, $maxLag, $maxIndex );
+               return $maxLagInfo;
        }
 
        /**