Use AutoLoader to load classes:
[lhc/web/wiklou.git] / includes / LoadBalancer.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /**
8 * Depends on the database object
9 */
10
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
16
17 # Obsolete aliases
18 define( 'DB_READ', -1 );
19 define( 'DB_WRITE', -2 );
20
21
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 );
25
26
27 /**
28 * Database load balancing object
29 *
30 * @todo document
31 * @package MediaWiki
32 */
33 class LoadBalancer {
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';
39
40 function LoadBalancer()
41 {
42 $this->mServers = array();
43 $this->mConnections = array();
44 $this->mFailFunction = false;
45 $this->mReadIndex = -1;
46 $this->mForce = -1;
47 $this->mLastIndex = -1;
48 $this->mErrorConnection = false;
49 $this->mAllowLag = false;
50 }
51
52 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
53 {
54 $lb = new LoadBalancer;
55 $lb->initialise( $servers, $failFunction, $waitTimeout );
56 return $lb;
57 }
58
59 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
60 {
61 $this->mServers = $servers;
62 $this->mFailFunction = $failFunction;
63 $this->mReadIndex = -1;
64 $this->mWriteIndex = -1;
65 $this->mForce = -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;
73
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();
80 }
81 $this->mGroupLoads[$group][$i] = $ratio;
82 }
83 }
84 }
85 }
86
87 /**
88 * Given an array of non-normalised probabilities, this function will select
89 * an element and return the appropriate key
90 */
91 function pickRandom( $weights )
92 {
93 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
94 return false;
95 }
96
97 $sum = array_sum( $weights );
98 if ( $sum == 0 ) {
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.
103 return false;
104 }
105 $max = mt_getrandmax();
106 $rand = mt_rand(0, $max) / $max * $sum;
107
108 $sum = 0;
109 foreach ( $weights as $i => $w ) {
110 $sum += $w;
111 if ( $sum >= $rand ) {
112 break;
113 }
114 }
115 return $i;
116 }
117
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'] ) {
123 unset( $loads[$i] );
124 }
125 }
126
127 # Find out if all the slaves with non-zero load are lagged
128 $sum = 0;
129 foreach ( $loads as $load ) {
130 $sum += $load;
131 }
132 if ( $sum == 0 ) {
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.
137 return false;
138 }
139
140 if ( count( $loads ) == 0 ) {
141 return false;
142 }
143
144 #wfDebugLog( 'connect', var_export( $loads, true ) );
145
146 # Return a random representative of the remainder
147 return $this->pickRandom( $loads );
148 }
149
150 /**
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
154 *
155 * Side effect: opens connections to databases
156 */
157 function getReaderIndex() {
158 global $wgReadOnly, $wgDBClusterTimeout;
159
160 $fname = 'LoadBalancer::getReaderIndex';
161 wfProfileIn( $fname );
162
163 $i = false;
164 if ( $this->mForce >= 0 ) {
165 $i = $this->mForce;
166 } else {
167 if ( $this->mReadIndex >= 0 ) {
168 $i = $this->mReadIndex;
169 } else {
170 # $loads is $this->mLoads except with elements knocked out if they
171 # don't work
172 $loads = $this->mLoads;
173 $done = false;
174 $totalElapsed = 0;
175 do {
176 if ( $wgReadOnly or $this->mAllowLagged ) {
177 $i = $this->pickRandom( $loads );
178 } else {
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 );
184 }
185 }
186 $serverIndex = $i;
187 if ( $i !== false ) {
188 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
189 $this->openConnection( $i );
190
191 if ( !$this->isOpen( $i ) ) {
192 wfDebug( "$fname: Failed\n" );
193 unset( $loads[$i] );
194 $sleepTime = 0;
195 } else {
196 $status = $this->mConnections[$i]->getStatus("Thread%");
197 if ( isset( $this->mServers[$i]['max threads'] ) &&
198 $status['Threads_running'] > $this->mServers[$i]['max threads'] )
199 {
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'];
204
205 # If we reach the timeout and exit the loop, don't use it
206 $i = false;
207 } else {
208 $done = true;
209 $sleepTime = 0;
210 }
211 }
212 } else {
213 $sleepTime = 500000;
214 }
215 if ( $sleepTime ) {
216 $totalElapsed += $sleepTime;
217 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
218 wfProfileIn( "$fname-sleep $x" );
219 usleep( $sleepTime );
220 wfProfileOut( "$fname-sleep $x" );
221 }
222 } while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
223
224 if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
225 $this->mErrorConnection = false;
226 $this->mLastError = 'All servers busy';
227 }
228
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();
234 }
235 }
236 if ( $i !== false ) {
237 $this->mReadIndex = $i;
238 }
239 } else {
240 $i = false;
241 }
242 }
243 }
244 wfProfileOut( $fname );
245 return $i;
246 }
247
248 /**
249 * Get a random server to use in a query group
250 */
251 function getGroupIndex( $group ) {
252 if ( isset( $this->mGroupLoads[$group] ) ) {
253 $i = $this->pickRandom( $this->mGroupLoads[$group] );
254 } else {
255 $i = false;
256 }
257 wfDebug( "Query group $group => $i\n" );
258 return $i;
259 }
260
261 /**
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
265 */
266 function waitFor( $file, $pos ) {
267 $fname = 'LoadBalancer::waitFor';
268 wfProfileIn( $fname );
269
270 wfDebug( "User master pos: $file $pos\n" );
271 $this->mWaitForFile = false;
272 $this->mWaitForPos = false;
273
274 if ( count( $this->mServers ) > 1 ) {
275 $this->mWaitForFile = $file;
276 $this->mWaitForPos = $pos;
277 $i = $this->mReadIndex;
278
279 if ( $i > 0 ) {
280 if ( !$this->doWait( $i ) ) {
281 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
282 $this->mLaggedSlaveMode = true;
283 }
284 }
285 }
286 wfProfileOut( $fname );
287 }
288
289 /**
290 * Wait for a given slave to catch up to the master pos stored in $this
291 */
292 function doWait( $index ) {
293 global $wgMemc;
294
295 $retVal = false;
296
297 # Debugging hacks
298 if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
299 return false;
300 } elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
301 return true;
302 }
303
304 $key = 'masterpos:' . $index;
305 $memcPos = $wgMemc->get( $key );
306 if ( $memcPos ) {
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 ) {
310 $retVal = true;
311 }
312 }
313
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 );
318
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" );
322 $retVal = false;
323 } else {
324 $retVal = true;
325 wfDebug( "Done\n" );
326 }
327 }
328 return $retVal;
329 }
330
331 /**
332 * Get a connection by index
333 */
334 function &getConnection( $i, $fail = true, $groups = array() )
335 {
336 global $wgDBtype;
337 $fname = 'LoadBalancer::getConnection';
338 wfProfileIn( $fname );
339
340
341 # Query groups
342 if ( !is_array( $groups ) ) {
343 $groupIndex = $this->getGroupIndex( $groups, $i );
344 if ( $groupIndex !== false ) {
345 $i = $groupIndex;
346 }
347 } else {
348 foreach ( $groups as $group ) {
349 $groupIndex = $this->getGroupIndex( $group, $i );
350 if ( $groupIndex !== false ) {
351 $i = $groupIndex;
352 break;
353 }
354 }
355 }
356
357 # For now, only go through all this for mysql databases
358 if ($wgDBtype != 'mysql') {
359 $i = $this->getWriterIndex();
360 }
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;
369 if ( $i === -1 ) {
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();
373 }
374 }
375 # Couldn't find a working server in getReaderIndex()?
376 if ( $i === false ) {
377 $this->reportConnectionError( $this->mErrorConnection );
378 }
379 # Now we have an explicit index into the servers array
380 $this->openConnection( $i, $fail );
381
382 wfProfileOut( $fname );
383 return $this->mConnections[$i];
384 }
385
386 /**
387 * Open a connection to the server given by the specified index
388 * Index must be an actual index into the array
389 * Returns success
390 * @access private
391 */
392 function openConnection( $i, $fail = false ) {
393 $fname = 'LoadBalancer::openConnection';
394 wfProfileIn( $fname );
395 $success = true;
396
397 if ( !$this->isOpen( $i ) ) {
398 $this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
399 }
400
401 if ( !$this->isOpen( $i ) ) {
402 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
403 if ( $fail ) {
404 $this->reportConnectionError( $this->mConnections[$i] );
405 }
406 $this->mErrorConnection = $this->mConnections[$i];
407 $this->mConnections[$i] = false;
408 $success = false;
409 }
410 $this->mLastIndex = $i;
411 wfProfileOut( $fname );
412 return $success;
413 }
414
415 /**
416 * Test if the specified index represents an open connection
417 * @access private
418 */
419 function isOpen( $index ) {
420 if( !is_integer( $index ) ) {
421 return false;
422 }
423 if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
424 $this->mConnections[$index]->isOpen() )
425 {
426 return true;
427 } else {
428 return false;
429 }
430 }
431
432 /**
433 * Really opens a connection
434 * @access private
435 */
436 function reallyOpenConnection( &$server ) {
437 if( !is_array( $server ) ) {
438 wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
439 }
440
441 extract( $server );
442
443 # Get class for this database type
444 if ($type != 'mysql' ) {
445 $class = 'Database' . ucfirst( $type );
446 } else {
447 $class = 'Database';
448 }
449
450 # Create object
451 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
452 $db->setLBInfo( $server );
453 return $db;
454 }
455
456 function reportConnectionError( &$conn )
457 {
458 $fname = 'LoadBalancer::reportConnectionError';
459 wfProfileIn( $fname );
460 # Prevent infinite recursion
461
462 static $reporting = false;
463 if ( !$reporting ) {
464 $reporting = true;
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 );
471 } else {
472 // If all servers were busy, mLastError will contain something sensible
473 wfEmergencyAbort( $conn, $this->mLastError );
474 }
475 } else {
476 if ( $this->mFailFunction ) {
477 $conn->failFunction( $this->mFailFunction );
478 } else {
479 $conn->failFunction( false );
480 }
481 $conn->reportConnectionError( "{$this->mLastError} ({$conn->mServer})" );
482 }
483 $reporting = false;
484 }
485 wfProfileOut( $fname );
486 }
487
488 function getWriterIndex()
489 {
490 return 0;
491 }
492
493 function force( $i )
494 {
495 $this->mForce = $i;
496 }
497
498 function haveIndex( $i )
499 {
500 return array_key_exists( $i, $this->mServers );
501 }
502
503 /**
504 * Get the number of defined servers (not the number of open connections)
505 */
506 function getServerCount() {
507 return count( $this->mServers );
508 }
509
510 /**
511 * Save master pos to the session and to memcached, if the session exists
512 */
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" );
522 } else {
523 $conn =& $this->getConnection( 0 );
524 list( $file, $pos ) = $conn->getMasterPos();
525 wfDebug( "Saving master pos: $file $pos\n" );
526 }
527 if ( $file !== false ) {
528 $_SESSION['master_log_file'] = $file;
529 $_SESSION['master_pos'] = $pos;
530 }
531 }
532 }
533
534 /**
535 * Loads the master pos from the session, waits for it if necessary
536 */
537 function loadMasterPos() {
538 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
539 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
540 }
541 }
542
543 /**
544 * Close all open connections
545 */
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();
551 }
552 }
553 }
554
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();
560 }
561 }
562 }
563
564 function waitTimeout( $value = NULL ) {
565 return wfSetVar( $this->mWaitTimeout, $value );
566 }
567
568 function getLaggedSlaveMode() {
569 return $this->mLaggedSlaveMode;
570 }
571
572 /* Disables/enables lag checks */
573 function allowLagged($mode=null) {
574 if ($mode===null)
575 return $this->mAllowLagged;
576 $this->mAllowLagged=$mode;
577 }
578
579 function pingAll() {
580 $success = true;
581 foreach ( $this->mConnections as $i => $conn ) {
582 if ( $this->isOpen( $i ) ) {
583 if ( !$this->mConnections[$i]->ping() ) {
584 $success = false;
585 }
586 }
587 }
588 return $success;
589 }
590
591 /**
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
594 */
595 function getMaxLag() {
596 $maxLag = -1;
597 $host = '';
598 foreach ( $this->mServers as $i => $conn ) {
599 if ( $this->openConnection( $i ) ) {
600 $lag = $this->mConnections[$i]->getLag();
601 if ( $lag > $maxLag ) {
602 $maxLag = $lag;
603 $host = $this->mServers[$i]['host'];
604 }
605 }
606 }
607 return array( $host, $maxLag );
608 }
609
610 /**
611 * Get lag time for each DB
612 * Results are cached for a short time in memcached
613 */
614 function getLagTimes() {
615 global $wgDBname;
616
617 $expiry = 5;
618 $requestRate = 10;
619
620 global $wgMemc;
621 $times = $wgMemc->get( "$wgDBname:lag_times" );
622 if ( $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'] );
628 return $times;
629 }
630 }
631
632 # Cache key missing or expired
633
634 $times = array();
635 foreach ( $this->mServers as $i => $conn ) {
636 if ($i==0) { # Master
637 $times[$i] = 0;
638 } elseif ( $this->openConnection( $i ) ) {
639 $times[$i] = $this->mConnections[$i]->getLag();
640 }
641 }
642
643 # Add a timestamp key so we know when it was cached
644 $times['timestamp'] = time();
645 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
646
647 # But don't give the timestamp to the caller
648 unset($times['timestamp']);
649 return $times;
650 }
651 }
652
653 ?>