wiki.techinc.nl/includes/LoadBalancer.php

611 lines
15 KiB
PHP
Raw Normal View History

<?php
/**
*
* @package MediaWiki
*/
2004-01-25 13:27:53 +00:00
/**
* Depends on the database object
*/
require_once( 'Database.php' );
# Valid database indexes
# Operation-based indexes
define( 'DB_SLAVE', -1 ); # Read from the slave (or only server)
define( 'DB_MASTER', -2 ); # Write to master (or only server)
define( 'DB_LAST', -3 ); # Whatever database was used last
# Obsolete aliases
define( 'DB_READ', -1 );
define( 'DB_WRITE', -2 );
/**
* Database load balancing object
*
* @todo document
* @package MediaWiki
*/
2004-01-25 13:27:53 +00:00
class LoadBalancer {
/* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
2005-08-14 04:42:55 +00:00
/* private */ var $mFailFunction, $mErrorConnection;
/* private */ var $mForce, $mReadIndex, $mLastIndex;
2005-01-15 10:13:36 +00:00
/* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
/* private */ var $mLaggedSlaveMode;
2004-01-25 13:27:53 +00:00
function LoadBalancer()
{
$this->mServers = array();
$this->mConnections = array();
$this->mFailFunction = false;
$this->mReadIndex = -1;
$this->mForce = -1;
$this->mLastIndex = -1;
2005-08-14 04:42:55 +00:00
$this->mErrorConnection = false;
2004-01-25 13:27:53 +00:00
}
2005-01-15 10:13:36 +00:00
function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
2004-01-25 13:27:53 +00:00
{
$lb = new LoadBalancer;
2005-05-06 03:48:15 +00:00
$lb->initialise( $servers, $failFunction, $waitTimeout );
2004-01-25 13:27:53 +00:00
return $lb;
}
2005-01-15 10:13:36 +00:00
function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
2004-01-25 13:27:53 +00:00
{
$this->mServers = $servers;
$this->mFailFunction = $failFunction;
$this->mReadIndex = -1;
$this->mWriteIndex = -1;
$this->mForce = -1;
$this->mConnections = array();
$this->mLastIndex = 1;
$this->mLoads = array();
$this->mWaitForFile = false;
$this->mWaitForPos = false;
2005-01-15 10:13:36 +00:00
$this->mWaitTimeout = $waitTimeout;
$this->mLaggedSlaveMode = false;
foreach( $servers as $i => $server ) {
$this->mLoads[$i] = $server['load'];
if ( isset( $server['groupLoads'] ) ) {
foreach ( $server['groupLoads'] as $group => $ratio ) {
if ( !isset( $this->mGroupLoads[$group] ) ) {
$this->mGroupLoads[$group] = array();
}
$this->mGroupLoads[$group][$i] = $ratio;
}
}
}
2004-01-25 13:27:53 +00:00
}
/**
* Given an array of non-normalised probabilities, this function will select
* an element and return the appropriate key
*/
2004-01-25 13:27:53 +00:00
function pickRandom( $weights )
{
if ( !is_array( $weights ) || count( $weights ) == 0 ) {
return false;
}
$sum = 0;
foreach ( $weights as $w ) {
$sum += $w;
}
2005-06-01 06:18:49 +00:00
if ( $sum == 0 ) {
# No loads on any of them
# Just pick one at random
foreach ( $weights as $i => $w ) {
$weights[$i] = 1;
}
}
2004-06-22 08:54:26 +00:00
$max = mt_getrandmax();
$rand = mt_rand(0, $max) / $max * $sum;
2004-01-25 13:27:53 +00:00
$sum = 0;
foreach ( $weights as $i => $w ) {
$sum += $w;
if ( $sum >= $rand ) {
break;
}
}
return $i;
}
2005-06-01 06:18:49 +00:00
function getRandomNonLagged( $loads ) {
# Unset excessively lagged servers
$lags = $this->getLagTimes();
foreach ( $lags as $i => $lag ) {
if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
unset( $loads[$i] );
}
}
# Find out if all the slaves with non-zero load are lagged
$sum = 0;
foreach ( $loads as $load ) {
$sum += $load;
}
if ( $sum == 0 ) {
# No appropriate DB servers except maybe the master and some slaves with zero load
# Do NOT use the master
# Instead, this function will return false, triggering read-only mode,
2005-06-01 06:18:49 +00:00
# and a lagged slave will be used instead.
unset ( $loads[0] );
}
if ( count( $loads ) == 0 ) {
return false;
}
#wfDebugLog( 'connect', var_export( $loads, true ) );
2005-06-01 06:18:49 +00:00
# Return a random representative of the remainder
return $this->pickRandom( $loads );
}
2005-06-25 13:48:02 +00:00
/**
* Get the index of the reader connection, which may be a slave
* This takes into account load ratios and lag times. It should
2005-06-25 13:48:02 +00:00
* always return a consistent index during a given invocation
*
* Side effect: opens connections to databases
*/
function getReaderIndex()
2004-01-25 13:27:53 +00:00
{
2005-06-19 02:39:43 +00:00
global $wgMaxLag, $wgReadOnly, $wgDBClusterTimeout;
2005-06-01 06:18:49 +00:00
$fname = 'LoadBalancer::getReaderIndex';
wfProfileIn( $fname );
$i = false;
2004-01-25 13:27:53 +00:00
if ( $this->mForce >= 0 ) {
$i = $this->mForce;
2004-01-25 13:27:53 +00:00
} else {
if ( $this->mReadIndex >= 0 ) {
$i = $this->mReadIndex;
2004-01-25 13:27:53 +00:00
} else {
# $loads is $this->mLoads except with elements knocked out if they
# don't work
$loads = $this->mLoads;
2005-06-01 06:18:49 +00:00
$done = false;
$totalElapsed = 0;
2004-01-25 13:27:53 +00:00
do {
2005-06-01 06:18:49 +00:00
if ( $wgReadOnly ) {
$i = $this->pickRandom( $loads );
} else {
$i = $this->getRandomNonLagged( $loads );
if ( $i === false && count( $loads ) != 0 ) {
# All slaves lagged. Switch to read-only mode
$wgReadOnly = wfMsgNoDB( 'readonly_lag' );
$i = $this->pickRandom( $loads );
}
}
$serverIndex = $i;
2004-01-25 13:27:53 +00:00
if ( $i !== false ) {
wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
$this->openConnection( $i );
2004-07-24 08:33:37 +00:00
if ( !$this->isOpen( $i ) ) {
wfDebug( "Failed\n" );
2004-01-25 13:27:53 +00:00
unset( $loads[$i] );
2005-06-01 06:18:49 +00:00
$sleepTime = 0;
} else {
2005-06-01 06:18:49 +00:00
$status = $this->mConnections[$i]->getStatus();
if ( isset( $this->mServers[$i]['max threads'] ) &&
$status['Threads_running'] > $this->mServers[$i]['max threads'] )
2005-06-01 06:18:49 +00:00
{
# Slave is lagged, wait for a while
$sleepTime = 5000 * $status['Threads_connected'];
# If we reach the timeout and exit the loop, don't use it
$i = false;
} else {
$done = true;
$sleepTime = 0;
}
2004-01-25 13:27:53 +00:00
}
2005-06-01 06:18:49 +00:00
} else {
$sleepTime = 500000;
}
if ( $sleepTime ) {
$totalElapsed += $sleepTime;
$x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
wfProfileIn( "$fname-sleep $x" );
2005-06-01 06:18:49 +00:00
usleep( $sleepTime );
wfProfileOut( "$fname-sleep $x" );
2004-01-25 13:27:53 +00:00
}
2005-06-19 02:39:43 +00:00
} while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
2004-07-24 08:33:37 +00:00
2005-06-01 06:18:49 +00:00
if ( $i !== false && $this->isOpen( $i ) ) {
2005-06-19 02:39:43 +00:00
# Wait for the session master pos for a short time
if ( $this->mWaitForFile ) {
if ( !$this->doWait( $i ) ) {
$this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
}
}
2005-06-25 13:48:02 +00:00
if ( $i !== false ) {
2005-06-19 02:39:43 +00:00
$this->mReadIndex = $i;
}
} else {
$i = false;
2004-01-25 13:27:53 +00:00
}
}
}
wfProfileOut( $fname );
return $i;
2004-01-25 13:27:53 +00:00
}
/**
* Get a random server to use in a query group
*/
function getGroupIndex( $group ) {
if ( isset( $this->mGroupLoads[$group] ) ) {
$i = $this->pickRandom( $this->mGroupLoads[$group] );
} else {
$i = false;
}
wfDebug( "Query group $group => $i\n" );
return $i;
}
/**
* 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
*/
function waitFor( $file, $pos ) {
$fname = 'LoadBalancer::waitFor';
wfProfileIn( $fname );
wfDebug( "User master pos: $file $pos\n" );
$this->mWaitForFile = false;
$this->mWaitForPos = false;
if ( count( $this->mServers ) > 1 ) {
$this->mWaitForFile = $file;
$this->mWaitForPos = $pos;
$i = $this->mReadIndex;
if ( $i > 0 ) {
if ( !$this->doWait( $i ) ) {
$this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
2005-01-15 10:13:36 +00:00
$this->mLaggedSlaveMode = true;
}
}
}
wfProfileOut( $fname );
}
/**
* Wait for a given slave to catch up to the master pos stored in $this
*/
function doWait( $index ) {
global $wgMemc;
$retVal = false;
# Debugging hacks
if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
return false;
} elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
return true;
}
$key = 'masterpos:' . $index;
$memcPos = $wgMemc->get( $key );
if ( $memcPos ) {
list( $file, $pos ) = explode( ' ', $memcPos );
# If the saved position is later than the requested position, return now
if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
$retVal = true;
}
}
if ( !$retVal && $this->isOpen( $index ) ) {
$conn =& $this->mConnections[$index];
wfDebug( "Waiting for slave #$index to catch up...\n" );
2005-01-15 10:13:36 +00:00
$result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, $this->mWaitTimeout );
if ( $result == -1 || is_null( $result ) ) {
# Timed out waiting for slave, use master instead
wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
$retVal = false;
} else {
$retVal = true;
wfDebug( "Done\n" );
}
}
2005-06-19 02:39:43 +00:00
return $retVal;
}
/**
* Get a connection by index
*/
function &getConnection( $i, $fail = true, $groups = array() )
2004-01-25 13:27:53 +00:00
{
$fname = 'LoadBalancer::getConnection';
wfProfileIn( $fname );
# Query groups
$groupIndex = false;
foreach ( $groups as $group ) {
$groupIndex = $this->getGroupIndex( $group );
if ( $groupIndex !== false ) {
$i = $groupIndex;
break;
}
}
# Operation-based index
if ( $i == DB_SLAVE ) {
$i = $this->getReaderIndex();
} elseif ( $i == DB_MASTER ) {
$i = $this->getWriterIndex();
} elseif ( $i == DB_LAST ) {
# Just use $this->mLastIndex, which should already be set
$i = $this->mLastIndex;
if ( $i === -1 ) {
# Oh dear, not set, best to use the writer for safety
wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
$i = $this->getWriterIndex();
2004-01-25 13:27:53 +00:00
}
}
2005-08-14 04:42:55 +00:00
# Couldn't find a working server in getReaderIndex()?
if ( $i === false ) {
$this->reportConnectionError( $this->mErrorConnection );
}
# Now we have an explicit index into the servers array
$this->openConnection( $i, $fail );
wfProfileOut( $fname );
return $this->mConnections[$i];
}
/**
* Open a connection to the server given by the specified index
* Index must be an actual index into the array
* Returns success
* @private
*/
function openConnection( $i, $fail = false ) {
$fname = 'LoadBalancer::openConnection';
wfProfileIn( $fname );
$success = true;
if ( !$this->isOpen( $i ) ) {
$this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
}
if ( !$this->isOpen( $i ) ) {
wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
if ( $fail ) {
$this->reportConnectionError( $this->mConnections[$i] );
}
2005-08-14 04:42:55 +00:00
$this->mErrorConnection = $this->mConnections[$i];
$this->mConnections[$i] = false;
$success = false;
2004-01-25 13:27:53 +00:00
}
$this->mLastIndex = $i;
wfProfileOut( $fname );
return $success;
2004-01-25 13:27:53 +00:00
}
/**
* Test if the specified index represents an open connection
* @private
*/
function isOpen( $index ) {
if( !is_integer( $index ) ) {
return false;
}
if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
$this->mConnections[$index]->isOpen() )
2004-07-24 08:33:37 +00:00
{
return true;
} else {
return false;
}
}
/**
* Really opens a connection
* @private
*/
function reallyOpenConnection( &$server ) {
if( !is_array( $server ) ) {
wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
}
extract( $server );
# Get class for this database type
$class = 'Database' . ucfirst( $type );
if ( !class_exists( $class ) ) {
require_once( "$class.php" );
}
# Create object
return new $class( $host, $user, $password, $dbname, 1, $flags );
}
2004-01-25 13:27:53 +00:00
function reportConnectionError( &$conn )
{
$fname = 'LoadBalancer::reportConnectionError';
wfProfileIn( $fname );
# Prevent infinite recursion
static $reporting = false;
if ( !$reporting ) {
$reporting = true;
if ( !is_object( $conn ) ) {
$conn = new Database;
}
if ( $this->mFailFunction ) {
$conn->failFunction( $this->mFailFunction );
} else {
2005-08-14 04:42:55 +00:00
$conn->failFunction( false );
}
$conn->reportConnectionError();
$reporting = false;
2004-01-25 13:27:53 +00:00
}
wfProfileOut( $fname );
2004-01-25 13:27:53 +00:00
}
function getWriterIndex()
2004-01-25 13:27:53 +00:00
{
return 0;
2004-01-25 13:27:53 +00:00
}
function force( $i )
{
$this->mForce = $i;
}
function haveIndex( $i )
{
return array_key_exists( $i, $this->mServers );
}
/**
* Get the number of defined servers (not the number of open connections)
*/
function getServerCount() {
return count( $this->mServers );
}
/**
* Save master pos to the session and to memcached, if the session exists
*/
function saveMasterPos() {
global $wgSessionStarted;
if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
# If this entire request was served from a slave without opening a connection to the
# master (however unlikely that may be), then we can fetch the position from the slave.
if ( empty( $this->mConnections[0] ) ) {
$conn =& $this->getConnection( DB_SLAVE );
list( $file, $pos ) = $conn->getSlavePos();
wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
} else {
$conn =& $this->getConnection( 0 );
list( $file, $pos ) = $conn->getMasterPos();
wfDebug( "Saving master pos: $file $pos\n" );
}
if ( $file !== false ) {
$_SESSION['master_log_file'] = $file;
$_SESSION['master_pos'] = $pos;
}
}
}
/**
* Loads the master pos from the session, waits for it if necessary
*/
function loadMasterPos() {
if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
$this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
}
}
/**
* Close all open connections
*/
function closeAll() {
foreach( $this->mConnections as $i => $conn ) {
if ( $this->isOpen( $i ) ) {
// Need to use this syntax because $conn is a copy not a reference
$this->mConnections[$i]->close();
}
}
}
function commitAll() {
foreach( $this->mConnections as $i => $conn ) {
if ( $this->isOpen( $i ) ) {
// Need to use this syntax because $conn is a copy not a reference
$this->mConnections[$i]->immediateCommit();
}
}
}
2005-01-15 10:13:36 +00:00
function waitTimeout( $value = NULL ) {
return wfSetVar( $this->mWaitTimeout, $value );
}
function getLaggedSlaveMode() {
return $this->mLaggedSlaveMode;
}
function pingAll() {
$success = true;
foreach ( $this->mConnections as $i => $conn ) {
if ( $this->isOpen( $i ) ) {
if ( !$this->mConnections[$i]->ping() ) {
$success = false;
}
}
}
return $success;
}
2005-06-01 06:18:49 +00:00
/**
* Get the hostname and lag time of the most-lagged slave
* This is useful for maintenance scripts that need to throttle their updates
*/
function getMaxLag() {
$maxLag = -1;
$host = '';
foreach ( $this->mServers as $i => $conn ) {
if ( $this->openConnection( $i ) ) {
$lag = $this->mConnections[$i]->getLag();
if ( $lag > $maxLag ) {
$maxLag = $lag;
$host = $this->mServers[$i]['host'];
}
}
}
return array( $host, $maxLag );
}
2005-06-01 06:18:49 +00:00
/**
* Get lag time for each DB
* Results are cached for a short time in memcached
*/
function getLagTimes() {
$expiry = 5;
$requestRate = 10;
global $wgMemc;
$times = $wgMemc->get( 'lag_times' );
if ( $times ) {
# Randomly recache with probability rising over $expiry
$elapsed = time() - $times['timestamp'];
$chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
if ( mt_rand( 0, $chance ) != 0 ) {
unset( $times['timestamp'] );
return $times;
}
}
# Cache key missing or expired
$times = array();
foreach ( $this->mServers as $i => $conn ) {
if ( $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 );
# But don't give the timestamp to the caller
unset($times['timestamp']);
return $times;
}
2004-01-25 13:27:53 +00:00
}
?>