Replacing var keyword with private / public as we now require PHP5.
[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 require_once( 'Database.php' );
11
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
17
18 # Obsolete aliases
19 define( 'DB_READ', -1 );
20 define( 'DB_WRITE', -2 );
21
22
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 );
26
27
28 /**
29 * Database load balancing object
30 *
31 * @todo document
32 * @package MediaWiki
33 */
34 class LoadBalancer {
35
36 private
37 $mAllowLagged,
38 $mConnections,
39 $mErrorConnection,
40 $mFailFunction,
41 $mForce,
42 $mGroupLoads,
43 $mLaggedSlaveMode,
44 $mLastError = 'Unknown error',
45 $mLastIndex,
46 $mLoads,
47 $mReadIndex,
48 $mServers,
49 $mWaitForFile,
50 $mWaitForPos,
51 $mWaitTimeout ;
52
53 function LoadBalancer()
54 {
55 $this->mServers = array();
56 $this->mConnections = array();
57 $this->mFailFunction = false;
58 $this->mReadIndex = -1;
59 $this->mForce = -1;
60 $this->mLastIndex = -1;
61 $this->mErrorConnection = false;
62 $this->mAllowLag = false;
63 }
64
65 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
66 {
67 $lb = new LoadBalancer;
68 $lb->initialise( $servers, $failFunction, $waitTimeout );
69 return $lb;
70 }
71
72 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
73 {
74 $this->mServers = $servers;
75 $this->mFailFunction = $failFunction;
76 $this->mReadIndex = -1;
77 $this->mWriteIndex = -1;
78 $this->mForce = -1;
79 $this->mConnections = array();
80 $this->mLastIndex = 1;
81 $this->mLoads = array();
82 $this->mWaitForFile = false;
83 $this->mWaitForPos = false;
84 $this->mWaitTimeout = $waitTimeout;
85 $this->mLaggedSlaveMode = false;
86
87 foreach( $servers as $i => $server ) {
88 $this->mLoads[$i] = $server['load'];
89 if ( isset( $server['groupLoads'] ) ) {
90 foreach ( $server['groupLoads'] as $group => $ratio ) {
91 if ( !isset( $this->mGroupLoads[$group] ) ) {
92 $this->mGroupLoads[$group] = array();
93 }
94 $this->mGroupLoads[$group][$i] = $ratio;
95 }
96 }
97 }
98 }
99
100 /**
101 * Given an array of non-normalised probabilities, this function will select
102 * an element and return the appropriate key
103 */
104 function pickRandom( $weights )
105 {
106 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
107 return false;
108 }
109
110 $sum = array_sum( $weights );
111 if ( $sum == 0 ) {
112 # No loads on any of them
113 # In previous versions, this triggered an unweighted random selection,
114 # but this feature has been removed as of April 2006 to allow for strict
115 # separation of query groups.
116 return false;
117 }
118 $max = mt_getrandmax();
119 $rand = mt_rand(0, $max) / $max * $sum;
120
121 $sum = 0;
122 foreach ( $weights as $i => $w ) {
123 $sum += $w;
124 if ( $sum >= $rand ) {
125 break;
126 }
127 }
128 return $i;
129 }
130
131 function getRandomNonLagged( $loads ) {
132 # Unset excessively lagged servers
133 $lags = $this->getLagTimes();
134 foreach ( $lags as $i => $lag ) {
135 if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
136 unset( $loads[$i] );
137 }
138 }
139
140 # Find out if all the slaves with non-zero load are lagged
141 $sum = 0;
142 foreach ( $loads as $load ) {
143 $sum += $load;
144 }
145 if ( $sum == 0 ) {
146 # No appropriate DB servers except maybe the master and some slaves with zero load
147 # Do NOT use the master
148 # Instead, this function will return false, triggering read-only mode,
149 # and a lagged slave will be used instead.
150 return false;
151 }
152
153 if ( count( $loads ) == 0 ) {
154 return false;
155 }
156
157 #wfDebugLog( 'connect', var_export( $loads, true ) );
158
159 # Return a random representative of the remainder
160 return $this->pickRandom( $loads );
161 }
162
163 /**
164 * Get the index of the reader connection, which may be a slave
165 * This takes into account load ratios and lag times. It should
166 * always return a consistent index during a given invocation
167 *
168 * Side effect: opens connections to databases
169 */
170 function getReaderIndex() {
171 global $wgReadOnly, $wgDBClusterTimeout;
172
173 $fname = 'LoadBalancer::getReaderIndex';
174 wfProfileIn( $fname );
175
176 $i = false;
177 if ( $this->mForce >= 0 ) {
178 $i = $this->mForce;
179 } else {
180 if ( $this->mReadIndex >= 0 ) {
181 $i = $this->mReadIndex;
182 } else {
183 # $loads is $this->mLoads except with elements knocked out if they
184 # don't work
185 $loads = $this->mLoads;
186 $done = false;
187 $totalElapsed = 0;
188 do {
189 if ( $wgReadOnly or $this->mAllowLagged ) {
190 $i = $this->pickRandom( $loads );
191 } else {
192 $i = $this->getRandomNonLagged( $loads );
193 if ( $i === false && count( $loads ) != 0 ) {
194 # All slaves lagged. Switch to read-only mode
195 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
196 $i = $this->pickRandom( $loads );
197 }
198 }
199 $serverIndex = $i;
200 if ( $i !== false ) {
201 wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
202 $this->openConnection( $i );
203
204 if ( !$this->isOpen( $i ) ) {
205 wfDebug( "Failed\n" );
206 unset( $loads[$i] );
207 $sleepTime = 0;
208 } else {
209 $status = $this->mConnections[$i]->getStatus("Thread%");
210 if ( isset( $this->mServers[$i]['max threads'] ) &&
211 $status['Threads_running'] > $this->mServers[$i]['max threads'] )
212 {
213 # Too much load, back off and wait for a while.
214 # The sleep time is scaled by the number of threads connected,
215 # to produce a roughly constant global poll rate.
216 $sleepTime = AVG_STATUS_POLL * $status['Threads_connected'];
217
218 # If we reach the timeout and exit the loop, don't use it
219 $i = false;
220 } else {
221 $done = true;
222 $sleepTime = 0;
223 }
224 }
225 } else {
226 $sleepTime = 500000;
227 }
228 if ( $sleepTime ) {
229 $totalElapsed += $sleepTime;
230 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
231 wfProfileIn( "$fname-sleep $x" );
232 usleep( $sleepTime );
233 wfProfileOut( "$fname-sleep $x" );
234 }
235 } while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
236
237 if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
238 $this->mErrorConnection = false;
239 $this->mLastError = 'All servers busy';
240 }
241
242 if ( $i !== false && $this->isOpen( $i ) ) {
243 # Wait for the session master pos for a short time
244 if ( $this->mWaitForFile ) {
245 if ( !$this->doWait( $i ) ) {
246 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
247 }
248 }
249 if ( $i !== false ) {
250 $this->mReadIndex = $i;
251 }
252 } else {
253 $i = false;
254 }
255 }
256 }
257 wfProfileOut( $fname );
258 return $i;
259 }
260
261 /**
262 * Get a random server to use in a query group
263 */
264 function getGroupIndex( $group ) {
265 if ( isset( $this->mGroupLoads[$group] ) ) {
266 $i = $this->pickRandom( $this->mGroupLoads[$group] );
267 } else {
268 $i = false;
269 }
270 wfDebug( "Query group $group => $i\n" );
271 return $i;
272 }
273
274 /**
275 * Set the master wait position
276 * If a DB_SLAVE connection has been opened already, waits
277 * Otherwise sets a variable telling it to wait if such a connection is opened
278 */
279 function waitFor( $file, $pos ) {
280 $fname = 'LoadBalancer::waitFor';
281 wfProfileIn( $fname );
282
283 wfDebug( "User master pos: $file $pos\n" );
284 $this->mWaitForFile = false;
285 $this->mWaitForPos = false;
286
287 if ( count( $this->mServers ) > 1 ) {
288 $this->mWaitForFile = $file;
289 $this->mWaitForPos = $pos;
290 $i = $this->mReadIndex;
291
292 if ( $i > 0 ) {
293 if ( !$this->doWait( $i ) ) {
294 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
295 $this->mLaggedSlaveMode = true;
296 }
297 }
298 }
299 wfProfileOut( $fname );
300 }
301
302 /**
303 * Wait for a given slave to catch up to the master pos stored in $this
304 */
305 function doWait( $index ) {
306 global $wgMemc;
307
308 $retVal = false;
309
310 # Debugging hacks
311 if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
312 return false;
313 } elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
314 return true;
315 }
316
317 $key = 'masterpos:' . $index;
318 $memcPos = $wgMemc->get( $key );
319 if ( $memcPos ) {
320 list( $file, $pos ) = explode( ' ', $memcPos );
321 # If the saved position is later than the requested position, return now
322 if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
323 $retVal = true;
324 }
325 }
326
327 if ( !$retVal && $this->isOpen( $index ) ) {
328 $conn =& $this->mConnections[$index];
329 wfDebug( "Waiting for slave #$index to catch up...\n" );
330 $result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, $this->mWaitTimeout );
331
332 if ( $result == -1 || is_null( $result ) ) {
333 # Timed out waiting for slave, use master instead
334 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
335 $retVal = false;
336 } else {
337 $retVal = true;
338 wfDebug( "Done\n" );
339 }
340 }
341 return $retVal;
342 }
343
344 /**
345 * Get a connection by index
346 */
347 function &getConnection( $i, $fail = true, $groups = array() )
348 {
349 global $wgDBtype;
350 $fname = 'LoadBalancer::getConnection';
351 wfProfileIn( $fname );
352
353
354 # Query groups
355 if ( !is_array( $groups ) ) {
356 $groupIndex = $this->getGroupIndex( $groups, $i );
357 if ( $groupIndex !== false ) {
358 $i = $groupIndex;
359 }
360 } else {
361 foreach ( $groups as $group ) {
362 $groupIndex = $this->getGroupIndex( $group, $i );
363 if ( $groupIndex !== false ) {
364 $i = $groupIndex;
365 break;
366 }
367 }
368 }
369
370 # For now, only go through all this for mysql databases
371 if ($wgDBtype != 'mysql') {
372 $i = $this->getWriterIndex();
373 }
374 # Operation-based index
375 elseif ( $i == DB_SLAVE ) {
376 $i = $this->getReaderIndex();
377 } elseif ( $i == DB_MASTER ) {
378 $i = $this->getWriterIndex();
379 } elseif ( $i == DB_LAST ) {
380 # Just use $this->mLastIndex, which should already be set
381 $i = $this->mLastIndex;
382 if ( $i === -1 ) {
383 # Oh dear, not set, best to use the writer for safety
384 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
385 $i = $this->getWriterIndex();
386 }
387 }
388 # Couldn't find a working server in getReaderIndex()?
389 if ( $i === false ) {
390 $this->reportConnectionError( $this->mErrorConnection );
391 }
392 # Now we have an explicit index into the servers array
393 $this->openConnection( $i, $fail );
394
395 wfProfileOut( $fname );
396 return $this->mConnections[$i];
397 }
398
399 /**
400 * Open a connection to the server given by the specified index
401 * Index must be an actual index into the array
402 * Returns success
403 * @access private
404 */
405 function openConnection( $i, $fail = false ) {
406 $fname = 'LoadBalancer::openConnection';
407 wfProfileIn( $fname );
408 $success = true;
409
410 if ( !$this->isOpen( $i ) ) {
411 $this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
412 }
413
414 if ( !$this->isOpen( $i ) ) {
415 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
416 if ( $fail ) {
417 $this->reportConnectionError( $this->mConnections[$i] );
418 }
419 $this->mErrorConnection = $this->mConnections[$i];
420 $this->mConnections[$i] = false;
421 $success = false;
422 }
423 $this->mLastIndex = $i;
424 wfProfileOut( $fname );
425 return $success;
426 }
427
428 /**
429 * Test if the specified index represents an open connection
430 * @access private
431 */
432 function isOpen( $index ) {
433 if( !is_integer( $index ) ) {
434 return false;
435 }
436 if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
437 $this->mConnections[$index]->isOpen() )
438 {
439 return true;
440 } else {
441 return false;
442 }
443 }
444
445 /**
446 * Really opens a connection
447 * @access private
448 */
449 function reallyOpenConnection( &$server ) {
450 if( !is_array( $server ) ) {
451 wfDebugDieBacktrace( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
452 }
453
454 extract( $server );
455 # Get class for this database type
456 $class = 'Database' . ucfirst( $type );
457 if ( !class_exists( $class ) ) {
458 require_once( "$class.php" );
459 }
460
461 # Create object
462 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
463 $db->setLBInfo( $server );
464 return $db;
465 }
466
467 function reportConnectionError( &$conn )
468 {
469 $fname = 'LoadBalancer::reportConnectionError';
470 wfProfileIn( $fname );
471 # Prevent infinite recursion
472
473 static $reporting = false;
474 if ( !$reporting ) {
475 $reporting = true;
476 if ( !is_object( $conn ) ) {
477 // No last connection, probably due to all servers being too busy
478 $conn = new Database;
479 if ( $this->mFailFunction ) {
480 $conn->failFunction( $this->mFailFunction );
481 $conn->reportConnectionError( $this->mLastError );
482 } else {
483 // If all servers were busy, mLastError will contain something sensible
484 wfEmergencyAbort( $conn, $this->mLastError );
485 }
486 } else {
487 if ( $this->mFailFunction ) {
488 $conn->failFunction( $this->mFailFunction );
489 } else {
490 $conn->failFunction( false );
491 }
492 $conn->reportConnectionError( "{$this->mLastError} ({$conn->mServer})" );
493 }
494 $reporting = false;
495 }
496 wfProfileOut( $fname );
497 }
498
499 function getWriterIndex()
500 {
501 return 0;
502 }
503
504 function force( $i )
505 {
506 $this->mForce = $i;
507 }
508
509 function haveIndex( $i )
510 {
511 return array_key_exists( $i, $this->mServers );
512 }
513
514 /**
515 * Get the number of defined servers (not the number of open connections)
516 */
517 function getServerCount() {
518 return count( $this->mServers );
519 }
520
521 /**
522 * Save master pos to the session and to memcached, if the session exists
523 */
524 function saveMasterPos() {
525 global $wgSessionStarted;
526 if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
527 # If this entire request was served from a slave without opening a connection to the
528 # master (however unlikely that may be), then we can fetch the position from the slave.
529 if ( empty( $this->mConnections[0] ) ) {
530 $conn =& $this->getConnection( DB_SLAVE );
531 list( $file, $pos ) = $conn->getSlavePos();
532 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
533 } else {
534 $conn =& $this->getConnection( 0 );
535 list( $file, $pos ) = $conn->getMasterPos();
536 wfDebug( "Saving master pos: $file $pos\n" );
537 }
538 if ( $file !== false ) {
539 $_SESSION['master_log_file'] = $file;
540 $_SESSION['master_pos'] = $pos;
541 }
542 }
543 }
544
545 /**
546 * Loads the master pos from the session, waits for it if necessary
547 */
548 function loadMasterPos() {
549 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
550 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
551 }
552 }
553
554 /**
555 * Close all open connections
556 */
557 function closeAll() {
558 foreach( $this->mConnections as $i => $conn ) {
559 if ( $this->isOpen( $i ) ) {
560 // Need to use this syntax because $conn is a copy not a reference
561 $this->mConnections[$i]->close();
562 }
563 }
564 }
565
566 function commitAll() {
567 foreach( $this->mConnections as $i => $conn ) {
568 if ( $this->isOpen( $i ) ) {
569 // Need to use this syntax because $conn is a copy not a reference
570 $this->mConnections[$i]->immediateCommit();
571 }
572 }
573 }
574
575 function waitTimeout( $value = NULL ) {
576 return wfSetVar( $this->mWaitTimeout, $value );
577 }
578
579 function getLaggedSlaveMode() {
580 return $this->mLaggedSlaveMode;
581 }
582
583 /* Disables/enables lag checks */
584 function allowLagged($mode=null) {
585 if ($mode===null)
586 return $this->mAllowLagged;
587 $this->mAllowLagged=$mode;
588 }
589
590 function pingAll() {
591 $success = true;
592 foreach ( $this->mConnections as $i => $conn ) {
593 if ( $this->isOpen( $i ) ) {
594 if ( !$this->mConnections[$i]->ping() ) {
595 $success = false;
596 }
597 }
598 }
599 return $success;
600 }
601
602 /**
603 * Get the hostname and lag time of the most-lagged slave
604 * This is useful for maintenance scripts that need to throttle their updates
605 */
606 function getMaxLag() {
607 $maxLag = -1;
608 $host = '';
609 foreach ( $this->mServers as $i => $conn ) {
610 if ( $this->openConnection( $i ) ) {
611 $lag = $this->mConnections[$i]->getLag();
612 if ( $lag > $maxLag ) {
613 $maxLag = $lag;
614 $host = $this->mServers[$i]['host'];
615 }
616 }
617 }
618 return array( $host, $maxLag );
619 }
620
621 /**
622 * Get lag time for each DB
623 * Results are cached for a short time in memcached
624 */
625 function getLagTimes() {
626 global $wgDBname;
627
628 $expiry = 5;
629 $requestRate = 10;
630
631 global $wgMemc;
632 $times = $wgMemc->get( "$wgDBname:lag_times" );
633 if ( $times ) {
634 # Randomly recache with probability rising over $expiry
635 $elapsed = time() - $times['timestamp'];
636 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
637 if ( mt_rand( 0, $chance ) != 0 ) {
638 unset( $times['timestamp'] );
639 return $times;
640 }
641 }
642
643 # Cache key missing or expired
644
645 $times = array();
646 foreach ( $this->mServers as $i => $conn ) {
647 if ($i==0) { # Master
648 $times[$i] = 0;
649 } elseif ( $this->openConnection( $i ) ) {
650 $times[$i] = $this->mConnections[$i]->getLag();
651 }
652 }
653
654 # Add a timestamp key so we know when it was cached
655 $times['timestamp'] = time();
656 $wgMemc->set( "$wgDBname:lag_times", $times, $expiry );
657
658 # But don't give the timestamp to the caller
659 unset($times['timestamp']);
660 return $times;
661 }
662 }
663
664 ?>