diff options
Diffstat (limited to 'includes/db/LoadBalancer.php')
-rw-r--r-- | includes/db/LoadBalancer.php | 458 |
1 files changed, 268 insertions, 190 deletions
diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php index 857109db..e517a025 100644 --- a/includes/db/LoadBalancer.php +++ b/includes/db/LoadBalancer.php @@ -28,19 +28,43 @@ * @ingroup Database */ class LoadBalancer { - private $mServers, $mConns, $mLoads, $mGroupLoads; + /** @var array Map of (server index => server config array) */ + private $mServers; + /** @var array Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */ + private $mConns; + /** @var array Map of (server index => weight) */ + private $mLoads; + /** @var array Map of (group => server index => weight) */ + private $mGroupLoads; + /** @var bool Whether to disregard slave lag as a factor in slave selection */ + private $mAllowLagged; + /** @var integer Seconds to spend waiting on slave lag to resolve */ + private $mWaitTimeout; + + /** @var array LBFactory information */ + private $mParentInfo; + /** @var string The LoadMonitor subclass name */ + private $mLoadMonitorClass; + /** @var LoadMonitor */ + private $mLoadMonitor; + + /** @var bool|DatabaseBase Database connection that caused a problem */ private $mErrorConnection; - private $mReadIndex, $mAllowLagged; - private $mWaitForPos, $mWaitTimeout; - private $mLaggedSlaveMode, $mLastError = 'Unknown error'; - private $mParentInfo, $mLagTimes; - private $mLoadMonitorClass, $mLoadMonitor; + /** @var integer The generic (not query grouped) slave index (of $mServers) */ + private $mReadIndex; + /** @var bool|DBMasterPos False if not set */ + private $mWaitForPos; + /** @var bool Whether the generic reader fell back to a lagged slave */ + private $mLaggedSlaveMode; + /** @var string The last DB selection or connection error */ + private $mLastError = 'Unknown error'; + /** @var array Process cache of LoadMonitor::getLagTimes() */ + private $mLagTimes; /** - * @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. + * @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 */ function __construct( $params ) { @@ -48,12 +72,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; @@ -72,9 +91,9 @@ class LoadBalancer { } else { $master = reset( $params['servers'] ); if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) { - $this->mLoadMonitorClass = 'LoadMonitor_MySQL'; + $this->mLoadMonitorClass = 'LoadMonitorMySQL'; } else { - $this->mLoadMonitorClass = 'LoadMonitor_Null'; + $this->mLoadMonitorClass = 'LoadMonitorNull'; } } @@ -101,13 +120,14 @@ class LoadBalancer { $class = $this->mLoadMonitorClass; $this->mLoadMonitor = new $class( $this ); } + return $this->mLoadMonitor; } /** * Get or set arbitrary data used by the parent object, usually an LBFactory - * @param $x - * @return Mixed + * @param mixed $x + * @return mixed */ function parentInfo( $x = null ) { return wfSetVar( $this->mParentInfo, $x ); @@ -119,8 +139,7 @@ class LoadBalancer { * * @deprecated since 1.21, use ArrayUtils::pickRandom() * - * @param $weights array - * + * @param array $weights * @return bool|int|string */ function pickRandom( $weights ) { @@ -128,8 +147,8 @@ class LoadBalancer { } /** - * @param $loads array - * @param $wiki bool + * @param array $loads + * @param bool|string $wiki Wiki to get non-lagged for * @return bool|int|string */ function getRandomNonLagged( $loads, $wiki = false ) { @@ -138,10 +157,10 @@ class LoadBalancer { foreach ( $lags as $i => $lag ) { if ( $i != 0 ) { if ( $lag === false ) { - wfDebugLog( 'replication', "Server #$i is not replicating\n" ); + wfDebugLog( 'replication', "Server #$i is not replicating" ); unset( $loads[$i] ); } elseif ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) { - wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)\n" ); + wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" ); unset( $loads[$i] ); } } @@ -176,13 +195,13 @@ class LoadBalancer { * always return a consistent index during a given invocation * * Side effect: opens connections to databases - * @param $group bool - * @param $wiki bool + * @param bool|string $group + * @param bool|string $wiki * @throws MWException * @return bool|int|string */ function getReaderIndex( $group = false, $wiki = false ) { - global $wgReadOnly, $wgDBClusterTimeout, $wgDBAvgStatusPoll, $wgDBtype; + global $wgReadOnly, $wgDBtype; # @todo FIXME: For now, only go through all this for mysql databases if ( $wgDBtype != 'mysql' ) { @@ -192,17 +211,12 @@ class LoadBalancer { if ( count( $this->mServers ) == 1 ) { # Skip the load balancing if there's only one server return 0; - } elseif ( $group === false and $this->mReadIndex >= 0 ) { + } elseif ( $group === false && $this->mReadIndex >= 0 ) { # Shortcut if generic reader exists already return $this->mReadIndex; } - wfProfileIn( __METHOD__ ); - - $totalElapsed = 0; - - # convert from seconds to microseconds - $timeout = $wgDBClusterTimeout * 1e6; + $section = new ProfileSection( __METHOD__ ); # Find the relevant load array if ( $group !== false ) { @@ -211,15 +225,14 @@ class LoadBalancer { } else { # No loads for this group, return false and the caller can use some other group wfDebug( __METHOD__ . ": no loads for group $group\n" ); - wfProfileOut( __METHOD__ ); + return false; } } else { $nonErrorLoads = $this->mLoads; } - if ( !$nonErrorLoads ) { - wfProfileOut( __METHOD__ ); + if ( !count( $nonErrorLoads ) ) { throw new MWException( "Empty server array given to LoadBalancer" ); } @@ -228,92 +241,60 @@ class LoadBalancer { $laggedSlaveMode = false; + # No server found yet + $i = false; # First try quickly looking through the available servers for a server that # meets our criteria - do { - $totalThreadsConnected = 0; - $overloadedServers = 0; - $currentLoads = $nonErrorLoads; - while ( count( $currentLoads ) ) { - if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) { + $currentLoads = $nonErrorLoads; + while ( count( $currentLoads ) ) { + if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) { + $i = ArrayUtils::pickRandom( $currentLoads ); + } else { + $i = $this->getRandomNonLagged( $currentLoads, $wiki ); + if ( $i === false && count( $currentLoads ) != 0 ) { + # All slaves lagged. Switch to read-only mode + wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" ); + $wgReadOnly = 'The database has been automatically locked ' . + 'while the slave database servers catch up to the master'; $i = ArrayUtils::pickRandom( $currentLoads ); - } else { - $i = $this->getRandomNonLagged( $currentLoads, $wiki ); - if ( $i === false && count( $currentLoads ) != 0 ) { - # All slaves lagged. Switch to read-only mode - wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode\n" ); - $wgReadOnly = 'The database has been automatically locked ' . - 'while the slave database servers catch up to the master'; - $i = ArrayUtils::pickRandom( $currentLoads ); - $laggedSlaveMode = true; - } - } - - if ( $i === false ) { - # pickRandom() returned false - # This is permanent and means the configuration or the load monitor - # wants us to return false. - wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false\n" ); - wfProfileOut( __METHOD__ ); - return false; + $laggedSlaveMode = true; } + } - wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: {$this->mServers[$i]['host']}...\n" ); - $conn = $this->openConnection( $i, $wiki ); - - if ( !$conn ) { - wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki\n" ); - unset( $nonErrorLoads[$i] ); - unset( $currentLoads[$i] ); - continue; - } - - // Perform post-connection backoff - $threshold = isset( $this->mServers[$i]['max threads'] ) - ? $this->mServers[$i]['max threads'] : false; - $backoff = $this->getLoadMonitor()->postConnectionBackoff( $conn, $threshold ); - - // Decrement reference counter, we are finished with this connection. - // It will be incremented for the caller later. - if ( $wiki !== false ) { - $this->reuseConnection( $conn ); - } + if ( $i === false ) { + # pickRandom() returned false + # This is permanent and means the configuration or the load monitor + # wants us to return false. + wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" ); - if ( $backoff ) { - # Post-connection overload, don't use this server for now - $totalThreadsConnected += $backoff; - $overloadedServers++; - unset( $currentLoads[$i] ); - } else { - # Return this server - break 2; - } + return false; } - # No server found yet - $i = false; + wfDebugLog( 'connect', __METHOD__ . + ": Using reader #$i: {$this->mServers[$i]['host']}..." ); - # If all servers were down, quit now - if ( !count( $nonErrorLoads ) ) { - wfDebugLog( 'connect', "All servers down\n" ); - break; + $conn = $this->openConnection( $i, $wiki ); + if ( !$conn ) { + wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" ); + unset( $nonErrorLoads[$i] ); + unset( $currentLoads[$i] ); + $i = false; + continue; } - # Some servers must have been overloaded - if ( $overloadedServers == 0 ) { - throw new MWException( __METHOD__ . ": unexpectedly found no overloaded servers" ); + // Decrement reference counter, we are finished with this connection. + // It will be incremented for the caller later. + if ( $wiki !== false ) { + $this->reuseConnection( $conn ); } - # Back off for a while - # Scale the sleep time by the number of connected threads, to produce a - # roughly constant global poll rate - $avgThreads = $totalThreadsConnected / $overloadedServers; - $totalElapsed += $this->sleep( $wgDBAvgStatusPoll * $avgThreads ); - } while ( $totalElapsed < $timeout ); - - if ( $totalElapsed >= $timeout ) { - wfDebugLog( 'connect', "All servers busy\n" ); - $this->mErrorConnection = false; - $this->mLastError = 'All servers busy'; + + # Return this server + break; + } + + # If all servers were down, quit now + if ( !count( $nonErrorLoads ) ) { + wfDebugLog( 'connect', "All servers down" ); } if ( $i !== false ) { @@ -324,17 +305,17 @@ class LoadBalancer { $this->mServers[$i]['slave pos'] = $conn->getSlavePos(); } } - if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $i !== false ) { + if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group !== false ) { $this->mReadIndex = $i; } } - wfProfileOut( __METHOD__ ); + return $i; } /** * Wait for a specified number of microseconds, and return the period waited - * @param $t int + * @param int $t * @return int */ function sleep( $t ) { @@ -342,6 +323,7 @@ class LoadBalancer { wfDebug( __METHOD__ . ": waiting $t us\n" ); usleep( $t ); wfProfileOut( __METHOD__ ); + return $t; } @@ -349,7 +331,7 @@ class LoadBalancer { * Set the master wait position * If a DB_SLAVE connection has been opened already, waits * Otherwise sets a variable telling it to wait if such a connection is opened - * @param $pos DBMasterPos + * @param DBMasterPos $pos */ public function waitFor( $pos ) { wfProfileIn( __METHOD__ ); @@ -367,22 +349,31 @@ class LoadBalancer { /** * Set the master wait position and wait for ALL slaves to catch up to it - * @param $pos DBMasterPos + * @param DBMasterPos $pos + * @param int $timeout Max seconds to wait; default is mWaitTimeout + * @return bool Success (able to connect and no timeouts reached) */ - public function waitForAll( $pos ) { + public function waitForAll( $pos, $timeout = null ) { wfProfileIn( __METHOD__ ); $this->mWaitForPos = $pos; - for ( $i = 1; $i < count( $this->mServers ); $i++ ) { - $this->doWait( $i, true ); + $serverCount = count( $this->mServers ); + + $ok = true; + for ( $i = 1; $i < $serverCount; $i++ ) { + if ( $this->mLoads[$i] > 0 ) { + $ok = $this->doWait( $i, true, $timeout ) && $ok; + } } wfProfileOut( __METHOD__ ); + + return $ok; } /** * Get any open connection to a given server index, local or foreign * Returns false if there is no connection open * - * @param $i int + * @param int $i * @return DatabaseBase|bool False on failure */ function getAnyOpenConnection( $i ) { @@ -391,40 +382,47 @@ class LoadBalancer { return reset( $conns[$i] ); } } + return false; } /** * Wait for a given slave to catch up to the master pos stored in $this - * @param $index - * @param $open bool + * @param int $index Server index + * @param bool $open Check the server even if a new connection has to be made + * @param int $timeout Max seconds to wait; default is mWaitTimeout * @return bool */ - protected function doWait( $index, $open = false ) { + protected function doWait( $index, $open = false, $timeout = null ) { # Find a connection to wait on $conn = $this->getAnyOpenConnection( $index ); if ( !$conn ) { if ( !$open ) { wfDebug( __METHOD__ . ": no connection open\n" ); + return false; } else { $conn = $this->openConnection( $index, '' ); if ( !$conn ) { wfDebug( __METHOD__ . ": failed to open connection\n" ); + return false; } } } wfDebug( __METHOD__ . ": Waiting for slave #$index to catch up...\n" ); - $result = $conn->masterPosWait( $this->mWaitForPos, $this->mWaitTimeout ); + $timeout = $timeout ?: $this->mWaitTimeout; + $result = $conn->masterPosWait( $this->mWaitForPos, $timeout ); if ( $result == -1 || is_null( $result ) ) { # Timed out waiting for slave, use master instead wfDebug( __METHOD__ . ": Timed out waiting for slave #$index pos {$this->mWaitForPos}\n" ); + return false; } else { wfDebug( __METHOD__ . ": Done\n" ); + return true; } } @@ -433,8 +431,8 @@ class LoadBalancer { * Get a connection by index * This is the main entry point for this class. * - * @param $i Integer: server index - * @param array $groups query groups + * @param int $i Server index + * @param array $groups Query groups * @param bool|string $wiki Wiki ID * * @throws MWException @@ -443,12 +441,10 @@ class LoadBalancer { public function &getConnection( $i, $groups = array(), $wiki = false ) { wfProfileIn( __METHOD__ ); - if ( $i == DB_LAST ) { + if ( $i === null || $i === false ) { wfProfileOut( __METHOD__ ); - throw new MWException( 'Attempt to call ' . __METHOD__ . ' with deprecated server index DB_LAST' ); - } elseif ( $i === null || $i === false ) { - wfProfileOut( __METHOD__ ); - throw new MWException( 'Attempt to call ' . __METHOD__ . ' with invalid server index' ); + throw new MWException( 'Attempt to call ' . __METHOD__ . + ' with invalid server index' ); } if ( $wiki === wfWikiID() ) { @@ -485,6 +481,7 @@ class LoadBalancer { if ( $i === false ) { $this->mLastError = 'No working slave server: ' . $this->mLastError; wfProfileOut( __METHOD__ ); + return $this->reportConnectionError(); } } @@ -493,10 +490,12 @@ class LoadBalancer { $conn = $this->openConnection( $i, $wiki ); if ( !$conn ) { wfProfileOut( __METHOD__ ); + return $this->reportConnectionError(); } wfProfileOut( __METHOD__ ); + return $conn; } @@ -511,15 +510,9 @@ class LoadBalancer { public function reuseConnection( $conn ) { $serverIndex = $conn->getLBInfo( 'serverIndex' ); $refCount = $conn->getLBInfo( 'foreignPoolRefCount' ); - $dbName = $conn->getDBname(); - $prefix = $conn->tablePrefix(); - if ( strval( $prefix ) !== '' ) { - $wiki = "$dbName-$prefix"; - } else { - $wiki = $dbName; - } if ( $serverIndex === null || $refCount === null ) { wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" ); + /** * This can happen in code like: * foreach ( $dbs as $db ) { @@ -530,10 +523,20 @@ class LoadBalancer { * When a connection to the local DB is opened in this way, reuseConnection() * should be ignored */ + return; } + + $dbName = $conn->getDBname(); + $prefix = $conn->tablePrefix(); + if ( strval( $prefix ) !== '' ) { + $wiki = "$dbName-$prefix"; + } else { + $wiki = $dbName; + } if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) { - throw new MWException( __METHOD__ . ": connection not found, has the connection been freed already?" ); + throw new MWException( __METHOD__ . ": connection not found, has " . + "the connection been freed already?" ); } $conn->setLBInfo( 'foreignPoolRefCount', --$refCount ); if ( $refCount <= 0 ) { @@ -552,9 +555,9 @@ class LoadBalancer { * * @see LoadBalancer::getConnection() for parameter information * - * @param integer $db + * @param int $db * @param mixed $groups - * @param string $wiki + * @param bool|string $wiki * @return DBConnRef */ public function getConnectionRef( $db, $groups = array(), $wiki = false ) { @@ -568,9 +571,9 @@ class LoadBalancer { * * @see LoadBalancer::getConnection() for parameter information * - * @param integer $db + * @param int $db * @param mixed $groups - * @param string $wiki + * @param bool|string $wiki * @return DBConnRef */ public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) { @@ -585,8 +588,8 @@ class LoadBalancer { * On error, returns false, and the connection which caused the * error will be available via $this->mErrorConnection. * - * @param $i Integer server index - * @param string $wiki wiki ID to open + * @param int $i Server index + * @param bool|string $wiki Wiki ID to open * @return DatabaseBase * * @access private @@ -596,6 +599,7 @@ class LoadBalancer { if ( $wiki !== false ) { $conn = $this->openForeignConnection( $i, $wiki ); wfProfileOut( __METHOD__ ); + return $conn; } if ( isset( $this->mConns['local'][$i][0] ) ) { @@ -614,6 +618,7 @@ class LoadBalancer { } } wfProfileOut( __METHOD__ ); + return $conn; } @@ -631,8 +636,8 @@ class LoadBalancer { * On error, returns false, and the connection which caused the * error will be available via $this->mErrorConnection. * - * @param $i Integer: server index - * @param string $wiki wiki ID to open + * @param int $i Server index + * @param string $wiki Wiki ID to open * @return DatabaseBase */ function openForeignConnection( $i, $wiki ) { @@ -688,13 +693,14 @@ class LoadBalancer { $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 ); } wfProfileOut( __METHOD__ ); + return $conn; } /** * Test if the specified index represents an open connection * - * @param $index Integer: server index + * @param int $index Server index * @access private * @return bool */ @@ -702,6 +708,7 @@ class LoadBalancer { if ( !is_integer( $index ) ) { return false; } + return (bool)$this->getAnyOpenConnection( $index ); } @@ -710,8 +717,8 @@ class LoadBalancer { * Returns a Database object whether or not the connection was successful. * @access private * - * @param $server - * @param $dbNameOverride bool + * @param array $server + * @param bool $dbNameOverride * @throws MWException * @return DatabaseBase */ @@ -741,6 +748,7 @@ class LoadBalancer { if ( isset( $server['fakeMaster'] ) ) { $db->setFakeMaster( true ); } + return $db; } @@ -753,15 +761,16 @@ class LoadBalancer { if ( !is_object( $conn ) ) { // No last connection, probably due to all servers being too busy - wfLogDBError( "LB failure with no last connection. Connection error: {$this->mLastError}\n" ); + wfLogDBError( "LB failure with no last connection. Connection error: {$this->mLastError}" ); // If all servers were busy, mLastError will contain something sensible throw new DBConnectionError( null, $this->mLastError ); } else { $server = $conn->getProperty( 'mServer' ); - wfLogDBError( "Connection error: {$this->mLastError} ({$server})\n" ); + wfLogDBError( "Connection error: {$this->mLastError} ({$server})" ); $conn->reportConnectionError( "{$this->mLastError} ({$server})" ); // throws DBConnectionError } + return false; /* not reached */ } @@ -775,7 +784,7 @@ class LoadBalancer { /** * Returns true if the specified index is a valid server index * - * @param $i + * @param string $i * @return bool */ function haveIndex( $i ) { @@ -785,7 +794,7 @@ class LoadBalancer { /** * Returns true if the specified index is valid and has non-zero load * - * @param $i + * @param string $i * @return bool */ function isNonZeroLoad( $i ) { @@ -804,7 +813,7 @@ class LoadBalancer { /** * Get the host name or IP address of the server with the specified index * Prefer a readable name if available. - * @param $i + * @param string $i * @return string */ function getServerName( $i ) { @@ -819,8 +828,8 @@ class LoadBalancer { /** * Return the server info structure for a given index, or false if the index is invalid. - * @param $i - * @return bool + * @param int $i + * @return array|bool */ function getServerInfo( $i ) { if ( isset( $this->mServers[$i] ) ) { @@ -831,9 +840,10 @@ class LoadBalancer { } /** - * Sets the server info structure for the given index. Entry at index $i is created if it doesn't exist - * @param $i - * @param $serverInfo + * Sets the server info structure for the given index. Entry at index $i + * is created if it doesn't exist + * @param int $i + * @param array $serverInfo */ function setServerInfo( $i, $serverInfo ) { $this->mServers[$i] = $serverInfo; @@ -848,17 +858,21 @@ class LoadBalancer { # master (however unlikely that may be), then we can fetch the position from the slave. $masterConn = $this->getAnyOpenConnection( 0 ); if ( !$masterConn ) { - for ( $i = 1; $i < count( $this->mServers ); $i++ ) { + $serverCount = count( $this->mServers ); + for ( $i = 1; $i < $serverCount; $i++ ) { $conn = $this->getAnyOpenConnection( $i ); if ( $conn ) { wfDebug( "Master pos fetched from slave\n" ); + return $conn->getSlavePos(); } } } else { wfDebug( "Master pos fetched from master\n" ); + return $masterConn->getMasterPos(); } + return false; } @@ -868,6 +882,7 @@ class LoadBalancer { function closeAll() { foreach ( $this->mConns as $conns2 ) { foreach ( $conns2 as $conns3 ) { + /** @var DatabaseBase $conn */ foreach ( $conns3 as $conn ) { $conn->close(); } @@ -881,21 +896,10 @@ class LoadBalancer { } /** - * Deprecated function, typo in function name - * - * @deprecated in 1.18 - * @param $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. * If you use $conn->close() directly, the load balancer won't update its state. - * @param $conn DatabaseBase + * @param DatabaseBase $conn */ function closeConnection( $conn ) { $done = false; @@ -922,6 +926,7 @@ class LoadBalancer { function commitAll() { foreach ( $this->mConns as $conns2 ) { foreach ( $conns2 as $conns3 ) { + /** @var DatabaseBase[] $conns3 */ foreach ( $conns3 as $conn ) { if ( $conn->trxLevel() ) { $conn->commit( __METHOD__, 'flush' ); @@ -941,6 +946,7 @@ class LoadBalancer { if ( empty( $conns2[$masterIndex] ) ) { continue; } + /** @var DatabaseBase $conn */ foreach ( $conns2[$masterIndex] as $conn ) { if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) { $conn->commit( __METHOD__, 'flush' ); @@ -950,8 +956,59 @@ class LoadBalancer { } /** - * @param $value null - * @return Mixed + * Issue ROLLBACK only on master, only if queries were done on connection + * @since 1.23 + */ + function rollbackMasterChanges() { + // Always 0, but who knows.. :) + $masterIndex = $this->getWriterIndex(); + foreach ( $this->mConns as $conns2 ) { + if ( empty( $conns2[$masterIndex] ) ) { + continue; + } + /** @var DatabaseBase $conn */ + foreach ( $conns2[$masterIndex] as $conn ) { + if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) { + $conn->rollback( __METHOD__, 'flush' ); + } + } + } + } + + /** + * @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. + * @since 1.23 + * @return bool + */ + function hasMasterChanges() { + // Always 0, but who knows.. :) + $masterIndex = $this->getWriterIndex(); + foreach ( $this->mConns as $conns2 ) { + if ( empty( $conns2[$masterIndex] ) ) { + continue; + } + /** @var DatabaseBase $conn */ + foreach ( $conns2[$masterIndex] as $conn ) { + if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) { + return true; + } + } + } + return false; + } + + /** + * @param mixed $value + * @return mixed */ function waitTimeout( $value = null ) { return wfSetVar( $this->mWaitTimeout, $value ); @@ -966,7 +1023,7 @@ class LoadBalancer { /** * Disables/enables lag checks - * @param $mode null + * @param null|bool $mode * @return bool */ function allowLagged( $mode = null ) { @@ -974,6 +1031,7 @@ class LoadBalancer { return $this->mAllowLagged; } $this->mAllowLagged = $mode; + return $this->mAllowLagged; } @@ -984,6 +1042,7 @@ class LoadBalancer { $success = true; foreach ( $this->mConns as $conns2 ) { foreach ( $conns2 as $conns3 ) { + /** @var DatabaseBase[] $conns3 */ foreach ( $conns3 as $conn ) { if ( !$conn->ping() ) { $success = false; @@ -991,12 +1050,13 @@ class LoadBalancer { } } } + return $success; } /** * Call a function with each open connection object - * @param $callback + * @param callable $callback * @param array $params */ function forEachOpenConnection( $callback, $params = array() ) { @@ -1016,16 +1076,29 @@ class LoadBalancer { * May attempt to open connections to slaves on the default DB. If there is * no lag, the maximum lag will be reported as -1. * - * @param string $wiki Wiki ID, or false for the default database - * + * @param bool|string $wiki Wiki ID, or false for the default database * @return array ( host, max lag, index of max lagged host ) */ function getMaxLag( $wiki = false ) { $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 ); @@ -1043,16 +1116,18 @@ class LoadBalancer { $maxIndex = $i; } } + $maxLagInfo = array( $host, $maxLag, $maxIndex ); + $cache->set( $key, $maxLagInfo, 5 ); } - return array( $host, $maxLag, $maxIndex ); + + return $maxLagInfo; } /** * Get lag time for each server * Results are cached for a short time in memcached, and indefinitely in the process cache * - * @param $wiki - * + * @param string|bool $wiki * @return array */ function getLagTimes( $wiki = false ) { @@ -1068,6 +1143,7 @@ class LoadBalancer { $this->mLagTimes = $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki ); } + return $this->mLagTimes; } @@ -1082,8 +1158,7 @@ class LoadBalancer { * function instead of Database::getLag() avoids a fatal error in this * case on many installations. * - * @param $conn DatabaseBase - * + * @param DatabaseBase $conn * @return int */ function safeGetLag( $conn ) { @@ -1112,14 +1187,16 @@ class LoadBalancer { class DBConnRef implements IDatabase { /** @var LoadBalancer */ protected $lb; + /** @var DatabaseBase|null */ protected $conn; - /** @var Array|null */ + + /** @var array|null */ protected $params; /** - * @param $lb LoadBalancer - * @param $conn DatabaseBase|array Connection or (server index, group, wiki ID) array + * @param LoadBalancer $lb + * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array */ public function __construct( LoadBalancer $lb, $conn ) { $this->lb = $lb; @@ -1135,6 +1212,7 @@ class DBConnRef implements IDatabase { list( $db, $groups, $wiki ) = $this->params; $this->conn = $this->lb->getConnection( $db, $groups, $wiki ); } + return call_user_func_array( array( $this->conn, $name ), $arguments ); } |