8 * Database load balancing object
13 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
14 /* private */ var $mFailFunction, $mErrorConnection;
15 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
16 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
17 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
20 * Scale polling time so that under overload conditions, the database server
21 * receives a SHOW STATUS query at an average interval of this many microseconds
23 const AVG_STATUS_POLL
= 2000;
25 function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
27 $this->mServers
= $servers;
28 $this->mFailFunction
= $failFunction;
29 $this->mReadIndex
= -1;
30 $this->mWriteIndex
= -1;
32 $this->mConnections
= array();
33 $this->mLastIndex
= -1;
34 $this->mLoads
= array();
35 $this->mWaitForFile
= false;
36 $this->mWaitForPos
= false;
37 $this->mWaitTimeout
= $waitTimeout;
38 $this->mLaggedSlaveMode
= false;
39 $this->mErrorConnection
= false;
40 $this->mAllowLag
= false;
42 foreach( $servers as $i => $server ) {
43 $this->mLoads
[$i] = $server['load'];
44 if ( isset( $server['groupLoads'] ) ) {
45 foreach ( $server['groupLoads'] as $group => $ratio ) {
46 if ( !isset( $this->mGroupLoads
[$group] ) ) {
47 $this->mGroupLoads
[$group] = array();
49 $this->mGroupLoads
[$group][$i] = $ratio;
53 if ( $waitForMasterNow ) {
54 $this->loadMasterPos();
58 static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
60 return new LoadBalancer( $servers, $failFunction, $waitTimeout );
64 * Given an array of non-normalised probabilities, this function will select
65 * an element and return the appropriate key
67 function pickRandom( $weights )
69 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
73 $sum = array_sum( $weights );
75 # No loads on any of them
76 # In previous versions, this triggered an unweighted random selection,
77 # but this feature has been removed as of April 2006 to allow for strict
78 # separation of query groups.
81 $max = mt_getrandmax();
82 $rand = mt_rand(0, $max) / $max * $sum;
85 foreach ( $weights as $i => $w ) {
87 if ( $sum >= $rand ) {
94 function getRandomNonLagged( $loads ) {
95 # Unset excessively lagged servers
96 $lags = $this->getLagTimes();
97 foreach ( $lags as $i => $lag ) {
98 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
103 # Find out if all the slaves with non-zero load are lagged
105 foreach ( $loads as $load ) {
109 # No appropriate DB servers except maybe the master and some slaves with zero load
110 # Do NOT use the master
111 # Instead, this function will return false, triggering read-only mode,
112 # and a lagged slave will be used instead.
116 if ( count( $loads ) == 0 ) {
120 #wfDebugLog( 'connect', var_export( $loads, true ) );
122 # Return a random representative of the remainder
123 return $this->pickRandom( $loads );
127 * Get the index of the reader connection, which may be a slave
128 * This takes into account load ratios and lag times. It should
129 * always return a consistent index during a given invocation
131 * Side effect: opens connections to databases
133 function getReaderIndex() {
134 global $wgReadOnly, $wgDBClusterTimeout;
136 $fname = 'LoadBalancer::getReaderIndex';
137 wfProfileIn( $fname );
140 if ( $this->mForce
>= 0 ) {
142 } elseif ( count( $this->mServers
) == 1 ) {
143 # Skip the load balancing if there's only one server
146 if ( $this->mReadIndex
>= 0 ) {
147 $i = $this->mReadIndex
;
149 # $loads is $this->mLoads except with elements knocked out if they
151 $loads = $this->mLoads
;
155 if ( $wgReadOnly or $this->mAllowLagged
) {
156 $i = $this->pickRandom( $loads );
158 $i = $this->getRandomNonLagged( $loads );
159 if ( $i === false && count( $loads ) != 0 ) {
160 # All slaves lagged. Switch to read-only mode
161 $wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
162 $i = $this->pickRandom( $loads );
166 if ( $i !== false ) {
167 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
168 $this->openConnection( $i );
170 if ( !$this->isOpen( $i ) ) {
171 wfDebug( "$fname: Failed\n" );
175 if ( isset( $this->mServers
[$i]['max threads'] ) ) {
176 $status = $this->mConnections
[$i]->getStatus("Thread%");
177 if ( $status['Threads_running'] > $this->mServers
[$i]['max threads'] ) {
178 # Too much load, back off and wait for a while.
179 # The sleep time is scaled by the number of threads connected,
180 # to produce a roughly constant global poll rate.
181 $sleepTime = self
::AVG_STATUS_POLL
* $status['Threads_connected'];
183 # If we reach the timeout and exit the loop, don't use it
198 $totalElapsed +
= $sleepTime;
199 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
200 wfProfileIn( "$fname-sleep $x" );
201 usleep( $sleepTime );
202 wfProfileOut( "$fname-sleep $x" );
204 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
206 if ( $totalElapsed / 1e6
>= $wgDBClusterTimeout ) {
207 $this->mErrorConnection
= false;
208 $this->mLastError
= 'All servers busy';
211 if ( $i !== false && $this->isOpen( $i ) ) {
212 # Wait for the session master pos for a short time
213 if ( $this->mWaitForFile
) {
214 if ( !$this->doWait( $i ) ) {
215 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
218 if ( $i !== false ) {
219 $this->mReadIndex
= $i;
226 wfProfileOut( $fname );
231 * Get a random server to use in a query group
233 function getGroupIndex( $group ) {
234 if ( isset( $this->mGroupLoads
[$group] ) ) {
235 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
239 wfDebug( "Query group $group => $i\n" );
244 * Set the master wait position
245 * If a DB_SLAVE connection has been opened already, waits
246 * Otherwise sets a variable telling it to wait if such a connection is opened
248 function waitFor( $file, $pos ) {
249 $fname = 'LoadBalancer::waitFor';
250 wfProfileIn( $fname );
252 wfDebug( "User master pos: $file $pos\n" );
253 $this->mWaitForFile
= false;
254 $this->mWaitForPos
= false;
256 if ( count( $this->mServers
) > 1 ) {
257 $this->mWaitForFile
= $file;
258 $this->mWaitForPos
= $pos;
259 $i = $this->mReadIndex
;
262 if ( !$this->doWait( $i ) ) {
263 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
264 $this->mLaggedSlaveMode
= true;
268 wfProfileOut( $fname );
272 * Wait for a given slave to catch up to the master pos stored in $this
274 function doWait( $index ) {
280 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
282 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
286 $key = 'masterpos:' . $index;
287 $memcPos = $wgMemc->get( $key );
289 list( $file, $pos ) = explode( ' ', $memcPos );
290 # If the saved position is later than the requested position, return now
291 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
296 if ( !$retVal && $this->isOpen( $index ) ) {
297 $conn =& $this->mConnections
[$index];
298 wfDebug( "Waiting for slave #$index to catch up...\n" );
299 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
301 if ( $result == -1 ||
is_null( $result ) ) {
302 # Timed out waiting for slave, use master instead
303 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
314 * Get a connection by index
316 function &getConnection( $i, $fail = true, $groups = array() )
319 $fname = 'LoadBalancer::getConnection';
320 wfProfileIn( $fname );
324 if ( !is_array( $groups ) ) {
325 $groupIndex = $this->getGroupIndex( $groups, $i );
326 if ( $groupIndex !== false ) {
330 foreach ( $groups as $group ) {
331 $groupIndex = $this->getGroupIndex( $group, $i );
332 if ( $groupIndex !== false ) {
339 # For now, only go through all this for mysql databases
340 if ($wgDBtype != 'mysql') {
341 $i = $this->getWriterIndex();
343 # Operation-based index
344 elseif ( $i == DB_SLAVE
) {
345 $i = $this->getReaderIndex();
346 } elseif ( $i == DB_MASTER
) {
347 $i = $this->getWriterIndex();
348 } elseif ( $i == DB_LAST
) {
349 # Just use $this->mLastIndex, which should already be set
350 $i = $this->mLastIndex
;
352 # Oh dear, not set, best to use the writer for safety
353 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
354 $i = $this->getWriterIndex();
357 # Couldn't find a working server in getReaderIndex()?
358 if ( $i === false ) {
359 $this->reportConnectionError( $this->mErrorConnection
);
361 # Now we have an explicit index into the servers array
362 $this->openConnection( $i, $fail );
364 wfProfileOut( $fname );
365 return $this->mConnections
[$i];
369 * Open a connection to the server given by the specified index
370 * Index must be an actual index into the array
374 function openConnection( $i, $fail = false ) {
375 $fname = 'LoadBalancer::openConnection';
376 wfProfileIn( $fname );
379 if ( !$this->isOpen( $i ) ) {
380 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
383 if ( !$this->isOpen( $i ) ) {
384 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
386 $this->reportConnectionError( $this->mConnections
[$i] );
388 $this->mErrorConnection
= $this->mConnections
[$i];
389 $this->mConnections
[$i] = false;
392 $this->mLastIndex
= $i;
393 wfProfileOut( $fname );
398 * Test if the specified index represents an open connection
401 function isOpen( $index ) {
402 if( !is_integer( $index ) ) {
405 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
406 $this->mConnections
[$index]->isOpen() )
415 * Really opens a connection
418 function reallyOpenConnection( &$server ) {
419 if( !is_array( $server ) ) {
420 throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
424 # Get class for this database type
425 $class = 'Database' . ucfirst( $type );
428 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
429 $db->setLBInfo( $server );
433 function reportConnectionError( &$conn )
435 $fname = 'LoadBalancer::reportConnectionError';
436 wfProfileIn( $fname );
437 # Prevent infinite recursion
439 static $reporting = false;
442 if ( !is_object( $conn ) ) {
443 // No last connection, probably due to all servers being too busy
444 $conn = new Database
;
445 if ( $this->mFailFunction
) {
446 $conn->failFunction( $this->mFailFunction
);
447 $conn->reportConnectionError( $this->mLastError
);
449 // If all servers were busy, mLastError will contain something sensible
450 throw new DBConnectionError( $conn, $this->mLastError
);
453 if ( $this->mFailFunction
) {
454 $conn->failFunction( $this->mFailFunction
);
456 $conn->failFunction( false );
458 $server = $conn->getProperty( 'mServer' );
459 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
463 wfProfileOut( $fname );
466 function getWriterIndex() {
471 * Force subsequent calls to getConnection(DB_SLAVE) to return the
472 * given index. Set to -1 to restore the original load balancing
473 * behaviour. I thought this was a good idea when I originally
474 * wrote this class, but it has never been used.
476 function force( $i ) {
481 * Returns true if the specified index is a valid server index
483 function haveIndex( $i ) {
484 return array_key_exists( $i, $this->mServers
);
488 * Returns true if the specified index is valid and has non-zero load
490 function isNonZeroLoad( $i ) {
491 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
495 * Get the number of defined servers (not the number of open connections)
497 function getServerCount() {
498 return count( $this->mServers
);
502 * Save master pos to the session and to memcached, if the session exists
504 function saveMasterPos() {
505 if ( session_id() != '' && count( $this->mServers
) > 1 ) {
506 # If this entire request was served from a slave without opening a connection to the
507 # master (however unlikely that may be), then we can fetch the position from the slave.
508 if ( empty( $this->mConnections
[0] ) ) {
509 $conn =& $this->getConnection( DB_SLAVE
);
510 list( $file, $pos ) = $conn->getSlavePos();
511 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
513 $conn =& $this->getConnection( 0 );
514 list( $file, $pos ) = $conn->getMasterPos();
515 wfDebug( "Saving master pos: $file $pos\n" );
517 if ( $file !== false ) {
518 $_SESSION['master_log_file'] = $file;
519 $_SESSION['master_pos'] = $pos;
525 * Loads the master pos from the session, waits for it if necessary
527 function loadMasterPos() {
528 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
529 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
534 * Close all open connections
536 function closeAll() {
537 foreach( $this->mConnections
as $i => $conn ) {
538 if ( $this->isOpen( $i ) ) {
539 // Need to use this syntax because $conn is a copy not a reference
540 $this->mConnections
[$i]->close();
545 function commitAll() {
546 foreach( $this->mConnections
as $i => $conn ) {
547 if ( $this->isOpen( $i ) ) {
548 // Need to use this syntax because $conn is a copy not a reference
549 $this->mConnections
[$i]->immediateCommit();
554 function waitTimeout( $value = NULL ) {
555 return wfSetVar( $this->mWaitTimeout
, $value );
558 function getLaggedSlaveMode() {
559 return $this->mLaggedSlaveMode
;
562 /* Disables/enables lag checks */
563 function allowLagged($mode=null) {
565 return $this->mAllowLagged
;
566 $this->mAllowLagged
=$mode;
571 foreach ( $this->mConnections
as $i => $conn ) {
572 if ( $this->isOpen( $i ) ) {
573 if ( !$this->mConnections
[$i]->ping() ) {
582 * Get the hostname and lag time of the most-lagged slave
583 * This is useful for maintenance scripts that need to throttle their updates
585 function getMaxLag() {
588 foreach ( $this->mServers
as $i => $conn ) {
589 if ( $this->openConnection( $i ) ) {
590 $lag = $this->mConnections
[$i]->getLag();
591 if ( $lag > $maxLag ) {
593 $host = $this->mServers
[$i]['host'];
597 return array( $host, $maxLag );
601 * Get lag time for each DB
602 * Results are cached for a short time in memcached
604 function getLagTimes() {
605 wfProfileIn( __METHOD__
);
610 $times = $wgMemc->get( wfMemcKey( 'lag_times' ) );
612 # Randomly recache with probability rising over $expiry
613 $elapsed = time() - $times['timestamp'];
614 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
615 if ( mt_rand( 0, $chance ) != 0 ) {
616 unset( $times['timestamp'] );
617 wfProfileOut( __METHOD__
);
620 wfIncrStats( 'lag_cache_miss_expired' );
622 wfIncrStats( 'lag_cache_miss_absent' );
625 # Cache key missing or expired
628 foreach ( $this->mServers
as $i => $conn ) {
629 if ($i==0) { # Master
631 } elseif ( $this->openConnection( $i ) ) {
632 $times[$i] = $this->mConnections
[$i]->getLag();
636 # Add a timestamp key so we know when it was cached
637 $times['timestamp'] = time();
638 $wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );
640 # But don't give the timestamp to the caller
641 unset($times['timestamp']);
642 wfProfileOut( __METHOD__
);