summaryrefslogtreecommitdiff
path: root/includes/LoadBalancer.php
blob: 396ef865be4fc280c2edb0a440db14b526c42ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
/**
 *
 * @package MediaWiki
 */


/**
 * Database load balancing object
 *
 * @todo document
 * @package MediaWiki
 */
class LoadBalancer {
	/* private */ var $mServers, $mConnections, $mLoads, $mGroupLoads;
	/* private */ var $mFailFunction, $mErrorConnection;
	/* private */ var $mForce, $mReadIndex, $mLastIndex, $mAllowLagged;
	/* private */ var $mWaitForFile, $mWaitForPos, $mWaitTimeout;
	/* private */ var $mLaggedSlaveMode, $mLastError = 'Unknown error';

	/**
	 * Scale polling time so that under overload conditions, the database server
	 * receives a SHOW STATUS query at an average interval of this many microseconds
	 */
	const AVG_STATUS_POLL = 2000;

	function LoadBalancer( $servers, $failFunction = false, $waitTimeout = 10, $waitForMasterNow = false )
	{
		$this->mServers = $servers;
		$this->mFailFunction = $failFunction;
		$this->mReadIndex = -1;
		$this->mWriteIndex = -1;
		$this->mForce = -1;
		$this->mConnections = array();
		$this->mLastIndex = 1;
		$this->mLoads = array();
		$this->mWaitForFile = false;
		$this->mWaitForPos = false;
		$this->mWaitTimeout = $waitTimeout;
		$this->mLaggedSlaveMode = false;
		$this->mErrorConnection = false;
		$this->mAllowLag = false;

		foreach( $servers as $i => $server ) {
			$this->mLoads[$i] = $server['load'];
			if ( isset( $server['groupLoads'] ) ) {
				foreach ( $server['groupLoads'] as $group => $ratio ) {
					if ( !isset( $this->mGroupLoads[$group] ) ) {
						$this->mGroupLoads[$group] = array();
					}
					$this->mGroupLoads[$group][$i] = $ratio;
				}
			}
		}
		if ( $waitForMasterNow ) {
			$this->loadMasterPos();
		}
	}

	static function newFromParams( $servers, $failFunction = false, $waitTimeout = 10 )
	{
		return new LoadBalancer( $servers, $failFunction, $waitTimeout );
	}

	/**
	 * Given an array of non-normalised probabilities, this function will select
	 * an element and return the appropriate key
	 */
	function pickRandom( $weights )
	{
		if ( !is_array( $weights ) || count( $weights ) == 0 ) {
			return false;
		}

		$sum = array_sum( $weights );
		if ( $sum == 0 ) {
			# No loads on any of them
			# In previous versions, this triggered an unweighted random selection,
			# but this feature has been removed as of April 2006 to allow for strict 
			# separation of query groups. 
			return false;
		}
		$max = mt_getrandmax();
		$rand = mt_rand(0, $max) / $max * $sum;

		$sum = 0;
		foreach ( $weights as $i => $w ) {
			$sum += $w;
			if ( $sum >= $rand ) {
				break;
			}
		}
		return $i;
	}

	function getRandomNonLagged( $loads ) {
		# Unset excessively lagged servers
		$lags = $this->getLagTimes();
		foreach ( $lags as $i => $lag ) {
			if ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
				unset( $loads[$i] );
			}
		}

		# Find out if all the slaves with non-zero load are lagged
		$sum = 0;
		foreach ( $loads as $load ) {
			$sum += $load;
		}
		if ( $sum == 0 ) {
			# No appropriate DB servers except maybe the master and some slaves with zero load
			# Do NOT use the master
			# Instead, this function will return false, triggering read-only mode,
			# and a lagged slave will be used instead.
			return false;
		}

		if ( count( $loads ) == 0 ) {
			return false;
		}

		#wfDebugLog( 'connect', var_export( $loads, true ) );

