MediaWiki  REL1_31
Wikimedia\Rdbms\LoadBalancer Class Reference

Database connection, tracking, load balancing, and transaction manager for a cluster. More...

Inheritance diagram for Wikimedia\Rdbms\LoadBalancer:
Collaboration diagram for Wikimedia\Rdbms\LoadBalancer:

Public Member Functions

 __construct (array $params)
 Construct a manager of IDatabase connection objects. More...
 
 __destruct ()
 
 allowLagged ( $mode=null)
 Disables/enables lag checks. More...
 
 approveMasterChanges (array $options)
 Perform all pre-commit checks for things like replication safety. More...
 
 beginMasterChanges ( $fname=__METHOD__)
 Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set) More...
 
 closeAll ()
 Close all open connections. More...
 
 closeConnection (IDatabase $conn)
 Close a connection. More...
 
 commitAll ( $fname=__METHOD__)
 Commit transactions on all open connections. More...
 
 commitMasterChanges ( $fname=__METHOD__)
 Issue COMMIT on all master connections where writes where done. More...
 
 disable ()
 Disable this load balancer. More...
 
 finalizeMasterChanges ()
 Perform all pre-commit callbacks that remain part of the atomic transactions and disable any post-commit callbacks until runMasterPostTrxCallbacks() More...
 
 flushReplicaSnapshots ( $fname=__METHOD__)
 Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot. More...
 
 forEachOpenConnection ( $callback, array $params=[])
 Call a function with each open connection object. More...
 
 forEachOpenMasterConnection ( $callback, array $params=[])
 Call a function with each open connection object to a master. More...
 
 forEachOpenReplicaConnection ( $callback, array $params=[])
 Call a function with each open replica DB connection object. More...
 
 getAnyOpenConnection ( $i, $flags=0)
 Get any open connection to a given server index, local or foreign. More...
 
 getConnection ( $i, $groups=[], $domain=false, $flags=0)
 Get a connection handle by server index. More...
 
 getConnectionRef ( $db, $groups=[], $domain=false, $flags=0)
 Get a database connection handle reference. More...
 
 getLaggedReplicaMode ( $domain=false)
 
 getLaggedSlaveMode ( $domain=false)
 
 getLagTimes ( $domain=false)
 Get an estimate of replication lag (in seconds) for each server. More...
 
 getLazyConnectionRef ( $db, $groups=[], $domain=false, $flags=0)
 Get a database connection handle reference without connecting yet. More...
 
 getLocalDomainID ()
 Get the local (and default) database domain ID of connection handles. More...
 
 getMaintenanceConnectionRef ( $db, $groups=[], $domain=false, $flags=0)
 Get a maintenance database connection handle reference for migrations and schema changes. More...
 
 getMasterPos ()
 Get the current master position for chronology control purposes. More...
 
 getMaxLag ( $domain=false)
 Get the hostname and lag time of the most-lagged replica DB. More...
 
 getReaderIndex ( $group=false, $domain=false)
 Get the index of the reader connection, which may be a replica DB. More...
 
 getReadOnlyReason ( $domain=false, IDatabase $conn=null)
 
 getServerAttributes ( $i)
 
 getServerCount ()
 Get the number of defined servers (not the number of open connections) More...
 
 getServerInfo ( $i)
 Return the server info structure for a given index, or false if the index is invalid. More...
 
 getServerName ( $i)
 Get the host name or IP address of the server with the specified index. More...
 
 getServerType ( $i)
 Get DB type of the server with the specified index. More...
 
 getWriterIndex ()
 
 hasMasterChanges ()
 Determine if there are pending changes in a transaction by this thread. More...
 
 hasMasterConnection ()
 
 hasOrMadeRecentMasterChanges ( $age=null)
 Check if this load balancer object had any recent or still pending writes issued against it by this PHP thread. More...
 
 haveIndex ( $i)
 Returns true if the specified index is a valid server index. More...
 
 isNonZeroLoad ( $i)
 Returns true if the specified index is valid and has non-zero load. More...
 
 laggedReplicaUsed ()
 Checks whether the database for generic connections this request was both: More...
 
 laggedSlaveUsed ()
 
 lastMasterChangeTimestamp ()
 Get the timestamp of the latest write query done by this thread. More...
 
 openConnection ( $i, $domain=false, $flags=0)
 Open a connection to the server given by the specified index. More...
 
 pendingMasterChangeCallers ()
 Get the list of callers that have pending master changes. More...
 
 pingAll ()
 
 reuseConnection (IDatabase $conn)
 Mark a foreign connection as being available for reuse under a different DB domain. More...
 
 rollbackMasterChanges ( $fname=__METHOD__)
 Issue ROLLBACK only on master, only if queries were done on connection. More...
 
 runMasterPostTrxCallbacks ( $type)
 Issue all pending post-COMMIT/ROLLBACK callbacks. More...
 
 safeGetLag (IDatabase $conn)
 Get the lag in seconds for a given connection, or zero if this load balancer does not have replication enabled. More...
 
 safeWaitForMasterPos (IDatabase $conn, $pos=false, $timeout=null)
 
 setDomainPrefix ( $prefix)
 Set a new table prefix for the existing local domain ID for testing. More...
 
 setIndexAliases (array $aliases)
 Convert certain index names to alternative names before querying the DB. More...
 
 setTableAliases (array $aliases)
 Make certain table names use their own database, schema, and table prefix when passed into SQL queries pre-escaped and without a qualified database name. More...
 
 setTransactionListener ( $name, callable $callback=null)
 Set a callback via IDatabase::setTransactionListener() on all current and future master connections of this load balancer. More...
 
 suppressTransactionEndCallbacks ()
 Suppress all pending post-COMMIT/ROLLBACK callbacks. More...
 
 waitFor ( $pos)
 Set the master wait position. More...
 
 waitForAll ( $pos, $timeout=null)
 Set the master wait position and wait for ALL replica DBs to catch up to it. More...
 
 waitForOne ( $pos, $timeout=null)
 Set the master wait position and wait for a "generic" replica DB to catch up to it. More...
 

Public Attributes

const KEY_FOREIGN_FREE = 'foreignFree'
 
const KEY_FOREIGN_FREE_NOROUND = 'foreignFreeAutoCommit'
 
const KEY_FOREIGN_INUSE = 'foreignInUse'
 
const KEY_FOREIGN_INUSE_NOROUND = 'foreignInUseAutoCommit'
 
const KEY_LOCAL = 'local'
 
const KEY_LOCAL_NOROUND = 'localAutoCommit'
 

Protected Member Functions

 doWait ( $index, $open=false, $timeout=null)
 Wait for a given replica DB to catch up to the master pos stored in $this. More...
 
 getScopedPHPBehaviorForCommit ()
 Make PHP ignore user aborts/disconnects until the returned value leaves scope. More...
 
 reallyOpenConnection (array $server, DatabaseDomain $domainOverride)
 Open a new network connection to a server (uncached) More...
 

