Merge "Enforce stricter slave lag limits for bot API requests"
[lhc/web/wiklou.git] / includes / db / loadbalancer / LoadBalancer.php
1 <?php
2 /**
3 * Database load balancing.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * Database load balancing object
26 *
27 * @todo document
28 * @ingroup Database
29 */
30 class LoadBalancer {
31 /** @var array[] Map of (server index => server config array) */
32 private $mServers;
33 /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */
34 private $mConns;
35 /** @var array Map of (server index => weight) */
36 private $mLoads;
37 /** @var array[] Map of (group => server index => weight) */
38 private $mGroupLoads;
39 /** @var bool Whether to disregard slave lag as a factor in slave selection */
40 private $mAllowLagged;
41 /** @var integer Seconds to spend waiting on slave lag to resolve */
42 private $mWaitTimeout;
43
44 /** @var array LBFactory information */
45 private $mParentInfo;
46 /** @var string The LoadMonitor subclass name */
47 private $mLoadMonitorClass;
48 /** @var LoadMonitor */
49 private $mLoadMonitor;
50
51 /** @var bool|DatabaseBase Database connection that caused a problem */
52 private $mErrorConnection;
53 /** @var integer The generic (not query grouped) slave index (of $mServers) */
54 private $mReadIndex;
55 /** @var bool|DBMasterPos False if not set */
56 private $mWaitForPos;
57 /** @var bool Whether the generic reader fell back to a lagged slave */
58 private $laggedSlaveMode = false;
59 /** @var bool Whether the generic reader fell back to a lagged slave */
60 private $slavesDownMode = false;
61 /** @var string The last DB selection or connection error */
62 private $mLastError = 'Unknown error';
63 /** @var string|bool Reason the LB is read-only or false if not */
64 private $readOnlyReason = false;
65 /** @var integer Total connections opened */
66 private $connsOpened = 0;
67
68 /** @var integer Warn when this many connection are held */
69 const CONN_HELD_WARN_THRESHOLD = 10;
70 /** @var integer Default 'max lag' when unspecified */
71 const MAX_LAG = 10;
72 /** @var integer Max time to wait for a slave to catch up (e.g. ChronologyProtector) */
73 const POS_WAIT_TIMEOUT = 10;
74
75 /**
76 * @param array $params Array with keys:
77 * - servers : Required. Array of server info structures.
78 * - loadMonitor : Name of a class used to fetch server lag and load.
79 * - readOnlyReason : Reason the master DB is read-only if so [optional]
80 * @throws MWException
81 */
82 public function __construct( array $params ) {
83 if ( !isset( $params['servers'] ) ) {
84 throw new MWException( __CLASS__ . ': missing servers parameter' );
85 }
86 $this->mServers = $params['servers'];
87 $this->mWaitTimeout = self::POS_WAIT_TIMEOUT;
88
89 $this->mReadIndex = -1;
90 $this->mWriteIndex = -1;
91 $this->mConns = array(
92 'local' => array(),
93 'foreignUsed' => array(),
94 'foreignFree' => array() );
95 $this->mLoads = array();
96 $this->mWaitForPos = false;
97 $this->mErrorConnection = false;
98 $this->mAllowLagged = false;
99
100 if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
101 $this->readOnlyReason = $params['readOnlyReason'];
102 }
103
104 if ( isset( $params['loadMonitor'] ) ) {
105 $this->mLoadMonitorClass = $params['loadMonitor'];
106 } else {
107 $master = reset( $params['servers'] );
108 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
109 $this->mLoadMonitorClass = 'LoadMonitorMySQL';
110 } else {
111 $this->mLoadMonitorClass = 'LoadMonitorNull';
112 }
113 }
114
115 foreach ( $params['servers'] as $i => $server ) {
116 $this->mLoads[$i] = $server['load'];
117 if ( isset( $server['groupLoads'] ) ) {
118 foreach ( $server['groupLoads'] as $group => $ratio ) {
119 if ( !isset( $this->mGroupLoads[$group] ) ) {
120 $this->mGroupLoads[$group] = array();
121 }
122 $this->mGroupLoads[$group][$i] = $ratio;
123 }
124 }
125 }
126 }
127
128 /**
129 * Get a LoadMonitor instance
130 *
131 * @return LoadMonitor
132 */
133 private function getLoadMonitor() {
134 if ( !isset( $this->mLoadMonitor ) ) {
135 $class = $this->mLoadMonitorClass;
136 $this->mLoadMonitor = new $class( $this );
137 }
138
139 return $this->mLoadMonitor;
140 }
141
142 /**
143 * Get or set arbitrary data used by the parent object, usually an LBFactory
144 * @param mixed $x
145 * @return mixed
146 */
147 public function parentInfo( $x = null ) {
148 return wfSetVar( $this->mParentInfo, $x );
149 }
150
151 /**
152 * Given an array of non-normalised probabilities, this function will select
153 * an element and return the appropriate key
154 *
155 * @deprecated since 1.21, use ArrayUtils::pickRandom()
156 *
157 * @param array $weights
158 * @return bool|int|string
159 */
160 public function pickRandom( array $weights ) {
161 return ArrayUtils::pickRandom( $weights );
162 }
163
164 /**
165 * @param array $loads
166 * @param bool|string $wiki Wiki to get non-lagged for
167 * @param int $maxLag Restrict the maximum allowed lag to this many seconds
168 * @return bool|int|string
169 */
170 private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self::MAX_LAG ) {
171 $lags = $this->getLagTimes( $wiki );
172
173 # Unset excessively lagged servers
174 foreach ( $lags as $i => $lag ) {
175 if ( $i != 0 ) {
176 $maxServerLag = $maxLag;
177 if ( isset( $this->mServers[$i]['max lag'] ) ) {
178 $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] );
179 }
180 if ( $lag === false ) {
181 wfDebugLog( 'replication', "Server #$i is not replicating" );
182 unset( $loads[$i] );
183 } elseif ( $lag > $maxServerLag ) {
184 wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
185 unset( $loads[$i] );
186 }
187 }
188 }
189
190 # Find out if all the slaves with non-zero load are lagged
191 $sum = 0;
192 foreach ( $loads as $load ) {
193 $sum += $load;
194 }
195 if ( $sum == 0 ) {
196 # No appropriate DB servers except maybe the master and some slaves with zero load
197 # Do NOT use the master
198 # Instead, this function will return false, triggering read-only mode,
199 # and a lagged slave will be used instead.
200 return false;
201 }
202
203 if ( count( $loads ) == 0 ) {
204 return false;
205 }
206
207 # wfDebugLog( 'connect', var_export( $loads, true ) );
208
209 # Return a random representative of the remainder
210 return ArrayUtils::pickRandom( $loads );
211 }
212
213 /**
214 * Get the index of the reader connection, which may be a slave
215 * This takes into account load ratios and lag times. It should
216 * always return a consistent index during a given invocation
217 *
218 * Side effect: opens connections to databases
219 * @param string|bool $group Query group, or false for the generic reader
220 * @param string|bool $wiki Wiki ID, or false for the current wiki
221 * @throws MWException
222 * @return bool|int|string
223 */
224 public function getReaderIndex( $group = false, $wiki = false ) {
225 global $wgDBtype;
226
227 # @todo FIXME: For now, only go through all this for mysql databases
228 if ( $wgDBtype != 'mysql' ) {
229 return $this->getWriterIndex();
230 }
231
232 if ( count( $this->mServers ) == 1 ) {
233 # Skip the load balancing if there's only one server
234 return 0;
235 } elseif ( $group === false && $this->mReadIndex >= 0 ) {
236 # Shortcut if generic reader exists already
237 return $this->mReadIndex;
238 }
239
240 # Find the relevant load array
241 if ( $group !== false ) {
242 if ( isset( $this->mGroupLoads[$group] ) ) {
243 $nonErrorLoads = $this->mGroupLoads[$group];
244 } else {
245 # No loads for this group, return false and the caller can use some other group
246 wfDebug( __METHOD__ . ": no loads for group $group\n" );
247
248 return false;
249 }
250 } else {
251 $nonErrorLoads = $this->mLoads;
252 }
253
254 if ( !count( $nonErrorLoads ) ) {
255 throw new MWException( "Empty server array given to LoadBalancer" );
256 }
257
258 # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
259 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
260
261 $laggedSlaveMode = false;
262
263 # No server found yet
264 $i = false;
265 $conn = false;
266 # First try quickly looking through the available servers for a server that
267 # meets our criteria
268 $currentLoads = $nonErrorLoads;
269 while ( count( $currentLoads ) ) {
270 if ( $this->mAllowLagged || $laggedSlaveMode ) {
271 $i = ArrayUtils::pickRandom( $currentLoads );
272 } else {
273 $i = false;
274 if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
275 # ChronologyProtecter causes mWaitForPos to be set via sessions.
276 # This triggers doWait() after connect, so it's especially good to
277 # avoid lagged servers so as to avoid just blocking in that method.
278 $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
279 # Aim for <= 1 second of waiting (being too picky can backfire)
280 $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 );
281 }
282 if ( $i === false ) {
283 # Any server with less lag than it's 'max lag' param is preferable
284 $i = $this->getRandomNonLagged( $currentLoads, $wiki );
285 }
286 if ( $i === false && count( $currentLoads ) != 0 ) {
287 # All slaves lagged. Switch to read-only mode
288 wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
289 $i = ArrayUtils::pickRandom( $currentLoads );
290 $laggedSlaveMode = true;
291 }
292 }
293
294 if ( $i === false ) {
295 # pickRandom() returned false
296 # This is permanent and means the configuration or the load monitor
297 # wants us to return false.
298 wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false" );
299
300 return false;
301 }
302
303 $serverName = $this->getServerName( $i );
304 wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
305
306 $conn = $this->openConnection( $i, $wiki );
307 if ( !$conn ) {
308 wfDebugLog( 'connect', __METHOD__ . ": Failed connecting to $i/$wiki" );
309 unset( $nonErrorLoads[$i] );
310 unset( $currentLoads[$i] );
311 $i = false;
312 continue;
313 }
314
315 // Decrement reference counter, we are finished with this connection.
316 // It will be incremented for the caller later.
317 if ( $wiki !== false ) {
318 $this->reuseConnection( $conn );
319 }
320
321 # Return this server
322 break;
323 }
324
325 # If all servers were down, quit now
326 if ( !count( $nonErrorLoads ) ) {
327 wfDebugLog( 'connect', "All servers down" );
328 }
329
330 if ( $i !== false ) {
331 # Slave connection successful
332 # Wait for the session master pos for a short time
333 if ( $this->mWaitForPos && $i > 0 ) {
334 if ( !$this->doWait( $i ) ) {
335 $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
336 }
337 }
338 if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
339 $this->mReadIndex = $i;
340 # Record if the generic reader index is in "lagged slave" mode
341 if ( $laggedSlaveMode ) {
342 $this->laggedSlaveMode = true;
343 }
344 }
345 $serverName = $this->getServerName( $i );
346 wfDebug( __METHOD__ . ": using server $serverName for group '$group'\n" );
347 }
348
349 return $i;
350 }
351
352 /**
353 * Set the master wait position
354 * If a DB_SLAVE connection has been opened already, waits
355 * Otherwise sets a variable telling it to wait if such a connection is opened
356 * @param DBMasterPos $pos
357 */
358 public function waitFor( $pos ) {
359 $this->mWaitForPos = $pos;
360 $i = $this->mReadIndex;
361
362 if ( $i > 0 ) {
363 if ( !$this->doWait( $i ) ) {
364 $this->mServers[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
365 $this->laggedSlaveMode = true;
366 }
367 }
368 }
369
370 /**
371 * Set the master wait position and wait for a "generic" slave to catch up to it
372 *
373 * This can be used a faster proxy for waitForAll()
374 *
375 * @param DBMasterPos $pos
376 * @param int $timeout Max seconds to wait; default is mWaitTimeout
377 * @return bool Success (able to connect and no timeouts reached)
378 * @since 1.26
379 */
380 public function waitForOne( $pos, $timeout = null ) {
381 $this->mWaitForPos = $pos;
382
383 $i = $this->mReadIndex;
384 if ( $i <= 0 ) {
385 // Pick a generic slave if there isn't one yet
386 $readLoads = $this->mLoads;
387 unset( $readLoads[$this->getWriterIndex()] ); // slaves only
388 $readLoads = array_filter( $readLoads ); // with non-zero load
389 $i = ArrayUtils::pickRandom( $readLoads );
390 }
391
392 if ( $i > 0 ) {
393 $ok = $this->doWait( $i, true, $timeout );
394 } else {
395 $ok = true; // no applicable loads
396 }
397
398 return $ok;
399 }
400
401 /**
402 * Set the master wait position and wait for ALL slaves to catch up to it
403 * @param DBMasterPos $pos
404 * @param int $timeout Max seconds to wait; default is mWaitTimeout
405 * @return bool Success (able to connect and no timeouts reached)
406 */
407 public function waitForAll( $pos, $timeout = null ) {
408 $this->mWaitForPos = $pos;
409 $serverCount = count( $this->mServers );
410
411 $ok = true;
412 for ( $i = 1; $i < $serverCount; $i++ ) {
413 if ( $this->mLoads[$i] > 0 ) {
414 $ok = $this->doWait( $i, true, $timeout ) && $ok;
415 }
416 }
417
418 return $ok;
419 }
420
421 /**
422 * Get any open connection to a given server index, local or foreign
423 * Returns false if there is no connection open
424 *
425 * @param int $i
426 * @return DatabaseBase|bool False on failure
427 */
428 public function getAnyOpenConnection( $i ) {
429 foreach ( $this->mConns as $conns ) {
430 if ( !empty( $conns[$i] ) ) {
431 return reset( $conns[$i] );
432 }
433 }
434
435 return false;
436 }
437
438 /**
439 * Wait for a given slave to catch up to the master pos stored in $this
440 * @param int $index Server index
441 * @param bool $open Check the server even if a new connection has to be made
442 * @param int $timeout Max seconds to wait; default is mWaitTimeout
443 * @return bool
444 */
445 protected function doWait( $index, $open = false, $timeout = null ) {
446 $close = false; // close the connection afterwards
447
448 # Find a connection to wait on, creating one if needed and allowed
449 $conn = $this->getAnyOpenConnection( $index );
450 if ( !$conn ) {
451 if ( !$open ) {
452 wfDebug( __METHOD__ . ": no connection open\n" );
453
454 return false;
455 } else {
456 $conn = $this->openConnection( $index, '' );
457 if ( !$conn ) {
458 wfDebug( __METHOD__ . ": failed to open connection\n" );
459
460 return false;
461 }
462 // Avoid connection spam in waitForAll() when connections
463 // are made just for the sake of doing this lag check.
464 $close = true;
465 }
466 }
467
468 wfDebug( __METHOD__ . ": Waiting for slave #$index to catch up...\n" );
469 $timeout = $timeout ?: $this->mWaitTimeout;
470 $result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
471
472 if ( $result == -1 || is_null( $result ) ) {
473 # Timed out waiting for slave, use master instead
474 $server = $server = $this->getServerName( $index );
475 $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
476 wfDebug( "$msg\n" );
477 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
478 $ok = false;
479 } else {
480 wfDebug( __METHOD__ . ": Done\n" );
481 $ok = true;
482 }
483
484 if ( $close ) {
485 $this->closeConnection( $conn );
486 }
487
488 return $ok;
489 }
490
491 /**
492 * Get a connection by index
493 * This is the main entry point for this class.
494 *
495 * @param int $i Server index
496 * @param array|string|bool $groups Query group(s), or false for the generic reader
497 * @param string|bool $wiki Wiki ID, or false for the current wiki
498 *
499 * @throws MWException
500 * @return DatabaseBase
501 */
502 public function getConnection( $i, $groups = array(), $wiki = false ) {
503 if ( $i === null || $i === false ) {
504 throw new MWException( 'Attempt to call ' . __METHOD__ .
505 ' with invalid server index' );
506 }
507
508 if ( $wiki === wfWikiID() ) {
509 $wiki = false;
510 }
511
512 $groups = ( $groups === false || $groups === array() )
513 ? array( false ) // check one "group": the generic pool
514 : (array)$groups;
515
516 $masterOnly = ( $i == DB_MASTER || $i == $this->getWriterIndex() );
517 $oldConnsOpened = $this->connsOpened; // connections open now
518
519 if ( $i == DB_MASTER ) {
520 $i = $this->getWriterIndex();
521 } else {
522 # Try to find an available server in any the query groups (in order)
523 foreach ( $groups as $group ) {
524 $groupIndex = $this->getReaderIndex( $group, $wiki );
525 if ( $groupIndex !== false ) {
526 $i = $groupIndex;
527 break;
528 }
529 }
530 }
531
532 # Operation-based index
533 if ( $i == DB_SLAVE ) {
534 $this->mLastError = 'Unknown error'; // reset error string
535 # Try the general server pool if $groups are unavailable.
536 $i = in_array( false, $groups, true )
537 ? false // don't bother with this if that is what was tried above
538 : $this->getReaderIndex( false, $wiki );
539 # Couldn't find a working server in getReaderIndex()?
540 if ( $i === false ) {
541 $this->mLastError = 'No working slave server: ' . $this->mLastError;
542
543 return $this->reportConnectionError();
544 }
545 }
546
547 # Now we have an explicit index into the servers array
548 $conn = $this->openConnection( $i, $wiki );
549 if ( !$conn ) {
550 return $this->reportConnectionError();
551 }
552
553 # Profile any new connections that happen
554 if ( $this->connsOpened > $oldConnsOpened ) {
555 $host = $conn->getServer();
556 $dbname = $conn->getDBname();
557 $trxProf = Profiler::instance()->getTransactionProfiler();
558 $trxProf->recordConnection( $host, $dbname, $masterOnly );
559 }
560
561 if ( $masterOnly ) {
562 # Make master-requested DB handles inherit any read-only mode setting
563 $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $wiki ) );
564 }
565
566 return $conn;
567 }
568
569 /**
570 * Mark a foreign connection as being available for reuse under a different
571 * DB name or prefix. This mechanism is reference-counted, and must be called
572 * the same number of times as getConnection() to work.
573 *
574 * @param DatabaseBase $conn
575 * @throws MWException
576 */
577 public function reuseConnection( $conn ) {
578 $serverIndex = $conn->getLBInfo( 'serverIndex' );
579 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
580 if ( $serverIndex === null || $refCount === null ) {
581 wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" );
582
583 /**
584 * This can happen in code like:
585 * foreach ( $dbs as $db ) {
586 * $conn = $lb->getConnection( DB_SLAVE, array(), $db );
587 * ...
588 * $lb->reuseConnection( $conn );
589 * }
590 * When a connection to the local DB is opened in this way, reuseConnection()
591 * should be ignored
592 */
593
594 return;
595 }
596
597 $dbName = $conn->getDBname();
598 $prefix = $conn->tablePrefix();
599 if ( strval( $prefix ) !== '' ) {
600 $wiki = "$dbName-$prefix";
601 } else {
602 $wiki = $dbName;
603 }
604 if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
605 throw new MWException( __METHOD__ . ": connection not found, has " .
606 "the connection been freed already?" );
607 }
608 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
609 if ( $refCount <= 0 ) {
610 $this->mConns['foreignFree'][$serverIndex][$wiki] = $conn;
611 unset( $this->mConns['foreignUsed'][$serverIndex][$wiki] );
612 wfDebug( __METHOD__ . ": freed connection $serverIndex/$wiki\n" );
613 } else {
614 wfDebug( __METHOD__ . ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
615 }
616 }
617
618 /**
619 * Get a database connection handle reference
620 *
621 * The handle's methods wrap simply wrap those of a DatabaseBase handle
622 *
623 * @see LoadBalancer::getConnection() for parameter information
624 *
625 * @param int $db
626 * @param array|string|bool $groups Query group(s), or false for the generic reader
627 * @param string|bool $wiki Wiki ID, or false for the current wiki
628 * @return DBConnRef
629 */
630 public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
631 return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
632 }
633
634 /**
635 * Get a database connection handle reference without connecting yet
636 *
637 * The handle's methods wrap simply wrap those of a DatabaseBase handle
638 *
639 * @see LoadBalancer::getConnection() for parameter information
640 *
641 * @param int $db
642 * @param array|string|bool $groups Query group(s), or false for the generic reader
643 * @param string|bool $wiki Wiki ID, or false for the current wiki
644 * @return DBConnRef
645 */
646 public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
647 return new DBConnRef( $this, array( $db, $groups, $wiki ) );
648 }
649
650 /**
651 * Open a connection to the server given by the specified index
652 * Index must be an actual index into the array.
653 * If the server is already open, returns it.
654 *
655 * On error, returns false, and the connection which caused the
656 * error will be available via $this->mErrorConnection.
657 *
658 * @param int $i Server index
659 * @param string|bool $wiki Wiki ID, or false for the current wiki
660 * @return DatabaseBase
661 *
662 * @access private
663 */
664 public function openConnection( $i, $wiki = false ) {
665 if ( $wiki !== false ) {
666 $conn = $this->openForeignConnection( $i, $wiki );
667 } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
668 $conn = $this->mConns['local'][$i][0];
669 } else {
670 $server = $this->mServers[$i];
671 $server['serverIndex'] = $i;
672 $conn = $this->reallyOpenConnection( $server, false );
673 $serverName = $this->getServerName( $i );
674 if ( $conn->isOpen() ) {
675 wfDebug( "Connected to database $i at $serverName\n" );
676 $this->mConns['local'][$i][0] = $conn;
677 } else {
678 wfDebug( "Failed to connect to database $i at $serverName\n" );
679 $this->mErrorConnection = $conn;
680 $conn = false;
681 }
682 }
683
684 if ( $conn && !$conn->isOpen() ) {
685 // Connection was made but later unrecoverably lost for some reason.
686 // Do not return a handle that will just throw exceptions on use,
687 // but let the calling code (e.g. getReaderIndex) try another server.
688 // See DatabaseMyslBase::ping() for how this can happen.
689 $this->mErrorConnection = $conn;
690 $conn = false;
691 }
692
693 return $conn;
694 }
695
696 /**
697 * Open a connection to a foreign DB, or return one if it is already open.
698 *
699 * Increments a reference count on the returned connection which locks the
700 * connection to the requested wiki. This reference count can be
701 * decremented by calling reuseConnection().
702 *
703 * If a connection is open to the appropriate server already, but with the wrong
704 * database, it will be switched to the right database and returned, as long as
705 * it has been freed first with reuseConnection().
706 *
707 * On error, returns false, and the connection which caused the
708 * error will be available via $this->mErrorConnection.
709 *
710 * @param int $i Server index
711 * @param string $wiki Wiki ID to open
712 * @return DatabaseBase
713 */
714 private function openForeignConnection( $i, $wiki ) {
715 list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
716 if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
717 // Reuse an already-used connection
718 $conn = $this->mConns['foreignUsed'][$i][$wiki];
719 wfDebug( __METHOD__ . ": reusing connection $i/$wiki\n" );
720 } elseif ( isset( $this->mConns['foreignFree'][$i][$wiki] ) ) {
721 // Reuse a free connection for the same wiki
722 $conn = $this->mConns['foreignFree'][$i][$wiki];
723 unset( $this->mConns['foreignFree'][$i][$wiki] );
724 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
725 wfDebug( __METHOD__ . ": reusing free connection $i/$wiki\n" );
726 } elseif ( !empty( $this->mConns['foreignFree'][$i] ) ) {
727 // Reuse a connection from another wiki
728 $conn = reset( $this->mConns['foreignFree'][$i] );
729 $oldWiki = key( $this->mConns['foreignFree'][$i] );
730
731 // The empty string as a DB name means "don't care".
732 // DatabaseMysqlBase::open() already handle this on connection.
733 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
734 $this->mLastError = "Error selecting database $dbName on server " .
735 $conn->getServer() . " from client host " . wfHostname() . "\n";
736 $this->mErrorConnection = $conn;
737 $conn = false;
738 } else {
739 $conn->tablePrefix( $prefix );
740 unset( $this->mConns['foreignFree'][$i][$oldWiki] );
741 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
742 wfDebug( __METHOD__ . ": reusing free connection from $oldWiki for $wiki\n" );
743 }
744 } else {
745 // Open a new connection
746 $server = $this->mServers[$i];
747 $server['serverIndex'] = $i;
748 $server['foreignPoolRefCount'] = 0;
749 $server['foreign'] = true;
750 $conn = $this->reallyOpenConnection( $server, $dbName );
751 if ( !$conn->isOpen() ) {
752 wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
753 $this->mErrorConnection = $conn;
754 $conn = false;
755 } else {
756 $conn->tablePrefix( $prefix );
757 $this->mConns['foreignUsed'][$i][$wiki] = $conn;
758 wfDebug( __METHOD__ . ": opened new connection for $i/$wiki\n" );
759 }
760 }
761
762 // Increment reference count
763 if ( $conn ) {
764 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
765 $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
766 }
767
768 return $conn;
769 }
770
771 /**
772 * Test if the specified index represents an open connection
773 *
774 * @param int $index Server index
775 * @access private
776 * @return bool
777 */
778 private function isOpen( $index ) {
779 if ( !is_integer( $index ) ) {
780 return false;
781 }
782
783 return (bool)$this->getAnyOpenConnection( $index );
784 }
785
786 /**
787 * Really opens a connection. Uncached.
788 * Returns a Database object whether or not the connection was successful.
789 * @access private
790 *
791 * @param array $server
792 * @param bool $dbNameOverride
793 * @throws MWException
794 * @return DatabaseBase
795 */
796 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
797 if ( !is_array( $server ) ) {
798 throw new MWException( 'You must update your load-balancing configuration. ' .
799 'See DefaultSettings.php entry for $wgDBservers.' );
800 }
801
802 if ( $dbNameOverride !== false ) {
803 $server['dbname'] = $dbNameOverride;
804 }
805
806 // Log when many connection are made on requests
807 if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
808 $masterAddr = $this->getServerName( 0 );
809 wfDebugLog( 'DBPerformance', __METHOD__ . ": " .
810 "{$this->connsOpened}+ connections made (master=$masterAddr)\n" .
811 wfBacktrace( true ) );
812 }
813
814 # Create object
815 try {
816 $db = DatabaseBase::factory( $server['type'], $server );
817 } catch ( DBConnectionError $e ) {
818 // FIXME: This is probably the ugliest thing I have ever done to
819 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
820 $db = $e->db;
821 }
822
823 $db->setLBInfo( $server );
824
825 return $db;
826 }
827
828 /**
829 * @throws DBConnectionError
830 * @return bool
831 */
832 private function reportConnectionError() {
833 $conn = $this->mErrorConnection; // The connection which caused the error
834 $context = array(
835 'method' => __METHOD__,
836 'last_error' => $this->mLastError,
837 );
838
839 if ( !is_object( $conn ) ) {
840 // No last connection, probably due to all servers being too busy
841 wfLogDBError(
842 "LB failure with no last connection. Connection error: {last_error}",
843 $context
844 );
845
846 // If all servers were busy, mLastError will contain something sensible
847 throw new DBConnectionError( null, $this->mLastError );
848 } else {
849 $context['db_server'] = $conn->getProperty( 'mServer' );
850 wfLogDBError(
851 "Connection error: {last_error} ({db_server})",
852 $context
853 );
854
855 // throws DBConnectionError
856 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
857 }
858
859 return false; /* not reached */
860 }
861
862 /**
863 * @return int
864 * @since 1.26
865 */
866 public function getWriterIndex() {
867 return 0;
868 }
869
870 /**
871 * Returns true if the specified index is a valid server index
872 *
873 * @param string $i
874 * @return bool
875 */
876 public function haveIndex( $i ) {
877 return array_key_exists( $i, $this->mServers );
878 }
879
880 /**
881 * Returns true if the specified index is valid and has non-zero load
882 *
883 * @param string $i
884 * @return bool
885 */
886 public function isNonZeroLoad( $i ) {
887 return array_key_exists( $i, $this->mServers ) && $this->mLoads[$i] != 0;
888 }
889
890 /**
891 * Get the number of defined servers (not the number of open connections)
892 *
893 * @return int
894 */
895 public function getServerCount() {
896 return count( $this->mServers );
897 }
898
899 /**
900 * Get the host name or IP address of the server with the specified index
901 * Prefer a readable name if available.
902 * @param string $i
903 * @return string
904 */
905 public function getServerName( $i ) {
906 if ( isset( $this->mServers[$i]['hostName'] ) ) {
907 $name = $this->mServers[$i]['hostName'];
908 } elseif ( isset( $this->mServers[$i]['host'] ) ) {
909 $name = $this->mServers[$i]['host'];
910 } else {
911 $name = '';
912 }
913
914 return ( $name != '' ) ? $name : 'localhost';
915 }
916
917 /**
918 * Return the server info structure for a given index, or false if the index is invalid.
919 * @param int $i
920 * @return array|bool
921 */
922 public function getServerInfo( $i ) {
923 if ( isset( $this->mServers[$i] ) ) {
924 return $this->mServers[$i];
925 } else {
926 return false;
927 }
928 }
929
930 /**
931 * Sets the server info structure for the given index. Entry at index $i
932 * is created if it doesn't exist
933 * @param int $i
934 * @param array $serverInfo
935 */
936 public function setServerInfo( $i, array $serverInfo ) {
937 $this->mServers[$i] = $serverInfo;
938 }
939
940 /**
941 * Get the current master position for chronology control purposes
942 * @return mixed
943 */
944 public function getMasterPos() {
945 # If this entire request was served from a slave without opening a connection to the
946 # master (however unlikely that may be), then we can fetch the position from the slave.
947 $masterConn = $this->getAnyOpenConnection( 0 );
948 if ( !$masterConn ) {
949 $serverCount = count( $this->mServers );
950 for ( $i = 1; $i < $serverCount; $i++ ) {
951 $conn = $this->getAnyOpenConnection( $i );
952 if ( $conn ) {
953 wfDebug( "Master pos fetched from slave\n" );
954
955 return $conn->getSlavePos();
956 }
957 }
958 } else {
959 wfDebug( "Master pos fetched from master\n" );
960
961 return $masterConn->getMasterPos();
962 }
963
964 return false;
965 }
966
967 /**
968 * Close all open connections
969 */
970 public function closeAll() {
971 foreach ( $this->mConns as $conns2 ) {
972 foreach ( $conns2 as $conns3 ) {
973 /** @var DatabaseBase $conn */
974 foreach ( $conns3 as $conn ) {
975 $conn->close();
976 }
977 }
978 }
979 $this->mConns = array(
980 'local' => array(),
981 'foreignFree' => array(),
982 'foreignUsed' => array(),
983 );
984 $this->connsOpened = 0;
985 }
986
987 /**
988 * Close a connection
989 * Using this function makes sure the LoadBalancer knows the connection is closed.
990 * If you use $conn->close() directly, the load balancer won't update its state.
991 * @param DatabaseBase $conn
992 */
993 public function closeConnection( $conn ) {
994 $done = false;
995 foreach ( $this->mConns as $i1 => $conns2 ) {
996 foreach ( $conns2 as $i2 => $conns3 ) {
997 foreach ( $conns3 as $i3 => $candidateConn ) {
998 if ( $conn === $candidateConn ) {
999 $conn->close();
1000 unset( $this->mConns[$i1][$i2][$i3] );
1001 --$this->connsOpened;
1002 $done = true;
1003 break;
1004 }
1005 }
1006 }
1007 }
1008 if ( !$done ) {
1009 $conn->close();
1010 }
1011 }
1012
1013 /**
1014 * Commit transactions on all open connections
1015 */
1016 public function commitAll() {
1017 foreach ( $this->mConns as $conns2 ) {
1018 foreach ( $conns2 as $conns3 ) {
1019 /** @var DatabaseBase[] $conns3 */
1020 foreach ( $conns3 as $conn ) {
1021 if ( $conn->trxLevel() ) {
1022 $conn->commit( __METHOD__, 'flush' );
1023 }
1024 }
1025 }
1026 }
1027 }
1028
1029 /**
1030 * Issue COMMIT only on master, only if queries were done on connection
1031 */
1032 public function commitMasterChanges() {
1033 $masterIndex = $this->getWriterIndex();
1034 foreach ( $this->mConns as $conns2 ) {
1035 if ( empty( $conns2[$masterIndex] ) ) {
1036 continue;
1037 }
1038 /** @var DatabaseBase $conn */
1039 foreach ( $conns2[$masterIndex] as $conn ) {
1040 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1041 $conn->commit( __METHOD__, 'flush' );
1042 }
1043 }
1044 }
1045 }
1046
1047 /**
1048 * Issue ROLLBACK only on master, only if queries were done on connection
1049 * @since 1.23
1050 */
1051 public function rollbackMasterChanges() {
1052 $failedServers = array();
1053
1054 $masterIndex = $this->getWriterIndex();
1055 foreach ( $this->mConns as $conns2 ) {
1056 if ( empty( $conns2[$masterIndex] ) ) {
1057 continue;
1058 }
1059 /** @var DatabaseBase $conn */
1060 foreach ( $conns2[$masterIndex] as $conn ) {
1061 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1062 try {
1063 $conn->rollback( __METHOD__, 'flush' );
1064 } catch ( DBError $e ) {
1065 MWExceptionHandler::logException( $e );
1066 $failedServers[] = $conn->getServer();
1067 }
1068 }
1069 }
1070 }
1071
1072 if ( $failedServers ) {
1073 throw new DBExpectedError( null, "Rollback failed on server(s) " .
1074 implode( ', ', array_unique( $failedServers ) ) );
1075 }
1076 }
1077
1078 /**
1079 * @return bool Whether a master connection is already open
1080 * @since 1.24
1081 */
1082 public function hasMasterConnection() {
1083 return $this->isOpen( $this->getWriterIndex() );
1084 }
1085
1086 /**
1087 * Determine if there are pending changes in a transaction by this thread
1088 * @since 1.23
1089 * @return bool
1090 */
1091 public function hasMasterChanges() {
1092 $masterIndex = $this->getWriterIndex();
1093 foreach ( $this->mConns as $conns2 ) {
1094 if ( empty( $conns2[$masterIndex] ) ) {
1095 continue;
1096 }
1097 /** @var DatabaseBase $conn */
1098 foreach ( $conns2[$masterIndex] as $conn ) {
1099 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1100 return true;
1101 }
1102 }
1103 }
1104 return false;
1105 }
1106
1107 /**
1108 * Get the timestamp of the latest write query done by this thread
1109 * @since 1.25
1110 * @return float|bool UNIX timestamp or false
1111 */
1112 public function lastMasterChangeTimestamp() {
1113 $lastTime = false;
1114 $masterIndex = $this->getWriterIndex();
1115 foreach ( $this->mConns as $conns2 ) {
1116 if ( empty( $conns2[$masterIndex] ) ) {
1117 continue;
1118 }
1119 /** @var DatabaseBase $conn */
1120 foreach ( $conns2[$masterIndex] as $conn ) {
1121 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1122 }
1123 }
1124 return $lastTime;
1125 }
1126
1127 /**
1128 * Check if this load balancer object had any recent or still
1129 * pending writes issued against it by this PHP thread
1130 *
1131 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1132 * @return bool
1133 * @since 1.25
1134 */
1135 public function hasOrMadeRecentMasterChanges( $age = null ) {
1136 $age = ( $age === null ) ? $this->mWaitTimeout : $age;
1137
1138 return ( $this->hasMasterChanges()
1139 || $this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1140 }
1141
1142 /**
1143 * @param mixed $value
1144 * @return mixed
1145 */
1146 public function waitTimeout( $value = null ) {
1147 return wfSetVar( $this->mWaitTimeout, $value );
1148 }
1149
1150 /**
1151 * @note This method will trigger a DB connection if not yet done
1152 *
1153 * @param string|bool $wiki Wiki ID, or false for the current wiki
1154 * @return bool Whether the generic connection for reads is highly "lagged"
1155 */
1156 public function getLaggedSlaveMode( $wiki = false ) {
1157 // No-op if there is only one DB (also avoids recursion)
1158 if ( !$this->laggedSlaveMode && $this->getServerCount() > 1 ) {
1159 try {
1160 // See if laggedSlaveMode gets set
1161 $this->getConnection( DB_SLAVE, false, $wiki );
1162 } catch ( DBConnectionError $e ) {
1163 // Avoid expensive re-connect attempts and failures
1164 $this->slavesDownMode = true;
1165 $this->laggedSlaveMode = true;
1166 }
1167 }
1168
1169 return $this->laggedSlaveMode;
1170 }
1171
1172 /**
1173 * @note This method will never cause a new DB connection
1174 * @return bool Whether any generic connection used for reads was highly "lagged"
1175 * @since 1.27
1176 */
1177 public function laggedSlaveUsed() {
1178 return $this->laggedSlaveMode;
1179 }
1180
1181 /**
1182 * @note This method may trigger a DB connection if not yet done
1183 * @param string|bool $wiki Wiki ID, or false for the current wiki
1184 * @return string|bool Reason the master is read-only or false if it is not
1185 * @since 1.27
1186 */
1187 public function getReadOnlyReason( $wiki = false ) {
1188 if ( $this->readOnlyReason !== false ) {
1189 return $this->readOnlyReason;
1190 } elseif ( $this->getLaggedSlaveMode( $wiki ) ) {
1191 if ( $this->slavesDownMode ) {
1192 return 'The database has been automatically locked ' .
1193 'until the slave database servers become available';
1194 } else {
1195 return 'The database has been automatically locked ' .
1196 'while the slave database servers catch up to the master.';
1197 }
1198 }
1199
1200 return false;
1201 }
1202
1203 /**
1204 * Disables/enables lag checks
1205 * @param null|bool $mode
1206 * @return bool
1207 */
1208 public function allowLagged( $mode = null ) {
1209 if ( $mode === null ) {
1210 return $this->mAllowLagged;
1211 }
1212 $this->mAllowLagged = $mode;
1213
1214 return $this->mAllowLagged;
1215 }
1216
1217 /**
1218 * @return bool
1219 */
1220 public function pingAll() {
1221 $success = true;
1222 foreach ( $this->mConns as $conns2 ) {
1223 foreach ( $conns2 as $conns3 ) {
1224 /** @var DatabaseBase[] $conns3 */
1225 foreach ( $conns3 as $conn ) {
1226 if ( !$conn->ping() ) {
1227 $success = false;
1228 }
1229 }
1230 }
1231 }
1232
1233 return $success;
1234 }
1235
1236 /**
1237 * Call a function with each open connection object
1238 * @param callable $callback
1239 * @param array $params
1240 */
1241 public function forEachOpenConnection( $callback, array $params = array() ) {
1242 foreach ( $this->mConns as $conns2 ) {
1243 foreach ( $conns2 as $conns3 ) {
1244 foreach ( $conns3 as $conn ) {
1245 $mergedParams = array_merge( array( $conn ), $params );
1246 call_user_func_array( $callback, $mergedParams );
1247 }
1248 }
1249 }
1250 }
1251
1252 /**
1253 * Get the hostname and lag time of the most-lagged slave
1254 *
1255 * This is useful for maintenance scripts that need to throttle their updates.
1256 * May attempt to open connections to slaves on the default DB. If there is
1257 * no lag, the maximum lag will be reported as -1.
1258 *
1259 * @param bool|string $wiki Wiki ID, or false for the default database
1260 * @return array ( host, max lag, index of max lagged host )
1261 */
1262 public function getMaxLag( $wiki = false ) {
1263 $maxLag = -1;
1264 $host = '';
1265 $maxIndex = 0;
1266
1267 if ( $this->getServerCount() <= 1 ) {
1268 return array( $host, $maxLag, $maxIndex ); // no replication = no lag
1269 }
1270
1271 $lagTimes = $this->getLagTimes( $wiki );
1272 foreach ( $lagTimes as $i => $lag ) {
1273 if ( $lag > $maxLag ) {
1274 $maxLag = $lag;
1275 $host = $this->mServers[$i]['host'];
1276 $maxIndex = $i;
1277 }
1278 }
1279
1280 return array( $host, $maxLag, $maxIndex );
1281 }
1282
1283 /**
1284 * Get an estimate of replication lag (in seconds) for each server
1285 *
1286 * Results are cached for a short time in memcached/process cache
1287 *
1288 * Values may be "false" if replication is too broken to estimate
1289 *
1290 * @param string|bool $wiki
1291 * @return int[] Map of (server index => float|int|bool)
1292 */
1293 public function getLagTimes( $wiki = false ) {
1294 if ( $this->getServerCount() <= 1 ) {
1295 return array( 0 => 0 ); // no replication = no lag
1296 }
1297
1298 # Send the request to the load monitor
1299 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
1300 }
1301
1302 /**
1303 * Get the lag in seconds for a given connection, or zero if this load
1304 * balancer does not have replication enabled.
1305 *
1306 * This should be used in preference to Database::getLag() in cases where
1307 * replication may not be in use, since there is no way to determine if
1308 * replication is in use at the connection level without running
1309 * potentially restricted queries such as SHOW SLAVE STATUS. Using this
1310 * function instead of Database::getLag() avoids a fatal error in this
1311 * case on many installations.
1312 *
1313 * @param DatabaseBase $conn
1314 * @return int
1315 */
1316 public function safeGetLag( $conn ) {
1317 if ( $this->getServerCount() == 1 ) {
1318 return 0;
1319 } else {
1320 return $conn->getLag();
1321 }
1322 }
1323
1324 /**
1325 * Clear the cache for slag lag delay times
1326 *
1327 * This is only used for testing
1328 */
1329 public function clearLagTimeCache() {
1330 $this->getLoadMonitor()->clearCaches();
1331 }
1332 }