MediaWiki  1.23.2
LoadBalancer Class Reference

Database load balancing object. More...

Inheritance diagram for LoadBalancer:
Collaboration diagram for LoadBalancer:

Public Member Functions

 __construct ( $params)
 
 allowLagged ( $mode=null)
 Disables/enables lag checks. More...
 
 clearLagTimeCache ()
 Clear the cache for getLagTimes. 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...
 
 closeConnecton ( $conn)
 Deprecated function, typo in function name. More...
 
 commitAll ()
 Commit transactions on all open connections. More...
 
 commitMasterChanges ()
 Issue COMMIT only on master, only if queries were done on connection. More...
 
 forEachOpenConnection ( $callback, $params=array())
 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=array(), $wiki=false)
 Get a connection by index This is the main entry point for this class. More...
 
 getConnectionRef ( $db, $groups=array(), $wiki=false)
 Get a database connection handle reference. More...
 
 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. More...
 
 getLazyConnectionRef ( $db, $groups=array(), $wiki=false)
 Get a database connection handle reference without connecting yet. More...
 
 getLoadMonitor ()
 Get a LoadMonitor instance. 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...
 
 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. More...
 
 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 any pending changes that need to be rolled back or committed. 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...
 
 isOpen ( $index)
 Test if the specified index represents an open connection. 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...
 
 openForeignConnection ( $i, $wiki)
 Open a connection to a foreign DB, or return one if it is already open. More...
 
 parentInfo ( $x=null)
 Get or set arbitrary data used by the parent object, usually an LBFactory. More...
 
 pickRandom ( $weights)
 Given an array of non-normalised probabilities, this function will select an element and return the appropriate key. More...
 
 pingAll ()
 
 reallyOpenConnection ( $server, $dbNameOverride=false)
 Really opens a connection. More...
 
 reuseConnection ( $conn)
 Mark a foreign connection as being available for reuse under a different DB name or prefix. More...
 
 rollbackMasterChanges ()
 Issue ROLLBACK only on master, only if queries were done on connection. More...
 
 safeGetLag ( $conn)
 Get the lag in seconds for a given connection, or zero if this load balancer does not have replication enabled. More...
 
 setServerInfo ( $i, $serverInfo)
 Sets the server info structure for the given index. More...
 
 sleep ( $t)
 Wait for a specified number of microseconds, and return the period waited. 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)
 Set the master wait position and wait for ALL slaves to catch up to it. More...
 
 waitTimeout ( $value=null)
 

Public Attributes

 $mAllowLagged
 
 $mConns
 
 $mGroupLoads
 
 $mLagTimes
 
 $mLastError = 'Unknown error'
 
 $mLoadMonitor
 
 $mLoads
 

Protected Member Functions

 doWait ( $index, $open=false)
 Wait for a given slave to catch up to the master pos stored in $this. More...
 

Private Member Functions

 reportConnectionError ()
 

Private Attributes

bool DatabaseBase $mErrorConnection
 Database connection that caused a problem *. More...
 
 $mLaggedSlaveMode
 
 $mLoadMonitorClass
 
 $mParentInfo
 
 $mReadIndex
 
 $mServers
 
bool DBMasterPos $mWaitForPos
 False if not set *. More...
 
 $mWaitTimeout
 

Detailed Description

Database load balancing object.

Todo:
document

Definition at line 30 of file LoadBalancer.php.

Constructor & Destructor Documentation

◆ __construct()

LoadBalancer::__construct (   $params)
Parameters
array$paramswith keys: servers Required. Array of server info structures. masterWaitTimeout Replication lag wait timeout loadMonitor Name of a class used to fetch server lag and load.
Exceptions
MWException

Reimplemented in LoadBalancerSingle.

Definition at line 50 of file LoadBalancer.php.

References $params, array(), and as.

Member Function Documentation

◆ allowLagged()

LoadBalancer::allowLagged (   $mode = null)

Disables/enables lag checks.

Parameters
null | bool$mode
Returns
bool

Definition at line 1013 of file LoadBalancer.php.

References $mAllowLagged.

◆ clearLagTimeCache()

LoadBalancer::clearLagTimeCache ( )

Clear the cache for getLagTimes.

Definition at line 1143 of file LoadBalancer.php.

◆ closeAll()

LoadBalancer::closeAll ( )

Close all open connections.

Definition at line 863 of file LoadBalancer.php.

References array(), and as.

◆ closeConnection()

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.

