* (bug 10721) Duplicate section anchors with differing case now disambiguated
[mediawiki.git] / includes / LoadBalancer.php
blob0cdadd1e8e1c4135982428b3fc89cf7f09ac10a3
1 <?php
2 /**
4 */
7 /**
8 * Database load balancing object
10 * @todo document
12 class LoadBalancer {
13 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
14 /* private */ var $mFailFunction, $mErrorConnection;
15 /* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
16 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
17 /* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';
19 function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
21 $this->mServers = $servers;
22 $this->mFailFunction = $failFunction;
23 $this->mReadIndex = -1;
24 $this->mWriteIndex = -1;
25 $this->mForce = -1;
26 $this->mConnections = array();
27 $this->mLastIndex = -1;
28 $this->mLoads = array();
29 $this->mWaitForFile = false;
30 $this->mWaitForPos = false;
31 $this->mWaitTimeout = $waitTimeout;
32 $this->mLaggedSlaveMode = false;
33 $this->mErrorConnection = false;
34 $this->mAllowLag = false;
36 foreach( $servers as $i => $server ) {
37 $this->mLoads[$i] = $server['load'];
38 if ( isset( $server['groupLoads'] ) ) {
39 foreach ( $server['groupLoads'] as $group => $ratio ) {
40 if ( !isset( $this->mGroupLoads[$group] ) ) {
41 $this->mGroupLoads[$group] = array();
43 $this->mGroupLoads[$group][$i] = $ratio;
47 if ( $waitForMasterNow ) {
48 $this->loadMasterPos();
52 static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
54 return new LoadBalancer( $servers, $failFunction, $waitTimeout );
57 /**
58 * Given an array of non-normalised probabilities, this function will select
59 * an element and return the appropriate key
61 function pickRandom( $weights )
63 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
64 return false;
67 $sum = array_sum( $weights );
68 if ( $sum == 0 ) {
69 # No loads on any of them
70 # In previous versions, this triggered an unweighted random selection,
71 # but this feature has been removed as of April 2006 to allow for strict
72 # separation of query groups.
73 return false;
75 $max = mt_getrandmax();
76 $rand = mt_rand(0, $max) / $max * $sum;
78 $sum = 0;
79 foreach ( $weights as $i => $w ) {
80 $sum += $w;
81 if ( $sum >= $rand ) {
82 break;
85 return $i;
88 function getRandomNonLagged( $loads ) {
89 # Unset excessively lagged servers
90 $lags = $this->getLagTimes();
91 foreach ( $lags as $i => $lag ) {
92 if ( $i != 0 && isset( $this->mServers[$i]['max lag'] ) &&
93 ( $lag === false || $lag > $this->mServers[$i]['max lag'] ) )
95 unset( $loads[$i] );
99 # Find out if all the slaves with non-zero load are lagged
100 $sum = 0;
101 foreach ( $loads as $load ) {
102 $sum += $load;
104 if ( $sum == 0 ) {
105 # No appropriate DB servers except maybe the master and some slaves with zero load
106 # Do NOT use the master
107 # Instead, this function will return false, triggering read-only mode,
108 # and a lagged slave will be used instead.
109 return false;
112 if ( count( $loads ) == 0 ) {
113 return false;
116 #wfDebugLog( 'connect', var_export( $loads, true ) );
118 # Return a random representative of the remainder
119 return $this->pickRandom( $loads );
123 * Get the index of the reader connection, which may be a slave
124 * This takes into account load ratios and lag times. It should
125 * always return a consistent index during a given invocation
127 * Side effect: opens connections to databases
129 function getReaderIndex() {
130 global $wgReadOnly, $wgDBClusterTimeout, $wgDBAvgStatusPoll;
132 $fname = 'LoadBalancer::getReaderIndex';
133 wfProfileIn( $fname );
135 $i = false;
136 if ( $this->mForce >= 0 ) {
137 $i = $this->mForce;
138 } elseif ( count( $this->mServers ) == 1 ) {
139 # Skip the load balancing if there's only one server
140 $i = 0;
141 } else {
142 if ( $this->mReadIndex >= 0 ) {
143 $i = $this->mReadIndex;
144 } else {
145 # $loads is $this->mLoads except with elements knocked out if they
146 # don't work
147 $loads = $this->mLoads;
148 $done = false;
149 $totalElapsed = 0;
150 do {
151 if ( $wgReadOnly or $this->mAllowLagged ) {
152 $i = $this->pickRandom( $loads );
153 } else {
154 $i = $this->getRandomNonLagged( $loads );
155 if ( $i === false && count( $loads ) != 0 ) {
156 # All slaves lagged. Switch to read-only mode
157 $wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
158 $i = $this->pickRandom( $loads );
161 $serverIndex = $i;
162 if ( $i !== false ) {
163 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
164 $this->openConnection( $i );
166 if ( !$this->isOpen( $i ) ) {
167 wfDebug( "$fname: Failed\n" );
168 unset( $loads[$i] );
169 $sleepTime = 0;
170 } else {
171 if ( isset( $this->mServers[$i]['max threads'] ) ) {
172 $status = $this->mConnections[$i]->getStatus("Thread%");
173 if ( $status['Threads_running'] > $this->mServers[$i]['max threads'] ) {
174 # Too much load, back off and wait for a while.
175 # The sleep time is scaled by the number of threads connected,
176 # to produce a roughly constant global poll rate.
177 $sleepTime = $wgDBAvgStatusPoll * $status['Threads_connected'];
179 # If we reach the timeout and exit the loop, don't use it
180 $i = false;
181 } else {
182 $done = true;
183 $sleepTime = 0;
185 } else {
186 $done = true;
187 $sleepTime = 0;
190 } else {
191 $sleepTime = 500000;
193 if ( $sleepTime ) {
194 $totalElapsed += $sleepTime;
195 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
196 wfProfileIn( "$fname-sleep $x" );
197 usleep( $sleepTime );
198 wfProfileOut( "$fname-sleep $x" );
200 } while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
202 if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
203 $this->mErrorConnection = false;
204 $this->mLastError = 'All servers busy';
207 if ( $i !== false && $this->isOpen( $i ) ) {
208 # Wait for the session master pos for a short time
209 if ( $this->mWaitForFile ) {
210 if ( !$this->doWait( $i ) ) {
211 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
214 if ( $i !== false ) {
215 $this->mReadIndex = $i;
217 } else {
218 $i = false;
222 wfProfileOut( $fname );
223 return $i;
227 * Get a random server to use in a query group
229 function getGroupIndex( $group ) {
230 if ( isset( $this->mGroupLoads[$group] ) ) {
231 $i = $this->pickRandom( $this->mGroupLoads[$group] );
232 } else {
233 $i = false;
235 wfDebug( "Query group $group => $i\n" );
236 return $i;
240 * Set the master wait position
241 * If a DB_SLAVE connection has been opened already, waits
242 * Otherwise sets a variable telling it to wait if such a connection is opened
244 function waitFor( $file, $pos ) {
245 $fname = 'LoadBalancer::waitFor';
246 wfProfileIn( $fname );
248 wfDebug( "User master pos: $file $pos\n" );
249 $this->mWaitForFile = false;
250 $this->mWaitForPos = false;
252 if ( count( $this->mServers ) > 1 ) {
253 $this->mWaitForFile = $file;
254 $this->mWaitForPos = $pos;
255 $i = $this->mReadIndex;
257 if ( $i > 0 ) {
258 if ( !$this->doWait( $i ) ) {
259 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
260 $this->mLaggedSlaveMode = true;
264 wfProfileOut( $fname );
268 * Wait for a given slave to catch up to the master pos stored in $this
270 function doWait( $index ) {
271 global $wgMemc;
273 $retVal = false;
275 # Debugging hacks
276 if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
277 return false;
278 } elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
279 return true;
282 $key = 'masterpos:' . $index;
283 $memcPos = $wgMemc->get( $key );
284 if ( $memcPos ) {
285 list( $file, $pos ) = explode( ' ', $memcPos );
286 # If the saved position is later than the requested position, return now
287 if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
288 $retVal = true;
292 if ( !$retVal && $this->isOpen( $index ) ) {
293 $conn =& $this->mConnections[$index];
294 wfDebug( "Waiting for slave #$index to catch up...\n" );
295 $result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, $this->mWaitTimeout );
297 if ( $result == -1 || is_null( $result ) ) {
298 # Timed out waiting for slave, use master instead
299 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
300 $retVal = false;
301 } else {
302 $retVal = true;
303 wfDebug( "Done\n" );
306 return $retVal;
310 * Get a connection by index
312 function &getConnection( $i, $fail = true, $groups = array() )
314 global $wgDBtype;
315 $fname = 'LoadBalancer::getConnection';
316 wfProfileIn( $fname );
319 # Query groups
320 if ( !is_array( $groups ) ) {
321 $groupIndex = $this->getGroupIndex( $groups );
322 if ( $groupIndex !== false ) {
323 $i = $groupIndex;
325 } else {
326 foreach ( $groups as $group ) {
327 $groupIndex = $this->getGroupIndex( $group );
328 if ( $groupIndex !== false ) {
329 $i = $groupIndex;
330 break;
335 # For now, only go through all this for mysql databases
336 if ($wgDBtype != 'mysql') {
337 $i = $this->getWriterIndex();
339 # Operation-based index
340 elseif ( $i == DB_SLAVE ) {
341 $i = $this->getReaderIndex();
342 } elseif ( $i == DB_MASTER ) {
343 $i = $this->getWriterIndex();
344 } elseif ( $i == DB_LAST ) {
345 # Just use $this->mLastIndex, which should already be set
346 $i = $this->mLastIndex;
347 if ( $i === -1 ) {
348 # Oh dear, not set, best to use the writer for safety
349 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
350 $i = $this->getWriterIndex();
353 # Couldn't find a working server in getReaderIndex()?
354 if ( $i === false ) {
355 $this->reportConnectionError( $this->mErrorConnection );
357 # Now we have an explicit index into the servers array
358 $this->openConnection( $i, $fail );
360 wfProfileOut( $fname );
361 return $this->mConnections[$i];
365 * Open a connection to the server given by the specified index
366 * Index must be an actual index into the array
367 * Returns success
368 * @access private
370 function openConnection( $i, $fail = false ) {
371 $fname = 'LoadBalancer::openConnection';
372 wfProfileIn( $fname );
373 $success = true;
375 if ( !$this->isOpen( $i ) ) {
376 $this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
379 if ( !$this->isOpen( $i ) ) {
380 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
381 if ( $fail ) {
382 $this->reportConnectionError( $this->mConnections[$i] );
384 $this->mErrorConnection = $this->mConnections[$i];
385 $this->mConnections[$i] = false;
386 $success = false;
388 $this->mLastIndex = $i;
389 wfProfileOut( $fname );
390 return $success;
394 * Test if the specified index represents an open connection
395 * @access private
397 function isOpen( $index ) {
398 if( !is_integer( $index ) ) {
399 return false;
401 if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
402 $this->mConnections[$index]->isOpen() )
404 return true;
405 } else {
406 return false;
411 * Really opens a connection
412 * @access private
414 function reallyOpenConnection( &$server ) {
415 if( !is_array( $server ) ) {
416 throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
419 extract( $server );
420 # Get class for this database type
421 $class = 'Database' . ucfirst( $type );
423 # Create object
424 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
425 $db->setLBInfo( $server );
426 return $db;
429 function reportConnectionError( &$conn ) {
430 $fname = 'LoadBalancer::reportConnectionError';
431 wfProfileIn( $fname );
432 # Prevent infinite recursion
434 static $reporting = false;
435 if ( !$reporting ) {
436 $reporting = true;
437 if ( !is_object( $conn ) ) {
438 // No last connection, probably due to all servers being too busy
439 $conn = new Database;
440 if ( $this->mFailFunction ) {
441 $conn->failFunction( $this->mFailFunction );
442 $conn->reportConnectionError( $this->mLastError );
443 } else {
444 // If all servers were busy, mLastError will contain something sensible
445 throw new DBConnectionError( $conn, $this->mLastError );
447 } else {
448 if ( $this->mFailFunction ) {
449 $conn->failFunction( $this->mFailFunction );
450 } else {
451 $conn->failFunction( false );
453 $server = $conn->getProperty( 'mServer' );
454 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
456 $reporting = false;
458 wfProfileOut( $fname );
461 function getWriterIndex() {
462 return 0;
466 * Force subsequent calls to getConnection(DB_SLAVE) to return the
467 * given index. Set to -1 to restore the original load balancing
468 * behaviour. I thought this was a good idea when I originally
469 * wrote this class, but it has never been used.
471 function force( $i ) {
472 $this->mForce = $i;
476 * Returns true if the specified index is a valid server index
478 function haveIndex( $i ) {
479 return array_key_exists( $i, $this->mServers );
483 * Returns true if the specified index is valid and has non-zero load
485 function isNonZeroLoad( $i ) {
486 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
490 * Get the number of defined servers (not the number of open connections)
492 function getServerCount() {
493 return count( $this->mServers );
497 * Save master pos to the session and to memcached, if the session exists
499 function saveMasterPos() {
500 if ( session_id() != '' && count( $this->mServers ) > 1 ) {
501 # If this entire request was served from a slave without opening a connection to the
502 # master (however unlikely that may be), then we can fetch the position from the slave.
503 if ( empty( $this->mConnections[0] ) ) {
504 $conn =& $this->getConnection( DB_SLAVE );
505 list( $file, $pos ) = $conn->getSlavePos();
506 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
507 } else {
508 $conn =& $this->getConnection( 0 );
509 list( $file, $pos ) = $conn->getMasterPos();
510 wfDebug( "Saving master pos: $file $pos\n" );
512 if ( $file !== false ) {
513 $_SESSION['master_log_file'] = $file;
514 $_SESSION['master_pos'] = $pos;
520 * Loads the master pos from the session, waits for it if necessary
522 function loadMasterPos() {
523 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
524 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
529 * Close all open connections
531 function closeAll() {
532 foreach( $this->mConnections as $i => $conn ) {
533 if ( $this->isOpen( $i ) ) {
534 // Need to use this syntax because $conn is a copy not a reference
535 $this->mConnections[$i]->close();
540 function commitAll() {
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]->immediateCommit();
549 /* Issue COMMIT only on master, only if queries were done on connection */
550 function commitMasterChanges() {
551 // Always 0, but who knows.. :)
552 $i = $this->getWriterIndex();
553 if (array_key_exists($i,$this->mConnections)) {
554 if ($this->mConnections[$i]->lastQuery() != '') {
555 $this->mConnections[$i]->immediateCommit();
560 function waitTimeout( $value = NULL ) {
561 return wfSetVar( $this->mWaitTimeout, $value );
564 function getLaggedSlaveMode() {
565 return $this->mLaggedSlaveMode;
568 /* Disables/enables lag checks */
569 function allowLagged($mode=null) {
570 if ($mode===null)
571 return $this->mAllowLagged;
572 $this->mAllowLagged=$mode;
575 function pingAll() {
576 $success = true;
577 foreach ( $this->mConnections as $i => $conn ) {
578 if ( $this->isOpen( $i ) ) {
579 if ( !$this->mConnections[$i]->ping() ) {
580 $success = false;
584 return $success;
588 * Get the hostname and lag time of the most-lagged slave
589 * This is useful for maintenance scripts that need to throttle their updates
591 function getMaxLag() {
592 $maxLag = -1;
593 $host = '';
594 foreach ( $this->mServers as $i => $conn ) {
595 if ( $this->openConnection( $i ) ) {
596 $lag = $this->mConnections[$i]->getLag();
597 if ( $lag > $maxLag ) {
598 $maxLag = $lag;
599 $host = $this->mServers[$i]['host'];
603 return array( $host, $maxLag );
607 * Get lag time for each DB
608 * Results are cached for a short time in memcached
610 function getLagTimes() {
611 wfProfileIn( __METHOD__ );
612 $expiry = 5;
613 $requestRate = 10;
615 global $wgMemc;
616 $times = $wgMemc->get( wfMemcKey( 'lag_times' ) );
617 if ( $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__ );
624 return $times;
626 wfIncrStats( 'lag_cache_miss_expired' );
627 } else {
628 wfIncrStats( 'lag_cache_miss_absent' );
631 # Cache key missing or expired
633 $times = array();
634 foreach ( $this->mServers as $i => $conn ) {
635 if ($i==0) { # Master
636 $times[$i] = 0;
637 } elseif ( $this->openConnection( $i ) ) {
638 $times[$i] = $this->mConnections[$i]->getLag();
642 # Add a timestamp key so we know when it was cached
643 $times['timestamp'] = time();
644 $wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );
646 # But don't give the timestamp to the caller
647 unset($times['timestamp']);
648 wfProfileOut( __METHOD__ );
649 return $times;