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