9 * Database load balancing object
15 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
16 /* private */ var $mFailFunction, $mErrorConnection;
17 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
18 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
19 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
22 * Scale polling time so that under overload conditions, the database server
23 * receives a SHOW STATUS query at an average interval of this many microseconds
25 const AVG_STATUS_POLL
= 2000;
27 function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
29 $this->mServers
= $servers;
30 $this->mFailFunction
= $failFunction;
31 $this->mReadIndex
= -1;
32 $this->mWriteIndex
= -1;
34 $this->mConnections
= array();
35 $this->mLastIndex
= 1;
36 $this->mLoads
= array();
37 $this->mWaitForFile
= false;
38 $this->mWaitForPos
= false;
39 $this->mWaitTimeout
= $waitTimeout;
40 $this->mLaggedSlaveMode
= false;
41 $this->mErrorConnection
= false;
42 $this->mAllowLag
= false;
44 foreach( $servers as $i => $server ) {
45 $this->mLoads
[$i] = $server['load'];
46 if ( isset( $server['groupLoads'] ) ) {
47 foreach ( $server['groupLoads'] as $group => $ratio ) {
48 if ( !isset( $this->mGroupLoads
[$group] ) ) {
49 $this->mGroupLoads
[$group] = array();
51 $this->mGroupLoads
[$group][$i] = $ratio;
55 if ( $waitForMasterNow ) {
56 $this->loadMasterPos();
60 static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
62 return new LoadBalancer( $servers, $failFunction, $waitTimeout );
66 * Given an array of non-normalised probabilities, this function will select
67 * an element and return the appropriate key
69 function pickRandom( $weights )
71 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
75 $sum = array_sum( $weights );
77 # No loads on any of them
78 # In previous versions, this triggered an unweighted random selection,
79 # but this feature has been removed as of April 2006 to allow for strict
80 # separation of query groups.
83 $max = mt_getrandmax();
84 $rand = mt_rand(0, $max) / $max * $sum;
87 foreach ( $weights as $i => $w ) {
89 if ( $sum >= $rand ) {
96 function getRandomNonLagged( $loads ) {
97 # Unset excessively lagged servers
98 $lags = $this->getLagTimes();
99 foreach ( $lags as $i => $lag ) {
100 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
105 # Find out if all the slaves with non-zero load are lagged
107 foreach ( $loads as $load ) {
111 # No appropriate DB servers except maybe the master and some slaves with zero load
112 # Do NOT use the master
113 # Instead, this function will return false, triggering read-only mode,
114 # and a lagged slave will be used instead.
118 if ( count( $loads ) == 0 ) {
122 #wfDebugLog( 'connect', var_export( $loads, true ) );
124 # Return a random representative of the remainder
125 return $this->pickRandom( $loads );
129 * Get the index of the reader connection, which may be a slave
130 * This takes into account load ratios and lag times. It should
131 * always return a consistent index during a given invocation
133 * Side effect: opens connections to databases
135 function getReaderIndex() {
136 global $wgReadOnly, $wgDBClusterTimeout;
138 $fname = 'LoadBalancer::getReaderIndex';
139 wfProfileIn( $fname );
142 if ( $this->mForce
>= 0 ) {
144 } elseif ( count( $this->mServers
) == 1 ) {
145 # Skip the load balancing if there's only one server
148 if ( $this->mReadIndex
>= 0 ) {
149 $i = $this->mReadIndex
;
151 # $loads is $this->mLoads except with elements knocked out if they
153 $loads = $this->mLoads
;
157 if ( $wgReadOnly or $this->mAllowLagged
) {
158 $i = $this->pickRandom( $loads );
160 $i = $this->getRandomNonLagged( $loads );
161 if ( $i === false && count( $loads ) != 0 ) {
162 # All slaves lagged. Switch to read-only mode
163 $wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
164 $i = $this->pickRandom( $loads );
168 if ( $i !== false ) {
169 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
170 $this->openConnection( $i );
172 if ( !$this->isOpen( $i ) ) {
173 wfDebug( "$fname: Failed\n" );
177 $status = $this->mConnections
[$i]->getStatus("Thread%");
178 if ( isset( $this->mServers
[$i]['max threads'] ) &&
179 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
181 # Too much load, back off and wait for a while.
182 # The sleep time is scaled by the number of threads connected,
183 # to produce a roughly constant global poll rate.
184 $sleepTime = self
::AVG_STATUS_POLL
* $status['Threads_connected'];
186 # If we reach the timeout and exit the loop, don't use it
197 $totalElapsed +
= $sleepTime;
198 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
199 wfProfileIn( "$fname-sleep $x" );
200 usleep( $sleepTime );
201 wfProfileOut( "$fname-sleep $x" );
203 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
205 if ( $totalElapsed / 1e6
>= $wgDBClusterTimeout ) {
206 $this->mErrorConnection
= false;
207 $this->mLastError
= 'All servers busy';
210 if ( $i !== false && $this->isOpen( $i ) ) {
211 # Wait for the session master pos for a short time
212 if ( $this->mWaitForFile
) {
213 if ( !$this->doWait( $i ) ) {
214 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
217 if ( $i !== false ) {
218 $this->mReadIndex
= $i;
225 wfProfileOut( $fname );
230 * Get a random server to use in a query group
232 function getGroupIndex( $group ) {
233 if ( isset( $this->mGroupLoads
[$group] ) ) {
234 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
238 wfDebug( "Query group $group => $i\n" );
243 * Set the master wait position
244 * If a DB_SLAVE connection has been opened already, waits
245 * Otherwise sets a variable telling it to wait if such a connection is opened
247 function waitFor( $file, $pos ) {
248 $fname = 'LoadBalancer::waitFor';
249 wfProfileIn( $fname );
251 wfDebug( "User master pos: $file $pos\n" );
252 $this->mWaitForFile
= false;
253 $this->mWaitForPos
= false;
255 if ( count( $this->mServers
) > 1 ) {
256 $this->mWaitForFile
= $file;
257 $this->mWaitForPos
= $pos;
258 $i = $this->mReadIndex
;
261 if ( !$this->doWait( $i ) ) {
262 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
263 $this->mLaggedSlaveMode
= true;
267 wfProfileOut( $fname );
271 * Wait for a given slave to catch up to the master pos stored in $this
273 function doWait( $index ) {
279 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
281 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
285 $key = 'masterpos:' . $index;
286 $memcPos = $wgMemc->get( $key );
288 list( $file, $pos ) = explode( ' ', $memcPos );
289 # If the saved position is later than the requested position, return now
290 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
295 if ( !$retVal && $this->isOpen( $index ) ) {
296 $conn =& $this->mConnections
[$index];
297 wfDebug( "Waiting for slave #$index to catch up...\n" );
298 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
300 if ( $result == -1 ||
is_null( $result ) ) {
301 # Timed out waiting for slave, use master instead
302 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
313 * Get a connection by index
315 function &getConnection( $i, $fail = true, $groups = array() )
318 $fname = 'LoadBalancer::getConnection';
319 wfProfileIn( $fname );
323 if ( !is_array( $groups ) ) {
324 $groupIndex = $this->getGroupIndex( $groups, $i );
325 if ( $groupIndex !== false ) {
329 foreach ( $groups as $group ) {
330 $groupIndex = $this->getGroupIndex( $group, $i );
331 if ( $groupIndex !== false ) {
338 # For now, only go through all this for mysql databases
339 if ($wgDBtype != 'mysql') {
340 $i = $this->getWriterIndex();
342 # Operation-based index
343 elseif ( $i == DB_SLAVE
) {
344 $i = $this->getReaderIndex();
345 } elseif ( $i == DB_MASTER
) {
346 $i = $this->getWriterIndex();
347 } elseif ( $i == DB_LAST
) {
348 # Just use $this->mLastIndex, which should already be set
349 $i = $this->mLastIndex
;
351 # Oh dear, not set, best to use the writer for safety
352 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
353 $i = $this->getWriterIndex();
356 # Couldn't find a working server in getReaderIndex()?
357 if ( $i === false ) {
358 $this->reportConnectionError( $this->mErrorConnection
);
360 # Now we have an explicit index into the servers array
361 $this->openConnection( $i, $fail );
363 wfProfileOut( $fname );
364 return $this->mConnections
[$i];
368 * Open a connection to the server given by the specified index
369 * Index must be an actual index into the array
373 function openConnection( $i, $fail = false ) {
374 $fname = 'LoadBalancer::openConnection';
375 wfProfileIn( $fname );
378 if ( !$this->isOpen( $i ) ) {
379 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
382 if ( !$this->isOpen( $i ) ) {
383 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
385 $this->reportConnectionError( $this->mConnections
[$i] );
387 $this->mErrorConnection
= $this->mConnections
[$i];
388 $this->mConnections
[$i] = false;
391 $this->mLastIndex
= $i;
392 wfProfileOut( $fname );
397 * Test if the specified index represents an open connection
400 function isOpen( $index ) {
401 if( !is_integer( $index ) ) {
404 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
405 $this->mConnections
[$index]->isOpen() )
414 * Really opens a connection
417 function reallyOpenConnection( &$server ) {
418 if( !is_array( $server ) ) {
419 throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
423 # Get class for this database type
424 $class = 'Database' . ucfirst( $type );
427 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
428 $db->setLBInfo( $server );
432 function reportConnectionError( &$conn )
434 $fname = 'LoadBalancer::reportConnectionError';
435 wfProfileIn( $fname );
436 # Prevent infinite recursion
438 static $reporting = false;
441 if ( !is_object( $conn ) ) {
442 // No last connection, probably due to all servers being too busy
443 $conn = new Database
;
444 if ( $this->mFailFunction
) {
445 $conn->failFunction( $this->mFailFunction
);
446 $conn->reportConnectionError( $this->mLastError
);
448 // If all servers were busy, mLastError will contain something sensible
449 throw new DBConnectionError( $conn, $this->mLastError
);
452 if ( $this->mFailFunction
) {
453 $conn->failFunction( $this->mFailFunction
);
455 $conn->failFunction( false );
457 $server = $conn->getProperty( 'mServer' );
458 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
462 wfProfileOut( $fname );
465 function getWriterIndex() {
470 * Force subsequent calls to getConnection(DB_SLAVE) to return the
471 * given index. Set to -1 to restore the original load balancing
472 * behaviour. I thought this was a good idea when I originally
473 * wrote this class, but it has never been used.
475 function force( $i ) {
480 * Returns true if the specified index is a valid server index
482 function haveIndex( $i ) {
483 return array_key_exists( $i, $this->mServers
);
487 * Returns true if the specified index is valid and has non-zero load
489 function isNonZeroLoad( $i ) {
490 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
494 * Get the number of defined servers (not the number of open connections)
496 function getServerCount() {
497 return count( $this->mServers
);
501 * Save master pos to the session and to memcached, if the session exists
503 function saveMasterPos() {
504 global $wgSessionStarted;
505 if ( $wgSessionStarted && 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__
);