MediaWiki REL1_39
Wikimedia\Rdbms\LoadMonitor Class Reference

Basic DB load monitor with no external dependencies. More...

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

Public Member Functions

 __construct (ILoadBalancer $lb, BagOStuff $srvCache, WANObjectCache $wCache, array $options=[])
 
 getLagTimes (array $serverIndexes, $domain)
 Get an estimate of replication lag (in seconds) for each server.
 
 scaleLoads (array &$weightByServer, $domain)
 Perform load ratio adjustment before deciding which server to use.
 
 setLogger (LoggerInterface $logger)
 
 setMockTime (&$time)
 
 setStatsdDataFactory (StatsdDataFactoryInterface $statsFactory)
 Sets a StatsdDataFactory instance on the object.
 

Protected Member Functions

 computeServerStates (array $serverIndexes, $domain, $priorStates)
 
 getCurrentTime ()
 
 getNewScaleViaMovingAve ( $lastScale, $naiveScale, $movAveRatio)
 Get the moving average weight scale given a naive and the last iteration value.
 
 getServerStates (array $serverIndexes, $domain)
 
 getWeightScale ( $index, IDatabase $conn=null)
 

Protected Attributes

ILoadBalancer $lb
 
LoggerInterface $replLogger
 
BagOStuff $srvCache
 
StatsdDataFactoryInterface $statsd
 
WANObjectCache $wanCache
 

Detailed Description

Basic DB load monitor with no external dependencies.

Uses both server-local and shared caches for server state information.

The "domain" parameters are unused, though they might be used in the future. Therefore, at present, this assumes one channel of replication per server.

Definition at line 41 of file LoadMonitor.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Rdbms\LoadMonitor::__construct ( ILoadBalancer $lb,
BagOStuff $srvCache,
WANObjectCache $wCache,
array $options = [] )
Parameters
ILoadBalancer$lb
BagOStuff$srvCache
WANObjectCache$wCache
array$options
  • movingAveRatio: moving average constant for server weight updates based on lag
  • lagWarnThreshold: how many seconds of lag trigger warnings

Implements Wikimedia\Rdbms\ILoadMonitor.

Reimplemented in Wikimedia\Rdbms\LoadMonitorMySQL.

Definition at line 81 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\$lb, Wikimedia\Rdbms\LoadMonitor\$srvCache, and Wikimedia\Rdbms\LoadBalancer\MAX_LAG_DEFAULT.

Member Function Documentation

◆ computeServerStates()

Wikimedia\Rdbms\LoadMonitor::computeServerStates ( array $serverIndexes,
$domain,
$priorStates )
protected

◆ getCurrentTime()

Wikimedia\Rdbms\LoadMonitor::getCurrentTime ( )
protected
Returns
float UNIX timestamp

Definition at line 407 of file LoadMonitor.php.

Referenced by Wikimedia\Rdbms\LoadMonitor\computeServerStates(), and Wikimedia\Rdbms\LoadMonitor\getServerStates().

◆ getLagTimes()

Wikimedia\Rdbms\LoadMonitor::getLagTimes ( array $serverIndexes,
$domain )
final

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

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

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

Implements Wikimedia\Rdbms\ILoadMonitor.

Definition at line 116 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\getServerStates().

◆ getNewScaleViaMovingAve()

Wikimedia\Rdbms\LoadMonitor::getNewScaleViaMovingAve ( $lastScale,
$naiveScale,
$movAveRatio )
protected

Get the moving average weight scale given a naive and the last iteration value.

One case of particular note is if a server totally cannot have its state queried. Ideally, the scale should be able to drop from 1.0 to a miniscule amount (say 0.001) fairly quickly. To get the time to reach 0.001, some calculations can be done:

SCALE = $naiveScale * $movAveRatio + $lastScale * (1 - $movAveRatio) SCALE = 0 * $movAveRatio + $lastScale * (1 - $movAveRatio) SCALE = $lastScale * (1 - $movAveRatio)

