/**
* @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.
* @throws MWException
*/
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;
public function &getConnection( $i, $groups = array(), $wiki = false ) {
wfProfileIn( __METHOD__ );
- if ( $i == DB_LAST ) {
- wfProfileOut( __METHOD__ );
- throw new MWException( 'Attempt to call ' . __METHOD__ .
- ' with deprecated server index DB_LAST' );
- } elseif ( $i === null || $i === false ) {
+ if ( $i === null || $i === false ) {
wfProfileOut( __METHOD__ );
throw new MWException( 'Attempt to call ' . __METHOD__ .
' with invalid server index' );
);
}
- /**
- * 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.
$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 );
$maxIndex = $i;
}
}
+ $maxLagInfo = array( $host, $maxLag, $maxIndex );
+ $cache->set( $key, $maxLagInfo, 5 );
}
- return array( $host, $maxLag, $maxIndex );
+ return $maxLagInfo;
}
/**