Load balancer bug fixes
[lhc/web/wiklou.git] / includes / LoadBalancer.php
1 <?php
2 # Database load balancing object
3
4 # Valid database indexes
5 # Operation-based indexes
6 define( "DB_READ", -1 ); # Read from the slave (or only server)
7 define( "DB_WRITE", -2 ); # Write to master (or only server)
8 define( "DB_LAST", -3 ); # Whatever database was used last
9
10 # Task-based indexes
11 # ***NOT USED YET, EXPERIMENTAL***
12 # These may be defined in $wgDBservers. If they aren't, the default reader or writer will be used
13 # Even numbers are always readers, odd numbers are writers
14 define( "DB_TASK_FIRST", 1000 ); # First in list
15 define( "DB_SEARCH_R", 1000 ); # Search read
16 define( "DB_SEARCH_W", 1001 ); # Search write
17 define( "DB_ASKSQL_R", 1002 ); # Special:Asksql read
18 define( "DB_WATCHLIST_R", 1004 ); # Watchlist read
19 define( "DB_TASK_LAST", 1004) ; # Last in list
20
21 class LoadBalancer {
22 /* private */ var $mServers, $mConnections, $mLoads;
23 /* private */ var $mUser, $mPassword, $mDbName, $mFailFunction;
24 /* private */ var $mForce, $mReadIndex, $mLastConn;
25
26 function LoadBalancer()
27 {
28 $this->mServers = array();
29 $this->mLoads = array();
30 $this->mConnections = array();
31 $this->mUser = false;
32 $this->mPassword = false;
33 $this->mDbName = false;
34 $this->mFailFunction = false;
35 $this->mReadIndex = -1;
36 $this->mForce = -1;
37 $this->mLastConn = false;
38 }
39
40 function newFromParams( $servers, $loads, $user, $password, $dbName, $failFunction = false )
41 {
42 $lb = new LoadBalancer;
43 $lb->initialise( $servers, $loads, $user, $password, $dbName, $failFunction = false );
44 return $lb;
45 }
46
47 function initialise( $servers, $loads, $user, $password, $dbName, $failFunction = false )
48 {
49 $this->mServers = $servers;
50 $this->mLoads = $loads;
51 $this->mUser = $user;
52 $this->mPassword = $password;
53 $this->mDbName = $dbName;
54 $this->mFailFunction = $failFunction;
55 $this->mReadIndex = -1;
56 $this->mWriteIndex = -1;
57 $this->mForce = -1;
58 $this->mConnections = array();
59 $this->mLastConn = false;
60 wfSeedRandom();
61 }
62
63 # Given an array of non-normalised probabilities, this function will select
64 # an element and return the appropriate key
65 function pickRandom( $weights )
66 {
67 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
68 return false;
69 }
70
71 $sum = 0;
72 foreach ( $weights as $w ) {
73 $sum += $w;
74 }
75 $max = mt_getrandmax();
76 $rand = mt_rand(0, $max) / $max * $sum;
77
78 $sum = 0;
79 foreach ( $weights as $i => $w ) {
80 $sum += $w;
81 if ( $sum >= $rand ) {
82 break;
83 }
84 }
85 return $i;
86 }
87
88 function &getReader()
89 {
90 if ( $this->mForce >= 0 ) {
91 $conn =& $this->getConnection( $this->mForce );
92 } else {
93 if ( $this->mReadIndex >= 0 ) {
94 $conn =& $this->getConnection( $this->mReadIndex );
95 } else {
96 # $loads is $this->mLoads except with elements knocked out if they
97 # don't work
98 $loads = $this->mLoads;
99 do {
100 $i = $this->pickRandom( $loads );
101 if ( $i !== false ) {
102 wfDebug( "Using reader #$i: {$this->mServers[$i]}\n" );
103
104 $conn =& $this->getConnection( $i );
105 if ( !$conn->isOpen() ) {
106 unset( $loads[$i] );
107 }
108 }
109 } while ( $i !== false && !$conn->isOpen() );
110 if ( $conn->isOpen() ) {
111 $this->mReadIndex = $i;
112 }
113 }
114 }
115 if ( $conn === false || !$conn->isOpen() ) {
116 $this->reportConnectionError( $conn );
117 $conn = false;
118 }
119 return $conn;
120 }
121
122 function &getConnection( $i, $fail = false )
123 {
124 /*
125 # Task-based index
126 if ( $i >= DB_TASK_FIRST && $i < DB_TASK_LAST ) {
127 if ( $i % 2 ) {
128 # Odd index use writer
129 $i = DB_WRITE;
130 } else {
131 # Even index use reader
132 $i = DB_READ;
133 }
134 }*/
135
136 # Operation-based index
137 # Note, getReader() and getWriter() will re-enter this function
138 if ( $i == DB_READ ) {
139 $this->mLastConn =& $this->getReader();
140 } elseif ( $i == DB_WRITE ) {
141 $this->mLastConn =& $this->getWriter();
142 } elseif ( $i == DB_LAST ) {
143 # Just use $this->mLastConn, which should already be set
144 if ( $this->mLastConn === false ) {
145 # Oh dear, not set, best to use the writer for safety
146 $this->mLastConn =& $this->getWriter();
147 }
148 } else {
149 # Explicit index
150 if ( !array_key_exists( $i, $this->mConnections) || !$this->mConnections[$i]->isOpen() ) {
151 $this->mConnections[$i] = Database::newFromParams( $this->mServers[$i], $this->mUser,
152 $this->mPassword, $this->mDbName, 1 );
153 }
154 if ( !$this->mConnections[$i]->isOpen() ) {
155 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]}\n" );
156 if ( $fail ) {
157 $this->reportConnectionError( $this->mConnections[$i] );
158 }
159 $this->mConnections[$i] = false;
160 }
161 $this->mLastConn =& $this->mConnections[$i];
162 }
163 return $this->mLastConn;
164 }
165
166 function reportConnectionError( &$conn )
167 {
168 if ( !is_object( $conn ) ) {
169 $conn = new Database;
170 }
171 if ( $this->mFailFunction ) {
172 $conn->setFailFunction( $this->mFailFunction );
173 } else {
174 $conn->setFailFunction( "wfEmergencyAbort" );
175 }
176 $conn->reportConnectionError();
177 }
178
179 function &getWriter()
180 {
181 $c =& $this->getConnection( 0 );
182 if ( $c === false || !$c->isOpen() ) {
183 $this->reportConnectionError( $c );
184 $c = false;
185 }
186 return $c;
187 }
188
189 function force( $i )
190 {
191 $this->mForce = $i;
192 }
193
194 function haveIndex( $i )
195 {
196 return array_key_exists( $i, $this->mServers );
197 }
198 }