return $i;
}
- function getRandomNonLagged( $loads ) {
+ function getRandomNonLagged( $loads, $wiki = false ) {
# Unset excessively lagged servers
- $lags = $this->getLagTimes();
+ $lags = $this->getLagTimes( $wiki );
foreach ( $lags as $i => $lag ) {
if ( $i != 0 && isset( $this->mServers[$i]['max lag'] ) ) {
if ( $lag === false ) {
if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) {
$i = $this->pickRandom( $currentLoads );
} else {
- $i = $this->getRandomNonLagged( $currentLoads );
+ $i = $this->getRandomNonLagged( $currentLoads, $wiki );
if ( $i === false && count( $currentLoads ) != 0 ) {
# All slaves lagged. Switch to read-only mode
$wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
$class = 'Database' . ucfirst( $type );
# Create object
- wfDebug( "Connecting to $host...\n" );
+ wfDebug( "Connecting to $host $dbname...\n" );
$db = new $class( $host, $user, $password, $dbname, 1, $flags );
if ( $db->isOpen() ) {
wfDebug( "Connected\n" );
# master (however unlikely that may be), then we can fetch the position from the slave.
$masterConn = $this->getAnyOpenConnection( 0 );
if ( !$masterConn ) {
- $conn = $this->getConnection( DB_SLAVE );
- $pos = $conn->getSlavePos();
- wfDebug( "Master pos fetched from slave\n" );
+ for ( $i = 1; $i < count( $this->mServers ); $i++ ) {
+ $conn = $this->getAnyOpenConnection( $i );
+ if ( $conn ) {
+ wfDebug( "Master pos fetched from slave\n" );
+ return $conn->getSlavePos();
+ }
+ }
} else {
- $pos = $masterConn->getMasterPos();
wfDebug( "Master pos fetched from master\n" );
+ return $masterConn->getMasterPos();
}
- return $pos;
+ return false;
}
/**
}
/**
- * Get the hostname and lag time of the most-lagged slave
- * This is useful for maintenance scripts that need to throttle their updates
+ * Get the hostname and lag time of the most-lagged slave.
+ * This is useful for maintenance scripts that need to throttle their updates.
+ * May attempt to open connections to slaves on the default DB.
*/
function getMaxLag() {
$maxLag = -1;
* Get lag time for each server
* Results are cached for a short time in memcached, and indefinitely in the process cache
*/
- function getLagTimes() {
+ function getLagTimes( $wiki = false ) {
wfProfileIn( __METHOD__ );
if ( !isset( $this->mLagTimes ) ) {
$times[$i] = 0;
} elseif ( false !== ( $conn = $this->getAnyOpenConnection( $i ) ) ) {
$times[$i] = $conn->getLag();
- } elseif ( false !== ( $conn = $this->openConnection( $i ) ) ) {
+ } elseif ( false !== ( $conn = $this->openConnection( $i, $wiki ) ) ) {
$times[$i] = $conn->getLag();
}
}