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