Given a starting weight scale of 1.0: 1.0 * (1 - $movAveRatio)^(# iterations) = 0.001 ceil( log<1 - $movAveRatio>(0.001) ) = (# iterations) t = (# iterations) * (POLL_PERIOD + SHARED_CACHE_TTL) t = (# iterations) * (1e3 * POLL_PERIOD_MS + SHARED_CACHE_TTL)

If $movAveRatio is 0.5, then: t = ceil( log<0.5>(0.01) ) * 1.5 = 7 * 1.5 = 10.5 seconds [for 1% scale] t = ceil( log<0.5>(0.001) ) * 1.5 = 10 * 1.5 = 15 seconds [for 0.1% scale]

If $movAveRatio is 0.8, then: t = ceil( log<0.2>(0.01) ) * 1.5 = 3 * 1.5 = 4.5 seconds [for 1% scale] t = ceil( log<0.2>(0.001) ) * 1.5 = 5 * 1.5 = 7.5 seconds [for 0.1% scale]

Use of connection failure rate can greatly speed this process up

Parameters
float$lastScaleCurrent moving average of scaling factors
float$naiveScaleNew scaling factor
float$movAveRatioWeight given to the new value
Returns
float
Since
1.35

Definition at line 368 of file LoadMonitor.php.

Referenced by Wikimedia\Rdbms\LoadMonitor\computeServerStates().

◆ getServerStates()

Wikimedia\Rdbms\LoadMonitor::getServerStates ( array $serverIndexes,
$domain )
protected
Parameters
array$serverIndexes
string | false$domain
Returns
array
Exceptions
DBAccessError

Definition at line 126 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\computeServerStates(), and Wikimedia\Rdbms\LoadMonitor\getCurrentTime().

Referenced by Wikimedia\Rdbms\LoadMonitor\getLagTimes(), and Wikimedia\Rdbms\LoadMonitor\scaleLoads().

◆ getWeightScale()

Wikimedia\Rdbms\LoadMonitor::getWeightScale ( $index,
IDatabase $conn = null )
protected
Parameters
int$indexServer index
IDatabase | null$connConnection handle or null on connection failure
Returns
float
Since
1.28

Reimplemented in Wikimedia\Rdbms\LoadMonitorMySQL.

Definition at line 331 of file LoadMonitor.php.

Referenced by Wikimedia\Rdbms\LoadMonitor\computeServerStates().

◆ scaleLoads()

Wikimedia\Rdbms\LoadMonitor::scaleLoads ( array & $weightByServer,
$domain )
final

Perform load ratio adjustment before deciding which server to use.

Parameters
int[]&$weightByServerMap of (server index => float weight)
string | false$domain

Implements Wikimedia\Rdbms\ILoadMonitor.

Definition at line 102 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\getServerStates().

◆ setLogger()

Wikimedia\Rdbms\LoadMonitor::setLogger ( LoggerInterface $logger)

Definition at line 94 of file LoadMonitor.php.

◆ setMockTime()

Wikimedia\Rdbms\LoadMonitor::setMockTime ( & $time)
Parameters
float | null&$timeMock UNIX timestamp for testing

Definition at line 415 of file LoadMonitor.php.

◆ setStatsdDataFactory()

Wikimedia\Rdbms\LoadMonitor::setStatsdDataFactory ( StatsdDataFactoryInterface $statsFactory)

Sets a StatsdDataFactory instance on the object.

Parameters
StatsdDataFactoryInterface$statsFactory
Returns
null

Implements StatsdAwareInterface.

Definition at line 98 of file LoadMonitor.php.

Member Data Documentation

◆ $lb

ILoadBalancer Wikimedia\Rdbms\LoadMonitor::$lb
protected

◆ $replLogger

LoggerInterface Wikimedia\Rdbms\LoadMonitor::$replLogger
protected

Definition at line 49 of file LoadMonitor.php.

◆ $srvCache

BagOStuff Wikimedia\Rdbms\LoadMonitor::$srvCache
protected

◆ $statsd

StatsdDataFactoryInterface Wikimedia\Rdbms\LoadMonitor::$statsd
protected

Definition at line 51 of file LoadMonitor.php.

◆ $wanCache

WANObjectCache Wikimedia\Rdbms\LoadMonitor::$wanCache
protected

Definition at line 47 of file LoadMonitor.php.


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