MediaWiki
1.27.4
|
Database load balancing object. More...
Public Member Functions | |
__construct (array $params) | |
allowLagged ($mode=null) | |
Disables/enables lag checks. More... | |
clearLagTimeCache () | |
Clear the cache for slag lag delay times. More... | |
closeAll () | |
Close all open connections. More... | |
closeConnection ($conn) | |
Close a connection Using this function makes sure the LoadBalancer knows the connection is closed. More... | |
commitAll ($fname=__METHOD__) | |
Commit transactions on all open connections. More... | |
commitMasterChanges ($fname=__METHOD__) | |
Issue COMMIT only on master, only if queries were done on connection. More... | |
forEachOpenConnection ($callback, array $params=[]) | |
Call a function with each open connection object. More... | |
getAnyOpenConnection ($i) | |
Get any open connection to a given server index, local or foreign Returns false if there is no connection open. More... | |
getConnection ($i, $groups=[], $wiki=false) | |
Get a connection by index This is the main entry point for this class. More... | |
getConnectionRef ($db, $groups=[], $wiki=false) | |
Get a database connection handle reference. More... | |
getLaggedSlaveMode ($wiki=false) | |
getLagTimes ($wiki=false) | |
Get an estimate of replication lag (in seconds) for each server. More... | |
getLazyConnectionRef ($db, $groups=[], $wiki=false) | |
Get a database connection handle reference without connecting yet. More... | |
getMasterPos () | |
Get the current master position for chronology control purposes. More... | |
getMaxLag ($wiki=false) | |
Get the hostname and lag time of the most-lagged slave. More... | |
getReaderIndex ($group=false, $wiki=false) | |
Get the index of the reader connection, which may be a slave This takes into account load ratios and lag times. More... | |
getReadOnlyReason ($wiki=false) | |
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 Prefer a readable name if available. 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... | |
laggedSlaveUsed () | |
lastMasterChangeTimestamp () | |
Get the timestamp of the latest write query done by this thread. More... | |
openConnection ($i, $wiki=false) | |
Open a connection to the server given by the specified index Index must be an actual index into the array. More... | |
parentInfo ($x=null) | |
Get or set arbitrary data used by the parent object, usually an LBFactory. More... | |
pendingMasterChangeCallers () | |
Get the list of callers that have pending master changes. More... | |
pingAll () | |
reuseConnection ($conn) | |
Mark a foreign connection as being available for reuse under a different DB name or prefix. More... | |
rollbackMasterChanges ($fname=__METHOD__) | |
Issue ROLLBACK only on master, only if queries were done on connection. 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=10) | |
Wait for a slave DB to reach a specified master position. More... | |
setServerInfo ($i, array $serverInfo) | |
Sets the server info structure for the given index. More... | |
waitFor ($pos) | |
Set the master wait position If a DB_SLAVE connection has been opened already, waits Otherwise sets a variable telling it to wait if such a connection is opened. More... | |
waitForAll ($pos, $timeout=null) | |
Set the master wait position and wait for ALL slaves to catch up to it. More... | |
waitForOne ($pos, $timeout=null) | |
Set the master wait position and wait for a "generic" slave to catch up to it. More... | |
waitTimeout ($value=null) | |
Protected Member Functions | |
doWait ($index, $open=false, $timeout=null) | |
Wait for a given slave to catch up to the master pos stored in $this. More... | |
reallyOpenConnection ($server, $dbNameOverride=false) | |
Really opens a connection. More... | |
Protected Attributes | |
TransactionProfiler | $trxProfiler |
Private Member Functions | |
getLoadMonitor () | |
Get a LoadMonitor instance. More... | |
getRandomNonLagged (array $loads, $wiki=false, $maxLag=self::MAX_LAG) | |
isOpen ($index) | |
Test if the specified index represents an open connection. More... | |
openForeignConnection ($i, $wiki) | |
Open a connection to a foreign DB, or return one if it is already open. More... | |
reportConnectionError () | |
Private Attributes | |
integer | $connsOpened = 0 |
Total connections opened. More... | |
bool | $laggedSlaveMode = false |
Whether the generic reader fell back to a lagged slave. More... | |
bool | $mAllowLagged |
Whether to disregard slave lag as a factor in slave selection. More... | |
array[] | $mConns |
Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array) More... | |
bool DatabaseBase | $mErrorConnection |
Database connection that caused a problem. More... | |
array[] | $mGroupLoads |
Map of (group => server index => weight) More... | |
string | $mLastError = 'Unknown error' |
The last DB selection or connection error. More... | |
LoadMonitor | $mLoadMonitor |
string | $mLoadMonitorClass |
The LoadMonitor subclass name. More... | |
array | $mLoads |
Map of (server index => weight) More... | |
array | $mParentInfo |
LBFactory information. More... | |
integer | $mReadIndex |
The generic (not query grouped) slave index (of $mServers) More... | |
array[] | $mServers |
Map of (server index => server config array) More... | |
bool DBMasterPos | $mWaitForPos |
False if not set. More... | |
integer | $mWaitTimeout |
Seconds to spend waiting on slave lag to resolve. More... | |
string bool | $readOnlyReason = false |
Reason the LB is read-only or false if not. More... | |
bool | $slavesDownMode = false |
Whether the generic reader fell back to a lagged slave. More... | |
BagOStuff | $srvCache |
LoadBalancer::__construct | ( | array | $params | ) |
array | $params | Array with keys:
|
MWException |
Definition at line 87 of file LoadBalancer.php.
References as, and ObjectCache\getLocalServerInstance().
LoadBalancer::allowLagged | ( | $mode = null | ) |
Disables/enables lag checks.
null | bool | $mode |
Definition at line 1248 of file LoadBalancer.php.
References $mAllowLagged.
LoadBalancer::clearLagTimeCache | ( | ) |
Clear the cache for slag lag delay times.
This is only used for testing
Definition at line 1404 of file LoadBalancer.php.
References getLoadMonitor().
LoadBalancer::closeAll | ( | ) |
LoadBalancer::closeConnection | ( | $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.
DatabaseBase | $conn |
Definition at line 1005 of file LoadBalancer.php.
References $connsOpened, and as.
Referenced by doWait().
LoadBalancer::commitAll | ( | $fname = __METHOD__ | ) |
Commit transactions on all open connections.
string | $fname | Caller name |
Definition at line 1029 of file LoadBalancer.php.
LoadBalancer::commitMasterChanges | ( | $fname = __METHOD__ | ) |
Issue COMMIT only on master, only if queries were done on connection.
string | $fname | Caller name |
Definition at line 1046 of file LoadBalancer.php.
References $fname, as, and getWriterIndex().
|
protected |
Wait for a given slave to catch up to the master pos stored in $this.
int | $index | Server index |
bool | $open | Check the server even if a new connection has to be made |
int | $timeout | Max seconds to wait; default is mWaitTimeout |
Definition at line 446 of file LoadBalancer.php.
References $key, $mWaitTimeout, closeConnection(), getAnyOpenConnection(), getServerName(), openConnection(), IExpiringStore\TTL_DAY, wfBacktrace(), and wfDebugLog().
Referenced by getReaderIndex(), waitFor(), waitForAll(), and waitForOne().
LoadBalancer::forEachOpenConnection | ( | $callback, | |
array | $params = [] |
||
) |
Call a function with each open connection object.
callable | $callback | |
array | $params |
Definition at line 1281 of file LoadBalancer.php.
Referenced by LBFactory\commitMasterChanges(), and JobRunner\commitMasterChanges().
LoadBalancer::getAnyOpenConnection | ( | $i | ) |
Get any open connection to a given server index, local or foreign Returns false if there is no connection open.
int | $i |
Definition at line 429 of file LoadBalancer.php.
References as.
Referenced by doWait(), getMasterPos(), isOpen(), ChronologyProtector\shutdownLB(), and waitFor().
LoadBalancer::getConnection | ( | $i, | |
$groups = [] , |
|||
$wiki = false |
|||
) |
Get a connection by index This is the main entry point for this class.
int | $i | Server index |
array | string | bool | $groups | Query group(s), or false for the generic reader |
string | bool | $wiki | Wiki ID, or false for the current wiki |
MWException |
Definition at line 515 of file LoadBalancer.php.
References $connsOpened, $mLastError, array(), as, DB_MASTER, DB_SLAVE, getReaderIndex(), getReadOnlyReason(), getWriterIndex(), Profiler\instance(), openConnection(), reportConnectionError(), and wfWikiID().
Referenced by getConnectionRef(), getLaggedSlaveMode(), and safeWaitForMasterPos().
LoadBalancer::getConnectionRef | ( | $db, | |
$groups = [] , |
|||
$wiki = false |
|||
) |
Get a database connection handle reference.
The handle's methods wrap simply wrap those of a DatabaseBase handle
int | $db | |
array | string | bool | $groups | Query group(s), or false for the generic reader |
string | bool | $wiki | Wiki ID, or false for the current wiki |
Definition at line 641 of file LoadBalancer.php.
References getConnection().
LoadBalancer::getLaggedSlaveMode | ( | $wiki = false | ) |
string | bool | $wiki | Wiki ID, or false for the current wiki |
Definition at line 1195 of file LoadBalancer.php.
References $e, $laggedSlaveMode, DB_SLAVE, getConnection(), getServerCount(), and reuseConnection().
Referenced by getReadOnlyReason().
LoadBalancer::getLagTimes | ( | $wiki = 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
string | bool | $wiki |
Definition at line 1333 of file LoadBalancer.php.
References getLoadMonitor(), and getServerCount().
Referenced by getMaxLag(), and getRandomNonLagged().
LoadBalancer::getLazyConnectionRef | ( | $db, | |
$groups = [] , |
|||
$wiki = false |
|||
) |
Get a database connection handle reference without connecting yet.
The handle's methods wrap simply wrap those of a DatabaseBase handle
int | $db | |
array | string | bool | $groups | Query group(s), or false for the generic reader |
string | bool | $wiki | Wiki ID, or false for the current wiki |
Definition at line 657 of file LoadBalancer.php.
Referenced by reallyOpenConnection().
|
private |
Get a LoadMonitor instance.
Definition at line 146 of file LoadBalancer.php.
References $mLoadMonitor, and $mLoadMonitorClass.
Referenced by clearLagTimeCache(), getLagTimes(), and getReaderIndex().
LoadBalancer::getMasterPos | ( | ) |
Get the current master position for chronology control purposes.
Definition at line 960 of file LoadBalancer.php.
References getAnyOpenConnection().
LoadBalancer::getMaxLag | ( | $wiki = false | ) |
Get the hostname and lag time of the most-lagged slave.
This is useful for maintenance scripts that need to throttle their updates. May attempt to open connections to slaves on the default DB. If there is no lag, the maximum lag will be reported as -1.
bool | string | $wiki | Wiki ID, or false for the default database |
Definition at line 1302 of file LoadBalancer.php.
References as, getLagTimes(), and getServerCount().
array | $loads | |
bool | string | $wiki | Wiki to get non-lagged for |
int | $maxLag | Restrict the maximum allowed lag to this many seconds |
Definition at line 170 of file LoadBalancer.php.
References as, getLagTimes(), getServerName(), ArrayUtils\pickRandom(), and wfDebugLog().
Referenced by getReaderIndex().
Get the index of the reader connection, which may be a slave 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
string | bool | $group | Query group, or false for the generic reader |
string | bool | $wiki | Wiki ID, or false for the current wiki |
MWException |
Definition at line 224 of file LoadBalancer.php.
References $mLoads, $mReadIndex, $wgDBtype, doWait(), getLoadMonitor(), getRandomNonLagged(), getServerName(), getWriterIndex(), global, openConnection(), ArrayUtils\pickRandom(), reuseConnection(), and wfDebugLog().
Referenced by getConnection().
LoadBalancer::getReadOnlyReason | ( | $wiki = false | ) |
string | bool | $wiki | Wiki ID, or false for the current wiki |
Definition at line 1227 of file LoadBalancer.php.
References $readOnlyReason, and getLaggedSlaveMode().
Referenced by getConnection().
LoadBalancer::getServerCount | ( | ) |
Get the number of defined servers (not the number of open connections)
Definition at line 911 of file LoadBalancer.php.
Referenced by getLaggedSlaveMode(), getLagTimes(), getMaxLag(), ChronologyProtector\initLB(), safeGetLag(), safeWaitForMasterPos(), and ChronologyProtector\shutdownLB().
LoadBalancer::getServerInfo | ( | $i | ) |
Return the server info structure for a given index, or false if the index is invalid.
int | $i |
Definition at line 938 of file LoadBalancer.php.
LoadBalancer::getServerName | ( | $i | ) |
Get the host name or IP address of the server with the specified index Prefer a readable name if available.
string | $i |
Definition at line 921 of file LoadBalancer.php.
References $name.
Referenced by doWait(), getRandomNonLagged(), getReaderIndex(), ChronologyProtector\initLB(), LBFactory\logMultiDbTransaction(), openConnection(), reallyOpenConnection(), LBFactory\shutdownChronologyProtector(), and ChronologyProtector\shutdownLB().
LoadBalancer::getWriterIndex | ( | ) |
Definition at line 882 of file LoadBalancer.php.
Referenced by commitMasterChanges(), getConnection(), getReaderIndex(), hasMasterChanges(), hasMasterConnection(), ChronologyProtector\initLB(), lastMasterChangeTimestamp(), LBFactory\logMultiDbTransaction(), pendingMasterChangeCallers(), rollbackMasterChanges(), LBFactory\shutdownChronologyProtector(), ChronologyProtector\shutdownLB(), and waitForOne().
LoadBalancer::hasMasterChanges | ( | ) |
Determine if there are pending changes in a transaction by this thread.
Definition at line 1107 of file LoadBalancer.php.
References as, and getWriterIndex().
Referenced by LBFactory\hasMasterChanges(), and hasOrMadeRecentMasterChanges().
LoadBalancer::hasMasterConnection | ( | ) |
Definition at line 1098 of file LoadBalancer.php.
References getWriterIndex(), and isOpen().
LoadBalancer::hasOrMadeRecentMasterChanges | ( | $age = null | ) |
Check if this load balancer object had any recent or still pending writes issued against it by this PHP thread.
float | $age | How many seconds ago is "recent" [defaults to mWaitTimeout] |
Definition at line 1151 of file LoadBalancer.php.
References hasMasterChanges(), and lastMasterChangeTimestamp().
Referenced by LBFactory\hasOrMadeRecentMasterChanges().
LoadBalancer::haveIndex | ( | $i | ) |
Returns true if the specified index is a valid server index.
string | $i |
Definition at line 892 of file LoadBalancer.php.
LoadBalancer::isNonZeroLoad | ( | $i | ) |
Returns true if the specified index is valid and has non-zero load.
string | $i |
Definition at line 902 of file LoadBalancer.php.
|
private |
Test if the specified index represents an open connection.
int | $index | Server index |
Definition at line 787 of file LoadBalancer.php.
References getAnyOpenConnection().
Referenced by hasMasterConnection().
LoadBalancer::laggedSlaveUsed | ( | ) |
Definition at line 1217 of file LoadBalancer.php.
References $laggedSlaveMode.
Referenced by LBFactory\laggedSlaveUsed().
LoadBalancer::lastMasterChangeTimestamp | ( | ) |
Get the timestamp of the latest write query done by this thread.
Definition at line 1128 of file LoadBalancer.php.
References as, and getWriterIndex().
Referenced by hasOrMadeRecentMasterChanges().
LoadBalancer::openConnection | ( | $i, | |
$wiki = false |
|||
) |
Open a connection to the server given by the specified index Index must be an actual index into the array.
If the server is already open, returns it.
On error, returns false, and the connection which caused the error will be available via $this->mErrorConnection.
int | $i | Server index |
string | bool | $wiki | Wiki ID, or false for the current wiki |
Definition at line 673 of file LoadBalancer.php.
References getServerName(), openForeignConnection(), reallyOpenConnection(), and wfDebugLog().
Referenced by doWait(), getConnection(), and getReaderIndex().
|
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 wiki. 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->mErrorConnection.
int | $i | Server index |
string | $wiki | Wiki ID to open |
Definition at line 723 of file LoadBalancer.php.
References key, list, reallyOpenConnection(), wfDebug(), wfHostname(), and wfSplitWikiID().
Referenced by openConnection().
LoadBalancer::parentInfo | ( | $x = null | ) |
Get or set arbitrary data used by the parent object, usually an LBFactory.
mixed | $x |
Definition at line 160 of file LoadBalancer.php.
References wfSetVar().
Referenced by ChronologyProtector\initLB(), and ChronologyProtector\shutdownLB().
LoadBalancer::pendingMasterChangeCallers | ( | ) |
Get the list of callers that have pending master changes.
Definition at line 1164 of file LoadBalancer.php.
References as, and getWriterIndex().
Referenced by LBFactory\logMultiDbTransaction().
LoadBalancer::pingAll | ( | ) |
|
protected |
Really opens a connection.
Uncached. Returns a Database object whether or not the connection was successful.
array | $server | |
bool | $dbNameOverride |
MWException |
Definition at line 805 of file LoadBalancer.php.
References $e, DB_MASTER, DatabaseBase\factory(), getLazyConnectionRef(), getServerName(), wfBacktrace(), and wfDebugLog().
Referenced by openConnection(), and openForeignConnection().
|
private |
DBConnectionError |
Definition at line 848 of file LoadBalancer.php.
References $context, $mErrorConnection, $mLastError, and wfLogDBError().
Referenced by getConnection().
LoadBalancer::reuseConnection | ( | $conn | ) |
Mark a foreign connection as being available for reuse under a different DB name or prefix.
This mechanism is reference-counted, and must be called the same number of times as getConnection() to work.
DatabaseBase | $conn |
MWException |
Definition at line 590 of file LoadBalancer.php.
References wfDebug().
Referenced by getLaggedSlaveMode(), and getReaderIndex().
LoadBalancer::rollbackMasterChanges | ( | $fname = __METHOD__ | ) |
Issue ROLLBACK only on master, only if queries were done on connection.
string | $fname | Caller name |
DBExpectedError |
Definition at line 1067 of file LoadBalancer.php.
References $e, $fname, as, getWriterIndex(), and MWExceptionHandler\logException().
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.
IDatabase | $conn |
Definition at line 1356 of file LoadBalancer.php.
References IDatabase\getLag(), and getServerCount().
Wait for a slave DB to reach a specified master position.
This will connect to the master to get an accurate position if $pos is not given
IDatabase | $conn | Slave DB |
DBMasterPos | bool | $pos | Master position; default: current position |
integer | $timeout | Timeout in seconds |
Definition at line 1375 of file LoadBalancer.php.
References DB_MASTER, getConnection(), IDatabase\getLBInfo(), getServerCount(), IDatabase\masterPosWait(), wfBacktrace(), and wfDebugLog().
LoadBalancer::setServerInfo | ( | $i, | |
array | $serverInfo | ||
) |
Sets the server info structure for the given index.
Entry at index $i is created if it doesn't exist
int | $i | |
array | $serverInfo |
Definition at line 952 of file LoadBalancer.php.
LoadBalancer::waitFor | ( | $pos | ) |
Set the master wait position If a DB_SLAVE connection has been opened already, waits Otherwise sets a variable telling it to wait if such a connection is opened.
DBMasterPos | $pos |
Definition at line 359 of file LoadBalancer.php.
References $mReadIndex, doWait(), and getAnyOpenConnection().
Referenced by ChronologyProtector\initLB().
LoadBalancer::waitForAll | ( | $pos, | |
$timeout = null |
|||
) |
Set the master wait position and wait for ALL slaves to catch up to it.
DBMasterPos | $pos | |
int | $timeout | Max seconds to wait; default is mWaitTimeout |
Definition at line 408 of file LoadBalancer.php.
References doWait().
Referenced by LBFactory\shutdownChronologyProtector().
LoadBalancer::waitForOne | ( | $pos, | |
$timeout = null |
|||
) |
Set the master wait position and wait for a "generic" slave to catch up to it.
This can be used a faster proxy for waitForAll()
DBMasterPos | $pos | |
int | $timeout | Max seconds to wait; default is mWaitTimeout |
Definition at line 381 of file LoadBalancer.php.
References $mLoads, $mReadIndex, doWait(), getWriterIndex(), and ArrayUtils\pickRandom().
LoadBalancer::waitTimeout | ( | $value = null | ) |
mixed | $value |
Definition at line 1185 of file LoadBalancer.php.
References $value, and wfSetVar().
|
private |
Total connections opened.
Definition at line 68 of file LoadBalancer.php.
Referenced by closeConnection(), and getConnection().
|
private |
Whether the generic reader fell back to a lagged slave.
Definition at line 60 of file LoadBalancer.php.
Referenced by getLaggedSlaveMode(), and laggedSlaveUsed().
|
private |
Whether to disregard slave lag as a factor in slave selection.
Definition at line 40 of file LoadBalancer.php.
Referenced by allowLagged().
|
private |
Map of (local/foreignUsed/foreignFree => server index => DatabaseBase array)
Definition at line 34 of file LoadBalancer.php.
|
private |
Database connection that caused a problem.
Definition at line 54 of file LoadBalancer.php.
Referenced by reportConnectionError().
|
private |
Map of (group => server index => weight)
Definition at line 38 of file LoadBalancer.php.
|
private |
The last DB selection or connection error.
Definition at line 64 of file LoadBalancer.php.
Referenced by getConnection(), and reportConnectionError().
|
private |
Definition at line 49 of file LoadBalancer.php.
Referenced by getLoadMonitor().
|
private |
The LoadMonitor subclass name.
Definition at line 47 of file LoadBalancer.php.
Referenced by getLoadMonitor().
|
private |
Map of (server index => weight)
Definition at line 36 of file LoadBalancer.php.
Referenced by getReaderIndex(), and waitForOne().
|
private |
LBFactory information.
Definition at line 44 of file LoadBalancer.php.
|
private |
The generic (not query grouped) slave index (of $mServers)
Definition at line 56 of file LoadBalancer.php.
Referenced by getReaderIndex(), waitFor(), and waitForOne().
|
private |
Map of (server index => server config array)
Definition at line 32 of file LoadBalancer.php.
|
private |
False if not set.
Definition at line 58 of file LoadBalancer.php.
|
private |
Seconds to spend waiting on slave lag to resolve.
Definition at line 42 of file LoadBalancer.php.
Referenced by doWait().
Reason the LB is read-only or false if not.
Definition at line 66 of file LoadBalancer.php.
Referenced by getReadOnlyReason().
|
private |
Whether the generic reader fell back to a lagged slave.
Definition at line 62 of file LoadBalancer.php.
|
private |
Definition at line 51 of file LoadBalancer.php.
|
protected |
Definition at line 71 of file LoadBalancer.php.
Referenced by LoadBalancerSingle\__construct().