From 52e6121447a2215e6a867548a726d4c17790f6c0 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 9 Dec 2014 17:34:48 -0800 Subject: [PATCH] Cleaned up LoadBalancer::getConnection * It will also no longer call getReaderIndex( false, ... ) twice * Removed various related ampersands Change-Id: Ia79e2007dbf84e7437f9439aa6371333aa3e1b23 --- includes/GlobalFunctions.php | 4 ++-- includes/db/LBFactory.php | 2 +- includes/db/LoadBalancer.php | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 14326ec29e..9cbf8157cd 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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(); } diff --git a/includes/db/LBFactory.php b/includes/db/LBFactory.php index 0e89a5b064..4551e2d760 100644 --- a/includes/db/LBFactory.php +++ b/includes/db/LBFactory.php @@ -43,7 +43,7 @@ abstract class LBFactory { * * @return LBFactory */ - public static function &singleton() { + public static function singleton() { global $wgLBFactoryConf; if ( is_null( self::$instance ) ) { diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 07645bdd85..90e8867aeb 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -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; -- 2.20.1