8 * Depends on the database object
10 require_once( 'Database.php' );
12 # Valid database indexes
13 # Operation-based indexes
14 define( 'DB_SLAVE', -1 ); # Read from the slave (or only server)
15 define( 'DB_MASTER', -2 ); # Write to master (or only server)
16 define( 'DB_LAST', -3 ); # Whatever database was used last
19 define( 'DB_READ', -1 );
20 define( 'DB_WRITE', -2 );
23 # Scale polling time so that under overload conditions, the database server
24 # receives a SHOW STATUS query at an average interval of this many microseconds
25 define( 'AVG_STATUS_POLL', 2000 );
29 * Database load balancing object
35 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
36 /* private */ var $mFailFunction, $mErrorConnection;
37 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
38 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
39 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
41 function LoadBalancer()
43 $this->mServers
= array();
44 $this->mConnections
= array();
45 $this->mFailFunction
= false;
46 $this->mReadIndex
= -1;
48 $this->mLastIndex
= -1;
49 $this->mErrorConnection
= false;
50 $this->mAllowLag
= false;
53 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
55 $lb = new LoadBalancer
;
56 $lb->initialise( $servers, $failFunction, $waitTimeout );
60 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
62 $this->mServers
= $servers;
63 $this->mFailFunction
= $failFunction;
64 $this->mReadIndex
= -1;
65 $this->mWriteIndex
= -1;
67 $this->mConnections
= array();
68 $this->mLastIndex
= 1;
69 $this->mLoads
= array();
70 $this->mWaitForFile
= false;
71 $this->mWaitForPos
= false;
72 $this->mWaitTimeout
= $waitTimeout;
73 $this->mLaggedSlaveMode
= false;
75 foreach( $servers as $i => $server ) {
76 $this->mLoads
[$i] = $server['load'];
77 if ( isset( $server['groupLoads'] ) ) {
78 foreach ( $server['groupLoads'] as $group => $ratio ) {
79 if ( !isset( $this->mGroupLoads
[$group] ) ) {
80 $this->mGroupLoads
[$group] = array();
82 $this->mGroupLoads
[$group][$i] = $ratio;
89 * Given an array of non-normalised probabilities, this function will select
90 * an element and return the appropriate key
92 function pickRandom( $weights )
94 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
98 $sum = array_sum( $weights );
100 # No loads on any of them
101 # In previous versions, this triggered an unweighted random selection,
102 # but this feature has been removed as of April 2006 to allow for strict
103 # separation of query groups.
106 $max = mt_getrandmax();
107 $rand = mt_rand(0, $max) / $max * $sum;
110 foreach ( $weights as $i => $w ) {
112 if ( $sum >= $rand ) {
119 function getRandomNonLagged( $loads ) {
120 # Unset excessively lagged servers
121 $lags = $this->getLagTimes();
122 foreach ( $lags as $i => $lag ) {
123 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
128 # Find out if all the slaves with non-zero load are lagged
130 foreach ( $loads as $load ) {
134 # No appropriate DB servers except maybe the master and some slaves with zero load
135 # Do NOT use the master
136 # Instead, this function will return false, triggering read-only mode,
137 # and a lagged slave will be used instead.
141 if ( count( $loads ) == 0 ) {
145 #wfDebugLog( 'connect', var_export( $loads, true ) );
147 # Return a random representative of the remainder
148 return $this->pickRandom( $loads );
152 * Get the index of the reader connection, which may be a slave
153 * This takes into account load ratios and lag times. It should
154 * always return a consistent index during a given invocation
156 * Side effect: opens connections to databases
158 function getReaderIndex() {
159 global $wgReadOnly, $wgDBClusterTimeout;
161 $fname = 'LoadBalancer::getReaderIndex';
162 wfProfileIn( $fname );
165 if ( $this->mForce
>= 0 ) {
168 if ( $this->mReadIndex
>= 0 ) {
169 $i = $this->mReadIndex
;
171 # $loads is $this->mLoads except with elements knocked out if they
173 $loads = $this->mLoads
;
177 if ( $wgReadOnly or $this->mAllowLagged
) {
178 $i = $this->pickRandom( $loads );
180 $i = $this->getRandomNonLagged( $loads );
181 if ( $i === false && count( $loads ) != 0 ) {
182 # All slaves lagged. Switch to read-only mode
183 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
184 $i = $this->pickRandom( $loads );
188 if ( $i !== false ) {
189 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
190 $this->openConnection( $i );
192 if ( !$this->isOpen( $i ) ) {
193 wfDebug( "$fname: Failed\n" );
197 $status = $this->mConnections
[$i]->getStatus("Thread%");
198 if ( isset( $this->mServers
[$i]['max threads'] ) &&
199 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
201 # Too much load, back off and wait for a while.
202 # The sleep time is scaled by the number of threads connected,
203 # to produce a roughly constant global poll rate.
204 $sleepTime = AVG_STATUS_POLL
* $status['Threads_connected'];
206 # If we reach the timeout and exit the loop, don't use it
217 $totalElapsed +
= $sleepTime;
218 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
219 wfProfileIn( "$fname-sleep $x" );
220 usleep( $sleepTime );
221 wfProfileOut( "$fname-sleep $x" );
223 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
225 if ( $totalElapsed / 1e6
>= $wgDBClusterTimeout ) {
226 $this->mErrorConnection
= false;
227 $this->mLastError
= 'All servers busy';
230 if ( $i !== false && $this->isOpen( $i ) ) {
231 # Wait for the session master pos for a short time
232 if ( $this->mWaitForFile
) {
233 if ( !$this->doWait( $i ) ) {
234 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
237 if ( $i !== false ) {
238 $this->mReadIndex
= $i;
245 wfProfileOut( $fname );
250 * Get a random server to use in a query group
252 function getGroupIndex( $group ) {
253 if ( isset( $this->mGroupLoads
[$group] ) ) {
254 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
258 wfDebug( "Query group $group => $i\n" );
263 * Set the master wait position
264 * If a DB_SLAVE connection has been opened already, waits
265 * Otherwise sets a variable telling it to wait if such a connection is opened
267 function waitFor( $file, $pos ) {
268 $fname = 'LoadBalancer::waitFor';
269 wfProfileIn( $fname );
271 wfDebug( "User master pos: $file $pos\n" );
272 $this->mWaitForFile
= false;
273 $this->mWaitForPos
= false;
275 if ( count( $this->mServers
) > 1 ) {
276 $this->mWaitForFile
= $file;
277 $this->mWaitForPos
= $pos;
278 $i = $this->mReadIndex
;
281 if ( !$this->doWait( $i ) ) {
282 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
283 $this->mLaggedSlaveMode
= true;
287 wfProfileOut( $fname );
291 * Wait for a given slave to catch up to the master pos stored in $this
293 function doWait( $index ) {
299 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
301 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
305 $key = 'masterpos:' . $index;
306 $memcPos = $wgMemc->get( $key );
308 list( $file, $pos ) = explode( ' ', $memcPos );
309 # If the saved position is later than the requested position, return now
310 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
315 if ( !$retVal && $this->isOpen( $index ) ) {
316 $conn =& $this->mConnections
[$index];
317 wfDebug( "Waiting for slave #$index to catch up...\n" );
318 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
320 if ( $result == -1 ||
is_null( $result ) ) {
321 # Timed out waiting for slave, use master instead
322 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
333 * Get a connection by index
335 function &getConnection( $i, $fail = true, $groups = array() )
338 $fname = 'LoadBalancer::getConnection';
339 wfProfileIn( $fname );
343 if ( !is_array( $groups ) ) {
344 $groupIndex = $this->getGroupIndex( $groups, $i );
345 if ( $groupIndex !== false ) {
349 foreach ( $groups as $group ) {
350 $groupIndex = $this->getGroupIndex( $group, $i );
351 if ( $groupIndex !== false ) {
358 # For now, only go through all this for mysql databases
359 if ($wgDBtype != 'mysql') {
360 $i = $this->getWriterIndex();
362 # Operation-based index
363 elseif ( $i == DB_SLAVE
) {
364 $i = $this->getReaderIndex();
365 } elseif ( $i == DB_MASTER
) {
366 $i = $this->getWriterIndex();
367 } elseif ( $i == DB_LAST
) {
368 # Just use $this->mLastIndex, which should already be set
369 $i = $this->mLastIndex
;
371 # Oh dear, not set, best to use the writer for safety
372 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
373 $i = $this->getWriterIndex();
376 # Couldn't find a working server in getReaderIndex()?
377 if ( $i === false ) {
378 $this->reportConnectionError( $this->mErrorConnection
);
380 # Now we have an explicit index into the servers array
381 $this->openConnection( $i, $fail );
383 wfProfileOut( $fname );
384 return $this->mConnections
[$i];
388 * Open a connection to the server given by the specified index
389 * Index must be an actual index into the array
393 function openConnection( $i, $fail = false ) {
394 $fname = 'LoadBalancer::openConnection';
395 wfProfileIn( $fname );
398 if ( !$this->isOpen( $i ) ) {
399 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
402 if ( !$this->isOpen( $i ) ) {
403 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
405 $this->reportConnectionError( $this->mConnections
[$i] );
407 $this->mErrorConnection
= $this->mConnections
[$i];
408 $this->mConnections
[$i] = false;
411 $this->mLastIndex
= $i;
412 wfProfileOut( $fname );
417 * Test if the specified index represents an open connection
420 function isOpen( $index ) {
421 if( !is_integer( $index ) ) {
424 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
425 $this->mConnections
[$index]->isOpen() )
434 * Really opens a connection
437 function reallyOpenConnection( &$server ) {
438 if( !is_array( $server ) ) {
439 throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
443 # Get class for this database type
444 $class = 'Database' . ucfirst( $type );
445 if ( !class_exists( $class ) ) {
446 require_once( "$class.php" );
450 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
451 $db->setLBInfo( $server );
455 function reportConnectionError( &$conn )
457 $fname = 'LoadBalancer::reportConnectionError';
458 wfProfileIn( $fname );
459 # Prevent infinite recursion
461 static $reporting = false;
464 if ( !is_object( $conn ) ) {
465 // No last connection, probably due to all servers being too busy
466 $conn = new Database
;
467 if ( $this->mFailFunction
) {
468 $conn->failFunction( $this->mFailFunction
);
469 $conn->reportConnectionError( $this->mLastError
);
471 // If all servers were busy, mLastError will contain something sensible
472 throw new DBConnectionError( $conn, $this->mLastError
);
475 if ( $this->mFailFunction
) {
476 $conn->failFunction( $this->mFailFunction
);
478 $conn->failFunction( false );
480 $server = $conn->getProperty( 'mServer' );
481 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
485 wfProfileOut( $fname );
488 function getWriterIndex() {
493 * Force subsequent calls to getConnection(DB_SLAVE) to return the
494 * given index. Set to -1 to restore the original load balancing
495 * behaviour. I thought this was a good idea when I originally
496 * wrote this class, but it has never been used.
498 function force( $i ) {
503 * Returns true if the specified index is a valid server index
505 function haveIndex( $i ) {
506 return array_key_exists( $i, $this->mServers
);
510 * Returns true if the specified index is valid and has non-zero load
512 function isNonZeroLoad( $i ) {
513 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
517 * Get the number of defined servers (not the number of open connections)
519 function getServerCount() {
520 return count( $this->mServers
);
524 * Save master pos to the session and to memcached, if the session exists
526 function saveMasterPos() {
527 global $wgSessionStarted;
528 if ( $wgSessionStarted && count( $this->mServers
) > 1 ) {
529 # If this entire request was served from a slave without opening a connection to the
530 # master (however unlikely that may be), then we can fetch the position from the slave.
531 if ( empty( $this->mConnections
[0] ) ) {
532 $conn =& $this->getConnection( DB_SLAVE
);
533 list( $file, $pos ) = $conn->getSlavePos();
534 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
536 $conn =& $this->getConnection( 0 );
537 list( $file, $pos ) = $conn->getMasterPos();
538 wfDebug( "Saving master pos: $file $pos\n" );
540 if ( $file !== false ) {
541 $_SESSION['master_log_file'] = $file;
542 $_SESSION['master_pos'] = $pos;
548 * Loads the master pos from the session, waits for it if necessary
550 function loadMasterPos() {
551 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
552 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
557 * Close all open connections
559 function closeAll() {
560 foreach( $this->mConnections
as $i => $conn ) {
561 if ( $this->isOpen( $i ) ) {
562 // Need to use this syntax because $conn is a copy not a reference
563 $this->mConnections
[$i]->close();
568 function commitAll() {
569 foreach( $this->mConnections
as $i => $conn ) {
570 if ( $this->isOpen( $i ) ) {
571 // Need to use this syntax because $conn is a copy not a reference
572 $this->mConnections
[$i]->immediateCommit();
577 function waitTimeout( $value = NULL ) {
578 return wfSetVar( $this->mWaitTimeout
, $value );
581 function getLaggedSlaveMode() {
582 return $this->mLaggedSlaveMode
;
585 /* Disables/enables lag checks */
586 function allowLagged($mode=null) {
588 return $this->mAllowLagged
;
589 $this->mAllowLagged
=$mode;
594 foreach ( $this->mConnections
as $i => $conn ) {
595 if ( $this->isOpen( $i ) ) {
596 if ( !$this->mConnections
[$i]->ping() ) {
605 * Get the hostname and lag time of the most-lagged slave
606 * This is useful for maintenance scripts that need to throttle their updates
608 function getMaxLag() {
611 foreach ( $this->mServers
as $i => $conn ) {
612 if ( $this->openConnection( $i ) ) {
613 $lag = $this->mConnections
[$i]->getLag();
614 if ( $lag > $maxLag ) {
616 $host = $this->mServers
[$i]['host'];
620 return array( $host, $maxLag );
624 * Get lag time for each DB
625 * Results are cached for a short time in memcached
627 function getLagTimes() {
634 $times = $wgMemc->get( "$wgDBname:lag_times" );
636 # Randomly recache with probability rising over $expiry
637 $elapsed = time() - $times['timestamp'];
638 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
639 if ( mt_rand( 0, $chance ) != 0 ) {
640 unset( $times['timestamp'] );
645 # Cache key missing or expired
648 foreach ( $this->mServers
as $i => $conn ) {
649 if ($i==0) { # Master
651 } elseif ( $this->openConnection( $i ) ) {
652 $times[$i] = $this->mConnections
[$i]->getLag();
656 # Add a timestamp key so we know when it was cached
657 $times['timestamp'] = time();
658 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
660 # But don't give the timestamp to the caller
661 unset($times['timestamp']);