MediaWiki
REL1_24
|
Database load balancing object. More...
Public Member Functions | |
__construct ($params) | |
allowLagged ($mode=null) | |
Disables/enables lag checks. | |
clearLagTimeCache () | |
Clear the cache for getLagTimes. | |
closeAll () | |
Close all open connections. | |
closeConnection ($conn) | |
Close a connection Using this function makes sure the LoadBalancer knows the connection is closed. | |
commitAll () | |
Commit transactions on all open connections. | |
commitMasterChanges () | |
Issue COMMIT only on master, only if queries were done on connection. | |
forEachOpenConnection ($callback, $params=array()) | |
Call a function with each open connection object. | |
getAnyOpenConnection ($i) | |
Get any open connection to a given server index, local or foreign Returns false if there is no connection open. | |
& | getConnection ($i, $groups=array(), $wiki=false) |
Get a connection by index This is the main entry point for this class. | |
getConnectionRef ($db, $groups=array(), $wiki=false) | |
Get a database connection handle reference. | |
getLaggedSlaveMode () | |
getLagTimes ($wiki=false) | |
Get lag time for each server Results are cached for a short time in memcached, and indefinitely in the process cache. | |
getLazyConnectionRef ($db, $groups=array(), $wiki=false) | |
Get a database connection handle reference without connecting yet. | |
getLoadMonitor () | |
Get a LoadMonitor instance. | |
getMasterPos () | |
Get the current master position for chronology control purposes. | |
getMaxLag ($wiki=false) | |
Get the hostname and lag time of the most-lagged slave. | |
getRandomNonLagged ($loads, $wiki=false) | |
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. | |
getServerCount () | |
Get the number of defined servers (not the number of open connections) | |
getServerInfo ($i) | |
Return the server info structure for a given index, or false if the index is invalid. | |
getServerName ($i) | |
Get the host name or IP address of the server with the specified index Prefer a readable name if available. | |
getWriterIndex () | |
hasMasterChanges () | |
Determine if there are any pending changes that need to be rolled back or committed. | |
hasMasterConnection () | |
haveIndex ($i) | |
Returns true if the specified index is a valid server index. | |
isNonZeroLoad ($i) | |
Returns true if the specified index is valid and has non-zero load. | |
isOpen ($index) | |
Test if the specified index represents an open connection. | |
openConnection ($i, $wiki=false) | |
Open a connection to the server given by the specified index Index must be an actual index into the array. | |
openForeignConnection ($i, $wiki) | |
Open a connection to a foreign DB, or return one if it is already open. | |
parentInfo ($x=null) | |
Get or set arbitrary data used by the parent object, usually an LBFactory. | |
pickRandom ($weights) | |
Given an array of non-normalised probabilities, this function will select an element and return the appropriate key. | |
pingAll () | |
reallyOpenConnection ($server, $dbNameOverride=false) | |
Really opens a connection. | |
reuseConnection ($conn) | |
Mark a foreign connection as being available for reuse under a different DB name or prefix. | |
rollbackMasterChanges () | |
Issue ROLLBACK only on master, only if queries were done on connection. | |
safeGetLag ($conn) | |
Get the lag in seconds for a given connection, or zero if this load balancer does not have replication enabled. | |
setServerInfo ($i, $serverInfo) | |
Sets the server info structure for the given index. | |
sleep ($t) | |
Wait for a specified number of microseconds, and return the period waited. | |
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. | |
waitForAll ($pos, $timeout=null) | |
Set the master wait position and wait for ALL slaves to catch up to it. | |
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. | |
Private Member Functions | |
reportConnectionError () | |
Private Attributes | |
bool | $mAllowLagged |
Whether to disregard slave lag as a factor in slave selection *. | |
$mConns | |
bool DatabaseBase | $mErrorConnection |
Database connection that caused a problem *. | |
array | $mGroupLoads |
Map of (group => server index => weight) *. | |
bool | $mLaggedSlaveMode |
Whether the generic reader fell back to a lagged slave *. | |
array | $mLagTimes |
Process cache of LoadMonitor::getLagTimes() *. | |
string | $mLastError = 'Unknown error' |
The last DB selection or connection error *. | |
LoadMonitor | $mLoadMonitor |
* | |
string | $mLoadMonitorClass |
The LoadMonitor subclass name *. | |
array | $mLoads |
Map of (server index => weight) *. | |
array | $mParentInfo |
LBFactory information *. | |
integer | $mReadIndex |
The generic (not query grouped) slave index (of $mServers) *. | |
array | $mServers |
Map of (server index => server config array) *. | |
bool DBMasterPos | $mWaitForPos |
False if not set *. | |
integer | $mWaitTimeout |
Seconds to spend waiting on slave lag to resolve *. |
LoadBalancer::__construct | ( | $ | params | ) |
array | $params | Array with keys: servers Required. Array of server info structures. loadMonitor Name of a class used to fetch server lag and load. |
MWException |
Reimplemented in LoadBalancerSingle.
Definition at line 56 of file LoadBalancer.php.
LoadBalancer::allowLagged | ( | $ | mode = null | ) |
Disables/enables lag checks.
null | bool | $mode |
Definition at line 1015 of file LoadBalancer.php.
Clear the cache for getLagTimes.
Definition at line 1161 of file LoadBalancer.php.
Close all open connections.
Definition at line 868 of file LoadBalancer.php.
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 890 of file LoadBalancer.php.
Commit transactions on all open connections.
Definition at line 912 of file LoadBalancer.php.
Issue COMMIT only on master, only if queries were done on connection.
Definition at line 928 of file LoadBalancer.php.
References as.
LoadBalancer::doWait | ( | $ | index, |
$ | open = false , |
||
$ | timeout = null |
||
) | [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 382 of file LoadBalancer.php.
LoadBalancer::forEachOpenConnection | ( | $ | callback, |
$ | params = array() |
||
) |
Call a function with each open connection object.
callable | $callback | |
array | $params |
Definition at line 1048 of file LoadBalancer.php.
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 365 of file LoadBalancer.php.
Referenced by waitForAll().
& LoadBalancer::getConnection | ( | $ | i, |
$ | groups = array() , |
||
$ | wiki = false |
||
) |
Get a connection by index This is the main entry point for this class.
int | $i | Server index |
array | $groups | Query groups |
bool | string | $wiki | Wiki ID |
MWException |
Definition at line 427 of file LoadBalancer.php.
LoadBalancer::getConnectionRef | ( | $ | db, |
$ | groups = array() , |
||
$ | wiki = false |
||
) |
Get a database connection handle reference.
The handle's methods wrap simply wrap those of a DatabaseBase handle
int | $db | |
mixed | $groups | |
bool | string | $wiki |
Definition at line 549 of file LoadBalancer.php.
Definition at line 1006 of file LoadBalancer.php.
LoadBalancer::getLagTimes | ( | $ | wiki = false | ) |
Get lag time for each server Results are cached for a short time in memcached, and indefinitely in the process cache.
string | bool | $wiki |
Definition at line 1119 of file LoadBalancer.php.
LoadBalancer::getLazyConnectionRef | ( | $ | db, |
$ | groups = array() , |
||
$ | 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 | |
mixed | $groups | |
bool | string | $wiki |
Definition at line 565 of file LoadBalancer.php.
Get a LoadMonitor instance.
Definition at line 104 of file LoadBalancer.php.
References array().
Get the current master position for chronology control purposes.
Definition at line 842 of file LoadBalancer.php.
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 1068 of file LoadBalancer.php.
LoadBalancer::getRandomNonLagged | ( | $ | loads, |
$ | wiki = false |
||
) |
array | $loads | |
bool | string | $wiki | Wiki to get non-lagged for |
Definition at line 140 of file LoadBalancer.php.
LoadBalancer::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.
It should always return a consistent index during a given invocation
Side effect: opens connections to databases
bool | string | $group | |
bool | string | $wiki |
MWException |
Definition at line 189 of file LoadBalancer.php.
Get the number of defined servers (not the number of open connections)
Definition at line 795 of file LoadBalancer.php.
LoadBalancer::getServerInfo | ( | $ | i | ) |
Return the server info structure for a given index, or false if the index is invalid.
int | $i |
Definition at line 820 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 805 of file LoadBalancer.php.
Definition at line 766 of file LoadBalancer.php.
Determine if there are any pending changes that need to be rolled back or committed.
Definition at line 978 of file LoadBalancer.php.
Definition at line 968 of file LoadBalancer.php.
LoadBalancer::haveIndex | ( | $ | i | ) |
Returns true if the specified index is a valid server index.
string | $i |
Definition at line 776 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 786 of file LoadBalancer.php.
LoadBalancer::isOpen | ( | $ | index | ) |
Test if the specified index represents an open connection.
int | $index | Server index |
Definition at line 693 of file LoadBalancer.php.
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 |
bool | string | $wiki | Wiki ID to open |
Definition at line 583 of file LoadBalancer.php.
LoadBalancer::openForeignConnection | ( | $ | i, |
$ | wiki | ||
) |
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 629 of file LoadBalancer.php.
LoadBalancer::parentInfo | ( | $ | x = null | ) |
Get or set arbitrary data used by the parent object, usually an LBFactory.
mixed | $x |
Definition at line 118 of file LoadBalancer.php.
LoadBalancer::pickRandom | ( | $ | weights | ) |
Given an array of non-normalised probabilities, this function will select an element and return the appropriate key.
array | $weights |
Definition at line 131 of file LoadBalancer.php.
Definition at line 1027 of file LoadBalancer.php.
LoadBalancer::reallyOpenConnection | ( | $ | server, |
$ | dbNameOverride = false |
||
) |
Really opens a connection.
Uncached. Returns a Database object whether or not the connection was successful.
array | $server | |
bool | $dbNameOverride |
MWException |
Reimplemented in LoadBalancerSingle.
Definition at line 711 of file LoadBalancer.php.
LoadBalancer::reportConnectionError | ( | ) | [private] |
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 496 of file LoadBalancer.php.
Issue ROLLBACK only on master, only if queries were done on connection.
Definition at line 948 of file LoadBalancer.php.
LoadBalancer::safeGetLag | ( | $ | 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.
DatabaseBase | $conn |
Definition at line 1150 of file LoadBalancer.php.
LoadBalancer::setServerInfo | ( | $ | i, |
$ | 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 834 of file LoadBalancer.php.
LoadBalancer::sleep | ( | $ | t | ) |
Wait for a specified number of microseconds, and return the period waited.
int | $t |
Definition at line 307 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 322 of file LoadBalancer.php.
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 342 of file LoadBalancer.php.
References getAnyOpenConnection().
LoadBalancer::waitTimeout | ( | $ | value = null | ) |
bool LoadBalancer::$mAllowLagged [private] |
Whether to disregard slave lag as a factor in slave selection *.
Definition at line 36 of file LoadBalancer.php.
LoadBalancer::$mConns [private] |
Definition at line 33 of file LoadBalancer.php.
bool DatabaseBase LoadBalancer::$mErrorConnection [private] |
Database connection that caused a problem *.
Definition at line 43 of file LoadBalancer.php.
array LoadBalancer::$mGroupLoads [private] |
Map of (group => server index => weight) *.
Definition at line 35 of file LoadBalancer.php.
bool LoadBalancer::$mLaggedSlaveMode [private] |
Whether the generic reader fell back to a lagged slave *.
Definition at line 46 of file LoadBalancer.php.
array LoadBalancer::$mLagTimes [private] |
Process cache of LoadMonitor::getLagTimes() *.
Definition at line 48 of file LoadBalancer.php.
string LoadBalancer::$mLastError = 'Unknown error' [private] |
The last DB selection or connection error *.
Definition at line 47 of file LoadBalancer.php.
LoadMonitor LoadBalancer::$mLoadMonitor [private] |
*
Definition at line 41 of file LoadBalancer.php.
string LoadBalancer::$mLoadMonitorClass [private] |
The LoadMonitor subclass name *.
Definition at line 40 of file LoadBalancer.php.
array LoadBalancer::$mLoads [private] |
Map of (server index => weight) *.
Definition at line 34 of file LoadBalancer.php.
array LoadBalancer::$mParentInfo [private] |
LBFactory information *.
Definition at line 39 of file LoadBalancer.php.
integer LoadBalancer::$mReadIndex [private] |
The generic (not query grouped) slave index (of $mServers) *.
Definition at line 44 of file LoadBalancer.php.
array LoadBalancer::$mServers [private] |
Map of (server index => server config array) *.
Definition at line 31 of file LoadBalancer.php.
bool DBMasterPos LoadBalancer::$mWaitForPos [private] |
False if not set *.
Definition at line 45 of file LoadBalancer.php.
integer LoadBalancer::$mWaitTimeout [private] |
Seconds to spend waiting on slave lag to resolve *.
Definition at line 37 of file LoadBalancer.php.