Protected Attributes

string $agent
 Agent name for query profiling. More...
 
bool $cliMode
 Whether this PHP instance is for a CLI script. More...
 
LoggerInterface $connLogger
 
LoggerInterface $perfLogger
 
object string $profiler
 Class name or object With profileIn/profileOut methods. More...
 
LoggerInterface $queryLogger
 
LoggerInterface $replLogger
 
TransactionProfiler $trxProfiler
 

Private Member Functions

 applyTransactionRoundFlags (IDatabase $conn)
 Make all DB servers with DBO_DEFAULT/DBO_TRX set join the transaction round. More...
 
 getLoadMonitor ()
 Get a LoadMonitor instance. More...
 
 getRandomNonLagged (array $loads, $domain=false, $maxLag=INF)
 
 isOpen ( $index)
 Test if the specified index represents an open connection. More...
 
 masterRunningReadOnly ( $domain, IDatabase $conn=null)
 
 openForeignConnection ( $i, $domain, $flags=0)
 Open a connection to a foreign DB, or return one if it is already open. More...
 
 pickReaderIndex (array $loads, $domain=false)
 
 reportConnectionError ()
 
 setLocalDomain (DatabaseDomain $domain)
 
 setWaitForPositionIfHigher ( $pos)
 
 undoTransactionRoundFlags (IDatabase $conn)
 

Private Attributes

bool $allowLagged
 Whether to disregard replica DB lag as a factor in replica DB selection. More...
 
bool $allReplicasDownMode = false
 Whether the generic reader fell back to a lagged replica DB. More...
 
callable null $chronologyCallback
 Callback to run before the first connection attempt. More...
 
bool $connectionAttempted = false
 Whether any connection has been attempted yet. More...
 
Database[][][] $conns
 Map of (connection category => server index => IDatabase[]) More...
 
int $connsOpened = 0
 Total connections opened. More...
 
callable $deprecationLogger
 Deprecation logger. More...
 
bool $disabled = false
 
Database $errorConnection
 DB connection object that caused a problem. More...
 
callable $errorLogger
 Exception logger. More...
 
array[] $groupLoads
 Map of (group => server index => weight) More...
 
string $host
 Current server name. More...
 
string[] $indexAliases = []
 Map of (index alias => index) More...
 
bool $laggedReplicaMode = false
 Whether the generic reader fell back to a lagged replica DB. More...
 
string $lastError = 'Unknown error'
 The last DB selection or connection error. More...
 
ILoadMonitor $loadMonitor
 
array $loadMonitorConfig
 The LoadMonitor configuration. More...
 
float[] $loads
 Map of (server index => weight) More...
 
DatabaseDomain $localDomain
 Local Domain ID and default for selectDB() calls. More...
 
string $localDomainIdAlias
 Alternate ID string for the domain instead of DatabaseDomain::getId() More...
 
int $maxLag = self::MAX_LAG_DEFAULT
 
int $readIndex
 The generic (not query grouped) replica DB index (of $mServers) More...
 
string bool $readOnlyReason = false
 Reason the LB is read-only or false if not. More...
 
array[] $servers
 Map of (server index => server config array) More...
 
BagOStuff $srvCache
 
 $tableAliases = []
 
array[] $trxRecurringCallbacks = []
 Map of (name => callable) More...
 
string bool $trxRoundId = false
 String if a requested DBO_TRX transaction round is active. More...
 
bool DBMasterPos $waitForPos
 False if not set. More...
 
int $waitTimeout
 Seconds to spend waiting on replica DB lag to resolve. More...
 
WANObjectCache $wanCache
 

Detailed Description

Database connection, tracking, load balancing, and transaction manager for a cluster.

Definition at line 41 of file LoadBalancer.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Rdbms\LoadBalancer::__construct ( array  $params)

Construct a manager of IDatabase connection objects.

Parameters
array$paramsParameter map with keys:
  • servers : Required. Array of server info structures.
  • localDomain: A DatabaseDomain or domain ID string.
  • loadMonitor : Name of a class used to fetch server lag and load.
  • readOnlyReason : Reason the master DB is read-only if so [optional]
  • waitTimeout : Maximum time to wait for replicas for consistency [optional]
  • maxLag: Avoid replica DB servers with more lag than this [optional]
  • srvCache : BagOStuff object for server cache [optional]
  • wanCache : WANObjectCache object [optional]
  • chronologyCallback: Callback to run before the first connection attempt [optional]
  • hostname : The name of the current server [optional]
  • cliMode: Whether the execution context is a CLI script. [optional]
  • profiler : Class name or instance with profileIn()/profileOut() methods. [optional]
  • trxProfiler: TransactionProfiler instance. [optional]
  • replLogger: PSR-3 logger instance. [optional]
  • connLogger: PSR-3 logger instance. [optional]
  • queryLogger: PSR-3 logger instance. [optional]
  • perfLogger: PSR-3 logger instance. [optional]
  • errorLogger : Callback that takes an Exception and logs it. [optional]
  • deprecationLogger: Callback to log a deprecation warning. [optional]
Exceptions
InvalidArgumentException

Implements Wikimedia\Rdbms\ILoadBalancer.

Reimplemented in Wikimedia\Rdbms\LoadBalancerSingle.

Definition at line 143 of file LoadBalancer.php.

References $e, Wikimedia\Rdbms\LoadBalancer\$localDomain, Wikimedia\Rdbms\LoadBalancer\$maxLag, $params, Wikimedia\Rdbms\LoadBalancer\allowLagged(), as, WANObjectCache\newEmpty(), Wikimedia\Rdbms\DatabaseDomain\newFromId(), Wikimedia\Rdbms\DatabaseDomain\newUnspecified(), servers, and Wikimedia\Rdbms\LoadBalancer\setLocalDomain().

◆ __destruct()

Wikimedia\Rdbms\LoadBalancer::__destruct ( )

Definition at line 1864 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\disable().

Member Function Documentation

◆ allowLagged()

Wikimedia\Rdbms\LoadBalancer::allowLagged (   $mode = null)

Disables/enables lag checks.

Parameters
null | bool$mode
Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1621 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$allowLagged.

Referenced by Wikimedia\Rdbms\LoadBalancer\__construct(), and Wikimedia\Rdbms\LoadBalancer\pickReaderIndex().

◆ applyTransactionRoundFlags()

Wikimedia\Rdbms\LoadBalancer::applyTransactionRoundFlags ( IDatabase  $conn)
private

Make all DB servers with DBO_DEFAULT/DBO_TRX set join the transaction round.

Some servers may have neither flag enabled, meaning that they opt out of such transaction rounds and remain in auto-commit mode. Such behavior might be desired when a DB server is used for something like simple key/value storage.

Parameters
IDatabase$conn

