massive double to single quotes conversion. I have not noticed any bug after a lot...
[lhc/web/wiklou.git] / includes / LoadBalancer.php
1 <?php
2 # Database load balancing object
3
4 require_once( 'Database.php' );
5
6 # Valid database indexes
7 # Operation-based indexes
8 define( 'DB_SLAVE', -1 ); # Read from the slave (or only server)
9 define( 'DB_MASTER', -2 ); # Write to master (or only server)
10 define( 'DB_LAST', -3 ); # Whatever database was used last
11
12 # Obsolete aliases
13 define( 'DB_READ', -1 );
14 define( 'DB_WRITE', -2 );
15
16 # Task-based indexes
17 # ***NOT USED YET, EXPERIMENTAL***
18 # These may be defined in $wgDBservers. If they aren't, the default reader or writer will be used
19 # Even numbers are always readers, odd numbers are writers
20 define( 'DB_TASK_FIRST', 1000 ); # First in list
21 define( 'DB_SEARCH_R', 1000 ); # Search read
22 define( 'DB_SEARCH_W', 1001 ); # Search write
23 define( 'DB_ASKSQL_R', 1002 ); # Special:Asksql read
24 define( 'DB_WATCHLIST_R', 1004 ); # Watchlist read
25 define( 'DB_TASK_LAST', 1004) ; # Last in list
26
27 define( 'MASTER_WAIT_TIMEOUT', 15 ); # Time to wait for a slave to synchronise
28
29 class LoadBalancer {
30 /* private */ var $mServers, $mConnections, $mLoads;
31 /* private */ var $mFailFunction;
32 /* private */ var $mForce, $mReadIndex, $mLastIndex;
33 /* private */ var $mWaitForFile, $mWaitForPos;
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 )
46 {
47 $lb = new LoadBalancer;
48 $lb->initialise( $servers, $failFunction = false );
49 return $lb;
50 }
51
52 function initialise( $servers, $failFunction = false )
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
65 foreach( $servers as $i => $server ) {
66 $this->mLoads[$i] = $server['load'];
67 }
68 }
69
70 # Given an array of non-normalised probabilities, this function will select
71 # an element and return the appropriate key
72 function pickRandom( $weights )
73 {
74 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
75 return false;
76 }
77
78 $sum = 0;
79 foreach ( $weights as $w ) {
80 $sum += $w;
81 }
82 $max = mt_getrandmax();
83 $rand = mt_rand(0, $max) / $max * $sum;
84
85 $sum = 0;
86 foreach ( $weights as $i => $w ) {
87 $sum += $w;
88 if ( $sum >= $rand ) {
89 break;
90 }
91 }
92 return $i;
93 }
94
95 function getReaderIndex()
96 {
97 $fname = 'LoadBalancer::getReaderIndex';
98 wfProfileIn( $fname );
99
100 $i = false;
101 if ( $this->mForce >= 0 ) {
102 $i = $this->mForce;
103 } else {
104 if ( $this->mReadIndex >= 0 ) {
105 $i = $this->mReadIndex;
106 } else {
107 # $loads is $this->mLoads except with elements knocked out if they
108 # don't work
109 $loads = $this->mLoads;
110 do {
111 $i = $this->pickRandom( $loads );
112 if ( $i !== false ) {
113 wfDebug( "Using reader #$i: {$this->mServers[$i]['host']}\n" );
114
115 $this->openConnection( $i );
116
117 if ( !$this->isOpen( $i ) ) {
118 unset( $loads[$i] );
119 }
120 }
121 } while ( $i !== false && !$this->isOpen( $i ) );
122
123 if ( $this->isOpen( $i ) ) {
124 $this->mReadIndex = $i;
125 } else {
126 $i = false;
127 }
128 }
129 }
130 wfProfileOut( $fname );
131 return $i;
132 }
133
134 # Set the master wait position
135 # If a DB_SLAVE connection has been opened already, waits
136 # Otherwise sets a variable telling it to wait if such a connection is opened
137 function waitFor( $file, $pos ) {
138 $fname = 'LoadBalancer::waitFor';
139 wfProfileIn( $fname );
140
141 wfDebug( "User master pos: $file $pos\n" );
142 $this->mWaitForFile = false;
143 $this->mWaitForPos = false;
144
145 if ( count( $this->mServers ) > 1 ) {
146 $this->mWaitForFile = $file;
147 $this->mWaitForPos = $pos;
148
149 if ( $this->mReadIndex > 0 ) {
150 if ( !$this->doWait( $this->mReadIndex ) ) {
151 # Use master instead
152 $this->mReadIndex = 0;
153 }
154 }
155 }
156 wfProfileOut( $fname );
157 }
158
159 # Wait for a given slave to catch up to the master pos stored in $this
160 function doWait( $index ) {
161 global $wgMemc;
162
163 $retVal = false;
164
165 $key = 'masterpos:' . $index;
166 $memcPos = $wgMemc->get( $key );
167 if ( $memcPos ) {
168 list( $file, $pos ) = explode( ' ', $memcPos );
169 # If the saved position is later than the requested position, return now
170 if ( $file == $this->mWaitForFile && $this->mWaitForPos <= $pos ) {
171 $retVal = true;
172 }
173 }
174
175 if ( !$retVal && $this->isOpen( $index ) ) {
176 $conn =& $this->mConnections( $index );
177 wfDebug( "Waiting for slave #$index to catch up...\n" );
178 $result = $conn->masterPosWait( $this->mWaitForFile, $this->mWaitForPos, MASTER_WAIT_TIMEOUT );
179
180 if ( $result == -1 || is_null( $result ) ) {
181 # Timed out waiting for slave, use master instead
182 wfDebug( "Timed out waiting for slave #$index pos {$this->mWaitForFile} {$this->mWaitForPos}\n" );
183 $retVal = false;
184 } else {
185 $retVal = true;
186 wfDebug( "Done\n" );
187 }
188 }
189 return $retVal;
190 }
191
192 # Get a connection by index
193 function &getConnection( $i, $fail = true )
194 {
195 $fname = 'LoadBalancer::getConnection';
196 wfProfileIn( $fname );
197 /*
198 # Task-based index
199 if ( $i >= DB_TASK_FIRST && $i < DB_TASK_LAST ) {
200 if ( $i % 2 ) {
201 # Odd index use writer
202 $i = DB_MASTER;
203 } else {
204 # Even index use reader
205 $i = DB_SLAVE;
206 }
207 }*/
208
209 # Operation-based index
210 if ( $i == DB_SLAVE ) {
211 $i = $this->getReaderIndex();
212 } elseif ( $i == DB_MASTER ) {
213 $i = $this->getWriterIndex();
214 } elseif ( $i == DB_LAST ) {
215 # Just use $this->mLastIndex, which should already be set
216 $i = $this->mLastIndex;
217 if ( $i === -1 ) {
218 # Oh dear, not set, best to use the writer for safety
219 wfDebug( "Warning: DB_LAST used when there was no previous index\n" );
220 $i = $this->getWriterIndex();
221 }
222 }
223 # Now we have an explicit index into the servers array
224 $this->openConnection( $i, $fail );
225 wfProfileOut( $fname );
226 return $this->mConnections[$i];
227 }
228
229 # Open a connection to the server given by the specified index
230 # Index must be an actual index into the array
231 /* private */ function openConnection( $i, $fail = false ) {
232 $fname = 'LoadBalancer::openConnection';
233 wfProfileIn( $fname );
234
235 if ( !$this->isOpen( $i ) ) {
236 $this->mConnections[$i] = $this->reallyOpenConnection( $this->mServers[$i] );
237
238 if ( $i != 0 && $this->mWaitForFile ) {
239 if ( !$this->doWait( $i ) ) {
240 # Error waiting for this slave, use master instead
241 $this->mReadIndex = 0;
242 $i = 0;
243 if ( !$this->isOpen( 0 ) ) {
244 $this->mConnections[0] = $this->reallyOpenConnection( $this->mServers[0] );
245 }
246 wfDebug( "Failed over to {$this->mConnections[0]->mServer}\n" );
247 }
248 }
249 }
250 if ( !$this->isOpen( $i ) ) {
251 wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
252 if ( $fail ) {
253 $this->reportConnectionError( $this->mConnections[$i] );
254 }
255 $this->mConnections[$i] = false;
256 }
257 $this->mLastIndex = $i;
258 wfProfileOut( $fname );
259 }
260
261 # Test if the specified index represents an open connection
262 /* private */ function isOpen( $index ) {
263 if( !is_integer( $index ) ) {
264 return false;
265 }
266 if ( array_key_exists( $index, $this->mConnections ) && is_object( $this->mConnections[$index] ) &&
267 $this->mConnections[$index]->isOpen() )
268 {
269 return true;
270 } else {
271 return false;
272 }
273 }
274
275 # Really opens a connection
276 /* private */ function reallyOpenConnection( &$server ) {
277 extract( $server );
278 # Get class for this database type
279 $class = 'Database' . ucfirst( $type );
280 if ( !class_exists( $class ) ) {
281 require_once( "$class.php" );
282 }
283
284 # Create object
285 return new $class( $host, $user, $password, $dbname, 1, $flags );
286 }
287
288 function reportConnectionError( &$conn )
289 {
290 $fname = 'LoadBalancer::reportConnectionError';
291 wfProfileIn( $fname );
292 # Prevent infinite recursion
293
294 static $reporting = false;
295 if ( !$reporting ) {
296 $reporting = true;
297 if ( !is_object( $conn ) ) {
298 $conn = new Database;
299 }
300 if ( $this->mFailFunction ) {
301 $conn->failFunction( $this->mFailFunction );
302 } else {
303 $conn->failFunction( 'wfEmergencyAbort' );
304 }
305 $conn->reportConnectionError();
306 $reporting = false;
307 }
308 wfProfileOut( $fname );
309 }
310
311 function getWriterIndex()
312 {
313 return 0;
314 }
315
316 function force( $i )
317 {
318 $this->mForce = $i;
319 }
320
321 function haveIndex( $i )
322 {
323 return array_key_exists( $i, $this->mServers );
324 }
325
326 # Get the number of defined servers (not the number of open connections)
327 function getServerCount() {
328 return count( $this->mServers );
329 }
330
331 # Save master pos to the session and to memcached, if the session exists
332 function saveMasterPos() {
333 global $wgSessionStarted;
334 if ( $wgSessionStarted && count( $this->mServers ) > 1 ) {
335 # If this entire request was served from a slave without opening a connection to the
336 # master (however unlikely that may be), then we can fetch the position from the slave.
337 if ( empty( $this->mConnections[0] ) ) {
338 $conn =& $this->getConnection( DB_SLAVE );
339 list( $file, $pos ) = $conn->getSlavePos();
340 wfDebug( "Saving master pos fetched from slave: $file $pos\n" );
341 } else {
342 $conn =& $this->getConnection( 0 );
343 list( $file, $pos ) = $conn->getMasterPos();
344 wfDebug( "Saving master pos: $file $pos\n" );
345 }
346 if ( $file !== false ) {
347 $_SESSION['master_log_file'] = $file;
348 $_SESSION['master_pos'] = $pos;
349 }
350 }
351 }
352
353 # Loads the master pos from the session, waits for it if necessary
354 function loadMasterPos() {
355 if ( isset( $_SESSION['master_log_file'] ) && isset( $_SESSION['master_pos'] ) ) {
356 $this->waitFor( $_SESSION['master_log_file'], $_SESSION['master_pos'] );
357 }
358 }
359
360 # Close all open connections
361 function closeAll() {
362 foreach( $this->mConnections as $i => $conn ) {
363 if ( $this->isOpen( $i ) ) {
364 $conn->close();
365 }
366 }
367 }
368
369 function commitAll() {
370 foreach( $this->mConnections as $i => $conn ) {
371 if ( $this->isOpen( $i ) ) {
372 $conn->immediateCommit();
373 }
374 }
375 }
376 }