* @ingroup Database
*/
+namespace Wikimedia\Rdbms;
+
+use InvalidArgumentException;
+
/**
* A multi-database, multi-master factory for Wikimedia and similar installations.
* Ignores the old configuration globals.
/** @var string */
private $lastSection;
+ /** @var int */
+ private $maxLag = self::MAX_LAG_DEFAULT;
+
+ /** @var int Default 'maxLag' when unspecified */
+ const MAX_LAG_DEFAULT = 10;
+
/**
* @see LBFactory::__construct()
*
* storage cluster.
* - masterTemplateOverrides Server configuration map overrides for all master servers.
* - loadMonitorClass Name of the LoadMonitor class to always use.
+ * - maxLag Avoid replica DBs with more lag than this many seconds.
* - readOnlyBySection A map of section name to read-only message.
* Missing or false for read/write.
*/
$optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
- 'readOnlyBySection', 'loadMonitorClass' ];
+ 'readOnlyBySection', 'maxLag', 'loadMonitorClass' ];
foreach ( $required as $key ) {
if ( !isset( $conf[$key] ) ) {
public function getMainLB( $domain = false ) {
$section = $this->getSectionForDomain( $domain );
if ( !isset( $this->mainLBs[$section] ) ) {
- $lb = $this->newMainLB( $domain );
- $this->getChronologyProtector()->initLB( $lb );
- $this->mainLBs[$section] = $lb;
+ $this->mainLBs[$section] = $this->newMainLB( $domain );
}
return $this->mainLBs[$section];
throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
}
$template = $this->serverTemplate;
- if ( isset( $this->externalTemplateOverrides ) ) {
+ if ( $this->externalTemplateOverrides ) {
$template = $this->externalTemplateOverrides + $template;
}
if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
public function getExternalLB( $cluster ) {
if ( !isset( $this->extLBs[$cluster] ) ) {
$this->extLBs[$cluster] = $this->newExternalLB( $cluster );
- $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
}
return $this->extLBs[$cluster];
}
+ public function getAllMainLBs() {
+ $lbs = [];
+ foreach ( $this->sectionsByDB as $db => $section ) {
+ if ( !isset( $lbs[$section] ) ) {
+ $lbs[$section] = $this->getMainLB( $db );
+ }
+ }
+
+ return $lbs;
+ }
+
+ public function getAllExternalLBs() {
+ $lbs = [];
+ foreach ( $this->externalLoads as $cluster => $unused ) {
+ $lbs[$cluster] = $this->getExternalLB( $cluster );
+ }
+
+ return $lbs;
+ }
+
/**
* Make a new load balancer object based on template and load array
*
$this->baseLoadBalancerParams(),
[
'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
+ 'maxLag' => $this->maxLag,
'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
'readOnlyReason' => $readOnlyReason
]
$serverInfo = $template;
if ( $master ) {
$serverInfo['master'] = true;
- if ( isset( $this->masterTemplateOverrides ) ) {
+ if ( $this->masterTemplateOverrides ) {
$serverInfo = $this->masterTemplateOverrides + $serverInfo;
}
$master = false;
*/
public function forEachLB( $callback, array $params = [] ) {
foreach ( $this->mainLBs as $lb ) {
- call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
+ $callback( $lb, ...$params );
}
foreach ( $this->extLBs as $lb ) {
- call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
+ $callback( $lb, ...$params );
}
}
}