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