Sleep time is highly variable, removing it from the profiling section name
[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 * Database load balancing object
24 *
25 * @todo document
26 * @package MediaWiki
27 */
28 class LoadBalancer {
29 /* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
30 /* private */ var $mFailFunction, $mErrorConnection;
31 /* private */ var $mForce, $mReadIndex, $mLastIndex;
32 /* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
33 /* private */ var $mLaggedSlaveMode;
34
35 function LoadBalancer()
36 {
37 $this->mServers = array();
38 $this->mConnections = array();
39 $this->mFailFunction = false;
40 $this->mReadIndex = -1;
41 $this->mForce = -1;
42 $this->mLastIndex = -1;
43 $this->mErrorConnection = false;
44 }
45
46 function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
47 {
48 $lb = new LoadBalancer;
49 $lb->initialise( $servers, $failFunction, $waitTimeout );
50 return $lb;
51 }
52
53 function initialise( $servers, $failFunction = false, $waitTimeout = 10 )
54 {
55 $this->mServers = $servers;
56 $this->mFailFunction = $failFunction;
57 $this->mReadIndex = -1;
58 $this->mWriteIndex = -1;
59 $this->mForce = -1;
60 $this->mConnections = array();
61 $this->mLastIndex = 1;
62 $this->mLoads = array();
63 $this->mWaitForFile = false;
64 $this->mWaitForPos = false;
65 $this->mWaitTimeout = $waitTimeout;
66 $this->mLaggedSlaveMode = false;
67
68 foreach( $servers as $i => $server ) {
69 $this->mLoads[$i] = $server['load'];
70 if ( isset( $server['groupLoads'] ) ) {
71 foreach ( $server['groupLoads'] as $group => $ratio ) {
72 if ( !isset( $this->mGroupLoads[$group] ) ) {
73 $this->mGroupLoads[$group] = array();
74 }
75 $this->mGroupLoads[$group][$i] = $ratio;
76 }
77 }
78 }
79 }
80
81 /**
82 * Given an array of non-normalised probabilities, this function will select
83 * an element and return the appropriate key
84 */
85 function pickRandom( $weights )
86 {
87 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
88 return false;
89 }
90
91 $sum = 0;
92 foreach ( $weights as $w ) {
93 $sum += $w;
94 }
95
96 if ( $sum == 0 ) {
97 # No loads on any of them
98 # Just pick one at random
99 foreach ( $weights as $i => $w ) {
100 $weights[$i] = 1;
101 }
102 }
103 $max = mt_getrandmax();
104 $rand = mt_rand(0, $max) / $max * $sum;
105
106 $sum = 0;
107 foreach ( $weights as $i => $w ) {
108 $sum += $w;
109 if ( $sum >= $rand ) {
110 break;
111 }
112 }
113 return $i;
114 }
115
116 function getRandomNonLagged( $loads ) {
117 # Unset excessively lagged servers
118 $lags = $this->getLagTimes();
119 foreach ( $lags as $i => $lag ) {
120 if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
121 unset( $loads[$i] );
122 }
123 }
124
125
126 # Find out if all the slaves with non-zero load are lagged
127 $sum = 0;
128 foreach ( $loads as $load ) {
129 $sum += $load;
130 }
131 if ( $sum == 0 ) {
132 # No appropriate DB servers except maybe the master and some slaves with zero load
133 # Do NOT use the master
134 # Instead, this function will return false, triggering read-only mode,
135 # and a lagged slave will be used instead.
136 unset ( $loads[0] );
137 }
138
139 if ( count( $loads ) == 0 ) {
140 return false;
141 }
142
143 #wfDebugLog( 'connect', var_export( $loads, true ) );
144
145 # Return a random representative of the remainder
146 return $this->pickRandom( $loads );
147 }
148
149 /**
150 * Get the index of the reader connection, which may be a slave
151 * This takes into account load ratios and lag times. It should
152 * always return a consistent index during a given invocation
153 *
154 * Side effect: opens connections to databases
155 */
156 function getReaderIndex()
157 {
158 global $wgMaxLag, $wgReadOnly, $wgDBClusterTimeout;
159
160 $fname = 'LoadBalancer::getReaderIndex';
161 wfProfileIn( $fname );
162
163 $i = false;
164 if ( $this->mForce >= 0 ) {
165 $i = $this->mForce;
166 } else {
167 if ( $this->mReadIndex >= 0 ) {
168 $i = $this->mReadIndex;
169 } else {
170 # $loads is $this->mLoads except with elements knocked out if they
171 # don't work
172 $loads = $this->mLoads;
173 $done = false;
174 $totalElapsed = 0;
175 do {
176 if ( $wgReadOnly ) {
177 $i = $this->pickRandom( $loads );
178 } else {
179 $i = $this->getRandomNonLagged( $loads );
180 if ( $i === false && count( $loads ) != 0 ) {
181 # All slaves lagged. Switch to read-only mode
182 $wgReadOnly = wfMsgNoDB( 'readonly_lag' );
183 $i = $this->pickRandom( $loads );
184 }
185 }
186 $serverIndex = $i;
187 if ( $i !== false ) {
188 wfDebugLog( 'connect', "Using reader #$i: {$this->mServers[$i]['host']}...\n" );
189 $this->openConnection( $i );
190
191 if ( !$this->isOpen( $i ) ) {
192 wfDebug( "Failed\n" );
193 unset( $loads[$i] );
194 $sleepTime = 0;
195 } else {
196 $status = $this->mConnections[$i]->getStatus();
197 if ( isset( $this->mServers[$i]['max threads'] ) &&
198 $status['Threads_running'] > $this->mServers[$i]['max threads'] )
199 {
200 # Slave is lagged, wait for a while
201 $sleepTime = 5000 * $status['Threads_connected'];
202
203 # If we reach the timeout and exit the loop, don't use it
204 $i = false;
205 } else {
206 $done = true;
207 $sleepTime = 0;
208 }
209 }
210 } else {
211 $sleepTime = 500000;
212 }
213 if ( $sleepTime ) {
214 $totalElapsed += $sleepTime;
215 $x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
216 wfProfileIn( "$fname-sleep $x" );
217 usleep( $sleepTime );
218 wfProfileOut( "$fname-sleep $x" );
219 }
220 } while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );
221
222 if ( $i !== false && $this->isOpen( $i ) ) {
223 # Wait for the session master pos for a short time
224 if ( $this->mWaitForFile ) {
225 if ( !$this->doWait( $i ) ) {
226 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
227 }
228 }
229 if ( $i !== false ) {
230 $this->mReadIndex = $i;
231 }
232 } else {
233 $i = false;
234 }
235 }
236 }
237 wfProfileOut( $fname );
238 return $i;
239 }
240
241 /**
242 * Get a random server to use in a query group
243 */
244 function getGroupIndex( $group ) {
245 if ( isset( $this->mGroupLoads[$group] ) ) {
246 $i = $this->pickRandom( $this->mGroupLoads[$group] );
247 } else {
248 $i = false;
249 }
250 wfDebug( "Query group $group => $i\n" );
251 return $i;
252 }
253
254 /**
255 * Set the master wait position
256 * If a DB_SLAVE connection has been opened already, waits
257 * Otherwise sets a variable telling it to wait if such a connection is opened
258 */
259 function waitFor( $file, $pos ) {
260 $fname = 'LoadBalancer::waitFor';
261 wfProfileIn( $fname );
262
263 wfDebug( "User master pos: $file $pos\n" );
264 $this->mWaitForFile = false;
265 $this->mWaitForPos = false;
266
267 if ( count( $this->mServers ) > 1 ) {
268 $this->mWaitForFile = $file;
269 $this->mWaitForPos = $pos;
270 $i = $this->mReadIndex;
271
272 if ( $i > 0 ) {
273 if ( !$this->doWait( $i ) ) {
274 $this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
275 $this->mLaggedSlaveMode = true;
276 }
277 }
278 }
279 wfProfileOut( $fname );
280 }
281
282 /**
283 * Wait for a given slave to catch up to the master pos stored in $this
284 */
285 function doWait( $index ) {
286 global $wgMemc;
287
288 $retVal = false;
289
290 # Debugging hacks
291 if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
292 return false;
293 } elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
294 return true;
295 }
296
297 $key = 'masterpos:' . $index;
298 $memcPos = $wgMemc->get( $key );
299 if ( $memcPos ) {
300 list( $file, $pos ) = explode( ' ', $memcPos );
301 # If the saved position is later than the requested position, return now
302 if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
303 $retVal = true;
304 }
305 }
306
307 if ( !$retVal && $this->isOpen( $index ) ) {
308 $conn =& $this->mConnections[$index];
309 wfDebug( "Waiting for slave #$index to catch up...\n" );
310 $result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, $this->mWaitTimeout );
311
312 if ( $result == -1 || is_null( $result ) ) {
313 # Timed out waiting for slave, use master instead
314 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
315 $retVal = false;
316 } else {
317 $retVal = true;
318 wfDebug( "Done\n" );
319 }
320 }
321 return $retVal;
322 }
323
324 /**
325 * Get a connection by index
326 */
327 function &getConnection( $i, $fail = true, $groups = array() )
328 {
329 $fname = 'LoadBalancer::getConnection';
330 wfProfileIn( $fname );
331
332 # Query groups
333 $groupIndex = false;
334 foreach ( $groups as $group ) {
335 $groupIndex = $this->getGroupIndex( $group );
336 if ( $groupIndex !== false ) {
337 $i = $groupIndex;
338 break;
339 }
340 }
341
342 # Operation-based index
343 if ( $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 * @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 * @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 * @private
416 */
417 function reallyOpenConnection( &$server ) {
418 if( !is_array( $server ) ) {
419 wfDebugDieBacktrace( '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 if ( !class_exists( $class ) ) {
426 require_once( "$class.php" );
427 }
428
429 # Create object
430 return new $class( $host, $user, $password, $dbname, 1, $flags );
431 }
432
433 function reportConnectionError( &$conn )
434 {
435 $fname = 'LoadBalancer::reportConnectionError';
436 wfProfileIn( $fname );
437 # Prevent infinite recursion
438
439 static $reporting = false;
440 if ( !$reporting ) {
441 $reporting = true;
442 if ( !is_object( $conn ) ) {
443 $conn = new Database;
444 }
445 if ( $this->mFailFunction ) {
446 $conn->failFunction( $this->mFailFunction );
447 } else {
448 $conn->failFunction( false );
449 }
450 $conn->reportConnectionError();
451 $reporting = false;
452 }
453 wfProfileOut( $fname );
454 }
455
456 function getWriterIndex()
457 {
458 return 0;
459 }
460
461 function force( $i )
462 {
463 $this->mForce = $i;
464 }
465
466 function haveIndex( $i )
467 {
468 return array_key_exists( $i, $this->mServers );
469 }
470
471 /**
472 * Get the number of defined servers (not the number of open connections)
473 */
474 function getServerCount() {
475 return count( $this->mServers );
476 }
477
478 /**
479 * Save master pos to the session and to memcached, if the session exists
480 */
481 function saveMasterPos() {
482 global $wgSessionStarted;
483 if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
484 # If this entire request was served from a slave without opening a connection to the
485 # master (however unlikely that may be), then we can fetch the position from the slave.
486 if ( empty( $this->mConnections[0] ) ) {
487 $conn =& $this->getConnection( DB_SLAVE );
488 list( $file, $pos ) = $conn->getSlavePos();
489 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
490 } else {
491 $conn =& $this->getConnection( 0 );
492 list( $file, $pos ) = $conn->getMasterPos();
493 wfDebug( "Saving master pos: $file $pos\n" );
494 }
495 if ( $file !== false ) {
496 $_SESSION['master_log_file'] = $file;
497 $_SESSION['master_pos'] = $pos;
498 }
499 }
500 }
501
502 /**
503 * Loads the master pos from the session, waits for it if necessary
504 */
505 function loadMasterPos() {
506 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
507 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
508 }
509 }
510
511 /**
512 * Close all open connections
513 */
514 function closeAll() {
515 foreach( $this->mConnections as $i => $conn ) {
516 if ( $this->isOpen( $i ) ) {
517 // Need to use this syntax because $conn is a copy not a reference
518 $this->mConnections[$i]->close();
519 }
520 }
521 }
522
523 function commitAll() {
524 foreach( $this->mConnections as $i => $conn ) {
525 if ( $this->isOpen( $i ) ) {
526 // Need to use this syntax because $conn is a copy not a reference
527 $this->mConnections[$i]->immediateCommit();
528 }
529 }
530 }
531
532 function waitTimeout( $value = NULL ) {
533 return wfSetVar( $this->mWaitTimeout, $value );
534 }
535
536 function getLaggedSlaveMode() {
537 return $this->mLaggedSlaveMode;
538 }
539
540 function pingAll() {
541 $success = true;
542 foreach ( $this->mConnections as $i => $conn ) {
543 if ( $this->isOpen( $i ) ) {
544 if ( !$this->mConnections[$i]->ping() ) {
545 $success = false;
546 }
547 }
548 }
549 return $success;
550 }
551
552 /**
553 * Get the hostname and lag time of the most-lagged slave
554 * This is useful for maintenance scripts that need to throttle their updates
555 */
556 function getMaxLag() {
557 $maxLag = -1;
558 $host = '';
559 foreach ( $this->mServers as $i => $conn ) {
560 if ( $this->openConnection( $i ) ) {
561 $lag = $this->mConnections[$i]->getLag();
562 if ( $lag > $maxLag ) {
563 $maxLag = $lag;
564 $host = $this->mServers[$i]['host'];
565 }
566 }
567 }
568 return array( $host, $maxLag );
569 }
570
571 /**
572 * Get lag time for each DB
573 * Results are cached for a short time in memcached
574 */
575 function getLagTimes() {
576 $expiry = 5;
577 $requestRate = 10;
578
579 global $wgMemc;
580 $times = $wgMemc->get( 'lag_times' );
581 if ( $times ) {
582 # Randomly recache with probability rising over $expiry
583 $elapsed = time() - $times['timestamp'];
584 $chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
585 if ( mt_rand( 0, $chance ) != 0 ) {
586 unset( $times['timestamp'] );
587 return $times;
588 }
589 }
590
591 # Cache key missing or expired
592
593 $times = array();
594 foreach ( $this->mServers as $i => $conn ) {
595 if ( $this->openConnection( $i ) ) {
596 $times[$i] = $this->mConnections[$i]->getLag();
597 }
598 }
599
600 # Add a timestamp key so we know when it was cached
601 $times['timestamp'] = time();
602 $wgMemc->set( 'lag_times', $times, $expiry );
603
604 # But don't give the timestamp to the caller
605 unset($times['timestamp']);
606 return $times;
607 }
608 }
609
610 ?>