26 use InvalidArgumentException;
28 use UnexpectedValueException;
111 parent::__construct( $conf );
113 $this->hostsByServerName = $conf[
'hostsByName'] ?? [];
114 $this->sectionsByDB = $conf[
'sectionsByDB'];
115 $this->groupLoadsBySection = $conf[
'groupLoadsBySection'] ?? [];
116 foreach ( ( $conf[
'sectionLoads'] ?? [] ) as $section => $loadsByServerName ) {
119 $this->externalLoadsByCluster = $conf[
'externalLoads'] ?? [];
120 $this->serverTemplate = $conf[
'serverTemplate'] ?? [];
121 $this->externalTemplateOverrides = $conf[
'externalTemplateOverrides'] ?? [];
122 $this->templateOverridesBySection = $conf[
'templateOverridesBySection'] ?? [];
123 $this->templateOverridesByCluster = $conf[
'templateOverridesByCluster'] ?? [];
124 $this->masterTemplateOverrides = $conf[
'masterTemplateOverrides'] ?? [];
125 $this->templateOverridesByServer = $conf[
'templateOverridesByServer'] ?? [];
126 $this->readOnlyBySection = $conf[
'readOnlyBySection'] ?? [];
128 if ( isset( $conf[
'loadMonitor'] ) ) {
129 $this->loadMonitorConfig = $conf[
'loadMonitor'];
130 } elseif ( isset( $conf[
'loadMonitorClass'] ) ) {
131 $this->loadMonitorConfig = [
'class' => $conf[
'loadMonitorClass'] ];
133 $this->loadMonitorConfig = [
'class' => LoadMonitor::class ];
136 foreach ( array_keys( $this->externalLoadsByCluster ) as $cluster ) {
137 if ( isset( $this->groupLoadsBySection[$cluster] ) ) {
138 throw new LogicException(
139 "External cluster '$cluster' has the same name as a main section/cluster"
147 $database = $domainInstance->getDatabase();
151 throw new UnexpectedValueException(
"Section '$section' has no hosts defined." );
157 $this->serverTemplate,
158 $this->templateOverridesBySection[$section] ?? []
160 $this->groupLoadsBySection[$section],
162 is_string( $this->readOnlyReason )
163 ? $this->readOnlyReason
164 : ( $this->readOnlyBySection[$section] ??
false )
169 $domainInstance = $this->resolveDomainInstance( $domain );
170 $section = $this->getSectionFromDatabase( $domainInstance->getDatabase() );
172 if ( !isset( $this->mainLBs[$section] ) ) {
173 $this->mainLBs[$section] = $this->newMainLB( $domain );
176 return $this->mainLBs[$section];
180 if ( !isset( $this->externalLoadsByCluster[$cluster] ) ) {
181 throw new InvalidArgumentException(
"Unknown cluster '$cluster'" );
183 return $this->newLoadBalancer(
186 $this->serverTemplate,
187 $this->externalTemplateOverrides,
188 $this->templateOverridesByCluster[$cluster] ?? []
190 [ ILoadBalancer::GROUP_GENERIC => $this->externalLoadsByCluster[$cluster] ],
191 $this->readOnlyReason
196 if ( !isset( $this->externalLBs[$cluster] ) ) {
197 $this->externalLBs[$cluster] = $this->newExternalLB(
202 return $this->externalLBs[$cluster];
207 foreach ( $this->sectionsByDB as $db => $section ) {
208 if ( !isset( $lbs[$section] ) ) {
209 $lbs[$section] = $this->getMainLB( $db );
218 foreach ( $this->externalLoadsByCluster as $cluster => $unused ) {
219 $lbs[$cluster] = $this->getExternalLB( $cluster );
225 public function forEachLB( $callback, array $params = [] ) {
227 foreach ( $this->mainLBs as $lb ) {
228 $callback( $lb, ...$params );
230 foreach ( $this->externalLBs as $lb ) {
231 $callback( $lb, ...$params );
236 foreach ( $this->mainLBs as $lb ) {
239 foreach ( $this->externalLBs as $lb ) {
255 array $serverTemplate,
260 $this->baseLoadBalancerParams(),
262 'servers' => $this->makeServerConfigArrays( $serverTemplate, $groupLoads ),
263 'loadMonitor' => $this->loadMonitorConfig,
264 'readOnlyReason' => $readOnlyReason,
265 'clusterName' => $clusterName
268 $this->initLoadBalancer( $lb );
282 if ( !$groupLoads[ILoadBalancer::GROUP_GENERIC] ) {
283 throw new UnexpectedValueException(
"Empty generic load array; no primary DB defined." );
285 $groupLoadsByServerName = $this->reindexGroupLoadsByServerName( $groupLoads );
287 $genericLoads = $groupLoads[ILoadBalancer::GROUP_GENERIC];
289 $genericLoads += array_fill_keys( array_keys( $groupLoadsByServerName ), 0 );
291 foreach ( $genericLoads as $serverName => $load ) {
292 $servers[] = array_merge(
294 $servers ? [] : $this->masterTemplateOverrides,
295 $this->templateOverridesByServer[$serverName] ?? [],
297 'host' => $this->hostsByServerName[$serverName] ?? $serverName,
298 'serverName' => $serverName,
300 'groupLoads' => $groupLoadsByServerName[$serverName] ?? []
315 $groupLoadsByServerName = [];
316 foreach ( $groupLoads as $group => $loadByServerName ) {
317 foreach ( $loadByServerName as $serverName => $load ) {
318 $groupLoadsByServerName[$serverName][$group] = $load;
322 return $groupLoadsByServerName;
330 return $this->sectionsByDB[$database] ?? self::CLUSTER_MAIN_DEFAULT;
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.