Definition at line 1457 of file LoadBalancer.php.

References DBO_DEFAULT, DBO_TRX, Wikimedia\Rdbms\IDatabase\getFlag(), Wikimedia\Rdbms\IDatabase\getLBInfo(), Wikimedia\Rdbms\IDatabase\setFlag(), and Wikimedia\Rdbms\IDatabase\setLBInfo().

Referenced by Wikimedia\Rdbms\LoadBalancer\beginMasterChanges(), and Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ approveMasterChanges()

Wikimedia\Rdbms\LoadBalancer::approveMasterChanges ( array  $options)

Perform all pre-commit checks for things like replication safety.

Use this only for mutli-database commits

Parameters
array$optionsIncludes:
  • maxWriteDuration : max write query duration time in seconds
Exceptions
DBTransactionError

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1296 of file LoadBalancer.php.

References $options, $time, Wikimedia\Rdbms\IDatabase\explicitTrxActive(), Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), if, Wikimedia\Rdbms\IDatabase\pendingWriteQueryDuration(), Wikimedia\Rdbms\IDatabase\ping(), use, and Wikimedia\Rdbms\IDatabase\writesOrCallbacksPending().

◆ beginMasterChanges()

Wikimedia\Rdbms\LoadBalancer::beginMasterChanges (   $fname = __METHOD__)

Flush any master transaction snapshots and set DBO_TRX (if DBO_DEFAULT is set)

The DBO_TRX setting will be reverted to the default in each of these methods:

Parameters
string$fname
Exceptions
DBExpectedError

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1329 of file LoadBalancer.php.

References $e, $fname, Wikimedia\Rdbms\LoadBalancer\applyTransactionRoundFlags(), Wikimedia\Rdbms\Database\flushSnapshot(), Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression(), and use.

◆ closeAll()

◆ closeConnection()

Wikimedia\Rdbms\LoadBalancer::closeConnection ( IDatabase  $conn)

Close a connection.

Using this function makes sure the LoadBalancer knows the connection is closed. If you use $conn->close() directly, the load balancer won't update its state.

Parameters
IDatabase$conn

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1237 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$connsOpened, Wikimedia\Rdbms\LoadBalancer\$host, $type, as, Wikimedia\Rdbms\IDatabase\close(), Wikimedia\Rdbms\IDatabase\getLBInfo(), and Wikimedia\Rdbms\LoadBalancer\getServerName().

Referenced by Wikimedia\Rdbms\LoadBalancer\doWait(), and Wikimedia\Rdbms\LoadBalancer\safeWaitForMasterPos().

◆ commitAll()

Wikimedia\Rdbms\LoadBalancer::commitAll (   $fname = __METHOD__)

◆ commitMasterChanges()

Wikimedia\Rdbms\LoadBalancer::commitMasterChanges (   $fname = __METHOD__)

◆ disable()

Wikimedia\Rdbms\LoadBalancer::disable ( )

Disable this load balancer.

All connections are closed, and any attempt to open a new connection will result in a DBAccessError.

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1213 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\closeAll().

Referenced by Wikimedia\Rdbms\LoadBalancer\__destruct().

◆ doWait()

Wikimedia\Rdbms\LoadBalancer::doWait (   $index,
  $open = false,
  $timeout = null 
)
protected

Wait for a given replica DB to catch up to the master pos stored in $this.

Parameters
int$indexServer index
bool$openCheck the server even if a new connection has to be made
int$timeoutMax seconds to wait; default is "waitTimeout" given to __construct()
Returns
bool

Definition at line 594 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\closeConnection(), Wikimedia\Rdbms\LoadBalancer\getAnyOpenConnection(), Wikimedia\Rdbms\LoadBalancer\getServerName(), Wikimedia\Rdbms\DBMasterPos\hasReached(), Wikimedia\Rdbms\LoadBalancer\openConnection(), and IExpiringStore\TTL_DAY.

Referenced by Wikimedia\Rdbms\LoadBalancer\getReaderIndex(), Wikimedia\Rdbms\LoadBalancer\waitFor(), Wikimedia\Rdbms\LoadBalancer\waitForAll(), and Wikimedia\Rdbms\LoadBalancer\waitForOne().

◆ finalizeMasterChanges()

Wikimedia\Rdbms\LoadBalancer::finalizeMasterChanges ( )

Perform all pre-commit callbacks that remain part of the atomic transactions and disable any post-commit callbacks until runMasterPostTrxCallbacks()

Use this only for mutli-database commits

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1286 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\Database\runOnTransactionPreCommitCallbacks(), and Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression().

◆ flushReplicaSnapshots()

Wikimedia\Rdbms\LoadBalancer::flushReplicaSnapshots (   $fname = __METHOD__)

Commit all replica DB transactions so as to flush any REPEATABLE-READ or SSI snapshot.

Parameters
string$fnameCaller name

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1490 of file LoadBalancer.php.

References Wikimedia\Rdbms\IDatabase\flushSnapshot(), and Wikimedia\Rdbms\LoadBalancer\forEachOpenReplicaConnection().

◆ forEachOpenConnection()

Wikimedia\Rdbms\LoadBalancer::forEachOpenConnection (   $callback,
array  $params = [] 
)

Call a function with each open connection object.

Parameters
callable$callback
array$params

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1641 of file LoadBalancer.php.

References $params, and as.

Referenced by Wikimedia\Rdbms\LoadBalancer\closeAll(), Wikimedia\Rdbms\LoadBalancer\commitAll(), Wikimedia\Rdbms\LoadBalancer\pingAll(), and Wikimedia\Rdbms\LoadBalancer\setDomainPrefix().

◆ forEachOpenMasterConnection()

◆ forEachOpenReplicaConnection()

Wikimedia\Rdbms\LoadBalancer::forEachOpenReplicaConnection (   $callback,
array  $params = [] 
)

Call a function with each open replica DB connection object.

Parameters
callable$callback
array$params

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1665 of file LoadBalancer.php.

References $params, as, and Wikimedia\Rdbms\LoadBalancer\getWriterIndex().

Referenced by Wikimedia\Rdbms\LoadBalancer\flushReplicaSnapshots().

◆ getAnyOpenConnection()

Wikimedia\Rdbms\LoadBalancer::getAnyOpenConnection (   $i,
  $flags = 0 
)

Get any open connection to a given server index, local or foreign.

Use CONN_TRX_AUTOCOMMIT to only look for connections opened with that flag

Parameters
int$iServer index or DB_MASTER/DB_REPLICA
int$flagsBitfield of CONN_* class constants
Returns
Database|bool False if no such connection is open

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 570 of file LoadBalancer.php.

References as.

Referenced by Wikimedia\Rdbms\LoadBalancer\doWait(), Wikimedia\Rdbms\LoadBalancer\getMasterPos(), Wikimedia\Rdbms\LoadBalancer\isOpen(), and Wikimedia\Rdbms\LoadBalancer\safeWaitForMasterPos().

