8 * Depends on the database object
10 require_once( 'Database.php' );
13 # Scale polling time so that under overload conditions, the database server
14 # receives a SHOW STATUS query at an average interval of this many microseconds
15 define( 'AVG_STATUS_POLL', 2000 );
19 * Database load balancing object
25 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
26 /* private */ var $mFailFunction, $mErrorConnection;
27 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
28 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
29 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
31 function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
33 $this->mServers
= $servers;
34 $this->mFailFunction
= $failFunction;
35 $this->mReadIndex
= -1;
36 $this->mWriteIndex
= -1;
38 $this->mConnections
= array();
39 $this->mLastIndex
= 1;
40 $this->mLoads
= array();
41 $this->mWaitForFile
= false;
42 $this->mWaitForPos
= false;
43 $this->mWaitTimeout
= $waitTimeout;
44 $this->mLaggedSlaveMode
= false;
45 $this->mErrorConnection
= false;
46 $this->mAllowLag
= false;
48 foreach( $servers as $i => $server ) {
49 $this->mLoads
[$i] = $server['load'];
50 if ( isset( $server['groupLoads'] ) ) {
51 foreach ( $server['groupLoads'] as $group => $ratio ) {
52 if ( !isset( $this->mGroupLoads
[$group] ) ) {
53 $this->mGroupLoads
[$group] = array();
55 $this->mGroupLoads
[$group][$i] = $ratio;
59 if ( $waitForMasterNow ) {
60 $this->loadMasterPos();
64 static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
66 return new LoadBalancer( $servers, $failFunction, $waitTimeout );
70 * Given an array of non-normalised probabilities, this function will select
71 * an element and return the appropriate key
73 function pickRandom( $weights )
75 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
79 $sum = array_sum( $weights );
81 # No loads on any of them
82 # In previous versions, this triggered an unweighted random selection,
83 # but this feature has been removed as of April 2006 to allow for strict
84 # separation of query groups.
87 $max = mt_getrandmax();
88 $rand = mt_rand(0, $max) / $max * $sum;
91 foreach ( $weights as $i => $w ) {
93 if ( $sum >= $rand ) {
100 function getRandomNonLagged( $loads ) {
101 # Unset excessively lagged servers
102 $lags = $this->getLagTimes();
103 foreach ( $lags as $i => $lag ) {
104 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
109 # Find out if all the slaves with non-zero load are lagged
111 foreach ( $loads as $load ) {
115 # No appropriate DB servers except maybe the master and some slaves with zero load
116 # Do NOT use the master
117 # Instead, this function will return false, triggering read-only mode,
118 # and a lagged slave will be used instead.
122 if ( count( $loads ) == 0 ) {
126 #wfDebugLog( 'connect', var_export( $loads, true ) );
128 # Return a random representative of the remainder
129 return $this->pickRandom( $loads );
133 * Get the index of the reader connection, which may be a slave
134 * This takes into account load ratios and lag times. It should
135 * always return a consistent index during a given invocation
137 * Side effect: opens connections to databases
139 function getReaderIndex() {
140 global $wgReadOnly, $wgDBClusterTimeout;
142 $fname = 'LoadBalancer::getReaderIndex';
143 wfProfileIn( $fname );
146 if ( $this->mForce
>= 0 ) {
149 if ( $this->mReadIndex
>= 0 ) {
150 $i = $this->mReadIndex
;
152 # $loads is $this->mLoads except with elements knocked out if they
154 $loads = $this->mLoads
;
158 if ( $wgReadOnly or $this->mAllowLagged
) {
159 $i = $this->pickRandom( $loads );
161 $i = $this->getRandomNonLagged( $loads );
162 if ( $i === false && count( $loads ) != 0 ) {
163 # All slaves lagged. Switch to read-only mode
164 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
165 $i = $this->pickRandom( $loads );
169 if ( $i !== false ) {
170 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
171 $this->openConnection( $i );
173 if ( !$this->isOpen( $i ) ) {
174 wfDebug( "$fname: Failed\n" );
178 $status = $this->mConnections
[$i]->getStatus("Thread%");
179 if ( isset( $this->mServers
[$i]['max threads'] ) &&
180 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
182 # Too much load, back off and wait for a while.
183 # The sleep time is scaled by the number of threads connected,
184 # to produce a roughly constant global poll rate.
185 $sleepTime = AVG_STATUS_POLL
* $status['Threads_connected'];
187 # 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 );
426 if ( !class_exists( $class ) ) {
427 require_once( "$class.php" );
431 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
432 $db->setLBInfo( $server );
436 function reportConnectionError( &$conn )
438 $fname = 'LoadBalancer::reportConnectionError';
439 wfProfileIn( $fname );
440 # Prevent infinite recursion
442 static $reporting = false;
445 if ( !is_object( $conn ) ) {
446 // No last connection, probably due to all servers being too busy
447 $conn = new Database
;
448 if ( $this->mFailFunction
) {
449 $conn->failFunction( $this->mFailFunction
);
450 $conn->reportConnectionError( $this->mLastError
);
452 // If all servers were busy, mLastError will contain something sensible
453 throw new DBConnectionError( $conn, $this->mLastError
);
456 if ( $this->mFailFunction
) {
457 $conn->failFunction( $this->mFailFunction
);
459 $conn->failFunction( false );
461 $server = $conn->getProperty( 'mServer' );
462 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
466 wfProfileOut( $fname );
469 function getWriterIndex() {
474 * Force subsequent calls to getConnection(DB_SLAVE) to return the
475 * given index. Set to -1 to restore the original load balancing
476 * behaviour. I thought this was a good idea when I originally
477 * wrote this class, but it has never been used.
479 function force( $i ) {
484 * Returns true if the specified index is a valid server index
486 function haveIndex( $i ) {
487 return array_key_exists( $i, $this->mServers
);
491 * Returns true if the specified index is valid and has non-zero load
493 function isNonZeroLoad( $i ) {
494 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
498 * Get the number of defined servers (not the number of open connections)
500 function getServerCount() {
501 return count( $this->mServers
);
505 * Save master pos to the session and to memcached, if the session exists
507 function saveMasterPos() {
508 global $wgSessionStarted;
509 if ( $wgSessionStarted && count( $this->mServers
) > 1 ) {
510 # If this entire request was served from a slave without opening a connection to the
511 # master (however unlikely that may be), then we can fetch the position from the slave.
512 if ( empty( $this->mConnections
[0] ) ) {
513 $conn =& $this->getConnection( DB_SLAVE
);
514 list( $file, $pos ) = $conn->getSlavePos();
515 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
517 $conn =& $this->getConnection( 0 );
518 list( $file, $pos ) = $conn->getMasterPos();
519 wfDebug( "Saving master pos: $file $pos\n" );
521 if ( $file !== false ) {
522 $_SESSION['master_log_file'] = $file;
523 $_SESSION['master_pos'] = $pos;
529 * Loads the master pos from the session, waits for it if necessary
531 function loadMasterPos() {
532 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
533 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
538 * Close all open connections
540 function closeAll() {
541 foreach( $this->mConnections
as $i => $conn ) {
542 if ( $this->isOpen( $i ) ) {
543 // Need to use this syntax because $conn is a copy not a reference
544 $this->mConnections
[$i]->close();
549 function commitAll() {
550 foreach( $this->mConnections
as $i => $conn ) {
551 if ( $this->isOpen( $i ) ) {
552 // Need to use this syntax because $conn is a copy not a reference
553 $this->mConnections
[$i]->immediateCommit();
558 function waitTimeout( $value = NULL ) {
559 return wfSetVar( $this->mWaitTimeout
, $value );
562 function getLaggedSlaveMode() {
563 return $this->mLaggedSlaveMode
;
566 /* Disables/enables lag checks */
567 function allowLagged($mode=null) {
569 return $this->mAllowLagged
;
570 $this->mAllowLagged
=$mode;
575 foreach ( $this->mConnections
as $i => $conn ) {
576 if ( $this->isOpen( $i ) ) {
577 if ( !$this->mConnections
[$i]->ping() ) {
586 * Get the hostname and lag time of the most-lagged slave
587 * This is useful for maintenance scripts that need to throttle their updates
589 function getMaxLag() {
592 foreach ( $this->mServers
as $i => $conn ) {
593 if ( $this->openConnection( $i ) ) {
594 $lag = $this->mConnections
[$i]->getLag();
595 if ( $lag > $maxLag ) {
597 $host = $this->mServers
[$i]['host'];
601 return array( $host, $maxLag );
605 * Get lag time for each DB
606 * Results are cached for a short time in memcached
608 function getLagTimes() {
611 wfProfileIn( __METHOD__
);
616 $times = $wgMemc->get( "$wgDBname:lag_times" );
618 # Randomly recache with probability rising over $expiry
619 $elapsed = time() - $times['timestamp'];
620 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
621 if ( mt_rand( 0, $chance ) != 0 ) {
622 unset( $times['timestamp'] );
623 wfProfileOut( __METHOD__
);
628 # Cache key missing or expired
631 foreach ( $this->mServers
as $i => $conn ) {
632 if ($i==0) { # Master
634 } elseif ( $this->openConnection( $i ) ) {
635 $times[$i] = $this->mConnections
[$i]->getLag();
639 # Add a timestamp key so we know when it was cached
640 $times['timestamp'] = time();
641 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
643 # But don't give the timestamp to the caller
644 unset($times['timestamp']);
645 wfIncrStats( 'lag_cache_miss' );
646 wfProfileOut( __METHOD__
);