Parameters
DatabaseBase$conn

Definition at line 896 of file LoadBalancer.php.

References as.

Referenced by closeConnecton().

◆ closeConnecton()

LoadBalancer::closeConnecton (   $conn)

Deprecated function, typo in function name.

Deprecated:
in 1.18
Parameters
DatabaseBase$conn

Definition at line 885 of file LoadBalancer.php.

References closeConnection(), and wfDeprecated().

◆ commitAll()

LoadBalancer::commitAll ( )

Commit transactions on all open connections.

Definition at line 918 of file LoadBalancer.php.

References as.

◆ commitMasterChanges()

LoadBalancer::commitMasterChanges ( )

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

Definition at line 934 of file LoadBalancer.php.

References as, and getWriterIndex().

◆ doWait()

LoadBalancer::doWait (   $index,
  $open = false 
)
protected

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

Parameters
$index
$openbool
Returns
bool

Definition at line 374 of file LoadBalancer.php.

References getAnyOpenConnection(), openConnection(), and wfDebug().

Referenced by getReaderIndex(), waitFor(), and waitForAll().

◆ forEachOpenConnection()

LoadBalancer::forEachOpenConnection (   $callback,
  $params = array() 
)

Call a function with each open connection object.

Parameters
callable$callback
array$params

Definition at line 1046 of file LoadBalancer.php.

References $params, array(), and as.

◆ getAnyOpenConnection()

LoadBalancer::getAnyOpenConnection (   $i)

Get any open connection to a given server index, local or foreign Returns false if there is no connection open.

Parameters
int$i
Returns
DatabaseBase|bool False on failure

Definition at line 358 of file LoadBalancer.php.

References as.

Referenced by doWait(), getMasterPos(), getMaxLag(), isOpen(), and waitFor().

◆ getConnection()

& LoadBalancer::getConnection (   $i,
  $groups = array(),
  $wiki = false 
)

Get a connection by index This is the main entry point for this class.

Parameters
$iInteger: server index
array$groupsquery groups
bool | string$wikiWiki ID
Exceptions
MWException
Returns
DatabaseBase

Definition at line 418 of file LoadBalancer.php.

References $mLastError, as, DB_LAST, DB_MASTER, DB_SLAVE, getReaderIndex(), getServerName(), getWriterIndex(), openConnection(), reportConnectionError(), wfDebug(), wfProfileIn(), wfProfileOut(), and wfWikiID().

Referenced by getConnectionRef().

◆ getConnectionRef()

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

See also
LoadBalancer::getConnection() for parameter information
Parameters
integer$db
mixed$groups
bool | string$wiki
Returns
DBConnRef

Definition at line 544 of file LoadBalancer.php.

References getConnection().

◆ getLaggedSlaveMode()

LoadBalancer::getLaggedSlaveMode ( )
Returns
bool

Definition at line 1004 of file LoadBalancer.php.

References $mLaggedSlaveMode.

◆ getLagTimes()

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.

Parameters
string | bool$wiki
Returns
array

Definition at line 1101 of file LoadBalancer.php.

References $mLagTimes, array(), getLoadMonitor(), and getServerCount().

Referenced by getRandomNonLagged().

◆ getLazyConnectionRef()

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

See also
LoadBalancer::getConnection() for parameter information
Parameters
integer$db
mixed$groups
bool | string$wiki
Returns
DBConnRef

Definition at line 560 of file LoadBalancer.php.

References array().

◆ getLoadMonitor()

LoadBalancer::getLoadMonitor ( )

Get a LoadMonitor instance.

Returns
LoadMonitor

Definition at line 103 of file LoadBalancer.php.

References $mLoadMonitor, and $mLoadMonitorClass.

Referenced by getLagTimes(), and getReaderIndex().

◆ getMasterPos()

LoadBalancer::getMasterPos ( )

Get the current master position for chronology control purposes.

Returns
mixed

Definition at line 837 of file LoadBalancer.php.

References getAnyOpenConnection(), and wfDebug().

◆ getMaxLag()

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.

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

Definition at line 1066 of file LoadBalancer.php.

References array(), as, getAnyOpenConnection(), getServerCount(), and openConnection().

◆ getRandomNonLagged()

LoadBalancer::getRandomNonLagged (   $loads,
  $wiki = false 
)
Parameters
array$loads
bool | string$wikiWiki to get non-lagged for
Returns
bool|int|string

Definition at line 139 of file LoadBalancer.php.

