3 * Database load balancing.
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.
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.
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
25 * Database load balancing object
31 /** @var array[] Map of (server index => server config array) */
33 /** @var array[] Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) */
35 /** @var array Map of (server index => weight) */
37 /** @var array[] Map of (group => server index => weight) */
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;
44 /** @var array LBFactory information */
46 /** @var string The LoadMonitor subclass name */
47 private $mLoadMonitorClass;
48 /** @var LoadMonitor */
49 private $mLoadMonitor;
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) */
55 /** @var bool|DBMasterPos False if not set */
57 /** @var bool Whether the generic reader fell back to a lagged slave */
58 private $mLaggedSlaveMode;
59 /** @var string The last DB selection or connection error */
60 private $mLastError = 'Unknown error';
61 /** @var integer Total connections opened */
62 private $connsOpened = 0;
64 /** @var integer Warn when this many connection are held */
65 const CONN_HELD_WARN_THRESHOLD
= 10;
66 /** @var integer Default 'max lag' when unspecified */
70 * @param array $params Array with keys:
71 * servers Required. Array of server info structures.
72 * loadMonitor Name of a class used to fetch server lag and load.
75 public function __construct( array $params ) {
76 if ( !isset( $params['servers'] ) ) {
77 throw new MWException( __CLASS__
. ': missing servers parameter' );
79 $this->mServers
= $params['servers'];
80 $this->mWaitTimeout
= 10;
82 $this->mReadIndex
= -1;
83 $this->mWriteIndex
= -1;
84 $this->mConns
= array(
86 'foreignUsed' => array(),
87 'foreignFree' => array() );
88 $this->mLoads
= array();
89 $this->mWaitForPos
= false;
90 $this->mLaggedSlaveMode
= false;
91 $this->mErrorConnection
= false;
92 $this->mAllowLagged
= false;
94 if ( isset( $params['loadMonitor'] ) ) {
95 $this->mLoadMonitorClass
= $params['loadMonitor'];
97 $master = reset( $params['servers'] );
98 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
99 $this->mLoadMonitorClass
= 'LoadMonitorMySQL';
101 $this->mLoadMonitorClass
= 'LoadMonitorNull';
105 foreach ( $params['servers'] as $i => $server ) {
106 $this->mLoads
[$i] = $server['load'];
107 if ( isset( $server['groupLoads'] ) ) {
108 foreach ( $server['groupLoads'] as $group => $ratio ) {
109 if ( !isset( $this->mGroupLoads
[$group] ) ) {
110 $this->mGroupLoads
[$group] = array();
112 $this->mGroupLoads
[$group][$i] = $ratio;
119 * Get a LoadMonitor instance
121 * @return LoadMonitor
123 private function getLoadMonitor() {
124 if ( !isset( $this->mLoadMonitor
) ) {
125 $class = $this->mLoadMonitorClass
;
126 $this->mLoadMonitor
= new $class( $this );
129 return $this->mLoadMonitor
;
133 * Get or set arbitrary data used by the parent object, usually an LBFactory
137 public function parentInfo( $x = null ) {
138 return wfSetVar( $this->mParentInfo
, $x );
142 * Given an array of non-normalised probabilities, this function will select
143 * an element and return the appropriate key
145 * @deprecated since 1.21, use ArrayUtils::pickRandom()
147 * @param array $weights
148 * @return bool|int|string
150 public function pickRandom( array $weights ) {
151 return ArrayUtils
::pickRandom( $weights );
155 * @param array $loads
156 * @param bool|string $wiki Wiki to get non-lagged for
157 * @param float $maxLag Restrict the maximum allowed lag to this many seconds
158 * @return bool|int|string
160 private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self
::MAX_LAG
) {
161 $lags = $this->getLagTimes( $wiki );
163 # Unset excessively lagged servers
164 foreach ( $lags as $i => $lag ) {
166 $maxServerLag = $maxLag;
167 if ( isset( $this->mServers
[$i]['max lag'] ) ) {
168 $maxServerLag = min( $maxServerLag, $this->mServers
[$i]['max lag'] );
170 if ( $lag === false ) {
171 wfDebugLog( 'replication', "Server #$i is not replicating" );
173 } elseif ( $lag > $maxServerLag ) {
174 wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
180 # Find out if all the slaves with non-zero load are lagged
182 foreach ( $loads as $load ) {
186 # No appropriate DB servers except maybe the master and some slaves with zero load
187 # Do NOT use the master
188 # Instead, this function will return false, triggering read-only mode,
189 # and a lagged slave will be used instead.
193 if ( count( $loads ) == 0 ) {
197 # wfDebugLog( 'connect', var_export( $loads, true ) );
199 # Return a random representative of the remainder
200 return ArrayUtils
::pickRandom( $loads );
204 * Get the index of the reader connection, which may be a slave
205 * This takes into account load ratios and lag times. It should
206 * always return a consistent index during a given invocation
208 * Side effect: opens connections to databases
209 * @param string|bool $group Query group, or false for the generic reader
210 * @param string|bool $wiki Wiki ID, or false for the current wiki
211 * @throws MWException
212 * @return bool|int|string
214 public function getReaderIndex( $group = false, $wiki = false ) {
217 # @todo FIXME: For now, only go through all this for mysql databases
218 if ( $wgDBtype != 'mysql' ) {
219 return $this->getWriterIndex();
222 if ( count( $this->mServers
) == 1 ) {
223 # Skip the load balancing if there's only one server
225 } elseif ( $group === false && $this->mReadIndex
>= 0 ) {
226 # Shortcut if generic reader exists already
227 return $this->mReadIndex
;
230 # Find the relevant load array
231 if ( $group !== false ) {
232 if ( isset( $this->mGroupLoads
[$group] ) ) {
233 $nonErrorLoads = $this->mGroupLoads
[$group];
235 # No loads for this group, return false and the caller can use some other group
236 wfDebug( __METHOD__
. ": no loads for group $group\n" );
241 $nonErrorLoads = $this->mLoads
;
244 if ( !count( $nonErrorLoads ) ) {
245 throw new MWException( "Empty server array given to LoadBalancer" );
248 # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
249 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
251 $laggedSlaveMode = false;
253 # No server found yet
256 # First try quickly looking through the available servers for a server that
258 $currentLoads = $nonErrorLoads;
259 while ( count( $currentLoads ) ) {
260 if ( $this->mAllowLagged ||
$laggedSlaveMode ) {
261 $i = ArrayUtils
::pickRandom( $currentLoads );
264 if ( $this->mWaitForPos
&& $this->mWaitForPos
->asOfTime() ) {
265 # ChronologyProtecter causes mWaitForPos to be set via sessions.
266 # This triggers doWait() after connect, so it's especially good to
267 # avoid lagged servers so as to avoid just blocking in that method.
268 $ago = microtime( true ) - $this->mWaitForPos
->asOfTime();
269 # Aim for <= 1 second of waiting (being too picky can backfire)
270 $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago +
1 );
272 if ( $i === false ) {
273 # Any server with less lag than it's 'max lag' param is preferable
274 $i = $this->getRandomNonLagged( $currentLoads, $wiki );
276 if ( $i === false && count( $currentLoads ) != 0 ) {
277 # All slaves lagged. Switch to read-only mode
278 wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
279 $i = ArrayUtils
::pickRandom( $currentLoads );
280 $laggedSlaveMode = true;
284 if ( $i === false ) {
285 # pickRandom() returned false
286 # This is permanent and means the configuration or the load monitor
287 # wants us to return false.
288 wfDebugLog( 'connect', __METHOD__
. ": pickRandom() returned false" );
293 $serverName = $this->getServerName( $i );
294 wfDebugLog( 'connect', __METHOD__
. ": Using reader #$i: $serverName..." );
296 $conn = $this->openConnection( $i, $wiki );
298 wfDebugLog( 'connect', __METHOD__
. ": Failed connecting to $i/$wiki" );
299 unset( $nonErrorLoads[$i] );
300 unset( $currentLoads[$i] );
305 // Decrement reference counter, we are finished with this connection.
306 // It will be incremented for the caller later.
307 if ( $wiki !== false ) {
308 $this->reuseConnection( $conn );
315 # If all servers were down, quit now
316 if ( !count( $nonErrorLoads ) ) {
317 wfDebugLog( 'connect', "All servers down" );
320 if ( $i !== false ) {
321 # Slave connection successful
322 # Wait for the session master pos for a short time
323 if ( $this->mWaitForPos
&& $i > 0 ) {
324 if ( !$this->doWait( $i ) ) {
325 $this->mServers
[$i]['slave pos'] = $conn->getSlavePos();
328 if ( $this->mReadIndex
<= 0 && $this->mLoads
[$i] > 0 && $group === false ) {
329 $this->mReadIndex
= $i;
330 # Record if the generic reader index is in "lagged slave" mode
331 if ( $laggedSlaveMode ) {
332 $this->mLaggedSlaveMode
= true;
335 $serverName = $this->getServerName( $i );
336 wfDebug( __METHOD__
. ": using server $serverName for group '$group'\n" );
343 * Set the master wait position
344 * If a DB_SLAVE connection has been opened already, waits
345 * Otherwise sets a variable telling it to wait if such a connection is opened
346 * @param DBMasterPos $pos
348 public function waitFor( $pos ) {
349 $this->mWaitForPos
= $pos;
350 $i = $this->mReadIndex
;
353 if ( !$this->doWait( $i ) ) {
354 $this->mServers
[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
355 $this->mLaggedSlaveMode
= true;
361 * Set the master wait position and wait for a "generic" slave to catch up to it
363 * This can be used a faster proxy for waitForAll()
365 * @param DBMasterPos $pos
366 * @param int $timeout Max seconds to wait; default is mWaitTimeout
367 * @return bool Success (able to connect and no timeouts reached)
370 public function waitForOne( $pos, $timeout = null ) {
371 $this->mWaitForPos
= $pos;
373 $i = $this->mReadIndex
;
375 // Pick a generic slave if there isn't one yet
376 $readLoads = $this->mLoads
;
377 unset( $readLoads[$this->getWriterIndex()] ); // slaves only
378 $readLoads = array_filter( $readLoads ); // with non-zero load
379 $i = ArrayUtils
::pickRandom( $readLoads );
383 $ok = $this->doWait( $i, true, $timeout );
385 $ok = true; // no applicable loads
392 * Set the master wait position and wait for ALL slaves to catch up to it
393 * @param DBMasterPos $pos
394 * @param int $timeout Max seconds to wait; default is mWaitTimeout
395 * @return bool Success (able to connect and no timeouts reached)
397 public function waitForAll( $pos, $timeout = null ) {
398 $this->mWaitForPos
= $pos;
399 $serverCount = count( $this->mServers
);
402 for ( $i = 1; $i < $serverCount; $i++
) {
403 if ( $this->mLoads
[$i] > 0 ) {
404 $ok = $this->doWait( $i, true, $timeout ) && $ok;
412 * Get any open connection to a given server index, local or foreign
413 * Returns false if there is no connection open
416 * @return DatabaseBase|bool False on failure
418 public function getAnyOpenConnection( $i ) {
419 foreach ( $this->mConns
as $conns ) {
420 if ( !empty( $conns[$i] ) ) {
421 return reset( $conns[$i] );
429 * Wait for a given slave to catch up to the master pos stored in $this
430 * @param int $index Server index
431 * @param bool $open Check the server even if a new connection has to be made
432 * @param int $timeout Max seconds to wait; default is mWaitTimeout
435 protected function doWait( $index, $open = false, $timeout = null ) {
436 $close = false; // close the connection afterwards
438 # Find a connection to wait on, creating one if needed and allowed
439 $conn = $this->getAnyOpenConnection( $index );
442 wfDebug( __METHOD__
. ": no connection open\n" );
446 $conn = $this->openConnection( $index, '' );
448 wfDebug( __METHOD__
. ": failed to open connection\n" );
452 // Avoid connection spam in waitForAll() when connections
453 // are made just for the sake of doing this lag check.
458 wfDebug( __METHOD__
. ": Waiting for slave #$index to catch up...\n" );
459 $timeout = $timeout ?
: $this->mWaitTimeout
;
460 $result = $conn->masterPosWait( $this->mWaitForPos
, $timeout );
462 if ( $result == -1 ||
is_null( $result ) ) {
463 # Timed out waiting for slave, use master instead
464 $server = $server = $this->getServerName( $index );
465 $msg = __METHOD__
. ": Timed out waiting on $server pos {$this->mWaitForPos}";
467 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
470 wfDebug( __METHOD__
. ": Done\n" );
475 $this->closeConnection( $conn );
482 * Get a connection by index
483 * This is the main entry point for this class.
485 * @param int $i Server index
486 * @param array|string|bool $groups Query group(s), or false for the generic reader
487 * @param string|bool $wiki Wiki ID, or false for the current wiki
489 * @throws MWException
490 * @return DatabaseBase
492 public function getConnection( $i, $groups = array(), $wiki = false ) {
493 if ( $i === null ||
$i === false ) {
494 throw new MWException( 'Attempt to call ' . __METHOD__
.
495 ' with invalid server index' );
498 if ( $wiki === wfWikiID() ) {
502 $groups = ( $groups === false ||
$groups === array() )
503 ?
array( false ) // check one "group": the generic pool
506 $masterOnly = ( $i == DB_MASTER ||
$i == $this->getWriterIndex() );
507 $oldConnsOpened = $this->connsOpened
; // connections open now
509 if ( $i == DB_MASTER
) {
510 $i = $this->getWriterIndex();
512 # Try to find an available server in any the query groups (in order)
513 foreach ( $groups as $group ) {
514 $groupIndex = $this->getReaderIndex( $group, $wiki );
515 if ( $groupIndex !== false ) {
522 # Operation-based index
523 if ( $i == DB_SLAVE
) {
524 $this->mLastError
= 'Unknown error'; // reset error string
525 # Try the general server pool if $groups are unavailable.
526 $i = in_array( false, $groups, true )
527 ?
false // don't bother with this if that is what was tried above
528 : $this->getReaderIndex( false, $wiki );
529 # Couldn't find a working server in getReaderIndex()?
530 if ( $i === false ) {
531 $this->mLastError
= 'No working slave server: ' . $this->mLastError
;
533 return $this->reportConnectionError();
537 # Now we have an explicit index into the servers array
538 $conn = $this->openConnection( $i, $wiki );
540 return $this->reportConnectionError();
543 # Profile any new connections that happen
544 if ( $this->connsOpened
> $oldConnsOpened ) {
545 $host = $conn->getServer();
546 $dbname = $conn->getDBname();
547 $trxProf = Profiler
::instance()->getTransactionProfiler();
548 $trxProf->recordConnection( $host, $dbname, $masterOnly );
551 # Make master connections read only if in lagged slave mode
552 if ( $masterOnly && $this->getServerCount() > 1 && $this->getLaggedSlaveMode() ) {
553 $conn->setLBInfo( 'readOnlyReason',
554 'The database has been automatically locked ' .
555 'while the slave database servers catch up to the master'
563 * Mark a foreign connection as being available for reuse under a different
564 * DB name or prefix. This mechanism is reference-counted, and must be called
565 * the same number of times as getConnection() to work.
567 * @param DatabaseBase $conn
568 * @throws MWException
570 public function reuseConnection( $conn ) {
571 $serverIndex = $conn->getLBInfo( 'serverIndex' );
572 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
573 if ( $serverIndex === null ||
$refCount === null ) {
574 wfDebug( __METHOD__
. ": this connection was not opened as a foreign connection\n" );
577 * This can happen in code like:
578 * foreach ( $dbs as $db ) {
579 * $conn = $lb->getConnection( DB_SLAVE, array(), $db );
581 * $lb->reuseConnection( $conn );
583 * When a connection to the local DB is opened in this way, reuseConnection()
590 $dbName = $conn->getDBname();
591 $prefix = $conn->tablePrefix();
592 if ( strval( $prefix ) !== '' ) {
593 $wiki = "$dbName-$prefix";
597 if ( $this->mConns
['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
598 throw new MWException( __METHOD__
. ": connection not found, has " .
599 "the connection been freed already?" );
601 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
602 if ( $refCount <= 0 ) {
603 $this->mConns
['foreignFree'][$serverIndex][$wiki] = $conn;
604 unset( $this->mConns
['foreignUsed'][$serverIndex][$wiki] );
605 wfDebug( __METHOD__
. ": freed connection $serverIndex/$wiki\n" );
607 wfDebug( __METHOD__
. ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
612 * Get a database connection handle reference
614 * The handle's methods wrap simply wrap those of a DatabaseBase handle
616 * @see LoadBalancer::getConnection() for parameter information
619 * @param array|string|bool $groups Query group(s), or false for the generic reader
620 * @param string|bool $wiki Wiki ID, or false for the current wiki
623 public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
624 return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
628 * Get a database connection handle reference without connecting yet
630 * The handle's methods wrap simply wrap those of a DatabaseBase handle
632 * @see LoadBalancer::getConnection() for parameter information
635 * @param array|string|bool $groups Query group(s), or false for the generic reader
636 * @param string|bool $wiki Wiki ID, or false for the current wiki
639 public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
640 return new DBConnRef( $this, array( $db, $groups, $wiki ) );
644 * Open a connection to the server given by the specified index
645 * Index must be an actual index into the array.
646 * If the server is already open, returns it.
648 * On error, returns false, and the connection which caused the
649 * error will be available via $this->mErrorConnection.
651 * @param int $i Server index
652 * @param string|bool $wiki Wiki ID, or false for the current wiki
653 * @return DatabaseBase
657 public function openConnection( $i, $wiki = false ) {
658 if ( $wiki !== false ) {
659 $conn = $this->openForeignConnection( $i, $wiki );
660 } elseif ( isset( $this->mConns
['local'][$i][0] ) ) {
661 $conn = $this->mConns
['local'][$i][0];
663 $server = $this->mServers
[$i];
664 $server['serverIndex'] = $i;
665 $conn = $this->reallyOpenConnection( $server, false );
666 $serverName = $this->getServerName( $i );
667 if ( $conn->isOpen() ) {
668 wfDebug( "Connected to database $i at $serverName\n" );
669 $this->mConns
['local'][$i][0] = $conn;
671 wfDebug( "Failed to connect to database $i at $serverName\n" );
672 $this->mErrorConnection
= $conn;
677 if ( $conn && !$conn->isOpen() ) {
678 // Connection was made but later unrecoverably lost for some reason.
679 // Do not return a handle that will just throw exceptions on use,
680 // but let the calling code (e.g. getReaderIndex) try another server.
681 // See DatabaseMyslBase::ping() for how this can happen.
682 $this->mErrorConnection
= $conn;
690 * Open a connection to a foreign DB, or return one if it is already open.
692 * Increments a reference count on the returned connection which locks the
693 * connection to the requested wiki. This reference count can be
694 * decremented by calling reuseConnection().
696 * If a connection is open to the appropriate server already, but with the wrong
697 * database, it will be switched to the right database and returned, as long as
698 * it has been freed first with reuseConnection().
700 * On error, returns false, and the connection which caused the
701 * error will be available via $this->mErrorConnection.
703 * @param int $i Server index
704 * @param string $wiki Wiki ID to open
705 * @return DatabaseBase
707 private function openForeignConnection( $i, $wiki ) {
708 list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
709 if ( isset( $this->mConns
['foreignUsed'][$i][$wiki] ) ) {
710 // Reuse an already-used connection
711 $conn = $this->mConns
['foreignUsed'][$i][$wiki];
712 wfDebug( __METHOD__
. ": reusing connection $i/$wiki\n" );
713 } elseif ( isset( $this->mConns
['foreignFree'][$i][$wiki] ) ) {
714 // Reuse a free connection for the same wiki
715 $conn = $this->mConns
['foreignFree'][$i][$wiki];
716 unset( $this->mConns
['foreignFree'][$i][$wiki] );
717 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
718 wfDebug( __METHOD__
. ": reusing free connection $i/$wiki\n" );
719 } elseif ( !empty( $this->mConns
['foreignFree'][$i] ) ) {
720 // Reuse a connection from another wiki
721 $conn = reset( $this->mConns
['foreignFree'][$i] );
722 $oldWiki = key( $this->mConns
['foreignFree'][$i] );
724 // The empty string as a DB name means "don't care".
725 // DatabaseMysqlBase::open() already handle this on connection.
726 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
727 $this->mLastError
= "Error selecting database $dbName on server " .
728 $conn->getServer() . " from client host " . wfHostname() . "\n";
729 $this->mErrorConnection
= $conn;
732 $conn->tablePrefix( $prefix );
733 unset( $this->mConns
['foreignFree'][$i][$oldWiki] );
734 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
735 wfDebug( __METHOD__
. ": reusing free connection from $oldWiki for $wiki\n" );
738 // Open a new connection
739 $server = $this->mServers
[$i];
740 $server['serverIndex'] = $i;
741 $server['foreignPoolRefCount'] = 0;
742 $server['foreign'] = true;
743 $conn = $this->reallyOpenConnection( $server, $dbName );
744 if ( !$conn->isOpen() ) {
745 wfDebug( __METHOD__
. ": error opening connection for $i/$wiki\n" );
746 $this->mErrorConnection
= $conn;
749 $conn->tablePrefix( $prefix );
750 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
751 wfDebug( __METHOD__
. ": opened new connection for $i/$wiki\n" );
755 // Increment reference count
757 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
758 $conn->setLBInfo( 'foreignPoolRefCount', $refCount +
1 );
765 * Test if the specified index represents an open connection
767 * @param int $index Server index
771 private function isOpen( $index ) {
772 if ( !is_integer( $index ) ) {
776 return (bool)$this->getAnyOpenConnection( $index );
780 * Really opens a connection. Uncached.
781 * Returns a Database object whether or not the connection was successful.
784 * @param array $server
785 * @param bool $dbNameOverride
786 * @throws MWException
787 * @return DatabaseBase
789 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
790 if ( !is_array( $server ) ) {
791 throw new MWException( 'You must update your load-balancing configuration. ' .
792 'See DefaultSettings.php entry for $wgDBservers.' );
795 if ( $dbNameOverride !== false ) {
796 $server['dbname'] = $dbNameOverride;
799 // Log when many connection are made on requests
800 if ( ++
$this->connsOpened
>= self
::CONN_HELD_WARN_THRESHOLD
) {
801 $masterAddr = $this->getServerName( 0 );
802 wfDebugLog( 'DBPerformance', __METHOD__
. ": " .
803 "{$this->connsOpened}+ connections made (master=$masterAddr)\n" .
804 wfBacktrace( true ) );
809 $db = DatabaseBase
::factory( $server['type'], $server );
810 } catch ( DBConnectionError
$e ) {
811 // FIXME: This is probably the ugliest thing I have ever done to
812 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
816 $db->setLBInfo( $server );
822 * @throws DBConnectionError
825 private function reportConnectionError() {
826 $conn = $this->mErrorConnection
; // The connection which caused the error
828 'method' => __METHOD__
,
829 'last_error' => $this->mLastError
,
832 if ( !is_object( $conn ) ) {
833 // No last connection, probably due to all servers being too busy
835 "LB failure with no last connection. Connection error: {last_error}",
839 // If all servers were busy, mLastError will contain something sensible
840 throw new DBConnectionError( null, $this->mLastError
);
842 $context['db_server'] = $conn->getProperty( 'mServer' );
844 "Connection error: {last_error} ({db_server})",
848 // throws DBConnectionError
849 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
852 return false; /* not reached */
859 public function getWriterIndex() {
864 * Returns true if the specified index is a valid server index
869 public function haveIndex( $i ) {
870 return array_key_exists( $i, $this->mServers
);
874 * Returns true if the specified index is valid and has non-zero load
879 public function isNonZeroLoad( $i ) {
880 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
884 * Get the number of defined servers (not the number of open connections)
888 public function getServerCount() {
889 return count( $this->mServers
);
893 * Get the host name or IP address of the server with the specified index
894 * Prefer a readable name if available.
898 public function getServerName( $i ) {
899 if ( isset( $this->mServers
[$i]['hostName'] ) ) {
900 $name = $this->mServers
[$i]['hostName'];
901 } elseif ( isset( $this->mServers
[$i]['host'] ) ) {
902 $name = $this->mServers
[$i]['host'];
907 return ( $name != '' ) ?
$name : 'localhost';
911 * Return the server info structure for a given index, or false if the index is invalid.
915 public function getServerInfo( $i ) {
916 if ( isset( $this->mServers
[$i] ) ) {
917 return $this->mServers
[$i];
924 * Sets the server info structure for the given index. Entry at index $i
925 * is created if it doesn't exist
927 * @param array $serverInfo
929 public function setServerInfo( $i, array $serverInfo ) {
930 $this->mServers
[$i] = $serverInfo;
934 * Get the current master position for chronology control purposes
937 public function getMasterPos() {
938 # If this entire request was served from a slave without opening a connection to the
939 # master (however unlikely that may be), then we can fetch the position from the slave.
940 $masterConn = $this->getAnyOpenConnection( 0 );
941 if ( !$masterConn ) {
942 $serverCount = count( $this->mServers
);
943 for ( $i = 1; $i < $serverCount; $i++
) {
944 $conn = $this->getAnyOpenConnection( $i );
946 wfDebug( "Master pos fetched from slave\n" );
948 return $conn->getSlavePos();
952 wfDebug( "Master pos fetched from master\n" );
954 return $masterConn->getMasterPos();
961 * Close all open connections
963 public function closeAll() {
964 foreach ( $this->mConns
as $conns2 ) {
965 foreach ( $conns2 as $conns3 ) {
966 /** @var DatabaseBase $conn */
967 foreach ( $conns3 as $conn ) {
972 $this->mConns
= array(
974 'foreignFree' => array(),
975 'foreignUsed' => array(),
977 $this->connsOpened
= 0;
982 * Using this function makes sure the LoadBalancer knows the connection is closed.
983 * If you use $conn->close() directly, the load balancer won't update its state.
984 * @param DatabaseBase $conn
986 public function closeConnection( $conn ) {
988 foreach ( $this->mConns
as $i1 => $conns2 ) {
989 foreach ( $conns2 as $i2 => $conns3 ) {
990 foreach ( $conns3 as $i3 => $candidateConn ) {
991 if ( $conn === $candidateConn ) {
993 unset( $this->mConns
[$i1][$i2][$i3] );
994 --$this->connsOpened
;
1007 * Commit transactions on all open connections
1009 public function commitAll() {
1010 foreach ( $this->mConns
as $conns2 ) {
1011 foreach ( $conns2 as $conns3 ) {
1012 /** @var DatabaseBase[] $conns3 */
1013 foreach ( $conns3 as $conn ) {
1014 if ( $conn->trxLevel() ) {
1015 $conn->commit( __METHOD__
, 'flush' );
1023 * Issue COMMIT only on master, only if queries were done on connection
1025 public function commitMasterChanges() {
1026 $masterIndex = $this->getWriterIndex();
1027 foreach ( $this->mConns
as $conns2 ) {
1028 if ( empty( $conns2[$masterIndex] ) ) {
1031 /** @var DatabaseBase $conn */
1032 foreach ( $conns2[$masterIndex] as $conn ) {
1033 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1034 $conn->commit( __METHOD__
, 'flush' );
1041 * Issue ROLLBACK only on master, only if queries were done on connection
1044 public function rollbackMasterChanges() {
1045 $failedServers = array();
1047 $masterIndex = $this->getWriterIndex();
1048 foreach ( $this->mConns
as $conns2 ) {
1049 if ( empty( $conns2[$masterIndex] ) ) {
1052 /** @var DatabaseBase $conn */
1053 foreach ( $conns2[$masterIndex] as $conn ) {
1054 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1056 $conn->rollback( __METHOD__
, 'flush' );
1057 } catch ( DBError
$e ) {
1058 MWExceptionHandler
::logException( $e );
1059 $failedServers[] = $conn->getServer();
1065 if ( $failedServers ) {
1066 throw new DBExpectedError( null, "Rollback failed on server(s) " .
1067 implode( ', ', array_unique( $failedServers ) ) );
1072 * @return bool Whether a master connection is already open
1075 public function hasMasterConnection() {
1076 return $this->isOpen( $this->getWriterIndex() );
1080 * Determine if there are pending changes in a transaction by this thread
1084 public function hasMasterChanges() {
1085 $masterIndex = $this->getWriterIndex();
1086 foreach ( $this->mConns
as $conns2 ) {
1087 if ( empty( $conns2[$masterIndex] ) ) {
1090 /** @var DatabaseBase $conn */
1091 foreach ( $conns2[$masterIndex] as $conn ) {
1092 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1101 * Get the timestamp of the latest write query done by this thread
1103 * @return float|bool UNIX timestamp or false
1105 public function lastMasterChangeTimestamp() {
1107 $masterIndex = $this->getWriterIndex();
1108 foreach ( $this->mConns
as $conns2 ) {
1109 if ( empty( $conns2[$masterIndex] ) ) {
1112 /** @var DatabaseBase $conn */
1113 foreach ( $conns2[$masterIndex] as $conn ) {
1114 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1121 * Check if this load balancer object had any recent or still
1122 * pending writes issued against it by this PHP thread
1124 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1128 public function hasOrMadeRecentMasterChanges( $age = null ) {
1129 $age = ( $age === null ) ?
$this->mWaitTimeout
: $age;
1131 return ( $this->hasMasterChanges()
1132 ||
$this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1136 * @param mixed $value
1139 public function waitTimeout( $value = null ) {
1140 return wfSetVar( $this->mWaitTimeout
, $value );
1144 * @note This method will trigger a DB connection if not yet done
1145 * @return bool Whether the generic connection for reads is highly "lagged"
1147 public function getLaggedSlaveMode() {
1148 # Get a generic reader connection
1149 $this->getConnection( DB_SLAVE
);
1151 return $this->mLaggedSlaveMode
;
1155 * @note This method will never cause a new DB connection
1156 * @return bool Whether any generic connection used for reads was highly "lagged"
1159 public function laggedSlaveUsed() {
1160 return $this->mLaggedSlaveMode
;
1164 * Disables/enables lag checks
1165 * @param null|bool $mode
1168 public function allowLagged( $mode = null ) {
1169 if ( $mode === null ) {
1170 return $this->mAllowLagged
;
1172 $this->mAllowLagged
= $mode;
1174 return $this->mAllowLagged
;
1180 public function pingAll() {
1182 foreach ( $this->mConns
as $conns2 ) {
1183 foreach ( $conns2 as $conns3 ) {
1184 /** @var DatabaseBase[] $conns3 */
1185 foreach ( $conns3 as $conn ) {
1186 if ( !$conn->ping() ) {
1197 * Call a function with each open connection object
1198 * @param callable $callback
1199 * @param array $params
1201 public function forEachOpenConnection( $callback, array $params = array() ) {
1202 foreach ( $this->mConns
as $conns2 ) {
1203 foreach ( $conns2 as $conns3 ) {
1204 foreach ( $conns3 as $conn ) {
1205 $mergedParams = array_merge( array( $conn ), $params );
1206 call_user_func_array( $callback, $mergedParams );
1213 * Get the hostname and lag time of the most-lagged slave
1215 * This is useful for maintenance scripts that need to throttle their updates.
1216 * May attempt to open connections to slaves on the default DB. If there is
1217 * no lag, the maximum lag will be reported as -1.
1219 * @param bool|string $wiki Wiki ID, or false for the default database
1220 * @return array ( host, max lag, index of max lagged host )
1222 public function getMaxLag( $wiki = false ) {
1227 if ( $this->getServerCount() <= 1 ) {
1228 return array( $host, $maxLag, $maxIndex ); // no replication = no lag
1231 $lagTimes = $this->getLagTimes( $wiki );
1232 foreach ( $lagTimes as $i => $lag ) {
1233 if ( $lag > $maxLag ) {
1235 $host = $this->mServers
[$i]['host'];
1240 return array( $host, $maxLag, $maxIndex );
1244 * Get an estimate of replication lag (in seconds) for each server
1246 * Results are cached for a short time in memcached/process cache
1248 * Values may be "false" if replication is too broken to estimate
1250 * @param string|bool $wiki
1251 * @return int[] Map of (server index => float|int|bool)
1253 public function getLagTimes( $wiki = false ) {
1254 if ( $this->getServerCount() <= 1 ) {
1255 return array( 0 => 0 ); // no replication = no lag
1258 # Send the request to the load monitor
1259 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers
), $wiki );
1263 * Get the lag in seconds for a given connection, or zero if this load
1264 * balancer does not have replication enabled.
1266 * This should be used in preference to Database::getLag() in cases where
1267 * replication may not be in use, since there is no way to determine if
1268 * replication is in use at the connection level without running
1269 * potentially restricted queries such as SHOW SLAVE STATUS. Using this
1270 * function instead of Database::getLag() avoids a fatal error in this
1271 * case on many installations.
1273 * @param DatabaseBase $conn
1276 public function safeGetLag( $conn ) {
1277 if ( $this->getServerCount() == 1 ) {
1280 return $conn->getLag();
1285 * Clear the cache for slag lag delay times
1287 * This is only used for testing
1289 public function clearLagTimeCache() {
1290 $this->getLoadMonitor()->clearCaches();