Cleaned up LoadBalancer::getConnection
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 10 Dec 2014 01:34:48 +0000 (17:34 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 10 Dec 2014 08:27:48 +0000 (00:27 -0800)
* It will also no longer call getReaderIndex( false, ... ) twice
* Removed various related ampersands

Change-Id: Ia79e2007dbf84e7437f9439aa6371333aa3e1b23

includes/GlobalFunctions.php
includes/db/LBFactory.php
includes/db/LoadBalancer.php

index 14326ec..9cbf815 100644 (file)
@@ -3551,7 +3551,7 @@ function wfSplitWikiID( $wiki ) {
  *
  * @return DatabaseBase
  */
-function &wfGetDB( $db, $groups = array(), $wiki = false ) {
+function wfGetDB( $db, $groups = array(), $wiki = false ) {
        return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
 }
 
@@ -3570,7 +3570,7 @@ function wfGetLB( $wiki = false ) {
  *
  * @return LBFactory
  */
-function &wfGetLBFactory() {
+function wfGetLBFactory() {
        return LBFactory::singleton();
 }
 
index 0e89a5b..4551e2d 100644 (file)
@@ -43,7 +43,7 @@ abstract class LBFactory {
         *
         * @return LBFactory
         */
-       public static function &singleton() {
+       public static function singleton() {
                global $wgLBFactoryConf;
 
                if ( is_null( self::$instance ) ) {
index 07645bd..90e8867 100644 (file)
@@ -438,7 +438,7 @@ class LoadBalancer {
         * @throws MWException
         * @return DatabaseBase
         */
-       public function &getConnection( $i, $groups = array(), $wiki = false ) {
+       public function getConnection( $i, $groups = array(), $wiki = false ) {
                wfProfileIn( __METHOD__ );
 
                if ( $i === null || $i === false ) {
@@ -451,17 +451,14 @@ class LoadBalancer {
                        $wiki = false;
                }
 
-               # Query groups
+               $groups = ( $groups === false || $groups === array() )
+                       ? array( false ) // check one "group": the generic pool
+                       : (array)$groups;
+
                if ( $i == DB_MASTER ) {
                        $i = $this->getWriterIndex();
-               } elseif ( !is_array( $groups ) ) {
-                       $groupIndex = $this->getReaderIndex( $groups, $wiki );
-                       if ( $groupIndex !== false ) {
-                               $serverName = $this->getServerName( $groupIndex );
-                               wfDebug( __METHOD__ . ": using server $serverName for group $groups\n" );
-                               $i = $groupIndex;
-                       }
                } else {
+                       # Try to find an available server in any the query groups (in order)
                        foreach ( $groups as $group ) {
                                $groupIndex = $this->getReaderIndex( $group, $wiki );
                                if ( $groupIndex !== false ) {
@@ -476,7 +473,10 @@ class LoadBalancer {
                # Operation-based index
                if ( $i == DB_SLAVE ) {
                        $this->mLastError = 'Unknown error'; // reset error string
-                       $i = $this->getReaderIndex( false, $wiki );
+                       # Try the general server pool if $groups are unavailable.
+                       $i = in_array( false, $groups, true )
+                               ? false // don't bother with this if that is what was tried above
+                               : $this->getReaderIndex( false, $wiki );
                        # Couldn't find a working server in getReaderIndex()?
                        if ( $i === false ) {
                                $this->mLastError = 'No working slave server: ' . $this->mLastError;