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