8 * Depends on the database object
11 # Valid database indexes
12 # Operation-based indexes
13 define( 'DB_SLAVE', -1 ); # Read from the slave (or only server)
14 define( 'DB_MASTER', -2 ); # Write to master (or only server)
15 define( 'DB_LAST', -3 ); # Whatever database was used last
18 define( 'DB_READ', -1 );
19 define( 'DB_WRITE', -2 );
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
24 define( 'AVG_STATUS_POLL', 2000 );
28 * Database load balancing object
34 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
35 /* private */ var $mFailFunction, $mErrorConnection;
36 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
37 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
38 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
40 function LoadBalancer()
42 $this->mServers
= array();
43 $this->mConnections
= array();
44 $this->mFailFunction
= false;
45 $this->mReadIndex
= -1;
47 $this->mLastIndex
= -1;
48 $this->mErrorConnection
= false;
49 $this->mAllowLag
= false;
52 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
54 $lb = new LoadBalancer
;
55 $lb->initialise( $servers, $failFunction, $waitTimeout );
59 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
61 $this->mServers
= $servers;
62 $this->mFailFunction
= $failFunction;
63 $this->mReadIndex
= -1;
64 $this->mWriteIndex
= -1;
66 $this->mConnections
= array();
67 $this->mLastIndex
= 1;
68 $this->mLoads
= array();
69 $this->mWaitForFile
= false;
70 $this->mWaitForPos
= false;
71 $this->mWaitTimeout
= $waitTimeout;
72 $this->mLaggedSlaveMode
= false;
74 foreach( $servers as $i => $server ) {
75 $this->mLoads
[$i] = $server['load'];
76 if ( isset( $server['groupLoads'] ) ) {
77 foreach ( $server['groupLoads'] as $group => $ratio ) {
78 if ( !isset( $this->mGroupLoads
[$group] ) ) {
79 $this->mGroupLoads
[$group] = array();
81 $this->mGroupLoads
[$group][$i] = $ratio;
88 * Given an array of non-normalised probabilities, this function will select
89 * an element and return the appropriate key
91 function pickRandom( $weights )
93 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
97 $sum = array_sum( $weights );
99 # No loads on any of them
100 # In previous versions, this triggered an unweighted random selection,
101 # but this feature has been removed as of April 2006 to allow for strict
102 # separation of query groups.
105 $max = mt_getrandmax();
106 $rand = mt_rand(0, $max) / $max * $sum;
109 foreach ( $weights as $i => $w ) {
111 if ( $sum >= $rand ) {
118 function getRandomNonLagged( $loads ) {
119 # Unset excessively lagged servers
120 $lags = $this->getLagTimes();
121 foreach ( $lags as $i => $lag ) {
122 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
127 # Find out if all the slaves with non-zero load are lagged
129 foreach ( $loads as $load ) {
133 # No appropriate DB servers except maybe the master and some slaves with zero load
134 # Do NOT use the master
135 # Instead, this function will return false, triggering read-only mode,
136 # and a lagged slave will be used instead.
140 if ( count( $loads ) == 0 ) {
144 #wfDebugLog( 'connect', var_export( $loads, true ) );
146 # Return a random representative of the remainder
147 return $this->pickRandom( $loads );
151 * Get the index of the reader connection, which may be a slave
152 * This takes into account load ratios and lag times. It should
153 * always return a consistent index during a given invocation
155 * Side effect: opens connections to databases
157 function getReaderIndex() {
158 global $wgReadOnly, $wgDBClusterTimeout;
160 $fname = 'LoadBalancer::getReaderIndex';
161 wfProfileIn( $fname );
164 if ( $this->mForce
>= 0 ) {
167 if ( $this->mReadIndex
>= 0 ) {
168 $i = $this->mReadIndex
;
170 # $loads is $this->mLoads except with elements knocked out if they
172 $loads = $this->mLoads
;
176 if ( $wgReadOnly or $this->mAllowLagged
) {
177 $i = $this->pickRandom( $loads );
179 $i = $this->getRandomNonLagged( $loads );
180 if ( $i === false && count( $loads ) != 0 ) {
181 # All slaves lagged. Switch to read-only mode
182 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
183 $i = $this->pickRandom( $loads );
187 if ( $i !== false ) {
188 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
189 $this->openConnection( $i );
191 if ( !$this->isOpen( $i ) ) {
192 wfDebug( "$fname: Failed\n" );
196 $status = $this->mConnections
[$i]->getStatus("Thread%");
197 if ( isset( $this->mServers
[$i]['max threads'] ) &&
198 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
200 # Too much load, back off and wait for a while.
201 # The sleep time is scaled by the number of threads connected,
202 # to produce a roughly constant global poll rate.
203 $sleepTime = AVG_STATUS_POLL
* $status['Threads_connected'];
205 # If we reach the timeout and exit the loop, don't use it
216 $totalElapsed +
= $sleepTime;
217 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
218 wfProfileIn( "$fname-sleep $x" );
219 usleep( $sleepTime );
220 wfProfileOut( "$fname-sleep $x" );
222 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
224 if ( $totalElapsed / 1e6
>= $wgDBClusterTimeout ) {
225 $this->mErrorConnection
= false;
226 $this->mLastError
= 'All servers busy';
229 if ( $i !== false && $this->isOpen( $i ) ) {
230 # Wait for the session master pos for a short time
231 if ( $this->mWaitForFile
) {
232 if ( !$this->doWait( $i ) ) {
233 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
236 if ( $i !== false ) {
237 $this->mReadIndex
= $i;
244 wfProfileOut( $fname );
249 * Get a random server to use in a query group
251 function getGroupIndex( $group ) {
252 if ( isset( $this->mGroupLoads
[$group] ) ) {
253 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
257 wfDebug( "Query group $group => $i\n" );
262 * Set the master wait position
263 * If a DB_SLAVE connection has been opened already, waits
264 * Otherwise sets a variable telling it to wait if such a connection is opened
266 function waitFor( $file, $pos ) {
267 $fname = 'LoadBalancer::waitFor';
268 wfProfileIn( $fname );
270 wfDebug( "User master pos: $file $pos\n" );
271 $this->mWaitForFile
= false;
272 $this->mWaitForPos
= false;
274 if ( count( $this->mServers
) > 1 ) {
275 $this->mWaitForFile
= $file;
276 $this->mWaitForPos
= $pos;
277 $i = $this->mReadIndex
;
280 if ( !$this->doWait( $i ) ) {
281 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
282 $this->mLaggedSlaveMode
= true;
286 wfProfileOut( $fname );
290 * Wait for a given slave to catch up to the master pos stored in $this
292 function doWait( $index ) {
298 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
300 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
304 $key = 'masterpos:' . $index;
305 $memcPos = $wgMemc->get( $key );
307 list( $file, $pos ) = explode( ' ', $memcPos );
308 # If the saved position is later than the requested position, return now
309 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
314 if ( !$retVal && $this->isOpen( $index ) ) {
315 $conn =& $this->mConnections
[$index];
316 wfDebug( "Waiting for slave #$index to catch up...\n" );
317 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
319 if ( $result == -1 ||
is_null( $result ) ) {
320 # Timed out waiting for slave, use master instead
321 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
332 * Get a connection by index
334 function &getConnection( $i, $fail = true, $groups = array() )
337 $fname = 'LoadBalancer::getConnection';
338 wfProfileIn( $fname );
342 if ( !is_array( $groups ) ) {
343 $groupIndex = $this->getGroupIndex( $groups, $i );
344 if ( $groupIndex !== false ) {
348 foreach ( $groups as $group ) {
349 $groupIndex = $this->getGroupIndex( $group, $i );
350 if ( $groupIndex !== false ) {
357 # For now, only go through all this for mysql databases
358 if ($wgDBtype != 'mysql') {
359 $i = $this->getWriterIndex();
361 # Operation-based index
362 elseif ( $i == DB_SLAVE
) {
363 $i = $this->getReaderIndex();
364 } elseif ( $i == DB_MASTER
) {
365 $i = $this->getWriterIndex();
366 } elseif ( $i == DB_LAST
) {
367 # Just use $this->mLastIndex, which should already be set
368 $i = $this->mLastIndex
;
370 # Oh dear, not set, best to use the writer for safety
371 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
372 $i = $this->getWriterIndex();
375 # Couldn't find a working server in getReaderIndex()?
376 if ( $i === false ) {
377 $this->reportConnectionError( $this->mErrorConnection
);
379 # Now we have an explicit index into the servers array
380 $this->openConnection( $i, $fail );
382 wfProfileOut( $fname );
383 return $this->mConnections
[$i];
387 * Open a connection to the server given by the specified index
388 * Index must be an actual index into the array
392 function openConnection( $i, $fail = false ) {
393 $fname = 'LoadBalancer::openConnection';
394 wfProfileIn( $fname );
397 if ( !$this->isOpen( $i ) ) {
398 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
401 if ( !$this->isOpen( $i ) ) {
402 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
404 $this->reportConnectionError( $this->mConnections
[$i] );
406 $this->mErrorConnection
= $this->mConnections
[$i];
407 $this->mConnections
[$i] = false;
410 $this->mLastIndex
= $i;
411 wfProfileOut( $fname );
416 * Test if the specified index represents an open connection
419 function isOpen( $index ) {
420 if( !is_integer( $index ) ) {
423 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
424 $this->mConnections
[$index]->isOpen() )
433 * Really opens a connection
436 function reallyOpenConnection( &$server ) {
437 if( !is_array( $server ) ) {
438 wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
443 # Get class for this database type
444 if ($type != 'mysql' ) {
445 $class = 'Database' . ucfirst( $type );
451 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
452 $db->setLBInfo( $server );
456 function reportConnectionError( &$conn )
458 $fname = 'LoadBalancer::reportConnectionError';
459 wfProfileIn( $fname );
460 # Prevent infinite recursion
462 static $reporting = false;
465 if ( !is_object( $conn ) ) {
466 // No last connection, probably due to all servers being too busy
467 $conn = new Database
;
468 if ( $this->mFailFunction
) {
469 $conn->failFunction( $this->mFailFunction
);
470 $conn->reportConnectionError( $this->mLastError
);
472 // If all servers were busy, mLastError will contain something sensible
473 wfEmergencyAbort( $conn, $this->mLastError
);
476 if ( $this->mFailFunction
) {
477 $conn->failFunction( $this->mFailFunction
);
479 $conn->failFunction( false );
481 $conn->reportConnectionError( "{$this->mLastError} ({$conn->mServer})" );
485 wfProfileOut( $fname );
488 function getWriterIndex()
498 function haveIndex( $i )
500 return array_key_exists( $i, $this->mServers
);
504 * Get the number of defined servers (not the number of open connections)
506 function getServerCount() {
507 return count( $this->mServers
);
511 * Save master pos to the session and to memcached, if the session exists
513 function saveMasterPos() {
514 global $wgSessionStarted;
515 if ( $wgSessionStarted && count( $this->mServers
) > 1 ) {
516 # If this entire request was served from a slave without opening a connection to the
517 # master (however unlikely that may be), then we can fetch the position from the slave.
518 if ( empty( $this->mConnections
[0] ) ) {
519 $conn =& $this->getConnection( DB_SLAVE
);
520 list( $file, $pos ) = $conn->getSlavePos();
521 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
523 $conn =& $this->getConnection( 0 );
524 list( $file, $pos ) = $conn->getMasterPos();
525 wfDebug( "Saving master pos: $file $pos\n" );
527 if ( $file !== false ) {
528 $_SESSION['master_log_file'] = $file;
529 $_SESSION['master_pos'] = $pos;
535 * Loads the master pos from the session, waits for it if necessary
537 function loadMasterPos() {
538 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
539 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
544 * Close all open connections
546 function closeAll() {
547 foreach( $this->mConnections
as $i => $conn ) {
548 if ( $this->isOpen( $i ) ) {
549 // Need to use this syntax because $conn is a copy not a reference
550 $this->mConnections
[$i]->close();
555 function commitAll() {
556 foreach( $this->mConnections
as $i => $conn ) {
557 if ( $this->isOpen( $i ) ) {
558 // Need to use this syntax because $conn is a copy not a reference
559 $this->mConnections
[$i]->immediateCommit();
564 function waitTimeout( $value = NULL ) {
565 return wfSetVar( $this->mWaitTimeout
, $value );
568 function getLaggedSlaveMode() {
569 return $this->mLaggedSlaveMode
;
572 /* Disables/enables lag checks */
573 function allowLagged($mode=null) {
575 return $this->mAllowLagged
;
576 $this->mAllowLagged
=$mode;
581 foreach ( $this->mConnections
as $i => $conn ) {
582 if ( $this->isOpen( $i ) ) {
583 if ( !$this->mConnections
[$i]->ping() ) {
592 * Get the hostname and lag time of the most-lagged slave
593 * This is useful for maintenance scripts that need to throttle their updates
595 function getMaxLag() {
598 foreach ( $this->mServers
as $i => $conn ) {
599 if ( $this->openConnection( $i ) ) {
600 $lag = $this->mConnections
[$i]->getLag();
601 if ( $lag > $maxLag ) {
603 $host = $this->mServers
[$i]['host'];
607 return array( $host, $maxLag );
611 * Get lag time for each DB
612 * Results are cached for a short time in memcached
614 function getLagTimes() {
621 $times = $wgMemc->get( "$wgDBname:lag_times" );
623 # Randomly recache with probability rising over $expiry
624 $elapsed = time() - $times['timestamp'];
625 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
626 if ( mt_rand( 0, $chance ) != 0 ) {
627 unset( $times['timestamp'] );
632 # Cache key missing or expired
635 foreach ( $this->mServers
as $i => $conn ) {
636 if ($i==0) { # Master
638 } elseif ( $this->openConnection( $i ) ) {
639 $times[$i] = $this->mConnections
[$i]->getLag();
643 # Add a timestamp key so we know when it was cached
644 $times['timestamp'] = time();
645 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
647 # But don't give the timestamp to the caller
648 unset($times['timestamp']);