◆ getConnection()

Wikimedia\Rdbms\LoadBalancer::getConnection (   $i,
  $groups = [],
  $domain = false,
  $flags = 0 
)

Get a connection handle by server index.

The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check such flags beforehand.

If the caller uses $domain or sets CONN_TRX_AUTOCOMMIT in $flags, then it must also call ILoadBalancer::reuseConnection() on the handle when finished using it. In all other cases, this is not necessary, though not harmful either.

Parameters
int$iServer index (overrides $groups) or DB_MASTER/DB_REPLICA
array | string | bool$groupsQuery group(s), or false for the generic reader
string | bool$domainDomain ID, or false for the current domain
int$flagsBitfield of CONN_* class constants
Note
This method throws DBAccessError if ILoadBalancer::disable() was called
Exceptions
DBError
Returns
Database

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 683 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$connsOpened, Wikimedia\Rdbms\LoadBalancer\$host, Wikimedia\Rdbms\LoadBalancer\$lastError, array(), as, DB_MASTER, DB_REPLICA, false, Wikimedia\Rdbms\LoadBalancer\getReaderIndex(), Wikimedia\Rdbms\LoadBalancer\getReadOnlyReason(), Wikimedia\Rdbms\LoadBalancer\getServerAttributes(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), Wikimedia\Rdbms\LoadBalancer\openConnection(), and Wikimedia\Rdbms\LoadBalancer\reportConnectionError().

Referenced by Wikimedia\Rdbms\LoadBalancer\getConnectionRef(), Wikimedia\Rdbms\LoadBalancer\getLaggedReplicaMode(), Wikimedia\Rdbms\LoadBalancer\getMaintenanceConnectionRef(), and Wikimedia\Rdbms\LoadBalancer\masterRunningReadOnly().

◆ getConnectionRef()

Wikimedia\Rdbms\LoadBalancer::getConnectionRef (   $i,
  $groups = [],
  $domain = false,
  $flags = 0 
)

Get a database connection handle reference.

The handle's methods simply wrap those of a Database handle

The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check such flags beforehand.

See also
ILoadBalancer::getConnection() for parameter information
Parameters
int$iServer index or DB_MASTER/DB_REPLICA
array | string | bool$groupsQuery group(s), or false for the generic reader
string | bool$domainDomain ID, or false for the current domain
int$flagsBitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
Returns
DBConnRef

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 829 of file LoadBalancer.php.

References false, and Wikimedia\Rdbms\LoadBalancer\getConnection().

Referenced by ImportableOldRevisionImporter\import().

◆ getLaggedReplicaMode()

Wikimedia\Rdbms\LoadBalancer::getLaggedReplicaMode (   $domain = false)
Note
This method will trigger a DB connection if not yet done
Parameters
string | bool$domainDomain ID, or false for the current domain
Returns
bool Whether the database for generic connections this request is highly "lagged"

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1534 of file LoadBalancer.php.

References $e, Wikimedia\Rdbms\LoadBalancer\$laggedReplicaMode, DB_REPLICA, Wikimedia\Rdbms\LoadBalancer\getConnection(), Wikimedia\Rdbms\LoadBalancer\getServerCount(), and Wikimedia\Rdbms\LoadBalancer\reuseConnection().

Referenced by Wikimedia\Rdbms\LoadBalancer\getLaggedSlaveMode(), and Wikimedia\Rdbms\LoadBalancer\getReadOnlyReason().

◆ getLaggedSlaveMode()

Wikimedia\Rdbms\LoadBalancer::getLaggedSlaveMode (   $domain = false)
Parameters
bool$domain
Returns
bool
Deprecated:
1.28; use getLaggedReplicaMode()

Definition at line 1556 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\getLaggedReplicaMode().

◆ getLagTimes()

Wikimedia\Rdbms\LoadBalancer::getLagTimes (   $domain = false)

Get an estimate of replication lag (in seconds) for each server.

Results are cached for a short time in memcached/process cache

Values may be "false" if replication is too broken to estimate

Parameters
string | bool$domain
Returns
int[] Map of (server index => float|int|bool)

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1700 of file LoadBalancer.php.

