define( 'DB_READ', -1 );
define( 'DB_WRITE', -2 );
+
+# Scale polling time so that under overload conditions, the database server
+# receives a SHOW STATUS query at an average interval of this many microseconds
+define( 'AVG_STATUS_POLL', 2000 );
+
+
/**
* Database load balancing object
*
class LoadBalancer {
/* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
/* private */ var $mFailFunction, $mErrorConnection;
- /* private */ var $mForce, $mReadIndex, $mLastIndex;
+ /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
/* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
- /* private */ var $mLaggedSlaveMode;
+ /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
function LoadBalancer()
{
$this->mForce = -1;
$this->mLastIndex = -1;
$this->mErrorConnection = false;
+ $this->mAllowLag = false;
}
function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
return false;
}
- $sum = 0;
- foreach ( $weights as $w ) {
- $sum += $w;
- }
-
+ $sum = array_sum( $weights );
if ( $sum == 0 ) {
# No loads on any of them
- # Just pick one at random
- foreach ( $weights as $i => $w ) {
- $weights[$i] = 1;
- }
+ # In previous versions, this triggered an unweighted random selection,
+ # but this feature has been removed as of April 2006 to allow for strict
+ # separation of query groups.
+ return false;
}
$max = mt_getrandmax();
$rand = mt_rand(0, $max) / $max * $sum;
}
}
-
# Find out if all the slaves with non-zero load are lagged
$sum = 0;
foreach ( $loads as $load ) {
# Do NOT use the master
# Instead, this function will return false, triggering read-only mode,
# and a lagged slave will be used instead.
- unset ( $loads[0] );
+ return false;
}
if ( count( $loads ) == 0 ) {
*
* Side effect: opens connections to databases
*/
- function getReaderIndex()
- {
- global $wgMaxLag, $wgReadOnly, $wgDBClusterTimeout;
+ function getReaderIndex() {
+ global $wgReadOnly, $wgDBClusterTimeout;
$fname = 'LoadBalancer::getReaderIndex';
wfProfileIn( $fname );
$done = false;
$totalElapsed = 0;
do {
- if ( $wgReadOnly ) {
+ if ( $wgReadOnly or $this->mAllowLagged ) {
$i = $this->pickRandom( $loads );
} else {
$i = $this->getRandomNonLagged( $loads );
}
$serverIndex = $i;
if ( $i !== false ) {
- wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
+ wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
$this->openConnection( $i );
if ( !$this->isOpen( $i ) ) {
- wfDebug( "Failed\n" );
+ wfDebug( "$fname: Failed\n" );
unset( $loads[$i] );
$sleepTime = 0;
} else {
- $status = $this->mConnections[$i]->getStatus();
+ $status = $this->mConnections[$i]->getStatus("Thread%");
if ( isset( $this->mServers[$i]['max threads'] ) &&
$status['Threads_running'] > $this->mServers[$i]['max threads'] )
{
- # Slave is lagged, wait for a while
- $sleepTime = 5000 * $status['Threads_connected'];
+ # Too much load, back off and wait for a while.
+ # The sleep time is scaled by the number of threads connected,
+ # to produce a roughly constant global poll rate.
+ $sleepTime = AVG_STATUS_POLL * $status['Threads_connected'];
# If we reach the timeout and exit the loop, don't use it
$i = false;
}
if ( $sleepTime ) {
$totalElapsed += $sleepTime;
- $x = "{$this->mServers[$serverIndex]['host']} $sleepTime [$serverIndex]";
+ $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
wfProfileIn( "$fname-sleep $x" );
usleep( $sleepTime );
wfProfileOut( "$fname-sleep $x" );
}
} while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
+ if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
+ $this->mErrorConnection = false;
+ $this->mLastError = 'All servers busy';
+ }
+
if ( $i !== false && $this->isOpen( $i ) ) {
# Wait for the session master pos for a short time
if ( $this->mWaitForFile ) {
*/
function &getConnection( $i, $fail = true, $groups = array() )
{
+ global $wgDBtype;
$fname = 'LoadBalancer::getConnection';
wfProfileIn( $fname );
+
# Query groups
- $groupIndex = false;
- foreach ( $groups as $group ) {
- $groupIndex = $this->getGroupIndex( $group );
+ if ( !is_array( $groups ) ) {
+ $groupIndex = $this->getGroupIndex( $groups, $i );
if ( $groupIndex !== false ) {
$i = $groupIndex;
- break;
+ }
+ } else {
+ foreach ( $groups as $group ) {
+ $groupIndex = $this->getGroupIndex( $group, $i );
+ if ( $groupIndex !== false ) {
+ $i = $groupIndex;
+ break;
+ }
}
}
+ # For now, only go through all this for mysql databases
+ if ($wgDBtype != 'mysql') {
+ $i = $this->getWriterIndex();
+ }
# Operation-based index
- if ( $i == DB_SLAVE ) {
+ elseif ( $i == DB_SLAVE ) {
$i = $this->getReaderIndex();
} elseif ( $i == DB_MASTER ) {
$i = $this->getWriterIndex();
* Open a connection to the server given by the specified index
* Index must be an actual index into the array
* Returns success
- * @private
+ * @access private
*/
function openConnection( $i, $fail = false ) {
$fname = 'LoadBalancer::openConnection';
/**
* Test if the specified index represents an open connection
- * @private
+ * @access private
*/
function isOpen( $index ) {
if( !is_integer( $index ) ) {
/**
* Really opens a connection
- * @private
+ * @access private
*/
function reallyOpenConnection( &$server ) {
if( !is_array( $server ) ) {
- wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
+ throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
}
extract( $server );
}
# Create object
- return new $class( $host, $user, $password, $dbname, 1, $flags );
+ $db = new $class( $host, $user, $password, $dbname, 1, $flags );
+ $db->setLBInfo( $server );
+ return $db;
}
function reportConnectionError( &$conn )
if ( !$reporting ) {
$reporting = true;
if ( !is_object( $conn ) ) {
+ // No last connection, probably due to all servers being too busy
$conn = new Database;
- }
- if ( $this->mFailFunction ) {
- $conn->failFunction( $this->mFailFunction );
+ if ( $this->mFailFunction ) {
+ $conn->failFunction( $this->mFailFunction );
+ $conn->reportConnectionError( $this->mLastError );
+ } else {
+ // If all servers were busy, mLastError will contain something sensible
+ throw new DBConnectionError( $conn, $this->mLastError );
+ }
} else {
- $conn->failFunction( false );
+ if ( $this->mFailFunction ) {
+ $conn->failFunction( $this->mFailFunction );
+ } else {
+ $conn->failFunction( false );
+ }
+ $server = $conn->getProperty( 'mServer' );
+ $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
}
- $conn->reportConnectionError();
$reporting = false;
}
wfProfileOut( $fname );
}
- function getWriterIndex()
- {
+ function getWriterIndex() {
return 0;
}
- function force( $i )
- {
+ /**
+ * Force subsequent calls to getConnection(DB_SLAVE) to return the
+ * given index. Set to -1 to restore the original load balancing
+ * behaviour. I thought this was a good idea when I originally
+ * wrote this class, but it has never been used.
+ */
+ function force( $i ) {
$this->mForce = $i;
}
- function haveIndex( $i )
- {
+ /**
+ * Returns true if the specified index is a valid server index
+ */
+ function haveIndex( $i ) {
return array_key_exists( $i, $this->mServers );
}
+ /**
+ * Returns true if the specified index is valid and has non-zero load
+ */
+ function isNonZeroLoad( $i ) {
+ return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
+ }
+
/**
* Get the number of defined servers (not the number of open connections)
*/
return $this->mLaggedSlaveMode;
}
+ /* Disables/enables lag checks */
+ function allowLagged($mode=null) {
+ if ($mode===null)
+ return $this->mAllowLagged;
+ $this->mAllowLagged=$mode;
+ }
+
function pingAll() {
$success = true;
foreach ( $this->mConnections as $i => $conn ) {
* Results are cached for a short time in memcached
*/
function getLagTimes() {
+ global $wgDBname;
+
$expiry = 5;
$requestRate = 10;
global $wgMemc;
- $times = $wgMemc->get( 'lag_times' );
+ $times = $wgMemc->get( "$wgDBname:lag_times" );
if ( $times ) {
# Randomly recache with probability rising over $expiry
$elapsed = time() - $times['timestamp'];
$times = array();
foreach ( $this->mServers as $i => $conn ) {
- if ( $this->openConnection( $i ) ) {
+ if ($i==0) { # Master
+ $times[$i] = 0;
+ } elseif ( $this->openConnection( $i ) ) {
$times[$i] = $this->mConnections[$i]->getLag();
}
}
# Add a timestamp key so we know when it was cached
$times['timestamp'] = time();
- $wgMemc->set( 'lag_times', $times, $expiry );
+ $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
# But don't give the timestamp to the caller
unset($times['timestamp']);