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 ) {
99 foreach ( $weights as $w ) {
104 # No loads on any of them
105 # Just pick one at random
106 foreach ( $weights as $i => $w ) {
110 $max = mt_getrandmax();
111 $rand = mt_rand(0, $max) / $max * $sum;
114 foreach ( $weights as $i => $w ) {
116 if ( $sum >= $rand ) {
123 function getRandomNonLagged( $loads ) {
124 # Unset excessively lagged servers
125 $lags = $this->getLagTimes();
126 foreach ( $lags as $i => $lag ) {
127 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
133 # Find out if all the slaves with non-zero load are lagged
135 foreach ( $loads as $load ) {
139 # No appropriate DB servers except maybe the master and some slaves with zero load
140 # Do NOT use the master
141 # Instead, this function will return false, triggering read-only mode,
142 # and a lagged slave will be used instead.
146 if ( count( $loads ) == 0 ) {
150 #wfDebugLog( 'connect', var_export( $loads, true ) );
152 # Return a random representative of the remainder
153 return $this->pickRandom( $loads );
157 * Get the index of the reader connection, which may be a slave
158 * This takes into account load ratios and lag times. It should
159 * always return a consistent index during a given invocation
161 * Side effect: opens connections to databases
163 function getReaderIndex() {
164 global $wgReadOnly, $wgDBClusterTimeout;
166 $fname = 'LoadBalancer::getReaderIndex';
167 wfProfileIn( $fname );
170 if ( $this->mForce
>= 0 ) {
173 if ( $this->mReadIndex
>= 0 ) {
174 $i = $this->mReadIndex
;
176 # $loads is $this->mLoads except with elements knocked out if they
178 $loads = $this->mLoads
;
182 if ( $wgReadOnly or $this->mAllowLagged
) {
183 $i = $this->pickRandom( $loads );
185 $i = $this->getRandomNonLagged( $loads );
186 if ( $i === false && count( $loads ) != 0 ) {
187 # All slaves lagged. Switch to read-only mode
188 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
189 $i = $this->pickRandom( $loads );
193 if ( $i !== false ) {
194 wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
195 $this->openConnection( $i );
197 if ( !$this->isOpen( $i ) ) {
198 wfDebug( "Failed\n" );
202 $status = $this->mConnections
[$i]->getStatus("Thread%");
203 if ( isset( $this->mServers
[$i]['max threads'] ) &&
204 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
206 # Slave is lagged, wait for a while
207 $sleepTime = AVG_STATUS_POLL
* $status['Threads_connected'];
209 # If we reach the timeout and exit the loop, don't use it
220 $totalElapsed +
= $sleepTime;
221 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
222 wfProfileIn( "$fname-sleep $x" );
223 usleep( $sleepTime );
224 wfProfileOut( "$fname-sleep $x" );
226 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
228 if ( $totalElapsed / 1e6
>= $wgDBClusterTimeout ) {
229 $this->mErrorConnection
= false;
230 $this->mLastError
= 'All servers busy';
233 if ( $i !== false && $this->isOpen( $i ) ) {
234 # Wait for the session master pos for a short time
235 if ( $this->mWaitForFile
) {
236 if ( !$this->doWait( $i ) ) {
237 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
240 if ( $i !== false ) {
241 $this->mReadIndex
= $i;
248 wfProfileOut( $fname );
253 * Get a random server to use in a query group
255 function getGroupIndex( $group ) {
256 if ( isset( $this->mGroupLoads
[$group] ) ) {
257 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
261 wfDebug( "Query group $group => $i\n" );
266 * Set the master wait position
267 * If a DB_SLAVE connection has been opened already, waits
268 * Otherwise sets a variable telling it to wait if such a connection is opened
270 function waitFor( $file, $pos ) {
271 $fname = 'LoadBalancer::waitFor';
272 wfProfileIn( $fname );
274 wfDebug( "User master pos: $file $pos\n" );
275 $this->mWaitForFile
= false;
276 $this->mWaitForPos
= false;
278 if ( count( $this->mServers
) > 1 ) {
279 $this->mWaitForFile
= $file;
280 $this->mWaitForPos
= $pos;
281 $i = $this->mReadIndex
;
284 if ( !$this->doWait( $i ) ) {
285 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
286 $this->mLaggedSlaveMode
= true;
290 wfProfileOut( $fname );
294 * Wait for a given slave to catch up to the master pos stored in $this
296 function doWait( $index ) {
302 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
304 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
308 $key = 'masterpos:' . $index;
309 $memcPos = $wgMemc->get( $key );
311 list( $file, $pos ) = explode( ' ', $memcPos );
312 # If the saved position is later than the requested position, return now
313 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
318 if ( !$retVal && $this->isOpen( $index ) ) {
319 $conn =& $this->mConnections
[$index];
320 wfDebug( "Waiting for slave #$index to catch up...\n" );
321 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
323 if ( $result == -1 ||
is_null( $result ) ) {
324 # Timed out waiting for slave, use master instead
325 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
336 * Get a connection by index
338 function &getConnection( $i, $fail = true, $groups = array() )
340 $fname = 'LoadBalancer::getConnection';
341 wfProfileIn( $fname );
345 foreach ( $groups as $group ) {
346 $groupIndex = $this->getGroupIndex( $group );
347 if ( $groupIndex !== false ) {
353 # Operation-based index
354 if ( $i == DB_SLAVE
) {
355 $i = $this->getReaderIndex();
356 } elseif ( $i == DB_MASTER
) {
357 $i = $this->getWriterIndex();
358 } elseif ( $i == DB_LAST
) {
359 # Just use $this->mLastIndex, which should already be set
360 $i = $this->mLastIndex
;
362 # Oh dear, not set, best to use the writer for safety
363 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
364 $i = $this->getWriterIndex();
367 # Couldn't find a working server in getReaderIndex()?
368 if ( $i === false ) {
369 $this->reportConnectionError( $this->mErrorConnection
);
371 # Now we have an explicit index into the servers array
372 $this->openConnection( $i, $fail );
374 wfProfileOut( $fname );
375 return $this->mConnections
[$i];
379 * Open a connection to the server given by the specified index
380 * Index must be an actual index into the array
384 function openConnection( $i, $fail = false ) {
385 $fname = 'LoadBalancer::openConnection';
386 wfProfileIn( $fname );
389 if ( !$this->isOpen( $i ) ) {
390 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
393 if ( !$this->isOpen( $i ) ) {
394 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
396 $this->reportConnectionError( $this->mConnections
[$i] );
398 $this->mErrorConnection
= $this->mConnections
[$i];
399 $this->mConnections
[$i] = false;
402 $this->mLastIndex
= $i;
403 wfProfileOut( $fname );
408 * Test if the specified index represents an open connection
411 function isOpen( $index ) {
412 if( !is_integer( $index ) ) {
415 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
416 $this->mConnections
[$index]->isOpen() )
425 * Really opens a connection
428 function reallyOpenConnection( &$server ) {
429 if( !is_array( $server ) ) {
430 wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
434 # Get class for this database type
435 $class = 'Database' . ucfirst( $type );
436 if ( !class_exists( $class ) ) {
437 require_once( "$class.php" );
441 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
442 $db->setLBInfo( $server );
446 function reportConnectionError( &$conn )
448 $fname = 'LoadBalancer::reportConnectionError';
449 wfProfileIn( $fname );
450 # Prevent infinite recursion
452 static $reporting = false;
455 if ( !is_object( $conn ) ) {
456 // No last connection, probably due to all servers being too busy
457 $conn = new Database
;
458 if ( $this->mFailFunction
) {
459 $conn->failFunction( $this->mFailFunction
);
460 $conn->reportConnectionError( $this->mLastError
);
462 // If all servers were busy, mLastError will contain something sensible
463 wfEmergencyAbort( $conn, $this->mLastError
);
466 if ( $this->mFailFunction
) {
467 $conn->failFunction( $this->mFailFunction
);
469 $conn->failFunction( false );
471 $conn->reportConnectionError( "{$this->mLastError} ({$conn->mServer})" );
475 wfProfileOut( $fname );
478 function getWriterIndex()
488 function haveIndex( $i )
490 return array_key_exists( $i, $this->mServers
);
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() {
611 $times = $wgMemc->get( "$wgDBname:lag_times" );
613 # Randomly recache with probability rising over $expiry
614 $elapsed = time() - $times['timestamp'];
615 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
616 if ( mt_rand( 0, $chance ) != 0 ) {
617 unset( $times['timestamp'] );
622 # Cache key missing or expired
625 foreach ( $this->mServers
as $i => $conn ) {
626 if ($i==0) { # Master
628 } elseif ( $this->openConnection( $i ) ) {
629 $times[$i] = $this->mConnections
[$i]->getLag();
633 # Add a timestamp key so we know when it was cached
634 $times['timestamp'] = time();
635 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
637 # But don't give the timestamp to the caller
638 unset($times['timestamp']);