References as, getLagTimes(), ArrayUtils\pickRandom(), and wfDebugLog().

Referenced by getReaderIndex().

◆ getReaderIndex()

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

Parameters
bool | string$group
bool | string$wiki
Exceptions
MWException
Returns
bool|int|string

Definition at line 188 of file LoadBalancer.php.

References $mLoads, $mReadIndex, $section, doWait(), getLoadMonitor(), getRandomNonLagged(), getWriterIndex(), global, openConnection(), ArrayUtils\pickRandom(), reuseConnection(), wfDebug(), and wfDebugLog().

Referenced by getConnection().

◆ getServerCount()

LoadBalancer::getServerCount ( )

Get the number of defined servers (not the number of open connections)

Returns
int

Definition at line 790 of file LoadBalancer.php.

Referenced by getLagTimes(), getMaxLag(), and safeGetLag().

◆ getServerInfo()

LoadBalancer::getServerInfo (   $i)

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

Parameters
$i
Returns
bool

Definition at line 815 of file LoadBalancer.php.

◆ getServerName()

LoadBalancer::getServerName (   $i)

Get the host name or IP address of the server with the specified index Prefer a readable name if available.

Parameters
string$i
Returns
string

Definition at line 800 of file LoadBalancer.php.

Referenced by getConnection().

◆ getWriterIndex()

LoadBalancer::getWriterIndex ( )

◆ hasMasterChanges()

LoadBalancer::hasMasterChanges ( )

Determine if there are any pending changes that need to be rolled back or committed.

Since
1.23
Returns
bool

Definition at line 976 of file LoadBalancer.php.

References as, and getWriterIndex().

◆ haveIndex()

LoadBalancer::haveIndex (   $i)

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

Parameters
string$i
Returns
bool

Definition at line 771 of file LoadBalancer.php.

◆ isNonZeroLoad()

LoadBalancer::isNonZeroLoad (   $i)

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

Parameters
string$i
Returns
bool

Definition at line 781 of file LoadBalancer.php.

◆ isOpen()

LoadBalancer::isOpen (   $index)

Test if the specified index represents an open connection.

Parameters
$indexInteger: server index
Access:\n private
Returns
bool

Definition at line 688 of file LoadBalancer.php.

References getAnyOpenConnection().

◆ openConnection()

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.

Parameters
$iInteger server index
bool | string$wikiwiki ID to open
Returns
DatabaseBase
Access:\n private

Definition at line 578 of file LoadBalancer.php.

References openForeignConnection(), reallyOpenConnection(), wfDebug(), wfProfileIn(), and wfProfileOut().

Referenced by doWait(), getConnection(), getMaxLag(), and getReaderIndex().

◆ openForeignConnection()

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.

Parameters
$iInteger: server index
string$wikiwiki ID to open
Returns
DatabaseBase

Definition at line 624 of file LoadBalancer.php.

References key, list, reallyOpenConnection(), wfDebug(), wfHostname(), wfProfileIn(), wfProfileOut(), and wfSplitWikiID().

Referenced by openConnection().

◆ parentInfo()

LoadBalancer::parentInfo (   $x = null)

Get or set arbitrary data used by the parent object, usually an LBFactory.

Parameters
$x
Returns
mixed

Definition at line 117 of file LoadBalancer.php.

References wfSetVar().

◆ pickRandom()

LoadBalancer::pickRandom (   $weights)

Given an array of non-normalised probabilities, this function will select an element and return the appropriate key.

Deprecated:
since 1.21, use ArrayUtils::pickRandom()
Parameters
array$weights
Returns
bool|int|string

Definition at line 130 of file LoadBalancer.php.

References ArrayUtils\pickRandom().

◆ pingAll()

LoadBalancer::pingAll ( )
Returns
bool

Definition at line 1025 of file LoadBalancer.php.

References $success, and as.

◆ reallyOpenConnection()

LoadBalancer::reallyOpenConnection (   $server,
  $dbNameOverride = false 
)

Really opens a connection.

Uncached. Returns a Database object whether or not the connection was successful.

Access:\n private
Parameters
$server
$dbNameOverridebool
Exceptions
MWException
Returns
DatabaseBase

Reimplemented in LoadBalancerSingle.

Definition at line 706 of file LoadBalancer.php.

References $e, and DatabaseBase\factory().

Referenced by openConnection(), and openForeignConnection().

◆ reportConnectionError()

