use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wikimedia\ScopedCallback;
-use Database;
use BagOStuff;
use EmptyBagOStuff;
use WANObjectCache;
use ArrayUtils;
-use DBError;
-use DBAccessError;
-use DBExpectedError;
-use DBUnexpectedError;
-use DBTransactionError;
-use DBTransactionSizeError;
-use DBConnectionError;
use InvalidArgumentException;
use RuntimeException;
use Exception;
class LoadBalancer implements ILoadBalancer {
/** @var array[] Map of (server index => server config array) */
private $mServers;
- /** @var \Database[][][] Map of local/foreignUsed/foreignFree => server index => IDatabase array */
+ /** @var Database[][][] Map of local/foreignUsed/foreignFree => server index => IDatabase[] */
private $mConns;
/** @var float[] Map of (server index => weight) */
private $mLoads;
/** @var LoggerInterface */
protected $perfLogger;
- /** @var \Database Database connection that caused a problem */
+ /** @var Database DB connection object that caused a problem */
private $errorConnection;
/** @var integer The generic (not query grouped) replica DB index (of $mServers) */
private $mReadIndex;
/** @var integer Seconds to cache master server read-only status */
const TTL_CACHE_READONLY = 5;
+ const KEY_LOCAL = 'local';
+ const KEY_FOREIGN_FREE = 'foreignFree';
+ const KEY_FOREIGN_INUSE = 'foreignInUse';
+
public function __construct( array $params ) {
if ( !isset( $params['servers'] ) ) {
throw new InvalidArgumentException( __CLASS__ . ': missing servers parameter' );
$this->mReadIndex = -1;
$this->mConns = [
- 'local' => [],
- 'foreignUsed' => [],
- 'foreignFree' => []
+ self::KEY_LOCAL => [],
+ self::KEY_FOREIGN_INUSE => [],
+ self::KEY_FOREIGN_FREE => []
];
$this->mLoads = [];
$this->mWaitForPos = false;
$host = $this->getServerName( $i );
if ( $lag === false && !is_infinite( $maxServerLag ) ) {
$this->replLogger->error(
- "Server {host} (#$i) is not replicating?", [ 'host' => $host ] );
+ "Server {host} is not replicating?", [ 'host' => $host ] );
unset( $loads[$i] );
} elseif ( $lag > $maxServerLag ) {
$this->replLogger->warning(
- "Server {host} (#$i) has {lag} seconds of lag (>= {maxlag})",
+ "Server {host} has {lag} seconds of lag (>= {maxlag})",
[ 'host' => $host, 'lag' => $lag, 'maxlag' => $maxServerLag ]
);
unset( $loads[$i] );
public function getReaderIndex( $group = false, $domain = false ) {
if ( count( $this->mServers ) == 1 ) {
- # Skip the load balancing if there's only one server
+ // Skip the load balancing if there's only one server
return $this->getWriterIndex();
} elseif ( $group === false && $this->mReadIndex >= 0 ) {
- # Shortcut if generic reader exists already
+ // Shortcut if the generic reader index was already cached
return $this->mReadIndex;
}
- # Find the relevant load array
if ( $group !== false ) {
+ // Use the server weight array for this load group
if ( isset( $this->mGroupLoads[$group] ) ) {
- $nonErrorLoads = $this->mGroupLoads[$group];
+ $loads = $this->mGroupLoads[$group];
} else {
- # No loads for this group, return false and the caller can use some other group
+ // No loads for this group, return false and the caller can use some other group
$this->connLogger->info( __METHOD__ . ": no loads for group $group" );
return false;
}
} else {
- $nonErrorLoads = $this->mLoads;
+ // Use the generic load group
+ $loads = $this->mLoads;
}
- if ( !count( $nonErrorLoads ) ) {
- throw new InvalidArgumentException( "Empty server array given to LoadBalancer" );
+ // Scale the configured load ratios according to each server's load and state
+ $this->getLoadMonitor()->scaleLoads( $loads, $domain );
+
+ // Pick a server to use, accounting for weights, load, lag, and mWaitForPos
+ list( $i, $laggedReplicaMode ) = $this->pickReaderIndex( $loads, $domain );
+ if ( $i === false ) {
+ // Replica DB connection unsuccessful
+ return false;
}
- # Scale the configured load ratios according to the dynamic load if supported
- $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $domain );
+ if ( $this->mWaitForPos && $i != $this->getWriterIndex() ) {
+ // Before any data queries are run, wait for the server to catch up to the
+ // specified position. This is used to improve session consistency. Note that
+ // when LoadBalancer::waitFor() sets mWaitForPos, the waiting triggers here,
+ // so update laggedReplicaMode as needed for consistency.
+ if ( !$this->doWait( $i ) ) {
+ $laggedReplicaMode = true;
+ }
+ }
- $laggedReplicaMode = false;
+ if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
+ // Cache the generic reader index for future ungrouped DB_REPLICA handles
+ $this->mReadIndex = $i;
+ // Record if the generic reader index is in "lagged replica DB" mode
+ if ( $laggedReplicaMode ) {
+ $this->laggedReplicaMode = true;
+ }
+ }
+
+ $serverName = $this->getServerName( $i );
+ $this->connLogger->debug( __METHOD__ . ": using server $serverName for group '$group'" );
+
+ return $i;
+ }
- # No server found yet
+ /**
+ * @param array $loads List of server weights
+ * @param string|bool $domain
+ * @return array (reader index, lagged replica mode) or false on failure
+ */
+ private function pickReaderIndex( array $loads, $domain = false ) {
+ if ( !count( $loads ) ) {
+ throw new InvalidArgumentException( "Empty server array given to LoadBalancer" );
+ }
+
+ /** @var $i int|bool Index of selected server */
$i = false;
- # First try quickly looking through the available servers for a server that
- # meets our criteria
- $currentLoads = $nonErrorLoads;
+ /** @var $laggedReplicaMode bool Whether server is considered lagged */
+ $laggedReplicaMode = false;
+
+ // Quickly look through the available servers for a server that meets criteria...
+ $currentLoads = $loads;
while ( count( $currentLoads ) ) {
if ( $this->mAllowLagged || $laggedReplicaMode ) {
$i = ArrayUtils::pickRandom( $currentLoads );
} else {
$i = false;
if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
- # ChronologyProtecter causes mWaitForPos to be set via sessions.
- # This triggers doWait() after connect, so it's especially good to
- # avoid lagged servers so as to avoid just blocking in that method.
+ // ChronologyProtecter sets mWaitForPos for session consistency.
+ // This triggers doWait() after connect, so it's especially good to
+ // avoid lagged servers so as to avoid excessive delay in that method.
$ago = microtime( true ) - $this->mWaitForPos->asOfTime();
- # Aim for <= 1 second of waiting (being too picky can backfire)
+ // Aim for <= 1 second of waiting (being too picky can backfire)
$i = $this->getRandomNonLagged( $currentLoads, $domain, $ago + 1 );
}
if ( $i === false ) {
- # Any server with less lag than it's 'max lag' param is preferable
+ // Any server with less lag than it's 'max lag' param is preferable
$i = $this->getRandomNonLagged( $currentLoads, $domain );
}
if ( $i === false && count( $currentLoads ) != 0 ) {
- # All replica DBs lagged. Switch to read-only mode
+ // All replica DBs lagged. Switch to read-only mode
$this->replLogger->error( "All replica DBs lagged. Switch to read-only mode" );
$i = ArrayUtils::pickRandom( $currentLoads );
$laggedReplicaMode = true;
}
if ( $i === false ) {
- # pickRandom() returned false
- # This is permanent and means the configuration or the load monitor
- # wants us to return false.
+ // pickRandom() returned false.
+ // This is permanent and means the configuration or the load monitor
+ // wants us to return false.
$this->connLogger->debug( __METHOD__ . ": pickRandom() returned false" );
- return false;
+ return [ false, false ];
}
$serverName = $this->getServerName( $i );
$conn = $this->openConnection( $i, $domain );
if ( !$conn ) {
$this->connLogger->warning( __METHOD__ . ": Failed connecting to $i/$domain" );
- unset( $nonErrorLoads[$i] );
- unset( $currentLoads[$i] );
+ unset( $currentLoads[$i] ); // avoid this server next iteration
$i = false;
continue;
}
$this->reuseConnection( $conn );
}
- # Return this server
+ // Return this server
break;
}
- # If all servers were down, quit now
- if ( !count( $nonErrorLoads ) ) {
+ // If all servers were down, quit now
+ if ( !count( $currentLoads ) ) {
$this->connLogger->error( "All servers down" );
}
- if ( $i !== false ) {
- # Replica DB connection successful.
- # Wait for the session master pos for a short time.
- if ( $this->mWaitForPos && $i > 0 ) {
- $this->doWait( $i );
- }
- if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
- $this->mReadIndex = $i;
- # Record if the generic reader index is in "lagged replica DB" mode
- if ( $laggedReplicaMode ) {
- $this->laggedReplicaMode = true;
- }
- }
- $serverName = $this->getServerName( $i );
- $this->connLogger->debug(
- __METHOD__ . ": using server $serverName for group '$group'" );
- }
-
- return $i;
+ return [ $i, $laggedReplicaMode ];
}
public function waitFor( $pos ) {
$oldPos = $this->mWaitForPos;
- $this->mWaitForPos = $pos;
-
- // If a generic reader connection was already established, then wait now
- $i = $this->mReadIndex;
- if ( $i > 0 ) {
- if ( !$this->doWait( $i ) ) {
- $this->laggedReplicaMode = true;
+ try {
+ $this->mWaitForPos = $pos;
+ // If a generic reader connection was already established, then wait now
+ $i = $this->mReadIndex;
+ if ( $i > 0 ) {
+ if ( !$this->doWait( $i ) ) {
+ $this->laggedReplicaMode = true;
+ }
}
+ } finally {
+ // Restore the older position if it was higher since this is used for lag-protection
+ $this->setWaitForPositionIfHigher( $oldPos );
}
-
- // Restore the older position if it was higher
- $this->setWaitForPositionIfHigher( $oldPos );
}
public function waitForOne( $pos, $timeout = null ) {
$oldPos = $this->mWaitForPos;
- $this->mWaitForPos = $pos;
+ try {
+ $this->mWaitForPos = $pos;
- $i = $this->mReadIndex;
- if ( $i <= 0 ) {
- // Pick a generic replica DB if there isn't one yet
- $readLoads = $this->mLoads;
- unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only
- $readLoads = array_filter( $readLoads ); // with non-zero load
- $i = ArrayUtils::pickRandom( $readLoads );
- }
+ $i = $this->mReadIndex;
+ if ( $i <= 0 ) {
+ // Pick a generic replica DB if there isn't one yet
+ $readLoads = $this->mLoads;
+ unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only
+ $readLoads = array_filter( $readLoads ); // with non-zero load
+ $i = ArrayUtils::pickRandom( $readLoads );
+ }
- if ( $i > 0 ) {
- $ok = $this->doWait( $i, true, $timeout );
- } else {
- $ok = true; // no applicable loads
+ if ( $i > 0 ) {
+ $ok = $this->doWait( $i, true, $timeout );
+ } else {
+ $ok = true; // no applicable loads
+ }
+ } finally {
+ # Restore the old position, as this is not used for lag-protection but for throttling
+ $this->mWaitForPos = $oldPos;
}
- // Restore the older position if it was higher
- $this->setWaitForPositionIfHigher( $oldPos );
-
return $ok;
}
public function waitForAll( $pos, $timeout = null ) {
$oldPos = $this->mWaitForPos;
- $this->mWaitForPos = $pos;
- $serverCount = count( $this->mServers );
+ try {
+ $this->mWaitForPos = $pos;
+ $serverCount = count( $this->mServers );
- $ok = true;
- for ( $i = 1; $i < $serverCount; $i++ ) {
- if ( $this->mLoads[$i] > 0 ) {
- $ok = $this->doWait( $i, true, $timeout ) && $ok;
+ $ok = true;
+ for ( $i = 1; $i < $serverCount; $i++ ) {
+ if ( $this->mLoads[$i] > 0 ) {
+ $ok = $this->doWait( $i, true, $timeout ) && $ok;
+ }
}
+ } finally {
+ # Restore the old position, as this is not used for lag-protection but for throttling
+ $this->mWaitForPos = $oldPos;
}
- // Restore the older position if it was higher
- $this->setWaitForPositionIfHigher( $oldPos );
-
return $ok;
}
}
$domain = $conn->getDomainID();
- if ( !isset( $this->mConns['foreignUsed'][$serverIndex][$domain] ) ) {
+ if ( !isset( $this->mConns[self::KEY_FOREIGN_INUSE][$serverIndex][$domain] ) ) {
throw new InvalidArgumentException( __METHOD__ .
": connection $serverIndex/$domain not found; it may have already been freed." );
- } elseif ( $this->mConns['foreignUsed'][$serverIndex][$domain] !== $conn ) {
+ } elseif ( $this->mConns[self::KEY_FOREIGN_INUSE][$serverIndex][$domain] !== $conn ) {
throw new InvalidArgumentException( __METHOD__ .
": connection $serverIndex/$domain mismatched; it may have already been freed." );
}
$conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
if ( $refCount <= 0 ) {
- $this->mConns['foreignFree'][$serverIndex][$domain] = $conn;
- unset( $this->mConns['foreignUsed'][$serverIndex][$domain] );
- if ( !$this->mConns['foreignUsed'][$serverIndex] ) {
- unset( $this->mConns[ 'foreignUsed' ][$serverIndex] ); // clean up
+ $this->mConns[self::KEY_FOREIGN_FREE][$serverIndex][$domain] = $conn;
+ unset( $this->mConns[self::KEY_FOREIGN_INUSE][$serverIndex][$domain] );
+ if ( !$this->mConns[self::KEY_FOREIGN_INUSE][$serverIndex] ) {
+ unset( $this->mConns[ self::KEY_FOREIGN_INUSE ][$serverIndex] ); // clean up
}
$this->connLogger->debug( __METHOD__ . ": freed connection $serverIndex/$domain" );
} else {
if ( $domain !== false ) {
$conn = $this->openForeignConnection( $i, $domain );
- } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
- $conn = $this->mConns['local'][$i][0];
+ } elseif ( isset( $this->mConns[self::KEY_LOCAL][$i][0] ) ) {
+ $conn = $this->mConns[self::KEY_LOCAL][$i][0];
} else {
if ( !isset( $this->mServers[$i] ) || !is_array( $this->mServers[$i] ) ) {
throw new InvalidArgumentException( "No server with index '$i'." );
$serverName = $this->getServerName( $i );
if ( $conn->isOpen() ) {
$this->connLogger->debug( "Connected to database $i at '$serverName'." );
- $this->mConns['local'][$i][0] = $conn;
+ $this->mConns[self::KEY_LOCAL][$i][0] = $conn;
} else {
$this->connLogger->warning( "Failed to connect to database $i at '$serverName'." );
$this->errorConnection = $conn;
$dbName = $domainInstance->getDatabase();
$prefix = $domainInstance->getTablePrefix();
- if ( isset( $this->mConns['foreignUsed'][$i][$domain] ) ) {
- // Reuse an already-used connection
- $conn = $this->mConns['foreignUsed'][$i][$domain];
+ if ( isset( $this->mConns[self::KEY_FOREIGN_INUSE][$i][$domain] ) ) {
+ // Reuse an in-use connection for the same domain that is not in-use
+ $conn = $this->mConns[self::KEY_FOREIGN_INUSE][$i][$domain];
$this->connLogger->debug( __METHOD__ . ": reusing connection $i/$domain" );
- } elseif ( isset( $this->mConns['foreignFree'][$i][$domain] ) ) {
- // Reuse a free connection for the same domain
- $conn = $this->mConns['foreignFree'][$i][$domain];
- unset( $this->mConns['foreignFree'][$i][$domain] );
- $this->mConns['foreignUsed'][$i][$domain] = $conn;
+ } elseif ( isset( $this->mConns[self::KEY_FOREIGN_FREE][$i][$domain] ) ) {
+ // Reuse a free connection for the same domain that is not in-use
+ $conn = $this->mConns[self::KEY_FOREIGN_FREE][$i][$domain];
+ unset( $this->mConns[self::KEY_FOREIGN_FREE][$i][$domain] );
+ $this->mConns[self::KEY_FOREIGN_INUSE][$i][$domain] = $conn;
$this->connLogger->debug( __METHOD__ . ": reusing free connection $i/$domain" );
- } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
+ } elseif ( !empty( $this->mConns[self::KEY_FOREIGN_FREE][$i] ) ) {
// Reuse a connection from another domain
- $conn = reset( $this->mConns['foreignFree'][$i] );
- $oldDomain = key( $this->mConns['foreignFree'][$i] );
+ $conn = reset( $this->mConns[self::KEY_FOREIGN_FREE][$i] );
+ $oldDomain = key( $this->mConns[self::KEY_FOREIGN_FREE][$i] );
// The empty string as a DB name means "don't care".
// DatabaseMysqlBase::open() already handle this on connection.
if ( strlen( $dbName ) && !$conn->selectDB( $dbName ) ) {
$conn = false;
} else {
$conn->tablePrefix( $prefix );
- unset( $this->mConns['foreignFree'][$i][$oldDomain] );
- $this->mConns['foreignUsed'][$i][$domain] = $conn;
+ unset( $this->mConns[self::KEY_FOREIGN_FREE][$i][$oldDomain] );
+ $this->mConns[self::KEY_FOREIGN_INUSE][$i][$domain] = $conn;
$this->connLogger->debug( __METHOD__ .
": reusing free connection from $oldDomain for $domain" );
}
$conn = false;
} else {
$conn->tablePrefix( $prefix );
- $this->mConns['foreignUsed'][$i][$domain] = $conn;
+ $this->mConns[self::KEY_FOREIGN_INUSE][$i][$domain] = $conn;
$this->connLogger->debug( __METHOD__ . ": opened new connection for $i/$domain" );
}
}
return ( $name != '' ) ? $name : 'localhost';
}
+ /**
+ * @deprecated Since 1.30, no alternative
+ */
public function getServerInfo( $i ) {
+ wfDeprecated( __METHOD__, '1.30' );
if ( isset( $this->mServers[$i] ) ) {
return $this->mServers[$i];
} else {
}
}
+ /**
+ * @deprecated Since 1.30, construct new object
+ */
public function setServerInfo( $i, array $serverInfo ) {
+ wfDeprecated( __METHOD__, '1.30' );
$this->mServers[$i] = $serverInfo;
}
} );
$this->mConns = [
- 'local' => [],
- 'foreignFree' => [],
- 'foreignUsed' => [],
+ self::KEY_LOCAL => [],
+ self::KEY_FOREIGN_FREE => [],
+ self::KEY_FOREIGN_INUSE => [],
];
$this->connsOpened = 0;
}
// This happens if onTransactionIdle() callbacks leave callbacks on *another* DB
// (which finished its callbacks already). Warn and recover in this case. Let the
// callbacks run in the final commitMasterChanges() in LBFactory::shutdown().
- $this->queryLogger->error( __METHOD__ . ": found writes/callbacks pending." );
+ $this->queryLogger->info( __METHOD__ . ": found writes/callbacks pending." );
return;
} elseif ( $conn->trxLevel() ) {
// This happens for single-DB setups where DB_REPLICA uses the master DB,
}
public function setDomainPrefix( $prefix ) {
- if ( $this->mConns['foreignUsed'] ) {
- // Do not switch connections to explicit foreign domains unless marked as free
- $domains = [];
- foreach ( $this->mConns['foreignUsed'] as $i => $connsByDomain ) {
- $domains = array_merge( $domains, array_keys( $connsByDomain ) );
+ // Find connections to explicit foreign domains still marked as in-use...
+ $domainsInUse = [];
+ $this->forEachOpenConnection( function ( IDatabase $conn ) use ( &$domainsInUse ) {
+ // Once reuseConnection() is called on a handle, its reference count goes from 1 to 0.
+ // Until then, it is still in use by the caller (explicitly or via DBConnRef scope).
+ if ( $conn->getLBInfo( 'foreignPoolRefCount' ) > 0 ) {
+ $domainsInUse[] = $conn->getDomainID();
}
- $domains = implode( ', ', $domains );
+ } );
+
+ // Do not switch connections to explicit foreign domains unless marked as safe
+ if ( $domainsInUse ) {
+ $domains = implode( ', ', $domainsInUse );
throw new DBUnexpectedError( null,
"Foreign domain connections are still in use ($domains)." );
}