References as, Wikimedia\Rdbms\LoadBalancer\getLoadMonitor(), Wikimedia\Rdbms\LoadBalancer\getServerCount(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\getMaxLag(), and Wikimedia\Rdbms\LoadBalancer\getRandomNonLagged().

◆ getLazyConnectionRef()

Wikimedia\Rdbms\LoadBalancer::getLazyConnectionRef (   $i,
  $groups = [],
  $domain = false,
  $flags = 0 
)

Get a database connection handle reference without connecting yet.

The handle's methods simply wrap those of a Database handle

The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check such flags beforehand.

See also
ILoadBalancer::getConnection() for parameter information
Parameters
int$iServer index or DB_MASTER/DB_REPLICA
array | string | bool$groupsQuery group(s), or false for the generic reader
string | bool$domainDomain ID, or false for the current domain
int$flagsBitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
Returns
DBConnRef

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 835 of file LoadBalancer.php.

References false.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ getLoadMonitor()

Wikimedia\Rdbms\LoadBalancer::getLoadMonitor ( )
private

◆ getLocalDomainID()

Wikimedia\Rdbms\LoadBalancer::getLocalDomainID ( )

Get the local (and default) database domain ID of connection handles.

See also
DatabaseDomain
Returns
string Database domain ID; this specifies DB name, schema, and table prefix
Since
1.31

Definition at line 259 of file LoadBalancer.php.

◆ getMaintenanceConnectionRef()

Wikimedia\Rdbms\LoadBalancer::getMaintenanceConnectionRef (   $db,
  $groups = [],
  $domain = false,
  $flags = 0 
)

Get a maintenance database connection handle reference for migrations and schema changes.

The handle's methods simply wrap those of a Database handle

The CONN_TRX_AUTOCOMMIT flag is ignored for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check such flags beforehand.

See also
ILoadBalancer::getConnection() for parameter information
Parameters
int$dbServer index or DB_MASTER/DB_REPLICA
array | string | bool$groupsQuery group(s), or false for the generic reader
string | bool$domainDomain ID, or false for the current domain
int$flagsBitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
Returns
MaintainableDBConnRef

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 841 of file LoadBalancer.php.

References false, and Wikimedia\Rdbms\LoadBalancer\getConnection().

◆ getMasterPos()

Wikimedia\Rdbms\LoadBalancer::getMasterPos ( )

Get the current master position for chronology control purposes.

Returns
DBMasterPos|bool Returns false if not applicable

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1194 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\getAnyOpenConnection(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), and servers.

◆ getMaxLag()

Wikimedia\Rdbms\LoadBalancer::getMaxLag (   $domain = false)

Get the hostname and lag time of the most-lagged replica DB.

This is useful for maintenance scripts that need to throttle their updates. May attempt to open connections to replica DBs on the default DB. If there is no lag, the maximum lag will be reported as -1.

Parameters
bool | string$domainDomain ID, or false for the default database
Returns
array ( host, max lag, index of max lagged host )

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1679 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$host, Wikimedia\Rdbms\LoadBalancer\$maxLag, as, Wikimedia\Rdbms\LoadBalancer\getLagTimes(), Wikimedia\Rdbms\LoadBalancer\getServerCount(), and servers.

◆ getRandomNonLagged()

Wikimedia\Rdbms\LoadBalancer::getRandomNonLagged ( array  $loads,
  $domain = false,
  $maxLag = INF 
)
private
Parameters
array$loads
bool | string$domainDomain to get non-lagged for
int$maxLagRestrict the maximum allowed lag to this many seconds
Returns
bool|int|string

Definition at line 295 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$host, Wikimedia\Rdbms\LoadBalancer\$loads, Wikimedia\Rdbms\LoadBalancer\$maxLag, as, Wikimedia\Rdbms\LoadBalancer\getLagTimes(), Wikimedia\Rdbms\LoadBalancer\getServerName(), ArrayUtils\pickRandom(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\pickReaderIndex().

◆ getReaderIndex()

Wikimedia\Rdbms\LoadBalancer::getReaderIndex (   $group = false,
  $domain = false 
)

Get the index of the reader connection, which may be a replica DB.

This takes into account load ratios and lag times. It should always return a consistent index during a given invocation.

Side effect: opens connections to databases

Parameters
string | bool$groupQuery group, or false for the generic reader
string | bool$domainDomain ID, or false for the current domain
Exceptions
DBError
Returns
bool|int|string

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 346 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$laggedReplicaMode, Wikimedia\Rdbms\LoadBalancer\$loads, Wikimedia\Rdbms\LoadBalancer\$readIndex, Wikimedia\Rdbms\LoadBalancer\doWait(), Wikimedia\Rdbms\LoadBalancer\getLoadMonitor(), Wikimedia\Rdbms\LoadBalancer\getServerName(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), list, Wikimedia\Rdbms\LoadBalancer\pickReaderIndex(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\getConnection().

◆ getReadOnlyReason()

Wikimedia\Rdbms\LoadBalancer::getReadOnlyReason (   $domain = false,
IDatabase  $conn = null 
)
Note
This method may trigger a DB connection if not yet done
Parameters
string | bool$domainDomain ID, or false for the current domain
IDatabase | null$connDB master connection; used to avoid loops [optional]
Returns
string|bool Reason the master is read-only or false if it is not

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1573 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$readOnlyReason, Wikimedia\Rdbms\LoadBalancer\getLaggedReplicaMode(), and Wikimedia\Rdbms\LoadBalancer\masterRunningReadOnly().

Referenced by Wikimedia\Rdbms\LoadBalancer\getConnection().

◆ getScopedPHPBehaviorForCommit()

Wikimedia\Rdbms\LoadBalancer::getScopedPHPBehaviorForCommit ( )
finalprotected

Make PHP ignore user aborts/disconnects until the returned value leaves scope.

This returns null and does nothing in CLI mode.

Returns
ScopedCallback|null

Definition at line 1853 of file LoadBalancer.php.

References use.

Referenced by Wikimedia\Rdbms\LoadBalancer\commitMasterChanges().

◆ getServerAttributes()

Wikimedia\Rdbms\LoadBalancer::getServerAttributes (   $i)
Parameters
int$iServer index
Returns
array (Database::ATTRIBUTE_* constant => value) for all such constants
Since
1.31

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1016 of file LoadBalancer.php.

References Wikimedia\Rdbms\Database\attributesFromType(), Wikimedia\Rdbms\LoadBalancer\getServerType(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\getConnection().

◆ getServerCount()

Wikimedia\Rdbms\LoadBalancer::getServerCount ( )

◆ getServerInfo()

Wikimedia\Rdbms\LoadBalancer::getServerInfo (   $i)

Return the server info structure for a given index, or false if the index is invalid.

Parameters
int$i
Returns
array|bool
Since
1.31

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1182 of file LoadBalancer.php.

References servers.

◆ getServerName()

Wikimedia\Rdbms\LoadBalancer::getServerName (   $i)

◆ getServerType()

Wikimedia\Rdbms\LoadBalancer::getServerType (   $i)

Get DB type of the server with the specified index.

Parameters
int$i
Returns
string One of (mysql,postgres,sqlite,...) or "unknown" for bad indexes
Since
1.30

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1190 of file LoadBalancer.php.

References servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\getServerAttributes().

◆ getWriterIndex()

◆ hasMasterChanges()

Wikimedia\Rdbms\LoadBalancer::hasMasterChanges ( )

Determine if there are pending changes in a transaction by this thread.

Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1500 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), use, and Wikimedia\Rdbms\IDatabase\writesOrCallbacksPending().

Referenced by Wikimedia\Rdbms\LoadBalancer\hasOrMadeRecentMasterChanges().

◆ hasMasterConnection()

Wikimedia\Rdbms\LoadBalancer::hasMasterConnection ( )
Returns
bool Whether a master connection is already open

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1496 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), and Wikimedia\Rdbms\LoadBalancer\isOpen().

◆ hasOrMadeRecentMasterChanges()

Wikimedia\Rdbms\LoadBalancer::hasOrMadeRecentMasterChanges (   $age = null)

Check if this load balancer object had any recent or still pending writes issued against it by this PHP thread.

Parameters
float$ageHow many seconds ago is "recent" [defaults to mWaitTimeout]
Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1518 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\hasMasterChanges(), and Wikimedia\Rdbms\LoadBalancer\lastMasterChangeTimestamp().

◆ haveIndex()

Wikimedia\Rdbms\LoadBalancer::haveIndex (   $i)

Returns true if the specified index is a valid server index.

Parameters
int$i
Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1158 of file LoadBalancer.php.

References servers.

◆ isNonZeroLoad()

Wikimedia\Rdbms\LoadBalancer::isNonZeroLoad (   $i)

Returns true if the specified index is valid and has non-zero load.

Parameters
int$i
Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1162 of file LoadBalancer.php.

References servers.

◆ isOpen()

Wikimedia\Rdbms\LoadBalancer::isOpen (   $index)
private

Test if the specified index represents an open connection.

Parameters
int$indexServer index @access private
Returns
bool

Definition at line 1030 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\getAnyOpenConnection().

Referenced by Wikimedia\Rdbms\LoadBalancer\hasMasterConnection().

◆ laggedReplicaUsed()

Wikimedia\Rdbms\LoadBalancer::laggedReplicaUsed ( )

Checks whether the database for generic connections this request was both:

  • a) Already choosen due to a prior connection attempt
  • b) Considered highly "lagged"
