Prettify upload form for RTL wikis
[lhc/web/wiklou.git] / includes / LoadBalancer.php
1 <?php
2 /**
3 *
4 */
5
6
7 /**
8 * Database load balancing object
9 *
10 * @todo document
11 */
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';
18
19 function __construct( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
20 {
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;
35
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();
42 }
43 $this->mGroupLoads[$group][$i] = $ratio;
44 }
45 }
46 }
47 if ( $waitForMasterNow ) {
48 $this->loadMasterPos();
49 }
50 }
51
52 static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
53 {
54 return new LoadBalancer( $servers, $failFunction, $waitTimeout );
55 }
56
57 /**
58 * Given an array of non-normalised probabilities, this function will select
59 * an element and return the appropriate key
60 */
61 function pickRandom( $weights )
62 {
63 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
64 return false;
65 }
66
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;
74 }
75 $max = mt_getrandmax();
76 $rand = mt_rand(0, $max) / $max * $sum;
77
78 $sum = 0;
79 foreach ( $weights as $i => $w ) {
80 $sum += $w;
81 if ( $sum >= $rand ) {
82 break;
83 }
84 }
85 return $i;
86 }
87
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'] ) )
94 {
95 unset( $loads[$i] );
96 }
97 }
98
99 # Find out if all the slaves with non-zero load are lagged
100 $sum = 0;
101 foreach ( $loads as $load ) {
102 $sum += $load;
103 }
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;
110 }
111
112 if ( count( $loads ) == 0 ) {
113 return false;
114 }
115
116 #wfDebugLog( 'connect', var_export( $loads, true ) );
117
118 # Return a random representative of the remainder
119 return $this->pickRandom( $loads );
120 }
121
122 /**
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
126 *
127 * Side effect: opens connections to databases
128 */
129 function getReaderIndex() {
130 global $wgReadOnly, $wgDBClusterTimeout, $wgDBAvgStatusPoll;
131
132 $fname = 'LoadBalancer::getReaderIndex';
133 wfProfileIn( $fname );
134
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 );
159 }
160 }
161 $serverIndex = $i;
162 if ( $i !== false ) {
163 wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
164 $this->openConnection( $i );
165
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'];
178
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;
184 }
185 } else {
186 $done = true;
187 $sleepTime = 0;
188 }
189 }
190 } else {
191 $sleepTime = 500000;
192 }
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" );
199 }
200 } while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
201
202 if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
203 $this->mErrorConnection = false;
204 $this->mLastError = 'All servers busy';
205 }
206
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();
212 }
213 }
214 if ( $i !== false ) {
215 $this->mReadIndex = $i;
216 }
217 } else {
218 $i = false;
219 }
220 }
221 }
222 wfProfileOut( $fname );
223 return $i;
224 }
225
226 /**
227 * Get a random server to use in a query group
228 */
229 function getGroupIndex( $group ) {
230 if ( isset( $this->mGroupLoads[$group] ) ) {
231 $i = $this->pickRandom( $this->mGroupLoads[$group] );
232 } else {
233 $i = false;
234 }
235 wfDebug( "Query group $group => $i\n" );
236 return $i;
237 }
238
239 /**
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
243 */
244 function waitFor( $file, $pos ) {
245 $fname = 'LoadBalancer::waitFor';
246 wfProfileIn( $fname );
247
248 wfDebug( "User master pos: $file $pos\n" );
249 $this->mWaitForFile = false;
250 $this->mWaitForPos = false;
251
252 if ( count( $this->mServers ) > 1 ) {
253 $this->mWaitForFile = $file;
254 $this->mWaitForPos = $pos;
255 $i = $this->mReadIndex;
256
257 if ( $i > 0 ) {
258 if ( !$this->doWait( $i ) ) {
259 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
260 $this->mLaggedSlaveMode = true;
261 }
262 }
263 }
264 wfProfileOut( $fname );
265 }
266
267 /**
268 * Wait for a given slave to catch up to the master pos stored in $this
269 */
270 function doWait( $index ) {
271 global $wgMemc;
272
273 $retVal = false;
274
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;
280 }
281
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;
289 }
290 }
291
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 );
296
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" );
304 }
305 }
306 return $retVal;
307 }
308
309 /**
310 * Get a connection by index
311 */
312 function &getConnection( $i, $fail = true, $groups = array() )
313 {
314 global $wgDBtype;
315 $fname = 'LoadBalancer::getConnection';
316 wfProfileIn( $fname );
317
318
319 # Query groups
320 if ( !is_array( $groups ) ) {
321 $groupIndex = $this->getGroupIndex( $groups );
322 if ( $groupIndex !== false ) {
323 $i = $groupIndex;
324 }
325 } else {
326 foreach ( $groups as $group ) {
327 $groupIndex = $this->getGroupIndex( $group );
328 if ( $groupIndex !== false ) {
329 $i = $groupIndex;
330 break;
331 }
332 }
333 }
334
335 # For now, only go through all this for mysql databases
336 if ($wgDBtype != 'mysql') {
337 $i = $this->getWriterIndex();
338 }
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();
351 }
352 }
353 # Couldn't find a working server in getReaderIndex()?
354 if ( $i === false ) {
355 $this->reportConnectionError( $this->mErrorConnection );
356 }
357 # Now we have an explicit index into the servers array
358 $this->openConnection( $i, $fail );
359
360 wfProfileOut( $fname );
361 return $this->mConnections[$i];
362 }
363
364 /**
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
369 */
370 function openConnection( $i, $fail = false ) {
371 $fname = 'LoadBalancer::openConnection';
372 wfProfileIn( $fname );
373 $success = true;
374
375 if ( !$this->isOpen( $i ) ) {
376 $this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
377 }
378
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] );
383 }
384 $this->mErrorConnection = $this->mConnections[$i];
385 $this->mConnections[$i] = false;
386 $success = false;
387 }
388 $this->mLastIndex = $i;
389 wfProfileOut( $fname );
390 return $success;
391 }
392
393 /**
394 * Test if the specified index represents an open connection
395 * @access private
396 */
397 function isOpen( $index ) {
398 if( !is_integer( $index ) ) {
399 return false;
400 }
401 if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
402 $this->mConnections[$index]->isOpen() )
403 {
404 return true;
405 } else {
406 return false;
407 }
408 }
409
410 /**
411 * Really opens a connection
412 * @access private
413 */
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.' );
417 }
418
419 extract( $server );
420 # Get class for this database type
421 $class = 'Database' . ucfirst( $type );
422
423 # Create object
424 $db = new $class( $host, $user, $password, $dbname, 1, $flags );
425 $db->setLBInfo( $server );
426 return $db;
427 }
428
429 function reportConnectionError( &$conn ) {
430 $fname = 'LoadBalancer::reportConnectionError';
431 wfProfileIn( $fname );
432 # Prevent infinite recursion
433
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 );
446 }
447 } else {
448 if ( $this->mFailFunction ) {
449 $conn->failFunction( $this->mFailFunction );
450 } else {
451 $conn->failFunction( false );
452 }
453 $server = $conn->getProperty( 'mServer' );
454 $conn->reportConnectionError( "{$this->mLastError} ({$server})" );
455 }
456 $reporting = false;
457 }
458 wfProfileOut( $fname );
459 }
460
461 function getWriterIndex() {
462 return 0;
463 }
464
465 /**
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.
470 */
471 function force( $i ) {
472 $this->mForce = $i;
473 }
474
475 /**
476 * Returns true if the specified index is a valid server index
477 */
478 function haveIndex( $i ) {
479 return array_key_exists( $i, $this->mServers );
480 }
481
482 /**
483 * Returns true if the specified index is valid and has non-zero load
484 */
485 function isNonZeroLoad( $i ) {
486 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
487 }
488
489 /**
490 * Get the number of defined servers (not the number of open connections)
491 */
492 function getServerCount() {
493 return count( $this->mServers );
494 }
495
496 /**
497 * Save master pos to the session and to memcached, if the session exists
498 */
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" );
511 }
512 if ( $file !== false ) {
513 $_SESSION['master_log_file'] = $file;
514 $_SESSION['master_pos'] = $pos;
515 }
516 }
517 }
518
519 /**
520 * Loads the master pos from the session, waits for it if necessary
521 */
522 function loadMasterPos() {
523 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
524 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
525 }
526 }
527
528 /**
529 * Close all open connections
530 */
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();
536 }
537 }
538 }
539
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();
545 }
546 }
547 }
548
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();
556 }
557 }
558 }
559
560 function waitTimeout( $value = NULL ) {
561 return wfSetVar( $this->mWaitTimeout, $value );
562 }
563
564 function getLaggedSlaveMode() {
565 return $this->mLaggedSlaveMode;
566 }
567
568 /* Disables/enables lag checks */
569 function allowLagged($mode=null) {
570 if ($mode===null)
571 return $this->mAllowLagged;
572 $this->mAllowLagged=$mode;
573 }
574
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;
581 }
582 }
583 }
584 return $success;
585 }
586
587 /**
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
590 */
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'];
600 }
601 }
602 }
603 return array( $host, $maxLag );
604 }
605
606 /**
607 * Get lag time for each DB
608 * Results are cached for a short time in memcached
609 */
610 function getLagTimes() {
611 wfProfileIn( __METHOD__ );
612 $expiry = 5;
613 $requestRate = 10;
614
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;
625 }
626 wfIncrStats( 'lag_cache_miss_expired' );
627 } else {
628 wfIncrStats( 'lag_cache_miss_absent' );
629 }
630
631 # Cache key missing or expired
632
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();
639 }
640 }
641
642 # Add a timestamp key so we know when it was cached
643 $times['timestamp'] = time();
644 $wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );
645
646 # But don't give the timestamp to the caller
647 unset($times['timestamp']);
648 wfProfileOut( __METHOD__ );
649 return $times;
650 }
651 }
652
653