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 replica DB lag as a factor in replica DB selection */
40 private $mAllowLagged;
41 /** @var integer Seconds to spend waiting on replica DB 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 */
54 /** @var TransactionProfiler */
55 protected $trxProfiler;
57 /** @var bool|DatabaseBase Database connection that caused a problem */
58 private $mErrorConnection;
59 /** @var integer The generic (not query grouped) replica DB index (of $mServers) */
61 /** @var bool|DBMasterPos False if not set */
63 /** @var bool Whether the generic reader fell back to a lagged replica DB */
64 private $laggedReplicaMode = false;
65 /** @var bool Whether the generic reader fell back to a lagged replica DB */
66 private $allReplicasDownMode = false;
67 /** @var string The last DB selection or connection error */
68 private $mLastError = 'Unknown error';
69 /** @var string|bool Reason the LB is read-only or false if not */
70 private $readOnlyReason = false;
71 /** @var integer Total connections opened */
72 private $connsOpened = 0;
73 /** @var string|bool String if a requested DBO_TRX transaction round is active */
74 private $trxRoundId = false;
75 /** @var array[] Map of (name => callable) */
76 private $trxRecurringCallbacks = [];
78 /** @var integer Warn when this many connection are held */
79 const CONN_HELD_WARN_THRESHOLD
= 10;
80 /** @var integer Default 'max lag' when unspecified */
81 const MAX_LAG_DEFAULT
= 10;
82 /** @var integer Max time to wait for a replica DB to catch up (e.g. ChronologyProtector) */
83 const POS_WAIT_TIMEOUT
= 10;
84 /** @var integer Seconds to cache master server read-only status */
85 const TTL_CACHE_READONLY
= 5;
90 private $disabled = false;
93 * @param array $params Array with keys:
94 * - servers : Required. Array of server info structures.
95 * - loadMonitor : Name of a class used to fetch server lag and load.
96 * - readOnlyReason : Reason the master DB is read-only if so [optional]
97 * - waitTimeout : Maximum time to wait for replicas for consistency [optional]
98 * - srvCache : BagOStuff object [optional]
99 * - wanCache : WANObjectCache object [optional]
100 * @throws MWException
102 public function __construct( array $params ) {
103 if ( !isset( $params['servers'] ) ) {
104 throw new MWException( __CLASS__
. ': missing servers parameter' );
106 $this->mServers
= $params['servers'];
107 $this->mWaitTimeout
= isset( $params['waitTimeout'] )
108 ?
$params['waitTimeout']
109 : self
::POS_WAIT_TIMEOUT
;
111 $this->mReadIndex
= -1;
112 $this->mWriteIndex
= -1;
116 'foreignFree' => [] ];
118 $this->mWaitForPos
= false;
119 $this->mErrorConnection
= false;
120 $this->mAllowLagged
= false;
122 if ( isset( $params['readOnlyReason'] ) && is_string( $params['readOnlyReason'] ) ) {
123 $this->readOnlyReason
= $params['readOnlyReason'];
126 if ( isset( $params['loadMonitor'] ) ) {
127 $this->mLoadMonitorClass
= $params['loadMonitor'];
129 $master = reset( $params['servers'] );
130 if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
131 $this->mLoadMonitorClass
= 'LoadMonitorMySQL';
133 $this->mLoadMonitorClass
= 'LoadMonitorNull';
137 foreach ( $params['servers'] as $i => $server ) {
138 $this->mLoads
[$i] = $server['load'];
139 if ( isset( $server['groupLoads'] ) ) {
140 foreach ( $server['groupLoads'] as $group => $ratio ) {
141 if ( !isset( $this->mGroupLoads
[$group] ) ) {
142 $this->mGroupLoads
[$group] = [];
144 $this->mGroupLoads
[$group][$i] = $ratio;
149 if ( isset( $params['srvCache'] ) ) {
150 $this->srvCache
= $params['srvCache'];
152 $this->srvCache
= new EmptyBagOStuff();
154 if ( isset( $params['wanCache'] ) ) {
155 $this->wanCache
= $params['wanCache'];
157 $this->wanCache
= WANObjectCache
::newEmpty();
159 if ( isset( $params['trxProfiler'] ) ) {
160 $this->trxProfiler
= $params['trxProfiler'];
162 $this->trxProfiler
= new TransactionProfiler();
167 * Get a LoadMonitor instance
169 * @return LoadMonitor
171 private function getLoadMonitor() {
172 if ( !isset( $this->mLoadMonitor
) ) {
173 $class = $this->mLoadMonitorClass
;
174 $this->mLoadMonitor
= new $class( $this );
177 return $this->mLoadMonitor
;
181 * @param array $loads
182 * @param bool|string $wiki Wiki to get non-lagged for
183 * @param int $maxLag Restrict the maximum allowed lag to this many seconds
184 * @return bool|int|string
186 private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = INF
) {
187 $lags = $this->getLagTimes( $wiki );
189 # Unset excessively lagged servers
190 foreach ( $lags as $i => $lag ) {
192 # How much lag this server nominally is allowed to have
193 $maxServerLag = isset( $this->mServers
[$i]['max lag'] )
194 ?
$this->mServers
[$i]['max lag']
195 : self
::MAX_LAG_DEFAULT
; // default
196 # Constrain that futher by $maxLag argument
197 $maxServerLag = min( $maxServerLag, $maxLag );
199 $host = $this->getServerName( $i );
200 if ( $lag === false && !is_infinite( $maxServerLag ) ) {
201 wfDebugLog( 'replication', "Server $host (#$i) is not replicating?" );
203 } elseif ( $lag > $maxServerLag ) {
204 wfDebugLog( 'replication', "Server $host (#$i) has >= $lag seconds of lag" );
210 # Find out if all the replica DBs with non-zero load are lagged
212 foreach ( $loads as $load ) {
216 # No appropriate DB servers except maybe the master and some replica DBs with zero load
217 # Do NOT use the master
218 # Instead, this function will return false, triggering read-only mode,
219 # and a lagged replica DB will be used instead.
223 if ( count( $loads ) == 0 ) {
227 # Return a random representative of the remainder
228 return ArrayUtils
::pickRandom( $loads );
232 * Get the index of the reader connection, which may be a replica DB
233 * This takes into account load ratios and lag times. It should
234 * always return a consistent index during a given invocation
236 * Side effect: opens connections to databases
237 * @param string|bool $group Query group, or false for the generic reader
238 * @param string|bool $wiki Wiki ID, or false for the current wiki
239 * @throws MWException
240 * @return bool|int|string
242 public function getReaderIndex( $group = false, $wiki = false ) {
245 # @todo FIXME: For now, only go through all this for mysql databases
246 if ( $wgDBtype != 'mysql' ) {
247 return $this->getWriterIndex();
250 if ( count( $this->mServers
) == 1 ) {
251 # Skip the load balancing if there's only one server
253 } elseif ( $group === false && $this->mReadIndex
>= 0 ) {
254 # Shortcut if generic reader exists already
255 return $this->mReadIndex
;
258 # Find the relevant load array
259 if ( $group !== false ) {
260 if ( isset( $this->mGroupLoads
[$group] ) ) {
261 $nonErrorLoads = $this->mGroupLoads
[$group];
263 # No loads for this group, return false and the caller can use some other group
264 wfDebugLog( 'connect', __METHOD__
. ": no loads for group $group\n" );
269 $nonErrorLoads = $this->mLoads
;
272 if ( !count( $nonErrorLoads ) ) {
273 throw new MWException( "Empty server array given to LoadBalancer" );
276 # Scale the configured load ratios according to the dynamic load (if the load monitor supports it)
277 $this->getLoadMonitor()->scaleLoads( $nonErrorLoads, $group, $wiki );
279 $laggedReplicaMode = false;
281 # No server found yet
283 # First try quickly looking through the available servers for a server that
285 $currentLoads = $nonErrorLoads;
286 while ( count( $currentLoads ) ) {
287 if ( $this->mAllowLagged ||
$laggedReplicaMode ) {
288 $i = ArrayUtils
::pickRandom( $currentLoads );
291 if ( $this->mWaitForPos
&& $this->mWaitForPos
->asOfTime() ) {
292 # ChronologyProtecter causes mWaitForPos to be set via sessions.
293 # This triggers doWait() after connect, so it's especially good to
294 # avoid lagged servers so as to avoid just blocking in that method.
295 $ago = microtime( true ) - $this->mWaitForPos
->asOfTime();
296 # Aim for <= 1 second of waiting (being too picky can backfire)
297 $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago +
1 );
299 if ( $i === false ) {
300 # Any server with less lag than it's 'max lag' param is preferable
301 $i = $this->getRandomNonLagged( $currentLoads, $wiki );
303 if ( $i === false && count( $currentLoads ) != 0 ) {
304 # All replica DBs lagged. Switch to read-only mode
305 wfDebugLog( 'replication', "All replica DBs lagged. Switch to read-only mode" );
306 $i = ArrayUtils
::pickRandom( $currentLoads );
307 $laggedReplicaMode = true;
311 if ( $i === false ) {
312 # pickRandom() returned false
313 # This is permanent and means the configuration or the load monitor
314 # wants us to return false.
315 wfDebugLog( 'connect', __METHOD__
. ": pickRandom() returned false" );
320 $serverName = $this->getServerName( $i );
321 wfDebugLog( 'connect', __METHOD__
. ": Using reader #$i: $serverName..." );
323 $conn = $this->openConnection( $i, $wiki );
325 wfDebugLog( 'connect', __METHOD__
. ": Failed connecting to $i/$wiki" );
326 unset( $nonErrorLoads[$i] );
327 unset( $currentLoads[$i] );
332 // Decrement reference counter, we are finished with this connection.
333 // It will be incremented for the caller later.
334 if ( $wiki !== false ) {
335 $this->reuseConnection( $conn );
342 # If all servers were down, quit now
343 if ( !count( $nonErrorLoads ) ) {
344 wfDebugLog( 'connect', "All servers down" );
347 if ( $i !== false ) {
348 # Replica DB connection successful.
349 # Wait for the session master pos for a short time.
350 if ( $this->mWaitForPos
&& $i > 0 ) {
353 if ( $this->mReadIndex
<= 0 && $this->mLoads
[$i] > 0 && $group === false ) {
354 $this->mReadIndex
= $i;
355 # Record if the generic reader index is in "lagged replica DB" mode
356 if ( $laggedReplicaMode ) {
357 $this->laggedReplicaMode
= true;
360 $serverName = $this->getServerName( $i );
361 wfDebugLog( 'connect', __METHOD__
.
362 ": using server $serverName for group '$group'\n" );
369 * Set the master wait position
370 * If a DB_REPLICA connection has been opened already, waits
371 * Otherwise sets a variable telling it to wait if such a connection is opened
372 * @param DBMasterPos $pos
374 public function waitFor( $pos ) {
375 $this->mWaitForPos
= $pos;
376 $i = $this->mReadIndex
;
379 if ( !$this->doWait( $i ) ) {
380 $this->laggedReplicaMode
= true;
386 * Set the master wait position and wait for a "generic" replica DB to catch up to it
388 * This can be used a faster proxy for waitForAll()
390 * @param DBMasterPos $pos
391 * @param int $timeout Max seconds to wait; default is mWaitTimeout
392 * @return bool Success (able to connect and no timeouts reached)
395 public function waitForOne( $pos, $timeout = null ) {
396 $this->mWaitForPos
= $pos;
398 $i = $this->mReadIndex
;
400 // Pick a generic replica DB if there isn't one yet
401 $readLoads = $this->mLoads
;
402 unset( $readLoads[$this->getWriterIndex()] ); // replica DBs only
403 $readLoads = array_filter( $readLoads ); // with non-zero load
404 $i = ArrayUtils
::pickRandom( $readLoads );
408 $ok = $this->doWait( $i, true, $timeout );
410 $ok = true; // no applicable loads
417 * Set the master wait position and wait for ALL replica DBs to catch up to it
418 * @param DBMasterPos $pos
419 * @param int $timeout Max seconds to wait; default is mWaitTimeout
420 * @return bool Success (able to connect and no timeouts reached)
422 public function waitForAll( $pos, $timeout = null ) {
423 $this->mWaitForPos
= $pos;
424 $serverCount = count( $this->mServers
);
427 for ( $i = 1; $i < $serverCount; $i++
) {
428 if ( $this->mLoads
[$i] > 0 ) {
429 $ok = $this->doWait( $i, true, $timeout ) && $ok;
437 * Get any open connection to a given server index, local or foreign
438 * Returns false if there is no connection open
440 * @param int $i Server index
441 * @return DatabaseBase|bool False on failure
443 public function getAnyOpenConnection( $i ) {
444 foreach ( $this->mConns
as $connsByServer ) {
445 if ( !empty( $connsByServer[$i] ) ) {
446 return reset( $connsByServer[$i] );
454 * Wait for a given replica DB to catch up to the master pos stored in $this
455 * @param int $index Server index
456 * @param bool $open Check the server even if a new connection has to be made
457 * @param int $timeout Max seconds to wait; default is mWaitTimeout
460 protected function doWait( $index, $open = false, $timeout = null ) {
461 $close = false; // close the connection afterwards
463 // Check if we already know that the DB has reached this point
464 $server = $this->getServerName( $index );
465 $key = $this->srvCache
->makeGlobalKey( __CLASS__
, 'last-known-pos', $server );
466 /** @var DBMasterPos $knownReachedPos */
467 $knownReachedPos = $this->srvCache
->get( $key );
468 if ( $knownReachedPos && $knownReachedPos->hasReached( $this->mWaitForPos
) ) {
469 wfDebugLog( 'replication', __METHOD__
.
470 ": replica DB $server known to be caught up (pos >= $knownReachedPos).\n" );
474 // Find a connection to wait on, creating one if needed and allowed
475 $conn = $this->getAnyOpenConnection( $index );
478 wfDebugLog( 'replication', __METHOD__
. ": no connection open for $server\n" );
482 $conn = $this->openConnection( $index, '' );
484 wfDebugLog( 'replication', __METHOD__
. ": failed to connect to $server\n" );
488 // Avoid connection spam in waitForAll() when connections
489 // are made just for the sake of doing this lag check.
494 wfDebugLog( 'replication', __METHOD__
. ": Waiting for replica DB $server to catch up...\n" );
495 $timeout = $timeout ?
: $this->mWaitTimeout
;
496 $result = $conn->masterPosWait( $this->mWaitForPos
, $timeout );
498 if ( $result == -1 ||
is_null( $result ) ) {
499 // Timed out waiting for replica DB, use master instead
500 $msg = __METHOD__
. ": Timed out waiting on $server pos {$this->mWaitForPos}";
501 wfDebugLog( 'replication', "$msg\n" );
502 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
505 wfDebugLog( 'replication', __METHOD__
. ": Done\n" );
507 // Remember that the DB reached this point
508 $this->srvCache
->set( $key, $this->mWaitForPos
, BagOStuff
::TTL_DAY
);
512 $this->closeConnection( $conn );
519 * Get a connection by index
520 * This is the main entry point for this class.
522 * @param int $i Server index
523 * @param array|string|bool $groups Query group(s), or false for the generic reader
524 * @param string|bool $wiki Wiki ID, or false for the current wiki
526 * @throws MWException
527 * @return DatabaseBase
529 public function getConnection( $i, $groups = [], $wiki = false ) {
530 if ( $i === null ||
$i === false ) {
531 throw new MWException( 'Attempt to call ' . __METHOD__
.
532 ' with invalid server index' );
535 if ( $wiki === wfWikiID() ) {
539 $groups = ( $groups === false ||
$groups === [] )
540 ?
[ false ] // check one "group": the generic pool
543 $masterOnly = ( $i == DB_MASTER ||
$i == $this->getWriterIndex() );
544 $oldConnsOpened = $this->connsOpened
; // connections open now
546 if ( $i == DB_MASTER
) {
547 $i = $this->getWriterIndex();
549 # Try to find an available server in any the query groups (in order)
550 foreach ( $groups as $group ) {
551 $groupIndex = $this->getReaderIndex( $group, $wiki );
552 if ( $groupIndex !== false ) {
559 # Operation-based index
560 if ( $i == DB_REPLICA
) {
561 $this->mLastError
= 'Unknown error'; // reset error string
562 # Try the general server pool if $groups are unavailable.
563 $i = in_array( false, $groups, true )
564 ?
false // don't bother with this if that is what was tried above
565 : $this->getReaderIndex( false, $wiki );
566 # Couldn't find a working server in getReaderIndex()?
567 if ( $i === false ) {
568 $this->mLastError
= 'No working replica DB server: ' . $this->mLastError
;
570 return $this->reportConnectionError();
574 # Now we have an explicit index into the servers array
575 $conn = $this->openConnection( $i, $wiki );
577 return $this->reportConnectionError();
580 # Profile any new connections that happen
581 if ( $this->connsOpened
> $oldConnsOpened ) {
582 $host = $conn->getServer();
583 $dbname = $conn->getDBname();
584 $trxProf = Profiler
::instance()->getTransactionProfiler();
585 $trxProf->recordConnection( $host, $dbname, $masterOnly );
589 # Make master-requested DB handles inherit any read-only mode setting
590 $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $wiki, $conn ) );
597 * Mark a foreign connection as being available for reuse under a different
598 * DB name or prefix. This mechanism is reference-counted, and must be called
599 * the same number of times as getConnection() to work.
601 * @param DatabaseBase $conn
602 * @throws MWException
604 public function reuseConnection( $conn ) {
605 $serverIndex = $conn->getLBInfo( 'serverIndex' );
606 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
607 if ( $serverIndex === null ||
$refCount === null ) {
609 * This can happen in code like:
610 * foreach ( $dbs as $db ) {
611 * $conn = $lb->getConnection( DB_REPLICA, [], $db );
613 * $lb->reuseConnection( $conn );
615 * When a connection to the local DB is opened in this way, reuseConnection()
621 $dbName = $conn->getDBname();
622 $prefix = $conn->tablePrefix();
623 if ( strval( $prefix ) !== '' ) {
624 $wiki = "$dbName-$prefix";
628 if ( $this->mConns
['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
629 throw new MWException( __METHOD__
. ": connection not found, has " .
630 "the connection been freed already?" );
632 $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
633 if ( $refCount <= 0 ) {
634 $this->mConns
['foreignFree'][$serverIndex][$wiki] = $conn;
635 unset( $this->mConns
['foreignUsed'][$serverIndex][$wiki] );
636 wfDebug( __METHOD__
. ": freed connection $serverIndex/$wiki\n" );
638 wfDebug( __METHOD__
. ": reference count for $serverIndex/$wiki reduced to $refCount\n" );
643 * Get a database connection handle reference
645 * The handle's methods wrap simply wrap those of a DatabaseBase handle
647 * @see LoadBalancer::getConnection() for parameter information
650 * @param array|string|bool $groups Query group(s), or false for the generic reader
651 * @param string|bool $wiki Wiki ID, or false for the current wiki
654 public function getConnectionRef( $db, $groups = [], $wiki = false ) {
655 return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
659 * Get a database connection handle reference without connecting yet
661 * The handle's methods wrap simply wrap those of a DatabaseBase handle
663 * @see LoadBalancer::getConnection() for parameter information
666 * @param array|string|bool $groups Query group(s), or false for the generic reader
667 * @param string|bool $wiki Wiki ID, or false for the current wiki
670 public function getLazyConnectionRef( $db, $groups = [], $wiki = false ) {
671 return new DBConnRef( $this, [ $db, $groups, $wiki ] );
675 * Open a connection to the server given by the specified index
676 * Index must be an actual index into the array.
677 * If the server is already open, returns it.
679 * On error, returns false, and the connection which caused the
680 * error will be available via $this->mErrorConnection.
682 * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
684 * @param int $i Server index
685 * @param string|bool $wiki Wiki ID, or false for the current wiki
686 * @return DatabaseBase|bool Returns false on errors
688 public function openConnection( $i, $wiki = false ) {
689 if ( $wiki !== false ) {
690 $conn = $this->openForeignConnection( $i, $wiki );
691 } elseif ( isset( $this->mConns
['local'][$i][0] ) ) {
692 $conn = $this->mConns
['local'][$i][0];
694 $server = $this->mServers
[$i];
695 $server['serverIndex'] = $i;
696 $conn = $this->reallyOpenConnection( $server, false );
697 $serverName = $this->getServerName( $i );
698 if ( $conn->isOpen() ) {
699 wfDebugLog( 'connect', "Connected to database $i at $serverName\n" );
700 $this->mConns
['local'][$i][0] = $conn;
702 wfDebugLog( 'connect', "Failed to connect to database $i at $serverName\n" );
703 $this->mErrorConnection
= $conn;
708 if ( $conn && !$conn->isOpen() ) {
709 // Connection was made but later unrecoverably lost for some reason.
710 // Do not return a handle that will just throw exceptions on use,
711 // but let the calling code (e.g. getReaderIndex) try another server.
712 // See DatabaseMyslBase::ping() for how this can happen.
713 $this->mErrorConnection
= $conn;
721 * Open a connection to a foreign DB, or return one if it is already open.
723 * Increments a reference count on the returned connection which locks the
724 * connection to the requested wiki. This reference count can be
725 * decremented by calling reuseConnection().
727 * If a connection is open to the appropriate server already, but with the wrong
728 * database, it will be switched to the right database and returned, as long as
729 * it has been freed first with reuseConnection().
731 * On error, returns false, and the connection which caused the
732 * error will be available via $this->mErrorConnection.
734 * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
736 * @param int $i Server index
737 * @param string $wiki Wiki ID to open
738 * @return DatabaseBase
740 private function openForeignConnection( $i, $wiki ) {
741 list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
742 if ( isset( $this->mConns
['foreignUsed'][$i][$wiki] ) ) {
743 // Reuse an already-used connection
744 $conn = $this->mConns
['foreignUsed'][$i][$wiki];
745 wfDebug( __METHOD__
. ": reusing connection $i/$wiki\n" );
746 } elseif ( isset( $this->mConns
['foreignFree'][$i][$wiki] ) ) {
747 // Reuse a free connection for the same wiki
748 $conn = $this->mConns
['foreignFree'][$i][$wiki];
749 unset( $this->mConns
['foreignFree'][$i][$wiki] );
750 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
751 wfDebug( __METHOD__
. ": reusing free connection $i/$wiki\n" );
752 } elseif ( !empty( $this->mConns
['foreignFree'][$i] ) ) {
753 // Reuse a connection from another wiki
754 $conn = reset( $this->mConns
['foreignFree'][$i] );
755 $oldWiki = key( $this->mConns
['foreignFree'][$i] );
757 // The empty string as a DB name means "don't care".
758 // DatabaseMysqlBase::open() already handle this on connection.
759 if ( $dbName !== '' && !$conn->selectDB( $dbName ) ) {
760 $this->mLastError
= "Error selecting database $dbName on server " .
761 $conn->getServer() . " from client host " . wfHostname() . "\n";
762 $this->mErrorConnection
= $conn;
765 $conn->tablePrefix( $prefix );
766 unset( $this->mConns
['foreignFree'][$i][$oldWiki] );
767 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
768 wfDebug( __METHOD__
. ": reusing free connection from $oldWiki for $wiki\n" );
771 // Open a new connection
772 $server = $this->mServers
[$i];
773 $server['serverIndex'] = $i;
774 $server['foreignPoolRefCount'] = 0;
775 $server['foreign'] = true;
776 $conn = $this->reallyOpenConnection( $server, $dbName );
777 if ( !$conn->isOpen() ) {
778 wfDebug( __METHOD__
. ": error opening connection for $i/$wiki\n" );
779 $this->mErrorConnection
= $conn;
782 $conn->tablePrefix( $prefix );
783 $this->mConns
['foreignUsed'][$i][$wiki] = $conn;
784 wfDebug( __METHOD__
. ": opened new connection for $i/$wiki\n" );
788 // Increment reference count
790 $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
791 $conn->setLBInfo( 'foreignPoolRefCount', $refCount +
1 );
798 * Test if the specified index represents an open connection
800 * @param int $index Server index
804 private function isOpen( $index ) {
805 if ( !is_integer( $index ) ) {
809 return (bool)$this->getAnyOpenConnection( $index );
813 * Really opens a connection. Uncached.
814 * Returns a Database object whether or not the connection was successful.
817 * @param array $server
818 * @param bool $dbNameOverride
819 * @throws MWException
820 * @return DatabaseBase
822 protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
823 if ( $this->disabled
) {
824 throw new DBAccessError();
827 if ( !is_array( $server ) ) {
828 throw new MWException( 'You must update your load-balancing configuration. ' .
829 'See DefaultSettings.php entry for $wgDBservers.' );
832 if ( $dbNameOverride !== false ) {
833 $server['dbname'] = $dbNameOverride;
836 // Let the handle know what the cluster master is (e.g. "db1052")
837 $masterName = $this->getServerName( $this->getWriterIndex() );
838 $server['clusterMasterHost'] = $masterName;
840 // Log when many connection are made on requests
841 if ( ++
$this->connsOpened
>= self
::CONN_HELD_WARN_THRESHOLD
) {
842 wfDebugLog( 'DBPerformance', __METHOD__
. ": " .
843 "{$this->connsOpened}+ connections made (master=$masterName)\n" .
844 wfBacktrace( true ) );
849 $db = DatabaseBase
::factory( $server['type'], $server );
850 } catch ( DBConnectionError
$e ) {
851 // FIXME: This is probably the ugliest thing I have ever done to
852 // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
856 $db->setLBInfo( $server );
857 $db->setLazyMasterHandle(
858 $this->getLazyConnectionRef( DB_MASTER
, [], $db->getWikiID() )
860 $db->setTransactionProfiler( $this->trxProfiler
);
862 if ( $server['serverIndex'] === $this->getWriterIndex() ) {
863 if ( $this->trxRoundId
!== false ) {
864 $this->applyTransactionRoundFlags( $db );
866 foreach ( $this->trxRecurringCallbacks
as $name => $callback ) {
867 $db->setTransactionListener( $name, $callback );
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
988 * @return DBMasterPos|bool Returns false if not applicable
990 public function getMasterPos() {
991 # If this entire request was served from a replica DB without opening a connection to the
992 # master (however unlikely that may be), then we can fetch the position from the replica DB.
993 $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
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
1039 * Using this function makes sure the LoadBalancer knows the connection is closed.
1040 * If you use $conn->close() directly, the load balancer won't update its state.
1042 * @param DatabaseBase $conn
1044 public function closeConnection( DatabaseBase
$conn ) {
1045 $serverIndex = $conn->getLBInfo( 'serverIndex' ); // second index level of mConns
1046 foreach ( $this->mConns
as $type => $connsByServer ) {
1047 if ( !isset( $connsByServer[$serverIndex] ) ) {
1051 foreach ( $connsByServer[$serverIndex] as $i => $trackedConn ) {
1052 if ( $conn === $trackedConn ) {
1053 unset( $this->mConns
[$type][$serverIndex][$i] );
1054 --$this->connsOpened
;
1064 * Commit transactions on all open connections
1065 * @param string $fname Caller name
1066 * @throws DBExpectedError
1068 public function commitAll( $fname = __METHOD__
) {
1071 $restore = ( $this->trxRoundId
!== false );
1072 $this->trxRoundId
= false;
1073 $this->forEachOpenConnection(
1074 function ( DatabaseBase
$conn ) use ( $fname, $restore, &$failures ) {
1076 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS
);
1077 } catch ( DBError
$e ) {
1078 MWExceptionHandler
::logException( $e );
1079 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1081 if ( $restore && $conn->getLBInfo( 'master' ) ) {
1082 $this->undoTransactionRoundFlags( $conn );
1088 throw new DBExpectedError(
1090 "Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
1096 * Perform all pre-commit callbacks that remain part of the atomic transactions
1097 * and disable any post-commit callbacks until runMasterPostTrxCallbacks()
1100 public function finalizeMasterChanges() {
1101 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) {
1102 // Any error should cause all DB transactions to be rolled back together
1103 $conn->setTrxEndCallbackSuppression( false );
1104 $conn->runOnTransactionPreCommitCallbacks();
1105 // Defer post-commit callbacks until COMMIT finishes for all DBs
1106 $conn->setTrxEndCallbackSuppression( true );
1111 * Perform all pre-commit checks for things like replication safety
1112 * @param array $options Includes:
1113 * - maxWriteDuration : max write query duration time in seconds
1114 * @throws DBTransactionError
1117 public function approveMasterChanges( array $options ) {
1118 $limit = isset( $options['maxWriteDuration'] ) ?
$options['maxWriteDuration'] : 0;
1119 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $limit ) {
1120 // If atomic sections or explicit transactions are still open, some caller must have
1121 // caught an exception but failed to properly rollback any changes. Detect that and
1122 // throw and error (causing rollback).
1123 if ( $conn->explicitTrxActive() ) {
1124 throw new DBTransactionError(
1126 "Explicit transaction still active. A caller may have caught an error."
1129 // Assert that the time to replicate the transaction will be sane.
1130 // If this fails, then all DB transactions will be rollback back together.
1131 $time = $conn->pendingWriteQueryDuration( $conn::ESTIMATE_DB_APPLY
);
1132 if ( $limit > 0 && $time > $limit ) {
1133 throw new DBTransactionError(
1135 wfMessage( 'transaction-duration-limit-exceeded', $time, $limit )->text()
1138 // If a connection sits idle while slow queries execute on another, that connection
1139 // may end up dropped before the commit round is reached. Ping servers to detect this.
1140 if ( $conn->writesOrCallbacksPending() && !$conn->ping() ) {
1141 throw new DBTransactionError(
1143 "A connection to the {$conn->getDBname()} database was lost before commit."
1150 * Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)
1152 * The DBO_TRX setting will be reverted to the default in each of these methods:
1153 * - commitMasterChanges()
1154 * - rollbackMasterChanges()
1156 * This allows for custom transaction rounds from any outer transaction scope.
1158 * @param string $fname
1159 * @throws DBExpectedError
1162 public function beginMasterChanges( $fname = __METHOD__
) {
1163 if ( $this->trxRoundId
!== false ) {
1164 throw new DBTransactionError(
1166 "$fname: Transaction round '{$this->trxRoundId}' already started."
1169 $this->trxRoundId
= $fname;
1172 $this->forEachOpenMasterConnection(
1173 function ( DatabaseBase
$conn ) use ( $fname, &$failures ) {
1174 $conn->setTrxEndCallbackSuppression( true );
1176 $conn->flushSnapshot( $fname );
1177 } catch ( DBError
$e ) {
1178 MWExceptionHandler
::logException( $e );
1179 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1181 $conn->setTrxEndCallbackSuppression( false );
1182 $this->applyTransactionRoundFlags( $conn );
1187 throw new DBExpectedError(
1189 "$fname: Flush failed on server(s) " . implode( "\n", array_unique( $failures ) )
1195 * Issue COMMIT on all master connections where writes where done
1196 * @param string $fname Caller name
1197 * @throws DBExpectedError
1199 public function commitMasterChanges( $fname = __METHOD__
) {
1202 $restore = ( $this->trxRoundId
!== false );
1203 $this->trxRoundId
= false;
1204 $this->forEachOpenMasterConnection(
1205 function ( DatabaseBase
$conn ) use ( $fname, $restore, &$failures ) {
1207 if ( $conn->writesOrCallbacksPending() ) {
1208 $conn->commit( $fname, $conn::FLUSHING_ALL_PEERS
);
1209 } elseif ( $restore ) {
1210 $conn->flushSnapshot( $fname );
1212 } catch ( DBError
$e ) {
1213 MWExceptionHandler
::logException( $e );
1214 $failures[] = "{$conn->getServer()}: {$e->getMessage()}";
1217 $this->undoTransactionRoundFlags( $conn );
1223 throw new DBExpectedError(
1225 "$fname: Commit failed on server(s) " . implode( "\n", array_unique( $failures ) )
1231 * Issue all pending post-COMMIT/ROLLBACK callbacks
1232 * @param integer $type IDatabase::TRIGGER_* constant
1233 * @return Exception|null The first exception or null if there were none
1236 public function runMasterPostTrxCallbacks( $type ) {
1237 $e = null; // first exception
1238 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( $type, &$e ) {
1239 $conn->setTrxEndCallbackSuppression( false );
1240 if ( $conn->writesOrCallbacksPending() ) {
1241 // This happens if onTransactionIdle() callbacks leave callbacks on *another* DB
1242 // (which finished its callbacks already). Warn and recover in this case. Let the
1243 // callbacks run in the final commitMasterChanges() in LBFactory::shutdown().
1244 wfWarn( __METHOD__
. ": did not expect writes/callbacks pending." );
1246 } elseif ( $conn->trxLevel() ) {
1247 // This happens for single-DB setups where DB_REPLICA uses the master DB,
1248 // thus leaving an implicit read-only transaction open at this point. It
1249 // also happens if onTransactionIdle() callbacks leave implicit transactions
1250 // open on *other* DBs (which is slightly improper). Let these COMMIT on the
1251 // next call to commitMasterChanges(), possibly in LBFactory::shutdown().
1255 $conn->runOnTransactionIdleCallbacks( $type );
1256 } catch ( Exception
$ex ) {
1260 $conn->runTransactionListenerCallbacks( $type );
1261 } catch ( Exception
$ex ) {
1270 * Issue ROLLBACK only on master, only if queries were done on connection
1271 * @param string $fname Caller name
1272 * @throws DBExpectedError
1275 public function rollbackMasterChanges( $fname = __METHOD__
) {
1276 $restore = ( $this->trxRoundId
!== false );
1277 $this->trxRoundId
= false;
1278 $this->forEachOpenMasterConnection(
1279 function ( DatabaseBase
$conn ) use ( $fname, $restore ) {
1280 if ( $conn->writesOrCallbacksPending() ) {
1281 $conn->rollback( $fname, $conn::FLUSHING_ALL_PEERS
);
1284 $this->undoTransactionRoundFlags( $conn );
1291 * Suppress all pending post-COMMIT/ROLLBACK callbacks
1292 * @return Exception|null The first exception or null if there were none
1295 public function suppressTransactionEndCallbacks() {
1296 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) {
1297 $conn->setTrxEndCallbackSuppression( true );
1302 * @param DatabaseBase $conn
1304 private function applyTransactionRoundFlags( DatabaseBase
$conn ) {
1305 if ( $conn->getFlag( DBO_DEFAULT
) ) {
1306 // DBO_TRX is controlled entirely by CLI mode presence with DBO_DEFAULT.
1307 // Force DBO_TRX even in CLI mode since a commit round is expected soon.
1308 $conn->setFlag( DBO_TRX
, $conn::REMEMBER_PRIOR
);
1309 // If config has explicitly requested DBO_TRX be either on or off by not
1310 // setting DBO_DEFAULT, then respect that. Forcing no transactions is useful
1311 // for things like blob stores (ExternalStore) which want auto-commit mode.
1316 * @param DatabaseBase $conn
1318 private function undoTransactionRoundFlags( DatabaseBase
$conn ) {
1319 if ( $conn->getFlag( DBO_DEFAULT
) ) {
1320 $conn->restoreFlags( $conn::RESTORE_PRIOR
);
1325 * Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot
1327 * @param string $fname Caller name
1330 public function flushReplicaSnapshots( $fname = __METHOD__
) {
1331 $this->forEachOpenReplicaConnection( function ( DatabaseBase
$conn ) {
1332 $conn->flushSnapshot( __METHOD__
);
1337 * @return bool Whether a master connection is already open
1340 public function hasMasterConnection() {
1341 return $this->isOpen( $this->getWriterIndex() );
1345 * Determine if there are pending changes in a transaction by this thread
1349 public function hasMasterChanges() {
1351 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( &$pending ) {
1352 $pending |
= $conn->writesOrCallbacksPending();
1355 return (bool)$pending;
1359 * Get the timestamp of the latest write query done by this thread
1361 * @return float|bool UNIX timestamp or false
1363 public function lastMasterChangeTimestamp() {
1365 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( &$lastTime ) {
1366 $lastTime = max( $lastTime, $conn->lastDoneWrites() );
1373 * Check if this load balancer object had any recent or still
1374 * pending writes issued against it by this PHP thread
1376 * @param float $age How many seconds ago is "recent" [defaults to mWaitTimeout]
1380 public function hasOrMadeRecentMasterChanges( $age = null ) {
1381 $age = ( $age === null ) ?
$this->mWaitTimeout
: $age;
1383 return ( $this->hasMasterChanges()
1384 ||
$this->lastMasterChangeTimestamp() > microtime( true ) - $age );
1388 * Get the list of callers that have pending master changes
1390 * @return string[] List of method names
1393 public function pendingMasterChangeCallers() {
1395 $this->forEachOpenMasterConnection( function ( DatabaseBase
$conn ) use ( &$fnames ) {
1396 $fnames = array_merge( $fnames, $conn->pendingWriteCallers() );
1403 * @note This method will trigger a DB connection if not yet done
1404 * @param string|bool $wiki Wiki ID, or false for the current wiki
1405 * @return bool Whether the generic connection for reads is highly "lagged"
1407 public function getLaggedReplicaMode( $wiki = false ) {
1408 // No-op if there is only one DB (also avoids recursion)
1409 if ( !$this->laggedReplicaMode
&& $this->getServerCount() > 1 ) {
1411 // See if laggedReplicaMode gets set
1412 $conn = $this->getConnection( DB_REPLICA
, false, $wiki );
1413 $this->reuseConnection( $conn );
1414 } catch ( DBConnectionError
$e ) {
1415 // Avoid expensive re-connect attempts and failures
1416 $this->allReplicasDownMode
= true;
1417 $this->laggedReplicaMode
= true;
1421 return $this->laggedReplicaMode
;
1427 * @deprecated 1.28; use getLaggedReplicaMode()
1429 public function getLaggedSlaveMode( $wiki = false ) {
1430 return $this->getLaggedReplicaMode( $wiki );
1434 * @note This method will never cause a new DB connection
1435 * @return bool Whether any generic connection used for reads was highly "lagged"
1438 public function laggedReplicaUsed() {
1439 return $this->laggedReplicaMode
;
1445 * @deprecated Since 1.28; use laggedReplicaUsed()
1447 public function laggedSlaveUsed() {
1448 return $this->laggedReplicaUsed();
1452 * @note This method may trigger a DB connection if not yet done
1453 * @param string|bool $wiki Wiki ID, or false for the current wiki
1454 * @param DatabaseBase|null DB master connection; used to avoid loops [optional]
1455 * @return string|bool Reason the master is read-only or false if it is not
1458 public function getReadOnlyReason( $wiki = false, DatabaseBase
$conn = null ) {
1459 if ( $this->readOnlyReason
!== false ) {
1460 return $this->readOnlyReason
;
1461 } elseif ( $this->getLaggedReplicaMode( $wiki ) ) {
1462 if ( $this->allReplicasDownMode
) {
1463 return 'The database has been automatically locked ' .
1464 'until the replica database servers become available';
1466 return 'The database has been automatically locked ' .
1467 'while the replica database servers catch up to the master.';
1469 } elseif ( $this->masterRunningReadOnly( $wiki, $conn ) ) {
1470 return 'The database master is running in read-only mode.';
1477 * @param string $wiki Wiki ID, or false for the current wiki
1478 * @param DatabaseBase|null DB master connectionl used to avoid loops [optional]
1481 private function masterRunningReadOnly( $wiki, DatabaseBase
$conn = null ) {
1482 $cache = $this->wanCache
;
1483 $masterServer = $this->getServerName( $this->getWriterIndex() );
1485 return (bool)$cache->getWithSetCallback(
1486 $cache->makeGlobalKey( __CLASS__
, 'server-read-only', $masterServer ),
1487 self
::TTL_CACHE_READONLY
,
1488 function () use ( $wiki, $conn ) {
1489 $this->trxProfiler
->setSilenced( true );
1491 $dbw = $conn ?
: $this->getConnection( DB_MASTER
, [], $wiki );
1492 $readOnly = (int)$dbw->serverIsReadOnly();
1493 } catch ( DBError
$e ) {
1496 $this->trxProfiler
->setSilenced( false );
1499 [ 'pcTTL' => $cache::TTL_PROC_LONG
, 'busyValue' => 0 ]
1504 * Disables/enables lag checks
1505 * @param null|bool $mode
1508 public function allowLagged( $mode = null ) {
1509 if ( $mode === null ) {
1510 return $this->mAllowLagged
;
1512 $this->mAllowLagged
= $mode;
1514 return $this->mAllowLagged
;
1520 public function pingAll() {
1522 $this->forEachOpenConnection( function ( DatabaseBase
$conn ) use ( &$success ) {
1523 if ( !$conn->ping() ) {
1532 * Call a function with each open connection object
1533 * @param callable $callback
1534 * @param array $params
1536 public function forEachOpenConnection( $callback, array $params = [] ) {
1537 foreach ( $this->mConns
as $connsByServer ) {
1538 foreach ( $connsByServer as $serverConns ) {
1539 foreach ( $serverConns as $conn ) {
1540 $mergedParams = array_merge( [ $conn ], $params );
1541 call_user_func_array( $callback, $mergedParams );
1548 * Call a function with each open connection object to a master
1549 * @param callable $callback
1550 * @param array $params
1553 public function forEachOpenMasterConnection( $callback, array $params = [] ) {
1554 $masterIndex = $this->getWriterIndex();
1555 foreach ( $this->mConns
as $connsByServer ) {
1556 if ( isset( $connsByServer[$masterIndex] ) ) {
1557 /** @var DatabaseBase $conn */
1558 foreach ( $connsByServer[$masterIndex] as $conn ) {
1559 $mergedParams = array_merge( [ $conn ], $params );
1560 call_user_func_array( $callback, $mergedParams );
1567 * Call a function with each open replica DB connection object
1568 * @param callable $callback
1569 * @param array $params
1572 public function forEachOpenReplicaConnection( $callback, array $params = [] ) {
1573 foreach ( $this->mConns
as $connsByServer ) {
1574 foreach ( $connsByServer as $i => $serverConns ) {
1575 if ( $i === $this->getWriterIndex() ) {
1576 continue; // skip master
1578 foreach ( $serverConns as $conn ) {
1579 $mergedParams = array_merge( [ $conn ], $params );
1580 call_user_func_array( $callback, $mergedParams );
1587 * Get the hostname and lag time of the most-lagged replica DB
1589 * This is useful for maintenance scripts that need to throttle their updates.
1590 * May attempt to open connections to replica DBs on the default DB. If there is
1591 * no lag, the maximum lag will be reported as -1.
1593 * @param bool|string $wiki Wiki ID, or false for the default database
1594 * @return array ( host, max lag, index of max lagged host )
1596 public function getMaxLag( $wiki = false ) {
1601 if ( $this->getServerCount() <= 1 ) {
1602 return [ $host, $maxLag, $maxIndex ]; // no replication = no lag
1605 $lagTimes = $this->getLagTimes( $wiki );
1606 foreach ( $lagTimes as $i => $lag ) {
1607 if ( $this->mLoads
[$i] > 0 && $lag > $maxLag ) {
1609 $host = $this->mServers
[$i]['host'];
1614 return [ $host, $maxLag, $maxIndex ];
1618 * Get an estimate of replication lag (in seconds) for each server
1620 * Results are cached for a short time in memcached/process cache
1622 * Values may be "false" if replication is too broken to estimate
1624 * @param string|bool $wiki
1625 * @return int[] Map of (server index => float|int|bool)
1627 public function getLagTimes( $wiki = false ) {
1628 if ( $this->getServerCount() <= 1 ) {
1629 return [ 0 => 0 ]; // no replication = no lag
1632 # Send the request to the load monitor
1633 return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers
), $wiki );
1637 * Get the lag in seconds for a given connection, or zero if this load
1638 * balancer does not have replication enabled.
1640 * This should be used in preference to Database::getLag() in cases where
1641 * replication may not be in use, since there is no way to determine if
1642 * replication is in use at the connection level without running
1643 * potentially restricted queries such as SHOW SLAVE STATUS. Using this
1644 * function instead of Database::getLag() avoids a fatal error in this
1645 * case on many installations.
1647 * @param IDatabase $conn
1648 * @return int|bool Returns false on error
1650 public function safeGetLag( IDatabase
$conn ) {
1651 if ( $this->getServerCount() == 1 ) {
1654 return $conn->getLag();
1659 * Wait for a replica DB to reach a specified master position
1661 * This will connect to the master to get an accurate position if $pos is not given
1663 * @param IDatabase $conn Replica DB
1664 * @param DBMasterPos|bool $pos Master position; default: current position
1665 * @param integer|null $timeout Timeout in seconds [optional]
1666 * @return bool Success
1669 public function safeWaitForMasterPos( IDatabase
$conn, $pos = false, $timeout = null ) {
1670 if ( $this->getServerCount() == 1 ||
!$conn->getLBInfo( 'replica' ) ) {
1671 return true; // server is not a replica DB
1674 $pos = $pos ?
: $this->getConnection( DB_MASTER
)->getMasterPos();
1675 if ( !( $pos instanceof DBMasterPos
) ) {
1676 return false; // something is misconfigured
1679 $timeout = $timeout ?
: $this->mWaitTimeout
;
1680 $result = $conn->masterPosWait( $pos, $timeout );
1681 if ( $result == -1 ||
is_null( $result ) ) {
1682 $msg = __METHOD__
. ": Timed out waiting on {$conn->getServer()} pos {$pos}";
1683 wfDebugLog( 'replication', "$msg\n" );
1684 wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
1687 wfDebugLog( 'replication', __METHOD__
. ": Done\n" );
1695 * Clear the cache for slag lag delay times
1697 * This is only used for testing
1699 public function clearLagTimeCache() {
1700 $this->getLoadMonitor()->clearCaches();
1704 * Set a callback via DatabaseBase::setTransactionListener() on
1705 * all current and future master connections of this load balancer
1707 * @param string $name Callback name
1708 * @param callable|null $callback
1711 public function setTransactionListener( $name, callable
$callback = null ) {
1713 $this->trxRecurringCallbacks
[$name] = $callback;
1715 unset( $this->trxRecurringCallbacks
[$name] );
1717 $this->forEachOpenMasterConnection(
1718 function ( DatabaseBase
$conn ) use ( $name, $callback ) {
1719 $conn->setTransactionListener( $name, $callback );