Note
This method will never cause a new DB connection
Returns
bool

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1560 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$laggedReplicaMode.

Referenced by Wikimedia\Rdbms\LoadBalancer\laggedSlaveUsed().

◆ laggedSlaveUsed()

Wikimedia\Rdbms\LoadBalancer::laggedSlaveUsed ( )
Returns
bool
Since
1.27
Deprecated:
Since 1.28; use laggedReplicaUsed()

Definition at line 1569 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\laggedReplicaUsed().

◆ lastMasterChangeTimestamp()

Wikimedia\Rdbms\LoadBalancer::lastMasterChangeTimestamp ( )

Get the timestamp of the latest write query done by this thread.

Returns
float|bool UNIX timestamp or false

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1509 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\IDatabase\lastDoneWrites(), and use.

Referenced by Wikimedia\Rdbms\LoadBalancer\hasOrMadeRecentMasterChanges().

◆ masterRunningReadOnly()

Wikimedia\Rdbms\LoadBalancer::masterRunningReadOnly (   $domain,
IDatabase  $conn = null 
)
private
Parameters
string$domainDomain ID, or false for the current domain
IDatabase | null$connDB master connectionl used to avoid loops [optional]
Returns
bool

Definition at line 1596 of file LoadBalancer.php.

References $cache, $e, Wikimedia\Rdbms\LoadBalancer\$wanCache, DB_MASTER, function, Wikimedia\Rdbms\LoadBalancer\getConnection(), Wikimedia\Rdbms\LoadBalancer\getServerName(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), Wikimedia\Rdbms\LoadBalancer\reuseConnection(), and use.

Referenced by Wikimedia\Rdbms\LoadBalancer\getReadOnlyReason().

◆ openConnection()

Wikimedia\Rdbms\LoadBalancer::openConnection (   $i,
  $domain = false,
  $flags = 0 
)

Open a connection to the server given by the specified index.

The index must be an actual index into the array. If a connection to the server is already open and not considered an "in use" foreign connection, this simply returns it.

Avoid using CONN_TRX_AUTOCOMMIT for databases with ATTR_DB_LEVEL_LOCKING (e.g. sqlite) in order to avoid deadlocks. ILoadBalancer::getServerAttributes() can be used to check such flags beforehand.

If the caller uses $domain or sets CONN_TRX_AUTOCOMMIT in $flags, then it must also call ILoadBalancer::reuseConnection() on the handle when finished using it. In all other cases, this is not necessary, though not harmful either.

Note
This method throws DBAccessError if ILoadBalancer::disable() was called
Parameters
int$iServer index (does not support DB_MASTER/DB_REPLICA)
string | bool$domainDomain ID, or false for the current domain
int$flagsBitfield of CONN_* class constants (e.g. CONN_TRX_AUTOCOMMIT)
Returns
Database|bool Returns false on errors
Exceptions
DBAccessError

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 848 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$host, DBO_TRX, Wikimedia\Rdbms\LoadBalancer\getServerName(), Wikimedia\Rdbms\IDatabase\isOpen(), Wikimedia\Rdbms\LoadBalancer\KEY_LOCAL, Wikimedia\Rdbms\LoadBalancer\KEY_LOCAL_NOROUND, Wikimedia\Rdbms\LoadBalancer\openForeignConnection(), Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\doWait(), Wikimedia\Rdbms\LoadBalancer\getConnection(), Wikimedia\Rdbms\LoadBalancer\pickReaderIndex(), and Wikimedia\Rdbms\LoadBalancer\safeWaitForMasterPos().

◆ openForeignConnection()

Wikimedia\Rdbms\LoadBalancer::openForeignConnection (   $i,
  $domain,
  $flags = 0 
)
private

Open a connection to a foreign DB, or return one if it is already open.

Increments a reference count on the returned connection which locks the connection to the requested domain. This reference count can be decremented by calling reuseConnection().

If a connection is open to the appropriate server already, but with the wrong database, it will be switched to the right database and returned, as long as it has been freed first with reuseConnection().

On error, returns false, and the connection which caused the error will be available via $this->errorConnection.

Note
If disable() was called on this LoadBalancer, this method will throw a DBAccessError.
Parameters
int$iServer index
string$domainDomain ID to open
int$flagsClass CONN_* constant bitfield
Returns
Database

Definition at line 938 of file LoadBalancer.php.

References key, Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_FREE, Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_FREE_NOROUND, Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_INUSE, Wikimedia\Rdbms\LoadBalancer\KEY_FOREIGN_INUSE_NOROUND, Wikimedia\Rdbms\DatabaseDomain\newFromId(), Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection(), and servers.

Referenced by Wikimedia\Rdbms\LoadBalancer\openConnection().

◆ pendingMasterChangeCallers()

Wikimedia\Rdbms\LoadBalancer::pendingMasterChangeCallers ( )

Get the list of callers that have pending master changes.

Returns
string[] List of method names

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1525 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\IDatabase\pendingWriteCallers(), and use.

◆ pickReaderIndex()

Wikimedia\Rdbms\LoadBalancer::pickReaderIndex ( array  $loads,
  $domain = false 
)
private

◆ pingAll()

Wikimedia\Rdbms\LoadBalancer::pingAll ( )

◆ reallyOpenConnection()

◆ reportConnectionError()

Wikimedia\Rdbms\LoadBalancer::reportConnectionError ( )
private

◆ reuseConnection()

Wikimedia\Rdbms\LoadBalancer::reuseConnection ( IDatabase  $conn)

◆ rollbackMasterChanges()

Wikimedia\Rdbms\LoadBalancer::rollbackMasterChanges (   $fname = __METHOD__)

Issue ROLLBACK only on master, only if queries were done on connection.

Parameters
string$fnameCaller name
Exceptions
DBExpectedError

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1429 of file LoadBalancer.php.

References $fname, false, Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\IDatabase\rollback(), Wikimedia\Rdbms\LoadBalancer\undoTransactionRoundFlags(), and use.

◆ runMasterPostTrxCallbacks()

Wikimedia\Rdbms\LoadBalancer::runMasterPostTrxCallbacks (   $type)

Issue all pending post-COMMIT/ROLLBACK callbacks.

Use this only for mutli-database commits

Parameters
int$typeIDatabase::TRIGGER_* constant
Returns
Exception|null The first exception or null if there were none

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1395 of file LoadBalancer.php.

References $e, $type, Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\Database\runTransactionListenerCallbacks(), Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression(), Wikimedia\Rdbms\Database\trxLevel(), use, and Wikimedia\Rdbms\Database\writesPending().

◆ safeGetLag()

Wikimedia\Rdbms\LoadBalancer::safeGetLag ( IDatabase  $conn)

Get the lag in seconds for a given connection, or zero if this load balancer does not have replication enabled.

This should be used in preference to Database::getLag() in cases where replication may not be in use, since there is no way to determine if replication is in use at the connection level without running potentially restricted queries such as SHOW SLAVE STATUS. Using this function instead of Database::getLag() avoids a fatal error in this case on many installations.