		# Return a random representative of the remainder
		return $this->pickRandom( $loads );
	}

	/**
	 * Get the index of the reader connection, which may be a slave
	 * This takes into account load ratios and lag times. It should
	 * always return a consistent index during a given invocation
	 *
	 * Side effect: opens connections to databases
	 */
	function getReaderIndex() {
		global $wgReadOnly, $wgDBClusterTimeout;

		$fname = 'LoadBalancer::getReaderIndex';
		wfProfileIn( $fname );

		$i = false;
		if ( $this->mForce >= 0 ) {
			$i = $this->mForce;
		} elseif ( count( $this->mServers ) == 1 )  {
			# Skip the load balancing if there's only one server
			$i = 0;
		} else {
			if ( $this->mReadIndex >= 0 ) {
				$i = $this->mReadIndex;
			} else {
				# $loads is $this->mLoads except with elements knocked out if they
				# don't work
				$loads = $this->mLoads;
				$done = false;
				$totalElapsed = 0;
				do {
					if ( $wgReadOnly or $this->mAllowLagged ) {
						$i = $this->pickRandom( $loads );
					} else {
						$i = $this->getRandomNonLagged( $loads );
						if ( $i === false && count( $loads ) != 0 )  {
							# All slaves lagged. Switch to read-only mode
							$wgReadOnly = wfMsgNoDBForContent( 'readonly_lag' );
							$i = $this->pickRandom( $loads );
						}
					}
					$serverIndex = $i;
					if ( $i !== false ) {
						wfDebugLog( 'connect', "$fname: Using reader #$i: {$this->mServers[$i]['host']}...\n" );
						$this->openConnection( $i );

						if ( !$this->isOpen( $i ) ) {
							wfDebug( "$fname: Failed\n" );
							unset( $loads[$i] );
							$sleepTime = 0;
						} else {
							if ( isset( $this->mServers[$i]['max threads'] ) ) {
							    $status = $this->mConnections[$i]->getStatus("Thread%");
							    if ( $status['Threads_running'] > $this->mServers[$i]['max threads'] ) {
								# Too much load, back off and wait for a while.
								# The sleep time is scaled by the number of threads connected,
								# to produce a roughly constant global poll rate.
								$sleepTime = self::AVG_STATUS_POLL * $status['Threads_connected'];

								# If we reach the timeout and exit the loop, don't use it
								$i = false;
							    } else {
								$done = true;
								$sleepTime = 0;
							    }
							} else {
							    $done = true;
							    $sleepTime = 0;
							}
						}
					} else {
						$sleepTime = 500000;
					}
					if ( $sleepTime ) {
							$totalElapsed += $sleepTime;
							$x = "{$this->mServers[$serverIndex]['host']} [$serverIndex]";
							wfProfileIn( "$fname-sleep $x" );
							usleep( $sleepTime );
							wfProfileOut( "$fname-sleep $x" );
					}
				} while ( count( $loads ) && !$done && $totalElapsed / 1e6 < $wgDBClusterTimeout );

				if ( $totalElapsed / 1e6 >= $wgDBClusterTimeout ) {
					$this->mErrorConnection = false;
					$this->mLastError = 'All servers busy';
				}

				if ( $i !== false && $this->isOpen( $i ) ) {
					# Wait for the session master pos for a short time
					if ( $this->mWaitForFile ) {
						if ( !$this->doWait( $i ) ) {
							$this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
						}
					}
					if ( $i !== false ) {
						$this->mReadIndex = $i;
					}
				} else {
					$i = false;
				}
			}
		}
		wfProfileOut( $fname );
		return $i;
	}

	/**
	 * Get a random server to use in a query group
	 */
	function getGroupIndex( $group ) {
		if ( isset( $this->mGroupLoads[$group] ) ) {
			$i = $this->pickRandom( $this->mGroupLoads[$group] );
		} else {
			$i = false;
		}
		wfDebug( "Query group $group => $i\n" );
		return $i;
	}

	/**
	 * Set the master wait position
	 * If a DB_SLAVE connection has been opened already, waits
	 * Otherwise sets a variable telling it to wait if such a connection is opened
	 */
	function waitFor( $file, $pos ) {
		$fname = 'LoadBalancer::waitFor';
		wfProfileIn( $fname );

		wfDebug( "User master pos: $file $pos\n" );
		$this->mWaitForFile = false;
		$this->mWaitForPos = false;

		if ( count( $this->mServers ) > 1 ) {
			$this->mWaitForFile = $file;
			$this->mWaitForPos = $pos;
			$i = $this->mReadIndex;

			if ( $i > 0 ) {
				if ( !$this->doWait( $i ) ) {
					$this->mServers[$i]['slave pos'] = $this->mConnections[$i]->getSlavePos();
					$this->mLaggedSlaveMode = true;
				}
			}
		}
		wfProfileOut( $fname );
	}

	/**
	 * Wait for a given slave to catch up to the master pos stored in $this
	 */
	function doWait( $index ) {
		global $wgMemc;

		$retVal = false;

		# Debugging hacks
		if ( isset( $this->mServers[$index]['lagged slave'] ) ) {
			return false;
		} elseif ( isset( $this->mServers[$index]['fake slave'] ) ) {
			return true;
		}

		$key = 'masterpos:' . $index;
		$memcPos = $wgMemc->get( $key );
		if ( $memcPos ) {
			list( $file, $pos ) = explode( ' ', $memcPos );
			# If the saved position is later than the requested position, return now
			if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
				$retVal = true;
			}
		}

		if ( !$retVal && $this->isOpen( $index ) ) {
			$conn =& $this->mConnections[$index];
			wfDebug( "Waiting for slave #$index to catch up...\n" );
			$result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, $this->mWaitTimeout );

			if ( $result == -1 || is_null( $result ) ) {
				# Timed out waiting for slave, use master instead
				wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
				$retVal = false;
			} else {
				$retVal = true;
				wfDebug( "Done\n" );
			}
		}
		return $retVal;
	}

	/**
	 * Get a connection by index
	 */
	function &getConnection( $i, $fail = true, $groups = array() )
	{
		global $wgDBtype;
		$fname = 'LoadBalancer::getConnection';
		wfProfileIn( $fname );


		# Query groups
		if ( !is_array( $groups ) ) {
			$groupIndex = $this->getGroupIndex( $groups, $i );
			if ( $groupIndex !== false ) {
				$i = $groupIndex;
			}
		} else {
			foreach ( $groups as $group ) {
				$groupIndex = $this->getGroupIndex( $group, $i );
				if ( $groupIndex !== false ) {
					$i = $groupIndex;
					break;
				}
			}
		}

		# For now, only go through all this for mysql databases
		if ($wgDBtype != 'mysql') {
			$i = $this->getWriterIndex();
		}
		# Operation-based index
		elseif ( $i == DB_SLAVE ) {
			$i = $this->getReaderIndex();
		} elseif ( $i == DB_MASTER ) {
			$i = $this->getWriterIndex();
		} elseif ( $i == DB_LAST ) {
			# Just use $this->mLastIndex, which should already be set
			$i = $this->mLastIndex;
			if ( $i === -1 ) {
				# Oh dear, not set, best to use the writer for safety
				wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
				$i = $this->getWriterIndex();
			}
		}
		# Couldn't find a working server in getReaderIndex()?
		if ( $i === false ) {
			$this->reportConnectionError( $this->mErrorConnection );
		}
		# Now we have an explicit index into the servers array
		$this->openConnection( $i, $fail );

		wfProfileOut( $fname );
		return $this->mConnections[$i];
	}

	/**
	 * Open a connection to the server given by the specified index
	 * Index must be an actual index into the array
	 * Returns success
	 * @access private
	 */
	function openConnection( $i, $fail = false ) {
		$fname = 'LoadBalancer::openConnection';
		wfProfileIn( $fname );
		$success = true;

		if ( !$this->isOpen( $i ) ) {
			$this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
		}

		if ( !$this->isOpen( $i ) ) {
			wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
			if ( $fail ) {
				$this->reportConnectionError( $this->mConnections[$i] );
			}
			$this->mErrorConnection = $this->mConnections[$i];
			$this->mConnections[$i] = false;
			$success = false;
		}
		$this->mLastIndex = $i;
		wfProfileOut( $fname );
		return $success;
	}

	/**
	 * Test if the specified index represents an open connection
	 * @access private
	 */
	function isOpen( $index ) {
		if( !is_integer( $index ) ) {
			return false;
		}
		if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
		  $this->mConnections[$index]->isOpen() )
		{
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Really opens a connection
	 * @access private
	 */
	function reallyOpenConnection( &$server ) {
		if( !is_array( $server ) ) {
			throw new MWException( 'You must update your load-balancing configuration. See DefaultSettings.php entry for $wgDBservers.' );
		}

		extract( $server );
		# Get class for this database type
		$class = 'Database' . ucfirst( $type );

		# Create object
		$db = new $class( $host, $user, $password, $dbname, 1, $flags );
		$db->setLBInfo( $server );
		return $db;
	}

	function reportConnectionError( &$conn )
	{
		$fname = 'LoadBalancer::reportConnectionError';
		wfProfileIn( $fname );
		# Prevent infinite recursion

		static $reporting = false;
		if ( !$reporting ) {
			$reporting = true;
			if ( !is_object( $conn ) ) {
				// No last connection, probably due to all servers being too busy
				$conn = new Database;
				if ( $this->mFailFunction ) {
					$conn->failFunction( $this->mFailFunction );
					$conn->reportConnectionError( $this->mLastError );
				} else {
					// If all servers were busy, mLastError will contain something sensible
					throw new DBConnectionError( $conn, $this->mLastError );
				}
			} else {
				if ( $this->mFailFunction ) {
					$conn->failFunction( $this->mFailFunction );
				} else {
					$conn->failFunction( false );
				}
				$server = $conn->getProperty( 'mServer' );
				$conn->reportConnectionError( "{$this->mLastError} ({$server})" );
			}
			$reporting = false;
		}
		wfProfileOut( $fname );
	}

	function getWriterIndex() {
		return 0;
	}

	/**
	 * Force subsequent calls to getConnection(DB_SLAVE) to return the 
	 * given index. Set to -1 to restore the original load balancing
	 * behaviour. I thought this was a good idea when I originally 
	 * wrote this class, but it has never been used.
	 */
	function force( $i ) {
		$this->mForce = $i;
	}

	/**
	 * Returns true if the specified index is a valid server index
	 */
	function haveIndex( $i ) {
		return array_key_exists( $i, $this->mServers );
	}

	/**
	 * Returns true if the specified index is valid and has non-zero load
	 */
	function isNonZeroLoad( $i ) {
		return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
	}

	/**
	 * Get the number of defined servers (not the number of open connections)
	 */
	function getServerCount() {
		return count( $this->mServers );
	}

	/**
	 * Save master pos to the session and to memcached, if the session exists
	 */
	function saveMasterPos() {
		global $wgSessionStarted;
		if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
			# If this entire request was served from a slave without opening a connection to the
			# master (however unlikely that may be), then we can fetch the position from the slave.
			if ( empty( $this->mConnections[0] ) ) {
				$conn =& $this->getConnection( DB_SLAVE );
				list( $file, $pos ) = $conn->getSlavePos();
				wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
			} else {
				$conn =& $this->getConnection( 0 );
				list( $file, $pos ) = $conn->getMasterPos();
				wfDebug( "Saving master pos: $file $pos\n" );
			}
			if ( $file !== false ) {
				$_SESSION['master_log_file'] = $file;
				$_SESSION['master_pos'] = $pos;
			}
		}
	}

	/**
	 * Loads the master pos from the session, waits for it if necessary
	 */
	function loadMasterPos() {
		if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
			$this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
		}
	}

	/**
	 * Close all open connections
	 */
	function closeAll() {
		foreach( $this->mConnections as $i => $conn ) {
			if ( $this->isOpen( $i ) ) {
				// Need to use this syntax because $conn is a copy not a reference
				$this->mConnections[$i]->close();
			}
		}
	}

	function commitAll() {
		foreach( $this->mConnections as $i => $conn ) {
			if ( $this->isOpen( $i ) ) {
				// Need to use this syntax because $conn is a copy not a reference
				$this->mConnections[$i]->immediateCommit();
			}
		}
	}

	function waitTimeout( $value = NULL ) {
		return wfSetVar( $this->mWaitTimeout, $value );
	}

	function getLaggedSlaveMode() {
		return $this->mLaggedSlaveMode;
	}

	/* Disables/enables lag checks */
	function allowLagged($mode=null) {
		if ($mode===null)
			return $this->mAllowLagged;
		$this->mAllowLagged=$mode;
	}

	function pingAll() {
		$success = true;
		foreach ( $this->mConnections as $i => $conn ) {
			if ( $this->isOpen( $i ) ) {
				if ( !$this->mConnections[$i]->ping() ) {
					$success = false;
				}
			}
		}
		return $success;
	}

	/**
	 * Get the hostname and lag time of the most-lagged slave
	 * This is useful for maintenance scripts that need to throttle their updates
	 */
	function getMaxLag() {
		$maxLag = -1;
		$host = '';
		foreach ( $this->mServers as $i => $conn ) {
			if ( $this->openConnection( $i ) ) {
				$lag = $this->mConnections[$i]->getLag();
				if ( $lag > $maxLag ) {
					$maxLag = $lag;
					$host = $this->mServers[$i]['host'];
				}
			}
		}
		return array( $host, $maxLag );
	}

	/**
	 * Get lag time for each DB
	 * Results are cached for a short time in memcached
	 */
	function getLagTimes() {
		wfProfileIn( __METHOD__ );
		$expiry = 5;
		$requestRate = 10;

		global $wgMemc;
		$times = $wgMemc->get( wfMemcKey( 'lag_times' ) );
		if ( $times ) {
			# Randomly recache with probability rising over $expiry
			$elapsed = time() - $times['timestamp'];
			$chance = max( 0, ( $expiry - $elapsed ) * $requestRate );
			if ( mt_rand( 0, $chance ) != 0 ) {
				unset( $times['timestamp'] );
				wfProfileOut( __METHOD__ );
				return $times;
			}
			wfIncrStats( 'lag_cache_miss_expired' );
		} else {
			wfIncrStats( 'lag_cache_miss_absent' );
		}

		# Cache key missing or expired

		$times = array();
		foreach ( $this->mServers as $i => $conn ) {
			if ($i==0) { # Master
				$times[$i] = 0;
			} elseif ( $this->openConnection( $i ) ) {
				$times[$i] = $this->mConnections[$i]->getLag();
			}
		}

		# Add a timestamp key so we know when it was cached
		$times['timestamp'] = time();
		$wgMemc->set( wfMemcKey( 'lag_times' ), $times, $expiry );

		# But don't give the timestamp to the caller
		unset($times['timestamp']);
		wfProfileOut( __METHOD__ );
		return $times;
	}
}

?>