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;
43 /** @var array LBFactory information */
46 /** @var string The LoadMonitor subclass name */
47 private $mLoadMonitorClass;
48 /** @var LoadMonitor */
49 private $mLoadMonitor;
52 /** @var WANObjectCache */
55 /** @var bool|DatabaseBase Database connection that caused a problem */
56 private $mErrorConnection;
57 /** @var integer The generic (not query grouped) slave index (of $mServers) */
59 /** @var bool|DBMasterPos False if not set */
61 /** @var bool Whether the generic reader fell back to a lagged slave */
62 private $laggedSlaveMode = false;
63 /** @var bool Whether the generic reader fell back to a lagged slave */
64 private $slavesDownMode = false;
65 /** @var string The last DB selection or connection error */
66 private $mLastError = 'Unknown error';
67 /** @var string|bool Reason the LB is read-only or false if not */
68 private $readOnlyReason = false;
69 /** @var integer Total connections opened */
70 private $connsOpened = 0;
72 /** @var TransactionProfiler */
73 protected $trxProfiler;
75 /** @var integer Warn when this many connection are held */
76 const CONN_HELD_WARN_THRESHOLD
= 10;
77 /** @var integer Default 'max lag' when unspecified */
79 /** @var integer Max time to wait for a slave to catch up (e.g. ChronologyProtector) */
80 const POS_WAIT_TIMEOUT
= 10;
81 /** @var integer Seconds to cache master server read-only status */
82 const TTL_CACHE_READONLY
= 5;
87 private $disabled = false;
90 * @param array $params Array with keys:
91 * - servers : Required. Array of server info structures.
92 * - loadMonitor : Name of a class used to fetch server lag and load.
93 * - readOnlyReason : Reason the master DB is read-only if so [optional]
96 public function __construct( array $params ) {
97 if ( !isset( $params['servers'] ) ) {
98 throw new MWException( __CLASS__
. ': missing servers parameter' );
100 $this->mServers
= $params['servers'];
101 $this->mWaitTimeout
= self
::POS_WAIT_TIMEOUT
;
103 $this->mReadIndex
= -1;
104 $this->mWriteIndex
= -1;
108 'foreignFree' => [] ];
110 $this->mWaitForPos
= false;
111 $this->mErrorConnection
= false;
112 $this->mAllowLagged
= false;
114 if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
115 $this->readOnlyReason
= $params['readOnlyReason'];
118 if ( isset( $params['loadMonitor'] ) ) {
119 $this->mLoadMonitorClass
= $params['loadMonitor'];
121 $master = reset( $params['servers'] );
122 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
123 $this->mLoadMonitorClass
= 'LoadMonitorMySQL';
125 $this->mLoadMonitorClass
= 'LoadMonitorNull';
129 foreach ( $params['servers'] as $i => $server ) {
130 $this->mLoads
[$i] = $server['load'];
131 if ( isset( $server['groupLoads'] ) ) {
132 foreach ( $server['groupLoads'] as $group => $ratio ) {
133 if ( !isset( $this->mGroupLoads
[$group] ) ) {
134 $this->mGroupLoads
[$group] = [];
136 $this->mGroupLoads
[$group][$i] = $ratio;
141 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
142 // @TODO: inject these in via LBFactory at some point
143 $cache = ObjectCache
::getLocalServerInstance();
144 if ( $cache->getQoS( $cache::ATTR_EMULATION
) > $cache::QOS_EMULATION_SQL
) {
145 $this->srvCache
= $cache;
147 $this->srvCache
= new EmptyBagOStuff();
149 $wCache = ObjectCache
::getMainWANInstance();
150 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
151 $this->wanCache
= $wCache;
153 $this->wanCache
= WANObjectCache
::newEmpty();
156 if ( isset( $params['trxProfiler'] ) ) {
157 $this->trxProfiler
= $params['trxProfiler'];
159 $this->trxProfiler
= new TransactionProfiler();
164 * Get a LoadMonitor instance
166 * @return LoadMonitor
168 private function getLoadMonitor() {
169 if ( !isset( $this->mLoadMonitor
) ) {
170 $class = $this->mLoadMonitorClass
;
171 $this->mLoadMonitor
= new $class( $this );
174 return $this->mLoadMonitor
;
178 * Get or set arbitrary data used by the parent object, usually an LBFactory
182 public function parentInfo( $x = null ) {
183 return wfSetVar( $this->mParentInfo
, $x );
187 * @param array $loads
188 * @param bool|string $wiki Wiki to get non-lagged for
189 * @param int $maxLag Restrict the maximum allowed lag to this many seconds
190 * @return bool|int|string
192 private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = self
::MAX_LAG
) {
193 $lags = $this->getLagTimes( $wiki );
195 # Unset excessively lagged servers
196 foreach ( $lags as $i => $lag ) {
198 $maxServerLag = $maxLag;
199 if ( isset( $this->mServers
[$i]['max lag'] ) ) {
200 $maxServerLag = min( $maxServerLag, $this->mServers
[$i]['max lag'] );
203 $host = $this->getServerName( $i );
204 if ( $lag === false ) {
205 wfDebugLog( 'replication', "Server $host (#$i) is not replicating?" );
207 } elseif ( $lag > $maxServerLag ) {
208 wfDebugLog( 'replication', "Server $host (#$i) has >= $lag seconds of lag" );
214 # Find out if all the slaves with non-zero load are lagged
216 foreach ( $loads as $load ) {
220 # No appropriate DB servers except maybe the master and some slaves with zero load
221 # Do NOT use the master
222 # Instead, this function will return false, triggering read-only mode,
223 # and a lagged slave will be used instead.
227 if ( count( $loads ) == 0 ) {
231 # Return a random representative of the remainder
232 return ArrayUtils
::pickRandom( $loads );
236 * Get the index of the reader connection, which may be a slave
237 * This takes into account load ratios and lag times. It should
238 * always return a consistent index during a given invocation
240 * Side effect: opens connections to databases
241 * @param string|bool $group Query group, or false for the generic reader
242 * @param string|bool $wiki Wiki ID, or false for the current wiki
243 * @throws MWException
244 * @return bool|int|string
246 public function getReaderIndex( $group = false, $wiki = false ) {
249 # @todo FIXME: For now, only go through all this for mysql databases
250 if ( $wgDBtype != 'mysql' ) {
251 return $this->getWriterIndex();
254 if ( count( $this->mServers
) == 1 ) {
255 # Skip the load balancing if there's only one server
257 } elseif ( $group === false && $this->mReadIndex
>= 0 ) {
258 # Shortcut if generic reader exists already
259 return $this->mReadIndex
;
262 # Find the relevant load array
263 if ( $group !== false ) {
264 if ( isset( $this->mGroupLoads
[$group] ) ) {
265 $nonErrorLoads = $this->mGroupLoads
[$group];
267 # No loads for this group, return false and the caller can use some other group
268 wfDebugLog( 'connect', __METHOD__
. ": no loads for group $group\n" );
273 $nonErrorLoads = $this->mLoads
;
276 if ( !count( $nonErrorLoads ) ) {
277 throw new MWException( "Empty server array given to LoadBalancer" );
280 # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
281 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
283 $laggedSlaveMode = false;
285 # No server found yet
288 # First try quickly looking through the available servers for a server that
290 $currentLoads = $nonErrorLoads;
291 while ( count( $currentLoads ) ) {
292 if ( $this->mAllowLagged ||
$laggedSlaveMode ) {
293 $i = ArrayUtils
::pickRandom( $currentLoads );
296 if ( $this->mWaitForPos
&& $this->mWaitForPos
->asOfTime() ) {
297 # ChronologyProtecter causes mWaitForPos to be set via sessions.
298 # This triggers doWait() after connect, so it's especially good to
299 # avoid lagged servers so as to avoid just blocking in that method.
300 $ago = microtime( true ) - $this->mWaitForPos
->asOfTime();
301 # Aim for <= 1 second of waiting (being too picky can backfire)
302 $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago +
1 );
304 if ( $i === false ) {
305 # Any server with less lag than it's 'max lag' param is preferable
306 $i = $this->getRandomNonLagged( $currentLoads, $wiki );
308 if ( $i === false && count( $currentLoads ) != 0 ) {
309 # All slaves lagged. Switch to read-only mode
310 wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
311 $i = ArrayUtils
::pickRandom( $currentLoads );
312 $laggedSlaveMode = true;
316 if ( $i === false ) {
317 # pickRandom() returned false
318 # This is permanent and means the configuration or the load monitor
319 # wants us to return false.
320 wfDebugLog( 'connect', __METHOD__
. ": pickRandom() returned false" );
325 $serverName = $this->getServerName( $i );
326 wfDebugLog( 'connect', __METHOD__
. ": Using reader #$i: $serverName..." );
328 $conn = $this->openConnection( $i, $wiki );
330 wfDebugLog( 'connect', __METHOD__
. ": Failed connecting to $i/$wiki" );
331 unset( $nonErrorLoads[$i] );
332 unset( $currentLoads[$i] );
337 // Decrement reference counter, we are finished with this connection.
338 // It will be incremented for the caller later.
339 if ( $wiki !== false ) {
340 $this->reuseConnection( $conn );
347 # If all servers were down, quit now
348 if ( !count( $nonErrorLoads ) ) {
349 wfDebugLog( 'connect', "All servers down" );
352 if ( $i !== false ) {
353 # Slave connection successful
354 # Wait for the session master pos for a short time
355 if ( $this->mWaitForPos
&& $i > 0 ) {
356 if ( !$this->doWait( $i ) ) {
357 $this->mServers
[$i]['slave pos'] = $conn->getSlavePos();
360 if ( $this->mReadIndex
<= 0 && $this->mLoads
[$i] > 0 && $group === false ) {
361 $this->mReadIndex
= $i;
362 # Record if the generic reader index is in "lagged slave" mode
363 if ( $laggedSlaveMode ) {
364 $this->laggedSlaveMode
= true;
367 $serverName = $this->getServerName( $i );
368 wfDebugLog( 'connect', __METHOD__
.
369 ": using server $serverName for group '$group'\n" );
376 * Set the master wait position
377 * If a DB_SLAVE connection has been opened already, waits
378 * Otherwise sets a variable telling it to wait if such a connection is opened
379 * @param DBMasterPos $pos
381 public function waitFor( $pos ) {
382 $this->mWaitForPos
= $pos;
383 $i = $this->mReadIndex
;
386 if ( !$this->doWait( $i ) ) {
387 $this->mServers
[$i]['slave pos'] = $this->getAnyOpenConnection( $i )->getSlavePos();
388 $this->laggedSlaveMode
= true;
394 * Set the master wait position and wait for a "generic" slave to catch up to it
396 * This can be used a faster proxy for waitForAll()
398 * @param DBMasterPos $pos
399 * @param int $timeout Max seconds to wait; default is mWaitTimeout
400 * @return bool Success (able to connect and no timeouts reached)
403 public function waitForOne( $pos, $timeout = null ) {
404 $this->mWaitForPos
= $pos;
406 $i = $this->mReadIndex
;
408 // Pick a generic slave if there isn't one yet
409 $readLoads = $this->mLoads
;
410 unset( $readLoads[$this->getWriterIndex()] ); // slaves only
411 $readLoads = array_filter( $readLoads ); // with non-zero load
412 $i = ArrayUtils
::pickRandom( $readLoads );
416 $ok = $this->doWait( $i, true, $timeout );
418 $ok = true; // no applicable loads
425 * Set the master wait position and wait for ALL slaves to catch up to it
426 * @param DBMasterPos $pos
427 * @param int $timeout Max seconds to wait; default is mWaitTimeout
428 * @return bool Success (able to connect and no timeouts reached)
430 public function waitForAll( $pos, $timeout = null ) {
431 $this->mWaitForPos
= $pos;
432 $serverCount = count( $this->mServers
);
435 for ( $i = 1; $i < $serverCount; $i++
) {
436 if ( $this->mLoads
[$i] > 0 ) {
437 $ok = $this->doWait( $i, true, $timeout ) && $ok;
445 * Get any open connection to a given server index, local or foreign
446 * Returns false if there is no connection open
449 * @return DatabaseBase|bool False on failure
451 public function getAnyOpenConnection( $i ) {
452 foreach ( $this->mConns
as $conns ) {
453 if ( !empty( $conns[$i] ) ) {
454 return reset( $conns[$i] );
462 * Wait for a given slave to catch up to the master pos stored in $this
463 * @param int $index Server index
464 * @param bool $open Check the server even if a new connection has to be made
465 * @param int $timeout Max seconds to wait; default is mWaitTimeout
468 protected function doWait( $index, $open = false, $timeout = null ) {
469 $close = false; // close the connection afterwards
471 // Check if we already know that the DB has reached this point
472 $server = $this->getServerName( $index );
473 $key = $this->srvCache
->makeGlobalKey( __CLASS__
, 'last-known-pos', $server );
474 /** @var DBMasterPos $knownReachedPos */
475 $knownReachedPos = $this->srvCache
->get( $key );
476 if ( $knownReachedPos && $knownReachedPos->hasReached( $this->mWaitForPos
) ) {
477 wfDebugLog( 'replication', __METHOD__
.
478 ": slave $server known to be caught up (pos >= $knownReachedPos).\n" );
482 // Find a connection to wait on, creating one if needed and allowed
483 $conn = $this->getAnyOpenConnection( $index );
486 wfDebugLog( 'replication', __METHOD__
. ": no connection open for $server\n" );
490 $conn = $this->openConnection( $index, '' );
492 wfDebugLog( 'replication', __METHOD__
. ": failed to connect to $server\n" );
496 // Avoid connection spam in waitForAll() when connections
497 // are made just for the sake of doing this lag check.
502 wfDebugLog( 'replication', __METHOD__
. ": Waiting for slave $server to catch up...\n" );
503 $timeout = $timeout ?
: $this->mWaitTimeout
;
504 $result = $conn->masterPosWait( $this->mWaitForPos
, $timeout );
506 if ( $result == -1 ||
is_null( $result ) ) {
507 // Timed out waiting for slave, use master instead
508 $msg = __METHOD__
. ": Timed out waiting on $server pos {$this->mWaitForPos}";
509 wfDebugLog( 'replication', "$msg\n" );
510 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
513 wfDebugLog( 'replication', __METHOD__
. ": Done\n" );
515 // Remember that the DB reached this point
516 $this->srvCache
->set( $key, $this->mWaitForPos
, BagOStuff
::TTL_DAY
);
520 $this->closeConnection( $conn );
527 * Get a connection by index
528 * This is the main entry point for this class.
530 * @param int $i Server index
531 * @param array|string|bool $groups Query group(s), or false for the generic reader
532 * @param string|bool $wiki Wiki ID, or false for the current wiki
534 * @throws MWException
535 * @return DatabaseBase
537 public function getConnection( $i, $groups = [], $wiki = false ) {
538 if ( $i === null ||
$i === false ) {
539 throw new MWException( 'Attempt to call ' . __METHOD__
.
540 ' with invalid server index' );
543 if ( $wiki === wfWikiID() ) {
547 $groups = ( $groups === false ||
$groups === [] )
548 ?
[ false ] // check one "group": the generic pool
551 $masterOnly = ( $i == DB_MASTER ||
$i == $this->getWriterIndex() );
552 $oldConnsOpened = $this->connsOpened
; // connections open now
554 if ( $i == DB_MASTER
) {
555 $i = $this->getWriterIndex();
557 # Try to find an available server in any the query groups (in order)
558 foreach ( $groups as $group ) {
559 $groupIndex = $this->getReaderIndex( $group, $wiki );
560 if ( $groupIndex !== false ) {
567 # Operation-based index
568 if ( $i == DB_SLAVE
) {
569 $this->mLastError
= 'Unknown error'; // reset error string
570 # Try the general server pool if $groups are unavailable.
571 $i = in_array( false, $groups, true )
572 ?
false // don't bother with this if that is what was tried above
573 : $this->getReaderIndex( false, $wiki );
574 # Couldn't find a working server in getReaderIndex()?
575 if ( $i === false ) {
576 $this->mLastError
= 'No working slave server: ' . $this->mLastError
;
578 return $this->reportConnectionError();
582 # Now we have an explicit index into the servers array
583 $conn = $this->openConnection( $i, $wiki );
585 return $this->reportConnectionError();
588 # Profile any new connections that happen
589 if ( $this->connsOpened
> $oldConnsOpened ) {
590 $host = $conn->getServer();
591 $dbname = $conn->getDBname();
592 $trxProf = Profiler
::instance()->getTransactionProfiler();
593 $trxProf->recordConnection( $host, $dbname, $masterOnly );
597 # Make master-requested DB handles inherit any read-only mode setting
598 $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $wiki, $conn ) );
605 * Mark a foreign connection as being available for reuse under a different
606 * DB name or prefix. This mechanism is reference-counted, and must be called
607 * the same number of times as getConnection() to work.
609 * @param DatabaseBase $conn
610 * @throws MWException
612 public function reuseConnection( $conn ) {
613 $serverIndex = $conn->getLBInfo( 'serverIndex' );
614 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
615 if ( $serverIndex === null ||
$refCount === null ) {
616 wfDebug( __METHOD__
. ": this connection was not opened as a foreign connection\n" );
618 * This can happen in code like:
619 * foreach ( $dbs as $db ) {
620 * $conn = $lb->getConnection( DB_SLAVE, [], $db );
622 * $lb->reuseConnection( $conn );
624 * When a connection to the local DB is opened in this way, reuseConnection()
630 $dbName = $conn->getDBname();
631 $prefix = $conn->tablePrefix();
632 if ( strval( $prefix ) !== '' ) {
633 $wiki = "$dbName-$prefix";
637 if ( $this->mConns
['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
638 throw new MWException( __METHOD__
. ": connection not found, has " .
639 "the connection been freed already?" );
641 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
642 if ( $refCount <= 0 ) {
643 $this->mConns
['foreignFree'][$serverIndex][$wiki] = $conn;
644 unset( $this->mConns
['foreignUsed'][$serverIndex][$wiki] );
645 wfDebug( __METHOD__
. ": freed connection $serverIndex/$wiki\n" );
647 wfDebug( __METHOD__
. ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
652 * Get a database connection handle reference
654 * The handle's methods wrap simply wrap those of a DatabaseBase handle
656 * @see LoadBalancer::getConnection() for parameter information
659 * @param array|string|bool $groups Query group(s), or false for the generic reader
660 * @param string|bool $wiki Wiki ID, or false for the current wiki
663 public function getConnectionRef( $db, $groups = [], $wiki = false ) {
664 return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
668 * Get a database connection handle reference without connecting yet
670 * The handle's methods wrap simply wrap those of a DatabaseBase handle
672 * @see LoadBalancer::getConnection() for parameter information
675 * @param array|string|bool $groups Query group(s), or false for the generic reader
676 * @param string|bool $wiki Wiki ID, or false for the current wiki
679 public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) {
680 return new DBConnRef( $this, [ $db, $groups, $wiki ] );
684 * Open a connection to the server given by the specified index
685 * Index must be an actual index into the array.
686 * If the server is already open, returns it.
688 * On error, returns false, and the connection which caused the
689 * error will be available via $this->mErrorConnection.
691 * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
693 * @param int $i Server index
694 * @param string|bool $wiki Wiki ID, or false for the current wiki
695 * @return DatabaseBase|bool Returns false on errors
697 public function openConnection( $i, $wiki = false ) {
698 if ( $wiki !== false ) {
699 $conn = $this->openForeignConnection( $i, $wiki );
700 } elseif ( isset( $this->mConns
['local'][$i][0] ) ) {
701 $conn = $this->mConns
['local'][$i][0];
703 $server = $this->mServers
[$i];
704 $server['serverIndex'] = $i;
705 $conn = $this->reallyOpenConnection( $server, false );
706 $serverName = $this->getServerName( $i );
707 if ( $conn->isOpen() ) {
708 wfDebugLog( 'connect', "Connected to database $i at $serverName\n" );
709 $this->mConns
['local'][$i][0] = $conn;
711 wfDebugLog( 'connect', "Failed to connect to database $i at $serverName\n" );
712 $this->mErrorConnection
= $conn;
717 if ( $conn && !$conn->isOpen() ) {
718 // Connection was made but later unrecoverably lost for some reason.
719 // Do not return a handle that will just throw exceptions on use,
720 // but let the calling code (e.g. getReaderIndex) try another server.
721 // See DatabaseMyslBase::ping() for how this can happen.
722 $this->mErrorConnection
= $conn;
730 * Open a connection to a foreign DB, or return one if it is already open.
732 * Increments a reference count on the returned connection which locks the
733 * connection to the requested wiki. This reference count can be
734 * decremented by calling reuseConnection().
736 * If a connection is open to the appropriate server already, but with the wrong
737 * database, it will be switched to the right database and returned, as long as
738 * it has been freed first with reuseConnection().
740 * On error, returns false, and the connection which caused the
741 * error will be available via $this->mErrorConnection.
743 * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
745 * @param int $i Server index
746 * @param string $wiki Wiki ID to open
747 * @return DatabaseBase
749 private function openForeignConnection( $i, $wiki ) {
750 list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
751 if ( isset( $this->mConns
['foreignUsed'][$i][$wiki] ) ) {
752 // Reuse an already-used connection
753 $conn = $this->mConns
['foreignUsed'][$i][$wiki];
754 wfDebug( __METHOD__
. ": reusing connection $i/$wiki\n" );
755 } elseif ( isset( $this->mConns
['foreignFree'][$i][$wiki] ) ) {
756 // Reuse a free connection for the same wiki
757 $conn = $this->mConns
['foreignFree'][$i][$wiki];
758 unset( $this->mConns
['foreignFree'][$i][$wiki] );
759 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
760 wfDebug( __METHOD__
. ": reusing free connection $i/$wiki\n" );
761 } elseif ( !empty( $this->mConns
['foreignFree'][$i] ) ) {
762 // Reuse a connection from another wiki
763 $conn = reset( $this->mConns
['foreignFree'][$i] );
764 $oldWiki = key( $this->mConns
['foreignFree'][$i] );
766 // The empty string as a DB name means "don't care".
767 // DatabaseMysqlBase::open() already handle this on connection.
768 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
769 $this->mLastError
= "Error selecting database $dbName on server " .
770 $conn->getServer() . " from client host " . wfHostname() . "\n";
771 $this->mErrorConnection
= $conn;
774 $conn->tablePrefix( $prefix );
775 unset( $this->mConns
['foreignFree'][$i][$oldWiki] );
776 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
777 wfDebug( __METHOD__
. ": reusing free connection from $oldWiki for $wiki\n" );
780 // Open a new connection
781 $server = $this->mServers
[$i];
782 $server['serverIndex'] = $i;
783 $server['foreignPoolRefCount'] = 0;
784 $server['foreign'] = true;
785 $conn = $this->reallyOpenConnection( $server, $dbName );
786 if ( !$conn->isOpen() ) {
787 wfDebug( __METHOD__
. ": error opening connection for $i/$wiki\n" );
788 $this->mErrorConnection
= $conn;
791 $conn->tablePrefix( $prefix );
792 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
793 wfDebug( __METHOD__
. ": opened new connection for $i/$wiki\n" );
797 // Increment reference count
799 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
800 $conn->setLBInfo( 'foreignPoolRefCount', $refCount +
1 );
807 * Test if the specified index represents an open connection
809 * @param int $index Server index
813 private function isOpen( $index ) {
814 if ( !is_integer( $index ) ) {
818 return (bool)$this->getAnyOpenConnection( $index );
822 * Really opens a connection. Uncached.
823 * Returns a Database object whether or not the connection was successful.
826 * @param array $server
827 * @param bool $dbNameOverride
828 * @throws MWException
829 * @return DatabaseBase
831 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
832 if ( $this->disabled
) {
833 throw new DBAccessError();
836 if ( !is_array( $server ) ) {
837 throw new MWException( 'You must update your load-balancing configuration. ' .
838 'See DefaultSettings.php entry for $wgDBservers.' );
841 if ( $dbNameOverride !== false ) {
842 $server['dbname'] = $dbNameOverride;
845 // Let the handle know what the cluster master is (e.g. "db1052")
846 $masterName = $this->getServerName( 0 );
847 $server['clusterMasterHost'] = $masterName;
849 // Log when many connection are made on requests
850 if ( ++
$this->connsOpened
>= self
::CONN_HELD_WARN_THRESHOLD
) {
851 wfDebugLog( 'DBPerformance', __METHOD__
. ": " .
852 "{$this->connsOpened}+ connections made (master=$masterName)\n" .
853 wfBacktrace( true ) );
858 $db = DatabaseBase
::factory( $server['type'], $server );
859 } catch ( DBConnectionError
$e ) {
860 // FIXME: This is probably the ugliest thing I have ever done to
861 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
865 $db->setLBInfo( $server );
866 $db->setLazyMasterHandle(
867 $this->getLazyConnectionRef( DB_MASTER
, [], $db->getWikiID() )
869 $db->setTransactionProfiler( $this->trxProfiler
);
875 * @throws DBConnectionError
878 private function reportConnectionError() {
879 $conn = $this->mErrorConnection
; // The connection which caused the error
881 'method' => __METHOD__
,
882 'last_error' => $this->mLastError
,
885 if ( !is_object( $conn ) ) {
886 // No last connection, probably due to all servers being too busy
888 "LB failure with no last connection. Connection error: {last_error}",
892 // If all servers were busy, mLastError will contain something sensible
893 throw new DBConnectionError( null, $this->mLastError
);
895 $context['db_server'] = $conn->getProperty( 'mServer' );
897 "Connection error: {last_error} ({db_server})",
901 // throws DBConnectionError
902 $conn->reportConnectionError( "{$this->mLastError} ({$context['db_server']})" );
905 return false; /* not reached */
912 public function getWriterIndex() {
917 * Returns true if the specified index is a valid server index
922 public function haveIndex( $i ) {
923 return array_key_exists( $i, $this->mServers
);
927 * Returns true if the specified index is valid and has non-zero load
932 public function isNonZeroLoad( $i ) {
933 return array_key_exists( $i, $this->mServers
) && $this->mLoads
[$i] != 0;
937 * Get the number of defined servers (not the number of open connections)
941 public function getServerCount() {
942 return count( $this->mServers
);
946 * Get the host name or IP address of the server with the specified index
947 * Prefer a readable name if available.
951 public function getServerName( $i ) {
952 if ( isset( $this->mServers
[$i]['hostName'] ) ) {
953 $name = $this->mServers
[$i]['hostName'];
954 } elseif ( isset( $this->mServers
[$i]['host'] ) ) {
955 $name = $this->mServers
[$i]['host'];
960 return ( $name != '' ) ?
$name : 'localhost';
964 * Return the server info structure for a given index, or false if the index is invalid.
968 public function getServerInfo( $i ) {
969 if ( isset( $this->mServers
[$i] ) ) {
970 return $this->mServers
[$i];
977 * Sets the server info structure for the given index. Entry at index $i
978 * is created if it doesn't exist
980 * @param array $serverInfo
982 public function setServerInfo( $i, array $serverInfo ) {
983 $this->mServers
[$i] = $serverInfo;
987 * Get the current master position for chronology control purposes
990 public function getMasterPos() {
991 # If this entire request was served from a slave without opening a connection to the
992 # master (however unlikely that may be), then we can fetch the position from the slave.
993 $masterConn = $this->getAnyOpenConnection( 0 );
994 if ( !$masterConn ) {
995 $serverCount = count( $this->mServers
);
996 for ( $i = 1; $i < $serverCount; $i++
) {
997 $conn = $this->getAnyOpenConnection( $i );
999 return $conn->getSlavePos();
1003 return $masterConn->getMasterPos();
1010 * Disable this load balancer. All connections are closed, and any attempt to
1011 * open a new connection will result in a DBAccessError.
1015 public function disable() {
1017 $this->disabled
= true;
1021 * Close all open connections
1023 public function closeAll() {
1024 $this->forEachOpenConnection( function ( DatabaseBase
$conn ) {
1030 'foreignFree' => [],
1031 'foreignUsed' => [],
1033 $this->connsOpened
= 0;
1037 * Close a connection
1038 * Using this function makes sure the LoadBalancer knows the connection is closed.
1039 * If you use $conn->close() directly, the load balancer won't update its state.
1040 * @param DatabaseBase $conn
1042 public function closeConnection( $conn ) {
1044 foreach ( $this->mConns
as $i1 => $conns2 ) {
1045 foreach ( $conns2 as $i2 => $conns3 ) {
1046 foreach ( $conns3 as $i3 => $candidateConn ) {
1047 if ( $conn === $candidateConn ) {
1049 unset( $this->mConns
[$i1][$i2][$i3] );
1050 --$this->connsOpened
;
1063 * Commit transactions on all open connections
1064 * @param string $fname Caller name
1066 public function commitAll( $fname = __METHOD__
) {
1067 $this->forEachOpenConnection( function ( DatabaseBase
$conn ) use ( $fname ) {
1068 $conn->commit( $fname, 'flush' );
1073 * Perform all pre-commit callbacks that remain part of the atomic transactions
1074 * and disable any post-commit callbacks until runMasterPostCommitCallbacks()
1077 public function runMasterPreCommitCallbacks() {
1078 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) {
1079 // Any error will cause all DB transactions to be rolled back together.
1080 $conn->runOnTransactionPreCommitCallbacks();
1081 // Defer post-commit callbacks until COMMIT finishes for all DBs.
1082 $conn->setPostCommitCallbackSupression( true );
1087 * Perform all pre-commit checks for things like replication safety
1088 * @param array $options Includes:
1089 * - maxWriteDuration : max write query duration time in seconds
1090 * @throws DBTransactionError
1093 public function approveMasterChanges( array $options ) {
1094 $limit = isset( $options['maxWriteDuration'] ) ?
$options['maxWriteDuration'] : 0;
1095 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $limit ) {
1096 // Assert that the time to replicate the transaction will be sane.
1097 // If this fails, then all DB transactions will be rollback back together.
1098 $time = $conn->pendingWriteQueryDuration();
1099 if ( $limit > 0 && $time > $limit ) {
1100 throw new DBTransactionError(
1102 wfMessage( 'transaction-duration-limit-exceeded', $time, $limit )->text()
1109 * Issue COMMIT on all master connections where writes where done
1110 * @param string $fname Caller name
1112 public function commitMasterChanges( $fname = __METHOD__
) {
1113 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $fname ) {
1114 if ( $conn->writesOrCallbacksPending() ) {
1115 $conn->commit( $fname, 'flush' );
1121 * Issue all pending post-commit callbacks
1124 public function runMasterPostCommitCallbacks() {
1125 $this->forEachOpenMasterConnection( function ( DatabaseBase
$db ) {
1126 $db->setPostCommitCallbackSupression( false );
1127 $db->runOnTransactionIdleCallbacks( IDatabase
::TRIGGER_COMMIT
);
1132 * Issue ROLLBACK only on master, only if queries were done on connection
1133 * @param string $fname Caller name
1134 * @throws DBExpectedError
1137 public function rollbackMasterChanges( $fname = __METHOD__
) {
1138 $failedServers = [];
1140 $masterIndex = $this->getWriterIndex();
1141 foreach ( $this->mConns
as $conns2 ) {
1142 if ( empty( $conns2[$masterIndex] ) ) {
1145 /** @var DatabaseBase $conn */
1146 foreach ( $conns2[$masterIndex] as $conn ) {
1147 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1149 $conn->rollback( $fname, 'flush' );
1150 } catch ( DBError
$e ) {
1151 MWExceptionHandler
::logException( $e );
1152 $failedServers[] = $conn->getServer();
1158 if ( $failedServers ) {
1159 throw new DBExpectedError( null, "Rollback failed on server(s) " .
1160 implode( ', ', array_unique( $failedServers ) ) );
1165 * @return bool Whether a master connection is already open
1168 public function hasMasterConnection() {
1169 return $this->isOpen( $this->getWriterIndex() );
1173 * Determine if there are pending changes in a transaction by this thread
1177 public function hasMasterChanges() {
1178 $masterIndex = $this->getWriterIndex();
1179 foreach ( $this->mConns
as $conns2 ) {
1180 if ( empty( $conns2[$masterIndex] ) ) {
1183 /** @var DatabaseBase $conn */
1184 foreach ( $conns2[$masterIndex] as $conn ) {
1185 if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
1194 * Get the timestamp of the latest write query done by this thread
1196 * @return float|bool UNIX timestamp or false
1198 public function lastMasterChangeTimestamp() {
1200 $masterIndex = $this->getWriterIndex();
1201 foreach ( $this->mConns
as $conns2 ) {
1202 if ( empty( $conns2[$masterIndex] ) ) {
1205 /** @var DatabaseBase $conn */
1206 foreach ( $conns2[$masterIndex] as $conn ) {
1207 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1214 * Check if this load balancer object had any recent or still
1215 * pending writes issued against it by this PHP thread
1217 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1221 public function hasOrMadeRecentMasterChanges( $age = null ) {
1222 $age = ( $age === null ) ?
$this->mWaitTimeout
: $age;
1224 return ( $this->hasMasterChanges()
1225 ||
$this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1229 * Get the list of callers that have pending master changes
1234 public function pendingMasterChangeCallers() {
1237 $masterIndex = $this->getWriterIndex();
1238 foreach ( $this->mConns
as $conns2 ) {
1239 if ( empty( $conns2[$masterIndex] ) ) {
1242 /** @var DatabaseBase $conn */
1243 foreach ( $conns2[$masterIndex] as $conn ) {
1244 $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1252 * @param mixed $value
1255 public function waitTimeout( $value = null ) {
1256 return wfSetVar( $this->mWaitTimeout
, $value );
1260 * @note This method will trigger a DB connection if not yet done
1262 * @param string|bool $wiki Wiki ID, or false for the current wiki
1263 * @return bool Whether the generic connection for reads is highly "lagged"
1265 public function getLaggedSlaveMode( $wiki = false ) {
1266 // No-op if there is only one DB (also avoids recursion)
1267 if ( !$this->laggedSlaveMode
&& $this->getServerCount() > 1 ) {
1269 // See if laggedSlaveMode gets set
1270 $conn = $this->getConnection( DB_SLAVE
, false, $wiki );
1271 $this->reuseConnection( $conn );
1272 } catch ( DBConnectionError
$e ) {
1273 // Avoid expensive re-connect attempts and failures
1274 $this->slavesDownMode
= true;
1275 $this->laggedSlaveMode
= true;
1279 return $this->laggedSlaveMode
;
1283 * @note This method will never cause a new DB connection
1284 * @return bool Whether any generic connection used for reads was highly "lagged"
1287 public function laggedSlaveUsed() {
1288 return $this->laggedSlaveMode
;
1292 * @note This method may trigger a DB connection if not yet done
1293 * @param string|bool $wiki Wiki ID, or false for the current wiki
1294 * @param DatabaseBase|null DB master connection; used to avoid loops [optional]
1295 * @return string|bool Reason the master is read-only or false if it is not
1298 public function getReadOnlyReason( $wiki = false, DatabaseBase
$conn = null ) {
1299 if ( $this->readOnlyReason
!== false ) {
1300 return $this->readOnlyReason
;
1301 } elseif ( $this->getLaggedSlaveMode( $wiki ) ) {
1302 if ( $this->slavesDownMode
) {
1303 return 'The database has been automatically locked ' .
1304 'until the slave database servers become available';
1306 return 'The database has been automatically locked ' .
1307 'while the slave database servers catch up to the master.';
1309 } elseif ( $this->masterRunningReadOnly( $wiki, $conn ) ) {
1310 return 'The database master is running in read-only mode.';
1317 * @param string $wiki Wiki ID, or false for the current wiki
1318 * @param DatabaseBase|null DB master connectionl used to avoid loops [optional]
1321 private function masterRunningReadOnly( $wiki, DatabaseBase
$conn = null ) {
1322 $cache = $this->wanCache
;
1323 $masterServer = $this->getServerName( $this->getWriterIndex() );
1325 return (bool)$cache->getWithSetCallback(
1326 $cache->makeGlobalKey( __CLASS__
, 'server-read-only', $masterServer ),
1327 self
::TTL_CACHE_READONLY
,
1328 function () use ( $wiki, $conn ) {
1329 $this->trxProfiler
->setSilenced( true );
1331 $dbw = $conn ?
: $this->getConnection( DB_MASTER
, [], $wiki );
1332 $readOnly = (int)$dbw->serverIsReadOnly();
1333 } catch ( DBError
$e ) {
1336 $this->trxProfiler
->setSilenced( false );
1339 [ 'pcTTL' => $cache::TTL_PROC_LONG
, 'busyValue' => 0 ]
1344 * Disables/enables lag checks
1345 * @param null|bool $mode
1348 public function allowLagged( $mode = null ) {
1349 if ( $mode === null ) {
1350 return $this->mAllowLagged
;
1352 $this->mAllowLagged
= $mode;
1354 return $this->mAllowLagged
;
1360 public function pingAll() {
1362 $this->forEachOpenConnection( function ( DatabaseBase
$conn ) use ( &$success ) {
1363 if ( !$conn->ping() ) {
1372 * Call a function with each open connection object
1373 * @param callable $callback
1374 * @param array $params
1376 public function forEachOpenConnection( $callback, array $params = [] ) {
1377 foreach ( $this->mConns
as $connsByServer ) {
1378 foreach ( $connsByServer as $serverConns ) {
1379 foreach ( $serverConns as $conn ) {
1380 $mergedParams = array_merge( [ $conn ], $params );
1381 call_user_func_array( $callback, $mergedParams );
1388 * Call a function with each open connection object to a master
1389 * @param callable $callback
1390 * @param array $params
1393 public function forEachOpenMasterConnection( $callback, array $params = [] ) {
1394 $masterIndex = $this->getWriterIndex();
1395 foreach ( $this->mConns
as $connsByServer ) {
1396 if ( isset( $connsByServer[$masterIndex] ) ) {
1397 /** @var DatabaseBase $conn */
1398 foreach ( $connsByServer[$masterIndex] as $conn ) {
1399 $mergedParams = array_merge( [ $conn ], $params );
1400 call_user_func_array( $callback, $mergedParams );
1407 * Get the hostname and lag time of the most-lagged slave
1409 * This is useful for maintenance scripts that need to throttle their updates.
1410 * May attempt to open connections to slaves on the default DB. If there is
1411 * no lag, the maximum lag will be reported as -1.
1413 * @param bool|string $wiki Wiki ID, or false for the default database
1414 * @return array ( host, max lag, index of max lagged host )
1416 public function getMaxLag( $wiki = false ) {
1421 if ( $this->getServerCount() <= 1 ) {
1422 return [ $host, $maxLag, $maxIndex ]; // no replication = no lag
1425 $lagTimes = $this->getLagTimes( $wiki );
1426 foreach ( $lagTimes as $i => $lag ) {
1427 if ( $this->mLoads
[$i] > 0 && $lag > $maxLag ) {
1429 $host = $this->mServers
[$i]['host'];
1434 return [ $host, $maxLag, $maxIndex ];
1438 * Get an estimate of replication lag (in seconds) for each server
1440 * Results are cached for a short time in memcached/process cache
1442 * Values may be "false" if replication is too broken to estimate
1444 * @param string|bool $wiki
1445 * @return int[] Map of (server index => float|int|bool)
1447 public function getLagTimes( $wiki = false ) {
1448 if ( $this->getServerCount() <= 1 ) {
1449 return [ 0 => 0 ]; // no replication = no lag
1452 # Send the request to the load monitor
1453 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers
), $wiki );
1457 * Get the lag in seconds for a given connection, or zero if this load
1458 * balancer does not have replication enabled.
1460 * This should be used in preference to Database::getLag() in cases where
1461 * replication may not be in use, since there is no way to determine if
1462 * replication is in use at the connection level without running
1463 * potentially restricted queries such as SHOW SLAVE STATUS. Using this
1464 * function instead of Database::getLag() avoids a fatal error in this
1465 * case on many installations.
1467 * @param IDatabase $conn
1468 * @return int|bool Returns false on error
1470 public function safeGetLag( IDatabase
$conn ) {
1471 if ( $this->getServerCount() == 1 ) {
1474 return $conn->getLag();
1479 * Wait for a slave DB to reach a specified master position
1481 * This will connect to the master to get an accurate position if $pos is not given
1483 * @param IDatabase $conn Slave DB
1484 * @param DBMasterPos|bool $pos Master position; default: current position
1485 * @param integer $timeout Timeout in seconds
1486 * @return bool Success
1489 public function safeWaitForMasterPos( IDatabase
$conn, $pos = false, $timeout = 10 ) {
1490 if ( $this->getServerCount() == 1 ||
!$conn->getLBInfo( 'slave' ) ) {
1491 return true; // server is not a slave DB
1494 $pos = $pos ?
: $this->getConnection( DB_MASTER
)->getMasterPos();
1495 if ( !( $pos instanceof DBMasterPos
) ) {
1496 return false; // something is misconfigured
1499 $result = $conn->masterPosWait( $pos, $timeout );
1500 if ( $result == -1 ||
is_null( $result ) ) {
1501 $msg = __METHOD__
. ": Timed out waiting on {$conn->getServer()} pos {$pos}";
1502 wfDebugLog( 'replication', "$msg\n" );
1503 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
1506 wfDebugLog( 'replication', __METHOD__
. ": Done\n" );
1514 * Clear the cache for slag lag delay times
1516 * This is only used for testing
1518 public function clearLagTimeCache() {
1519 $this->getLoadMonitor()->clearCaches();