Parameters
IDatabase$conn
Returns
int|bool Returns false on error

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1718 of file LoadBalancer.php.

References Wikimedia\Rdbms\IDatabase\getLag(), and Wikimedia\Rdbms\LoadBalancer\getServerCount().

◆ safeWaitForMasterPos()

◆ setDomainPrefix()

Wikimedia\Rdbms\LoadBalancer::setDomainPrefix (   $prefix)

Set a new table prefix for the existing local domain ID for testing.

Parameters
string$prefix

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1800 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenConnection(), Wikimedia\Rdbms\IDatabase\getLBInfo(), Wikimedia\Rdbms\LoadBalancer\setLocalDomain(), and use.

◆ setIndexAliases()

Wikimedia\Rdbms\LoadBalancer::setIndexAliases ( array  $aliases)

Convert certain index names to alternative names before querying the DB.

Note that this applies to indexes regardless of the table they belong to.

This can be employed when an index was renamed X => Y in code, but the new Y-named indexes were not yet built on all DBs. After all the Y-named ones are added by the DBA, the aliases can be removed, and then the old X-named indexes dropped.

Parameters
string[]$aliases
Returns
mixed
Since
1.31

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1796 of file LoadBalancer.php.

◆ setLocalDomain()

Wikimedia\Rdbms\LoadBalancer::setLocalDomain ( DatabaseDomain  $domain)
private

◆ setTableAliases()

Wikimedia\Rdbms\LoadBalancer::setTableAliases ( array  $aliases)

Make certain table names use their own database, schema, and table prefix when passed into SQL queries pre-escaped and without a qualified database name.

For example, "user" can be converted to "myschema.mydbname.user" for convenience. Appearances like user, somedb.user, somedb.someschema.user will used literally.

Calling this twice will completely clear any old table aliases. Also, note that callers are responsible for making sure the schemas and databases actually exist.

Parameters
array[]$aliasesMap of (table => (dbname, schema, prefix) map)

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1792 of file LoadBalancer.php.

◆ setTransactionListener()

Wikimedia\Rdbms\LoadBalancer::setTransactionListener (   $name,
callable  $callback = null 
)

Set a callback via IDatabase::setTransactionListener() on all current and future master connections of this load balancer.

Parameters
string$nameCallback name
callable | null$callback

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1779 of file LoadBalancer.php.

References $name, Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), Wikimedia\Rdbms\IDatabase\setTransactionListener(), and use.

◆ setWaitForPositionIfHigher()

Wikimedia\Rdbms\LoadBalancer::setWaitForPositionIfHigher (   $pos)
private
Parameters
DBMasterPos | bool$pos

Definition at line 560 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\waitFor().

◆ suppressTransactionEndCallbacks()

Wikimedia\Rdbms\LoadBalancer::suppressTransactionEndCallbacks ( )

Suppress all pending post-COMMIT/ROLLBACK callbacks.

Use this only for mutli-database commits

Returns
Exception|null The first exception or null if there were none

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 1442 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\forEachOpenMasterConnection(), and Wikimedia\Rdbms\Database\setTrxEndCallbackSuppression().

◆ undoTransactionRoundFlags()

◆ waitFor()

Wikimedia\Rdbms\LoadBalancer::waitFor (   $pos)

Set the master wait position.

If a DB_REPLICA connection has been opened already, then wait immediately. Otherwise sets a variable telling it to wait if such a connection is opened.

This only applies to connections to the generic replica DB for this request. If a timeout happens when waiting, then getLaggedReplicaMode()/laggedReplicaUsed() will return true.

Parameters
DBMasterPos | bool$posMaster position or false

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 486 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$readIndex, Wikimedia\Rdbms\LoadBalancer\$waitForPos, Wikimedia\Rdbms\LoadBalancer\doWait(), and Wikimedia\Rdbms\LoadBalancer\setWaitForPositionIfHigher().

◆ waitForAll()

Wikimedia\Rdbms\LoadBalancer::waitForAll (   $pos,
  $timeout = null 
)

Set the master wait position and wait for ALL replica DBs to catch up to it.

Parameters
DBMasterPos | bool$posMaster position or false
int$timeoutMax seconds to wait; default is mWaitTimeout
Returns
bool Success (able to connect and no timeouts reached)

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 530 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$waitForPos, Wikimedia\Rdbms\LoadBalancer\$waitTimeout, Wikimedia\Rdbms\LoadBalancer\doWait(), and servers.

◆ waitForOne()

Wikimedia\Rdbms\LoadBalancer::waitForOne (   $pos,
  $timeout = null 
)

Set the master wait position and wait for a "generic" replica DB to catch up to it.

This can be used a faster proxy for waitForAll()

Parameters
DBMasterPos | bool$posMaster position or false
int$timeoutMax seconds to wait; default is mWaitTimeout
Returns
bool Success (able to connect and no timeouts reached)

Implements Wikimedia\Rdbms\ILoadBalancer.

Definition at line 503 of file LoadBalancer.php.

References Wikimedia\Rdbms\LoadBalancer\$loads, Wikimedia\Rdbms\LoadBalancer\$readIndex, Wikimedia\Rdbms\LoadBalancer\$waitForPos, Wikimedia\Rdbms\LoadBalancer\doWait(), Wikimedia\Rdbms\LoadBalancer\getWriterIndex(), and ArrayUtils\pickRandom().

Member Data Documentation

◆ $agent

string Wikimedia\Rdbms\LoadBalancer::$agent
protected

Agent name for query profiling.

Definition at line 111 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $allowLagged

bool Wikimedia\Rdbms\LoadBalancer::$allowLagged
private

Whether to disregard replica DB lag as a factor in replica DB selection.

Definition at line 51 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\allowLagged().

◆ $allReplicasDownMode

bool Wikimedia\Rdbms\LoadBalancer::$allReplicasDownMode = false
private

Whether the generic reader fell back to a lagged replica DB.

Definition at line 91 of file LoadBalancer.php.

◆ $chronologyCallback

callable null Wikimedia\Rdbms\LoadBalancer::$chronologyCallback
private

Callback to run before the first connection attempt.

Definition at line 64 of file LoadBalancer.php.

◆ $cliMode

bool Wikimedia\Rdbms\LoadBalancer::$cliMode
protected

Whether this PHP instance is for a CLI script.

Definition at line 109 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $connectionAttempted

bool Wikimedia\Rdbms\LoadBalancer::$connectionAttempted = false
private

Whether any connection has been attempted yet.

Definition at line 121 of file LoadBalancer.php.

◆ $connLogger

LoggerInterface Wikimedia\Rdbms\LoadBalancer::$connLogger
protected

Definition at line 76 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $conns

Database [][][] Wikimedia\Rdbms\LoadBalancer::$conns
private

