summaryrefslogtreecommitdiff
path: root/includes/LoadBalancer.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/LoadBalancer.php')
-rw-r--r--includes/LoadBalancer.php74
1 files changed, 26 insertions, 48 deletions
diff --git a/includes/LoadBalancer.php b/includes/LoadBalancer.php
index f985a7b4..3e81aea9 100644
--- a/includes/LoadBalancer.php
+++ b/includes/LoadBalancer.php
@@ -4,26 +4,6 @@
* @package MediaWiki
*/
-/**
- * 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 );
-
-
-# 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
@@ -38,26 +18,13 @@ class LoadBalancer {
/* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
/* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
- function LoadBalancer()
- {
- $this->mServers = array();
- $this->mConnections = array();
- $this->mFailFunction = false;
- $this->mReadIndex = -1;
- $this->mForce = -1;
- $this->mLastIndex = -1;
- $this->mErrorConnection = false;
- $this->mAllowLag = false;
- }
-
- function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
- {
- $lb = new LoadBalancer;
- $lb->initialise( $servers, $failFunction, $waitTimeout );
- return $lb;
- }
+ /**
+ * Scale polling time so that under overload conditions, the database server
+ * receives a SHOW STATUS query at an average interval of this many microseconds
+ */
+ const AVG_STATUS_POLL = 2000;
- function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
+ function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
{
$this->mServers = $servers;
$this->mFailFunction = $failFunction;
@@ -71,6 +38,8 @@ class LoadBalancer {
$this->mWaitForPos = false;
$this->mWaitTimeout = $waitTimeout;
$this->mLaggedSlaveMode = false;
+ $this->mErrorConnection = false;
+ $this->mAllowLag = false;
foreach( $servers as $i => $server ) {
$this->mLoads[$i] = $server['load'];
@@ -83,6 +52,14 @@ class LoadBalancer {
}
}
}
+ if ( $waitForMasterNow ) {
+ $this->loadMasterPos();
+ }
+ }
+
+ static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
+ {
+ return new LoadBalancer( $servers, $failFunction, $waitTimeout );
}
/**
@@ -180,7 +157,7 @@ class LoadBalancer {
$i = $this->getRandomNonLagged( $loads );
if ( $i === false && count( $loads ) != 0 ) {
# All slaves lagged. Switch to read-only mode
- $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
+ $wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
$i = $this->pickRandom( $loads );
}
}
@@ -201,7 +178,7 @@ class LoadBalancer {
# 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'];
+ $sleepTime = self::AVG_STATUS_POLL * $status['Threads_connected'];
# If we reach the timeout and exit the loop, don't use it
$i = false;
@@ -442,9 +419,6 @@ class LoadBalancer {
extract( $server );
# Get class for this database type
$class = 'Database' . ucfirst( $type );
- if ( !class_exists( $class ) ) {
- require_once( "$class.php" );
- }
# Create object
$db = new $class( $host, $user, $password, $dbname, 1, $flags );
@@ -625,21 +599,24 @@ class LoadBalancer {
* Results are cached for a short time in memcached
*/
function getLagTimes() {
- global $wgDBname;
-
+ wfProfileIn( __METHOD__ );
$expiry = 5;
$requestRate = 10;
global $wgMemc;
- $times = $wgMemc->get( "$wgDBname:lag_times" );
+ $times = $wgMemc->get( wfMemcKey( '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'] );
+ wfProfileOut( __METHOD__ );
return $times;
}
+ wfIncrStats( 'lag_cache_miss_expired' );
+ } else {
+ wfIncrStats( 'lag_cache_miss_absent' );
}
# Cache key missing or expired
@@ -655,10 +632,11 @@ class LoadBalancer {
# Add a timestamp key so we know when it was cached
$times['timestamp'] = time();
- $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
+ $wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );
# But don't give the timestamp to the caller
unset($times['timestamp']);
+ wfProfileOut( __METHOD__ );
return $times;
}
}