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 * Database load balancing object
29 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
30 /* private */ var $mFailFunction;
31 /* private */ var $mForce, $mReadIndex, $mLastIndex;
32 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
33 /* private */ var $mLaggedSlaveMode;
35 function LoadBalancer()
37 $this->mServers
= array();
38 $this->mConnections
= array();
39 $this->mFailFunction
= false;
40 $this->mReadIndex
= -1;
42 $this->mLastIndex
= -1;
45 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
47 $lb = new LoadBalancer
;
48 $lb->initialise( $servers, $failFunction, $waitTimeout );
52 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
54 $this->mServers
= $servers;
55 $this->mFailFunction
= $failFunction;
56 $this->mReadIndex
= -1;
57 $this->mWriteIndex
= -1;
59 $this->mConnections
= array();
60 $this->mLastIndex
= 1;
61 $this->mLoads
= array();
62 $this->mWaitForFile
= false;
63 $this->mWaitForPos
= false;
64 $this->mWaitTimeout
= $waitTimeout;
65 $this->mLaggedSlaveMode
= false;
67 foreach( $servers as $i => $server ) {
68 $this->mLoads
[$i] = $server['load'];
69 if ( isset( $server['groupLoads'] ) ) {
70 foreach ( $server['groupLoads'] as $group => $ratio ) {
71 if ( !isset( $this->mGroupLoads
[$group] ) ) {
72 $this->mGroupLoads
[$group] = array();
74 $this->mGroupLoads
[$group][$i] = $ratio;
81 * Given an array of non-normalised probabilities, this function will select
82 * an element and return the appropriate key
84 function pickRandom( $weights )
86 if ( !is_array( $weights ) ||
count( $weights ) == 0 ) {
91 foreach ( $weights as $w ) {
96 # No loads on any of them
97 # Just pick one at random
98 foreach ( $weights as $i => $w ) {
102 $max = mt_getrandmax();
103 $rand = mt_rand(0, $max) / $max * $sum;
106 foreach ( $weights as $i => $w ) {
108 if ( $sum >= $rand ) {
115 function getRandomNonLagged( $loads ) {
116 # Unset excessively lagged servers
117 $lags = $this->getLagTimes();
118 foreach ( $lags as $i => $lag ) {
119 if ( isset( $this->mServers
[$i]['max lag'] ) && $lag > $this->mServers
[$i]['max lag'] ) {
125 # Find out if all the slaves with non-zero load are lagged
127 foreach ( $loads as $load ) {
131 # No appropriate DB servers except maybe the master and some slaves with zero load
132 # Do NOT use the master
133 # Instead, this function will return false, triggering read-only mode,
134 # and a lagged slave will be used instead.
138 if ( count( $loads ) == 0 ) {
142 #wfDebug( var_export( $loads, true ) );
144 # Return a random representative of the remainder
145 return $this->pickRandom( $loads );
149 * Get the index of the reader connection, which may be a slave
150 * This takes into account load ratios and lag times. It should
151 * always return a consistent index during a given invocation
153 * Side effect: opens connections to databases
155 function getReaderIndex()
157 global $wgMaxLag, $wgReadOnly, $wgDBClusterTimeout;
159 $fname = 'LoadBalancer::getReaderIndex';
160 wfProfileIn( $fname );
163 if ( $this->mForce
>= 0 ) {
166 if ( $this->mReadIndex
>= 0 ) {
167 $i = $this->mReadIndex
;
169 # $loads is $this->mLoads except with elements knocked out if they
171 $loads = $this->mLoads
;
176 $i = $this->pickRandom( $loads );
178 $i = $this->getRandomNonLagged( $loads );
179 if ( $i === false && count( $loads ) != 0 ) {
180 # All slaves lagged. Switch to read-only mode
181 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
182 $i = $this->pickRandom( $loads );
185 if ( $i !== false ) {
186 wfDebug( "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
187 $this->openConnection( $i );
189 if ( !$this->isOpen( $i ) ) {
190 wfDebug( "Failed\n" );
194 $status = $this->mConnections
[$i]->getStatus();
195 if ( isset( $this->mServers
[$i]['max threads'] ) &&
196 $status['Threads_running'] > $this->mServers
[$i]['max threads'] )
198 # Slave is lagged, wait for a while
199 $sleepTime = 5000 * $status['Threads_connected'];
201 # If we reach the timeout and exit the loop, don't use it
212 $totalElapsed +
= $sleepTime;
213 usleep( $sleepTime );
215 } while ( count( $loads ) && !$done && $totalElapsed / 1e6
< $wgDBClusterTimeout );
217 if ( $i !== false && $this->isOpen( $i ) ) {
218 # Wait for the session master pos for a short time
219 if ( $this->mWaitForFile
) {
220 if ( !$this->doWait( $i ) ) {
221 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
224 if ( $i !== false ) {
225 $this->mReadIndex
= $i;
232 wfProfileOut( $fname );
237 * Get a random server to use in a query group
239 function getGroupIndex( $group ) {
240 if ( isset( $this->mGroupLoads
[$group] ) ) {
241 $i = $this->pickRandom( $this->mGroupLoads
[$group] );
245 wfDebug( "Query group $group => $i\n" );
250 * Set the master wait position
251 * If a DB_SLAVE connection has been opened already, waits
252 * Otherwise sets a variable telling it to wait if such a connection is opened
254 function waitFor( $file, $pos ) {
255 $fname = 'LoadBalancer::waitFor';
256 wfProfileIn( $fname );
258 wfDebug( "User master pos: $file $pos\n" );
259 $this->mWaitForFile
= false;
260 $this->mWaitForPos
= false;
262 if ( count( $this->mServers
) > 1 ) {
263 $this->mWaitForFile
= $file;
264 $this->mWaitForPos
= $pos;
265 $i = $this->mReadIndex
;
268 if ( !$this->doWait( $i ) ) {
269 $this->mServers
[$i]['slave pos'] = $this->mConnections
[$i]->getSlavePos();
270 $this->mLaggedSlaveMode
= true;
274 wfProfileOut( $fname );
278 * Wait for a given slave to catch up to the master pos stored in $this
280 function doWait( $index ) {
286 if ( isset( $this->mServers
[$index]['lagged slave'] ) ) {
288 } elseif ( isset( $this->mServers
[$index]['fake slave'] ) ) {
292 $key = 'masterpos:' . $index;
293 $memcPos = $wgMemc->get( $key );
295 list( $file, $pos ) = explode( ' ', $memcPos );
296 # If the saved position is later than the requested position, return now
297 if ( $file == $this->mWaitForFile
&& $this->mWaitForPos
<= $pos ) {
302 if ( !$retVal && $this->isOpen( $index ) ) {
303 $conn =& $this->mConnections
[$index];
304 wfDebug( "Waiting for slave #$index to catch up...\n" );
305 $result = $conn->masterPosWait( $this->mWaitForFile
, $this->mWaitForPos
, $this->mWaitTimeout
);
307 if ( $result == -1 ||
is_null( $result ) ) {
308 # Timed out waiting for slave, use master instead
309 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
320 * Get a connection by index
322 function &getConnection( $i, $fail = true, $groups = array() )
324 $fname = 'LoadBalancer::getConnection';
325 wfProfileIn( $fname );
329 foreach ( $groups as $group ) {
330 $groupIndex = $this->getGroupIndex( $group );
331 if ( $groupIndex !== false ) {
337 # Operation-based index
338 if ( $i == DB_SLAVE
) {
339 $i = $this->getReaderIndex();
340 } elseif ( $i == DB_MASTER
) {
341 $i = $this->getWriterIndex();
342 } elseif ( $i == DB_LAST
) {
343 # Just use $this->mLastIndex, which should already be set
344 $i = $this->mLastIndex
;
346 # Oh dear, not set, best to use the writer for safety
347 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
348 $i = $this->getWriterIndex();
351 # Now we have an explicit index into the servers array
352 $this->openConnection( $i, $fail );
354 wfProfileOut( $fname );
355 return $this->mConnections
[$i];
359 * Open a connection to the server given by the specified index
360 * Index must be an actual index into the array
364 function openConnection( $i, $fail = false ) {
365 $fname = 'LoadBalancer::openConnection';
366 wfProfileIn( $fname );
369 if ( !$this->isOpen( $i ) ) {
370 $this->mConnections
[$i] = $this->reallyOpenConnection( $this->mServers
[$i] );
372 if ( !$this->isOpen( $i ) ) {
373 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
375 $this->reportConnectionError( $this->mConnections
[$i] );
377 $this->mConnections
[$i] = false;
380 $this->mLastIndex
= $i;
381 wfProfileOut( $fname );
386 * Test if the specified index represents an open connection
389 function isOpen( $index ) {
390 if( !is_integer( $index ) ) {
393 if ( array_key_exists( $index, $this->mConnections
) && is_object( $this->mConnections
[$index] ) &&
394 $this->mConnections
[$index]->isOpen() )
403 * Really opens a connection
406 function reallyOpenConnection( &$server ) {
407 if( !is_array( $server ) ) {
408 wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
412 # Get class for this database type
413 $class = 'Database' . ucfirst( $type );
414 if ( !class_exists( $class ) ) {
415 require_once( "$class.php" );
419 return new $class( $host, $user, $password, $dbname, 1, $flags );
422 function reportConnectionError( &$conn )
424 $fname = 'LoadBalancer::reportConnectionError';
425 wfProfileIn( $fname );
426 # Prevent infinite recursion
428 static $reporting = false;
431 if ( !is_object( $conn ) ) {
432 $conn = new Database
;
434 if ( $this->mFailFunction
) {
435 $conn->failFunction( $this->mFailFunction
);
437 $conn->failFunction( 'wfEmergencyAbort' );
439 $conn->reportConnectionError();
442 wfProfileOut( $fname );
445 function getWriterIndex()
455 function haveIndex( $i )
457 return array_key_exists( $i, $this->mServers
);
461 * Get the number of defined servers (not the number of open connections)
463 function getServerCount() {
464 return count( $this->mServers
);
468 * Save master pos to the session and to memcached, if the session exists
470 function saveMasterPos() {
471 global $wgSessionStarted;
472 if ( $wgSessionStarted && count( $this->mServers
) > 1 ) {
473 # If this entire request was served from a slave without opening a connection to the
474 # master (however unlikely that may be), then we can fetch the position from the slave.
475 if ( empty( $this->mConnections
[0] ) ) {
476 $conn =& $this->getConnection( DB_SLAVE
);
477 list( $file, $pos ) = $conn->getSlavePos();
478 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
480 $conn =& $this->getConnection( 0 );
481 list( $file, $pos ) = $conn->getMasterPos();
482 wfDebug( "Saving master pos: $file $pos\n" );
484 if ( $file !== false ) {
485 $_SESSION['master_log_file'] = $file;
486 $_SESSION['master_pos'] = $pos;
492 * Loads the master pos from the session, waits for it if necessary
494 function loadMasterPos() {
495 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
496 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
501 * Close all open connections
503 function closeAll() {
504 foreach( $this->mConnections
as $i => $conn ) {
505 if ( $this->isOpen( $i ) ) {
506 // Need to use this syntax because $conn is a copy not a reference
507 $this->mConnections
[$i]->close();
512 function commitAll() {
513 foreach( $this->mConnections
as $i => $conn ) {
514 if ( $this->isOpen( $i ) ) {
515 // Need to use this syntax because $conn is a copy not a reference
516 $this->mConnections
[$i]->immediateCommit();
521 function waitTimeout( $value = NULL ) {
522 return wfSetVar( $this->mWaitTimeout
, $value );
525 function getLaggedSlaveMode() {
526 return $this->mLaggedSlaveMode
;
531 foreach ( $this->mConnections
as $i => $conn ) {
532 if ( $this->isOpen( $i ) ) {
533 if ( !$this->mConnections
[$i]->ping() ) {
542 * Get the hostname and lag time of the most-lagged slave
543 * This is useful for maintenance scripts that need to throttle their updates
545 function getMaxLag() {
548 foreach ( $this->mServers
as $i => $conn ) {
549 if ( $this->openConnection( $i ) ) {
550 $lag = $this->mConnections
[$i]->getLag();
551 if ( $lag > $maxLag ) {
553 $host = $this->mServers
[$i]['host'];
557 return array( $host, $maxLag );
561 * Get lag time for each DB
562 * Results are cached for a short time in memcached
564 function getLagTimes() {
569 $times = $wgMemc->get( 'lag_times' );
571 # Randomly recache with probability rising over $expiry
572 $elapsed = time() - $times['timestamp'];
573 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
574 if ( mt_rand( 0, $chance ) != 0 ) {
575 unset( $times['timestamp'] );
580 # Cache key missing or expired
583 foreach ( $this->mServers
as $i => $conn ) {
584 if ( $this->openConnection( $i ) ) {
585 $times[$i] = $this->mConnections
[$i]->getLag();
589 # Add a timestamp key so we know when it was cached
590 $times['timestamp'] = time();
591 $wgMemc->set( 'lag_times', $times, $expiry );
593 # But don't give the timestamp to the caller
594 unset($times['timestamp']);