LoadBalancer::reportConnectionError ( )
private
Exceptions
DBConnectionError
Returns
bool

Definition at line 740 of file LoadBalancer.php.

References $mErrorConnection, and wfLogDBError().

Referenced by getConnection().

◆ reuseConnection()

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.

Parameters
DatabaseBase$conn
Exceptions
MWException

Definition at line 491 of file LoadBalancer.php.

References wfDebug().

Referenced by getReaderIndex().

◆ rollbackMasterChanges()

LoadBalancer::rollbackMasterChanges ( )

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

Since
1.23

Definition at line 954 of file LoadBalancer.php.

References as, and getWriterIndex().

◆ safeGetLag()

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.

Parameters
DatabaseBase$conn
Returns
int

Definition at line 1132 of file LoadBalancer.php.

References getServerCount().

◆ setServerInfo()

LoadBalancer::setServerInfo (   $i,
  $serverInfo 
)

Sets the server info structure for the given index.

Entry at index $i is created if it doesn't exist

Parameters
$i
$serverInfo

Definition at line 829 of file LoadBalancer.php.

◆ sleep()

LoadBalancer::sleep (   $t)

Wait for a specified number of microseconds, and return the period waited.

Parameters
int$t
Returns
int

Definition at line 306 of file LoadBalancer.php.

References $t, wfDebug(), wfProfileIn(), and wfProfileOut().

◆ waitFor()

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.

Parameters
DBMasterPos$pos

Definition at line 321 of file LoadBalancer.php.

References $mReadIndex, doWait(), getAnyOpenConnection(), wfProfileIn(), and wfProfileOut().

◆ waitForAll()

LoadBalancer::waitForAll (   $pos)

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

Parameters
DBMasterPos$pos

Definition at line 339 of file LoadBalancer.php.

References doWait(), wfProfileIn(), and wfProfileOut().

◆ waitTimeout()

LoadBalancer::waitTimeout (   $value = null)
Parameters
$valuenull
Returns
Mixed

Definition at line 997 of file LoadBalancer.php.

References $value, and wfSetVar().

Member Data Documentation

◆ $mAllowLagged

LoadBalancer::$mAllowLagged

Definition at line 34 of file LoadBalancer.php.

Referenced by allowLagged().

◆ $mConns

LoadBalancer::$mConns

Definition at line 31 of file LoadBalancer.php.

◆ $mErrorConnection

bool DatabaseBase LoadBalancer::$mErrorConnection
private

Database connection that caused a problem *.

Definition at line 33 of file LoadBalancer.php.

Referenced by reportConnectionError().

◆ $mGroupLoads

LoadBalancer::$mGroupLoads

Definition at line 31 of file LoadBalancer.php.

◆ $mLaggedSlaveMode

LoadBalancer::$mLaggedSlaveMode
private

Definition at line 39 of file LoadBalancer.php.

Referenced by getLaggedSlaveMode().

◆ $mLagTimes

LoadBalancer::$mLagTimes

Definition at line 40 of file LoadBalancer.php.

Referenced by getLagTimes().

◆ $mLastError

LoadBalancer::$mLastError = 'Unknown error'

Definition at line 39 of file LoadBalancer.php.

Referenced by getConnection().

◆ $mLoadMonitor

LoadBalancer::$mLoadMonitor

Definition at line 41 of file LoadBalancer.php.

Referenced by getLoadMonitor().

◆ $mLoadMonitorClass

LoadBalancer::$mLoadMonitorClass
private

Definition at line 41 of file LoadBalancer.php.

Referenced by getLoadMonitor().

◆ $mLoads

LoadBalancer::$mLoads

Definition at line 31 of file LoadBalancer.php.

Referenced by getReaderIndex().

◆ $mParentInfo

LoadBalancer::$mParentInfo
private

Definition at line 40 of file LoadBalancer.php.

◆ $mReadIndex

LoadBalancer::$mReadIndex
private

Definition at line 34 of file LoadBalancer.php.

Referenced by getReaderIndex(), and waitFor().

◆ $mServers

LoadBalancer::$mServers
private

Definition at line 31 of file LoadBalancer.php.

◆ $mWaitForPos

bool DBMasterPos LoadBalancer::$mWaitForPos
private

False if not set *.

Definition at line 36 of file LoadBalancer.php.

◆ $mWaitTimeout

LoadBalancer::$mWaitTimeout
private

Definition at line 38 of file LoadBalancer.php.


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