MediaWiki REL1_35
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)
 

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
 
WANObjectCache $wanCache
 

Private Member Functions

 acquireServerStatesLoopGuard ()
 
 getPlaceholderServerStates (array $serverIndexes)
 
 getStatesCacheKey ( $cache, array $serverIndexes)
 

Private Attributes

int $lagWarnThreshold
 Amount of replication lag in seconds before warnings are logged.
 
float $movingAveRatio
 Moving average ratio (e.g.
 
bool $serverStatesKeyLocked = false
 Whether the "server states" cache key is in the process of being updated.
 
float null $wallClockOverride
 

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 82 of file LoadMonitor.php.

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

Member Function Documentation

◆ acquireServerStatesLoopGuard()

Wikimedia\Rdbms\LoadMonitor::acquireServerStatesLoopGuard ( )
private
Returns
ScopedCallback|null

Definition at line 379 of file LoadMonitor.php.

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

◆ computeServerStates()

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

Definition at line 204 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\getCurrentTime(), Wikimedia\Rdbms\LoadMonitor\getNewScaleViaMovingAve(), Wikimedia\Rdbms\LoadMonitor\getPlaceholderServerStates(), and Wikimedia\Rdbms\LoadMonitor\getWeightScale().

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

◆ getCurrentTime()

Wikimedia\Rdbms\LoadMonitor::getCurrentTime ( )
protected

◆ 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|bool)

Implements Wikimedia\Rdbms\ILoadMonitor.

Definition at line 112 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 356 of file LoadMonitor.php.

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

◆ getPlaceholderServerStates()

Wikimedia\Rdbms\LoadMonitor::getPlaceholderServerStates ( array  $serverIndexes)
private
Parameters
int[]$serverIndexes
Returns
array

Definition at line 305 of file LoadMonitor.php.

References Wikimedia\Rdbms\LoadMonitor\getCurrentTime().

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

◆ getServerStates()

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

◆ getStatesCacheKey()

Wikimedia\Rdbms\LoadMonitor::getStatesCacheKey (   $cache,
array  $serverIndexes 
)
private
Parameters
WANObjectCache | BagOStuff$cache
array$serverIndexes
Returns
string

Definition at line 365 of file LoadMonitor.php.

References $cache.

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

◆ 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 319 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 | bool$domain

Implements Wikimedia\Rdbms\ILoadMonitor.

Definition at line 98 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 403 of file LoadMonitor.php.

Member Data Documentation

◆ $lagWarnThreshold

int Wikimedia\Rdbms\LoadMonitor::$lagWarnThreshold
private

Amount of replication lag in seconds before warnings are logged.

Definition at line 54 of file LoadMonitor.php.

◆ $lb

ILoadBalancer Wikimedia\Rdbms\LoadMonitor::$lb
protected

◆ $movingAveRatio

float Wikimedia\Rdbms\LoadMonitor::$movingAveRatio
private

Moving average ratio (e.g.

0.1 for 10% weight to new weight)

Definition at line 52 of file LoadMonitor.php.

◆ $replLogger

LoggerInterface Wikimedia\Rdbms\LoadMonitor::$replLogger
protected

Definition at line 49 of file LoadMonitor.php.

◆ $serverStatesKeyLocked

bool Wikimedia\Rdbms\LoadMonitor::$serverStatesKeyLocked = false
private

Whether the "server states" cache key is in the process of being updated.

Definition at line 60 of file LoadMonitor.php.

◆ $srvCache

BagOStuff Wikimedia\Rdbms\LoadMonitor::$srvCache
protected

◆ $wallClockOverride

float null Wikimedia\Rdbms\LoadMonitor::$wallClockOverride
private

Definition at line 57 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: