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