aboutsummaryrefslogtreecommitdiffstats
path: root/includes/LoadBalancer.php
blob: 3df2ac58c604e7e2a20beac127645230e9ec4a4c (plain) (blame)
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
<?php
# Database load balancing object

class LoadBalancer {
	/* private */ var $mServers, $mConnections, $mLoads;
	/* private */ var $mUser, $mPassword, $mDbName, $mFailFunction;
	/* private */ var $mForce, $mReadIndex;

	function LoadBalancer()
	{
		$this->mServers = array();
		$this->mLoads = array();
		$this->mConnections = array();
		$this->mUser = false;
		$this->mPassword = false;
		$this->mDbName = false;
		$this->mFailFunction = false;
		$this->mReadIndex = -1;
		$this->mForce = -1;
	}

	function newFromParams( $servers, $loads, $user, $password, $dbName, $failFunction = false )
	{
		$lb = new LoadBalancer;
		$lb->initialise( $servers, $loads, $user, $password, $dbName, $failFunction = false );
		return $lb;
	}

	function initialise( $servers, $loads, $user, $password, $dbName, $failFunction = false )
	{
		$this->mServers = $servers;
		$this->mLoads = $loads;
		$this->mUser = $user;
		$this->mPassword = $password;
		$this->mDbName = $dbName;
		$this->mFailFunction = $failFunction;
		$this->mReadIndex = -1;
		$this->mWriteIndex = -1;
		$this->mForce = -1;
		$this->mConnections = array();
		wfSeedRandom();
	}
	
	# 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 = 0;
		foreach ( $weights as $w ) {
			$sum += $w;
		}
		$rand = mt_rand() / RAND_MAX * $sum;
		
		$sum = 0;
		foreach ( $weights as $i => $w ) {
			$sum += $w;
			if ( $sum >= $rand ) {
				break;
			}
		}
		return $i;
	}

	function &getReader()
	{
		if ( $this->mForce >= 0 ) {
			$conn =& $this->getConnection( $this->mForce );
		} else {
			if ( $this->mReadIndex >= 0 ) {
				$conn =& $this->getConnection( $this->mReadIndex );
			} else {
				# $loads is $this->mLoads except with elements knocked out if they
				# don't work
				$loads = $this->mLoads;
				do {
					$i = pickRandom( $loads );
					if ( $i !== false ) {
						$conn =& $this->getConnection( $i );
						if ( !$conn->isOpen() ) {
							unset( $loads[$i] );
						}
					}
				} while ( $i !== false && !$conn->isOpen() );
				if ( $conn->isOpen() ) {
					$this->mReadIndex = $i;
				}
			}
		}
		if ( $conn === false || !$conn->isOpen() ) {
			$this->reportConnectionError( $conn );
			$conn = false;
		}
		return $conn;
	}
	
	function &getConnection( $i, $fail = false )
	{
		if ( !array_key_exists( $i, $this->mConnections) || !$this->mConnections[$i]->isOpen() ) {
			$this->mConnections[$i] = Database::newFromParams( $this->mServers[$i], $this->mUser, 
			  $this->mPassword, $this->mDbName, 1 );
		}
		if ( !$this->mConnections[$i]->isOpen() ) {
			wfDebug( "Failed to connect to database $i at {$this->mServers[$i]}\n" );
			if ( $fail ) {
				$this->reportConnectionError( $this->mConnections[$i] );
			}
			$this->mConnections[$i] = false;
		}
		return $this->mConnections[$i];
	}

	function reportConnectionError( &$conn )
	{
		if ( !is_object( $conn ) ) {
			$conn = new Database;
		}
		if ( $this->mFailFunction ) {
			$conn->setFailFunction( $this->mFailFunction );
		} else {
			$conn->setFailFunction( "wfEmergencyAbort" );
		}
		$conn->reportConnectionError();
	}
	
	function &getWriter()
	{
		$c =& $this->getConnection( 0 );
		if ( !$c->isOpen() ) {
			reportConnectionError( $conn );
			$c = false;
		}
		return $c;
	}

	function force( $i )
	{
		$this->mForce = $i;
	}
}