Map of (connection category => server index => IDatabase[])

Definition at line 45 of file LoadBalancer.php.

◆ $connsOpened

int Wikimedia\Rdbms\LoadBalancer::$connsOpened = 0
private

Total connections opened.

Definition at line 97 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\closeConnection(), and Wikimedia\Rdbms\LoadBalancer\getConnection().

◆ $deprecationLogger

callable Wikimedia\Rdbms\LoadBalancer::$deprecationLogger
private

Deprecation logger.

Definition at line 116 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $disabled

bool Wikimedia\Rdbms\LoadBalancer::$disabled = false
private

Definition at line 119 of file LoadBalancer.php.

◆ $errorConnection

Database Wikimedia\Rdbms\LoadBalancer::$errorConnection
private

DB connection object that caused a problem.

Definition at line 83 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reportConnectionError().

◆ $errorLogger

callable Wikimedia\Rdbms\LoadBalancer::$errorLogger
private

Exception logger.

Definition at line 114 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $groupLoads

array [] Wikimedia\Rdbms\LoadBalancer::$groupLoads
private

Map of (group => server index => weight)

Definition at line 49 of file LoadBalancer.php.

◆ $host

◆ $indexAliases

string [] Wikimedia\Rdbms\LoadBalancer::$indexAliases = []
private

Map of (index alias => index)

Definition at line 59 of file LoadBalancer.php.

◆ $laggedReplicaMode

bool Wikimedia\Rdbms\LoadBalancer::$laggedReplicaMode = false
private

◆ $lastError

string Wikimedia\Rdbms\LoadBalancer::$lastError = 'Unknown error'
private

The last DB selection or connection error.

Definition at line 93 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\getConnection(), and Wikimedia\Rdbms\LoadBalancer\reportConnectionError().

◆ $loadMonitor

ILoadMonitor Wikimedia\Rdbms\LoadBalancer::$loadMonitor
private

Definition at line 62 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\getLoadMonitor().

◆ $loadMonitorConfig

array Wikimedia\Rdbms\LoadBalancer::$loadMonitorConfig
private

The LoadMonitor configuration.

Definition at line 55 of file LoadBalancer.php.

◆ $loads

float [] Wikimedia\Rdbms\LoadBalancer::$loads
private

◆ $localDomain

DatabaseDomain Wikimedia\Rdbms\LoadBalancer::$localDomain
private

Local Domain ID and default for selectDB() calls.

Definition at line 103 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\__construct().

◆ $localDomainIdAlias

string Wikimedia\Rdbms\LoadBalancer::$localDomainIdAlias
private

Alternate ID string for the domain instead of DatabaseDomain::getId()

Definition at line 105 of file LoadBalancer.php.

◆ $maxLag

int Wikimedia\Rdbms\LoadBalancer::$maxLag = self::MAX_LAG_DEFAULT
private

◆ $perfLogger

LoggerInterface Wikimedia\Rdbms\LoadBalancer::$perfLogger
protected

Definition at line 80 of file LoadBalancer.php.

◆ $profiler

object string Wikimedia\Rdbms\LoadBalancer::$profiler
protected

Class name or object With profileIn/profileOut methods.

Definition at line 70 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $queryLogger

LoggerInterface Wikimedia\Rdbms\LoadBalancer::$queryLogger
protected

Definition at line 78 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $readIndex

int Wikimedia\Rdbms\LoadBalancer::$readIndex
private

The generic (not query grouped) replica DB index (of $mServers)

Definition at line 85 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\getReaderIndex(), Wikimedia\Rdbms\LoadBalancer\waitFor(), and Wikimedia\Rdbms\LoadBalancer\waitForOne().

◆ $readOnlyReason

string bool Wikimedia\Rdbms\LoadBalancer::$readOnlyReason = false
private

Reason the LB is read-only or false if not.

Definition at line 95 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\getReadOnlyReason().

◆ $replLogger

LoggerInterface Wikimedia\Rdbms\LoadBalancer::$replLogger
protected

Definition at line 74 of file LoadBalancer.php.

◆ $servers

array [] Wikimedia\Rdbms\LoadBalancer::$servers
private

Map of (server index => server config array)

Definition at line 43 of file LoadBalancer.php.

◆ $srvCache

BagOStuff Wikimedia\Rdbms\LoadBalancer::$srvCache
private

Definition at line 66 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $tableAliases

Wikimedia\Rdbms\LoadBalancer::$tableAliases = []
private

Definition at line 57 of file LoadBalancer.php.

◆ $trxProfiler

TransactionProfiler Wikimedia\Rdbms\LoadBalancer::$trxProfiler
protected

Definition at line 72 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\reallyOpenConnection().

◆ $trxRecurringCallbacks

array [] Wikimedia\Rdbms\LoadBalancer::$trxRecurringCallbacks = []
private

Map of (name => callable)

Definition at line 101 of file LoadBalancer.php.

◆ $trxRoundId

string bool Wikimedia\Rdbms\LoadBalancer::$trxRoundId = false
private

String if a requested DBO_TRX transaction round is active.

Definition at line 99 of file LoadBalancer.php.

◆ $waitForPos

bool DBMasterPos Wikimedia\Rdbms\LoadBalancer::$waitForPos
private

◆ $waitTimeout

int Wikimedia\Rdbms\LoadBalancer::$waitTimeout
private

Seconds to spend waiting on replica DB lag to resolve.

Definition at line 53 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\waitForAll().

◆ $wanCache

WANObjectCache Wikimedia\Rdbms\LoadBalancer::$wanCache
private

Definition at line 68 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\masterRunningReadOnly().

◆ KEY_FOREIGN_FREE

const Wikimedia\Rdbms\LoadBalancer::KEY_FOREIGN_FREE = 'foreignFree'

◆ KEY_FOREIGN_FREE_NOROUND

const Wikimedia\Rdbms\LoadBalancer::KEY_FOREIGN_FREE_NOROUND = 'foreignFreeAutoCommit'

◆ KEY_FOREIGN_INUSE

const Wikimedia\Rdbms\LoadBalancer::KEY_FOREIGN_INUSE = 'foreignInUse'

◆ KEY_FOREIGN_INUSE_NOROUND

const Wikimedia\Rdbms\LoadBalancer::KEY_FOREIGN_INUSE_NOROUND = 'foreignInUseAutoCommit'

◆ KEY_LOCAL

const Wikimedia\Rdbms\LoadBalancer::KEY_LOCAL = 'local'

Definition at line 135 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\openConnection().

◆ KEY_LOCAL_NOROUND

const Wikimedia\Rdbms\LoadBalancer::KEY_LOCAL_NOROUND = 'localAutoCommit'

Definition at line 139 of file LoadBalancer.php.

Referenced by Wikimedia\Rdbms\LoadBalancer\openConnection().


The documentation for this class was generated from the following file: