Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
LBFactoryMulti.php
Go to the documentation of this file.
1<?php
20namespaceWikimedia\Rdbms;
21
22use InvalidArgumentException;
23use LogicException;
24use UnexpectedValueException;
25
53classLBFactoryMultiextendsLBFactory {
55private $mainLBs = [];
57private $externalLBs = [];
58
60private $hostsByServerName;
62private $sectionsByDB;
64private $groupLoadsBySection;
66private $externalLoadsByCluster;
68private $serverTemplate;
70private $externalTemplateOverrides;
72private $templateOverridesBySection;
74private $templateOverridesByCluster;
76private $masterTemplateOverrides;
78private $templateOverridesByServer;
80private $readOnlyBySection;
82private $loadMonitorConfig;
84private $nonLocalDomainCache = [];
85
135publicfunction__construct( array $conf ) {
136 parent::__construct( $conf );
137
138 $this->hostsByServerName = $conf['hostsByName'] ?? [];
139 $this->sectionsByDB = $conf['sectionsByDB'];
140 $this->sectionsByDB += [ self::CLUSTER_MAIN_DEFAULT =>self::CLUSTER_MAIN_DEFAULT ];
141 $this->groupLoadsBySection = $conf['groupLoadsBySection'] ?? [];
142foreach ( ( $conf['sectionLoads'] ?? [] ) as $section => $loadsByServerName ) {
143 $this->groupLoadsBySection[$section][ILoadBalancer::GROUP_GENERIC] = $loadsByServerName;
144 }
145 $this->externalLoadsByCluster = $conf['externalLoads'] ?? [];
146 $this->serverTemplate = $conf['serverTemplate'] ?? [];
147 $this->externalTemplateOverrides = $conf['externalTemplateOverrides'] ?? [];
148 $this->templateOverridesBySection = $conf['templateOverridesBySection'] ?? [];
149 $this->templateOverridesByCluster = $conf['templateOverridesByCluster'] ?? [];
150 $this->masterTemplateOverrides = $conf['masterTemplateOverrides'] ?? [];
151 $this->templateOverridesByServer = $conf['templateOverridesByServer'] ?? [];
152 $this->readOnlyBySection = $conf['readOnlyBySection'] ?? [];
153
154if ( isset( $conf['loadMonitor'] ) ) {
155 $this->loadMonitorConfig = $conf['loadMonitor'];
156 } elseif ( isset( $conf['loadMonitorClass'] ) ) {// b/c
157 $this->loadMonitorConfig = ['class' => $conf['loadMonitorClass'] ];
158 }else {
159 $this->loadMonitorConfig = ['class' => LoadMonitor::class ];
160 }
161
162foreach ( $this->externalLoadsByCluster as $cluster => $_ ) {
163if ( isset( $this->groupLoadsBySection[$cluster] ) ) {
164thrownew LogicException(
165"External cluster '$cluster' has the same name as a main section/cluster"
166 );
167 }
168 }
169 }
170
171publicfunctionnewMainLB( $domain =false ):ILoadBalancerForOwner {
172 $domainInstance = $this->resolveDomainInstance( $domain );
173 $database = $domainInstance->getDatabase();
174 $section = $this->getSectionFromDatabase( $database );
175
176if ( !isset( $this->groupLoadsBySection[$section][ILoadBalancer::GROUP_GENERIC] ) ) {
177thrownew UnexpectedValueException("Section '$section' has no hosts defined." );
178 }
179
180return $this->newLoadBalancer(
181 $section,
182 array_merge(
183 $this->serverTemplate,
184 $this->templateOverridesBySection[$section] ?? []
185 ),
186 $this->groupLoadsBySection[$section],
187// Use the LB-specific read-only reason if everything isn't already read-only
188 is_string( $this->readOnlyReason )
189 ? $this->readOnlyReason
190 : ( $this->readOnlyBySection[$section] ?? false )
191 );
192 }
193
198privatefunction resolveDomainInstance( $domain ) {
199if ( $domain instanceof DatabaseDomain ) {
200return $domain;// already a domain instance
201 } elseif ( $domain ===false || $domain === $this->localDomain->getId() ) {
202return $this->localDomain;
203 } elseif ( isset( $this->domainAliases[$domain] ) ) {
204// This array acts as both the original map and as instance cache.
205// Instances pass-through DatabaseDomain::newFromId as-is.
206 $this->domainAliases[$domain] =
207DatabaseDomain::newFromId( $this->domainAliases[$domain] );
208
209return $this->domainAliases[$domain];
210 }
211
212 $cachedDomain = $this->nonLocalDomainCache[$domain] ??null;
213if ( $cachedDomain ===null ) {
214 $cachedDomain =DatabaseDomain::newFromId( $domain );
215 $this->nonLocalDomainCache = [ $domain => $cachedDomain ];
216 }
217
218return $cachedDomain;
219 }
220
221publicfunctiongetMainLB( $domain =false ):ILoadBalancer {
222 $domainInstance = $this->resolveDomainInstance( $domain );
223 $section = $this->getSectionFromDatabase( $domainInstance->getDatabase() );
224
225if ( !isset( $this->mainLBs[$section] ) ) {
226 $this->mainLBs[$section] = $this->newMainLB( $domain );
227 }
228
229return $this->mainLBs[$section];
230 }
231
232publicfunctionnewExternalLB( $cluster ):ILoadBalancerForOwner {
233if ( !isset( $this->externalLoadsByCluster[$cluster] ) ) {
234thrownew InvalidArgumentException("Unknown cluster '$cluster'" );
235 }
236return $this->newLoadBalancer(
237 $cluster,
238 array_merge(
239 $this->serverTemplate,
240 $this->externalTemplateOverrides,
241 $this->templateOverridesByCluster[$cluster] ?? []
242 ),
243 [ ILoadBalancer::GROUP_GENERIC => $this->externalLoadsByCluster[$cluster] ],
244 $this->readOnlyReason
245 );
246 }
247
248publicfunctiongetExternalLB( $cluster ):ILoadBalancer {
249if ( !isset( $this->externalLBs[$cluster] ) ) {
250 $this->externalLBs[$cluster] = $this->newExternalLB(
251 $cluster
252 );
253 }
254
255return $this->externalLBs[$cluster];
256 }
257
258publicfunctiongetAllMainLBs(): array {
259 $lbs = [];
260foreach ( $this->sectionsByDB as $db => $section ) {
261if ( !isset( $lbs[$section] ) ) {
262 $lbs[$section] = $this->getMainLB( $db );
263 }
264 }
265
266return $lbs;
267 }
268
269publicfunctiongetAllExternalLBs(): array {
270 $lbs = [];
271foreach ( $this->externalLoadsByCluster as $cluster => $unused ) {
272 $lbs[$cluster] = $this->getExternalLB( $cluster );
273 }
274
275return $lbs;
276 }
277
278protectedfunctiongetLBsForOwner() {
279foreach ( $this->mainLBs as $lb ) {
280 yield $lb;
281 }
282foreach ( $this->externalLBs as $lb ) {
283 yield $lb;
284 }
285 }
286
296privatefunction newLoadBalancer(
297string $clusterName,
298 array $serverTemplate,
299 array $groupLoads,
300 $readOnlyReason
301 ) {
302 $lb =newLoadBalancer( array_merge(
303 $this->baseLoadBalancerParams(),
304 [
305'servers' => $this->makeServerConfigArrays( $serverTemplate, $groupLoads ),
306'loadMonitor' => $this->loadMonitorConfig,
307'readOnlyReason' => $readOnlyReason,
308'clusterName' => $clusterName
309 ]
310 ) );
311 $this->initLoadBalancer( $lb );
312
313return $lb;
314 }
315
323privatefunction makeServerConfigArrays( array $serverTemplate, array $groupLoads ) {
324// The primary DB server is the first host explicitly listed in the generic load group
325if ( !$groupLoads[ILoadBalancer::GROUP_GENERIC] ) {
326thrownew UnexpectedValueException("Empty generic load array; no primary DB defined." );
327 }
328 $groupLoadsByServerName = [];
329foreach ( $groupLoads as $group => $loadByServerName ) {
330foreach ( $loadByServerName as $serverName => $load ) {
331 $groupLoadsByServerName[$serverName][$group] = $load;
332 }
333 }
334
335// Get the ordered map of (server name => load); the primary DB server is first
336 $genericLoads = $groupLoads[ILoadBalancer::GROUP_GENERIC];
337// Implicitly append any hosts that only appear in custom load groups
338 $genericLoads += array_fill_keys( array_keys( $groupLoadsByServerName ), 0 );
339 $servers = [];
340foreach ( $genericLoads as $serverName => $load ) {
341 $servers[] = array_merge(
342 $serverTemplate,
343 $servers ? [] : $this->masterTemplateOverrides,
344 $this->templateOverridesByServer[$serverName] ?? [],
345 [
346'host' => $this->hostsByServerName[$serverName] ?? $serverName,
347'serverName' => $serverName,
348'load' => $load,
349'groupLoads' => $groupLoadsByServerName[$serverName] ?? []
350 ]
351 );
352 }
353
354return $servers;
355 }
356
361privatefunction getSectionFromDatabase( $database ) {
362return $this->sectionsByDB[$database]
363 ?? $this->sectionsByDB[self::CLUSTER_MAIN_DEFAULT]
364 ?? self::CLUSTER_MAIN_DEFAULT;
365 }
366
367publicfunctionreconfigure( array $conf ): void {
368if ( !$conf ) {
369return;
370 }
371
372foreach ( $this->mainLBs as $lb ) {
373// Approximate what LBFactoryMulti::__construct does (T346365)
374 $groupLoads = $conf['groupLoadsBySection'][$lb->getClusterName()] ?? [];
375 $groupLoads[ILoadBalancer::GROUP_GENERIC] = $conf['sectionLoads'][$lb->getClusterName()];
376 $config = [
377'servers' => $this->makeServerConfigArrays( $conf['serverTemplate'] ?? [], $groupLoads )
378 ];
379 $lb->reconfigure( $config );
380
381 }
382foreach ( $this->externalLBs as $lb ) {
383 $groupLoads = [
384 ILoadBalancer::GROUP_GENERIC => $conf['externalLoads'][$lb->getClusterName()]
385 ];
386 $config = [
387'servers' => $this->makeServerConfigArrays( $conf['serverTemplate'] ?? [], $groupLoads )
388 ];
389 $lb->reconfigure( $config );
390 }
391 }
392}
if
if(!defined('MW_SETUP_CALLBACK'))
DefinitionWebStart.php:82
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
DefinitionDatabaseDomain.php:42
Wikimedia\Rdbms\DatabaseDomain\newFromId
static newFromId( $domain)
DefinitionDatabaseDomain.php:79
Wikimedia\Rdbms\LBFactoryMulti
LoadBalancer manager for sites with several "main" database clusters.
DefinitionLBFactoryMulti.php:53
Wikimedia\Rdbms\LBFactoryMulti\newMainLB
newMainLB( $domain=false)
Create a new load balancer instance for the main cluster that handles the given domain.
DefinitionLBFactoryMulti.php:171
Wikimedia\Rdbms\LBFactoryMulti\getAllMainLBs
getAllMainLBs()
Get the tracked load balancer instances for all main clusters.
DefinitionLBFactoryMulti.php:258
Wikimedia\Rdbms\LBFactoryMulti\getAllExternalLBs
getAllExternalLBs()
Get the tracked load balancer instances for all external clusters.
DefinitionLBFactoryMulti.php:269
Wikimedia\Rdbms\LBFactoryMulti\getExternalLB
getExternalLB( $cluster)
Get the tracked load balancer instance for an external cluster.
DefinitionLBFactoryMulti.php:248
Wikimedia\Rdbms\LBFactoryMulti\getMainLB
getMainLB( $domain=false)
Get the tracked load balancer instance for the main cluster that handles the given domain.
DefinitionLBFactoryMulti.php:221
Wikimedia\Rdbms\LBFactoryMulti\newExternalLB
newExternalLB( $cluster)
Create a new load balancer instance for an external cluster.
DefinitionLBFactoryMulti.php:232
Wikimedia\Rdbms\LBFactoryMulti\getLBsForOwner
getLBsForOwner()
Get all tracked load balancers with the internal "for owner" interface.
DefinitionLBFactoryMulti.php:278
Wikimedia\Rdbms\LBFactoryMulti\reconfigure
reconfigure(array $conf)
Reconfigure using the given config array.
DefinitionLBFactoryMulti.php:367
Wikimedia\Rdbms\LBFactoryMulti\__construct
__construct(array $conf)
Template override precedence (highest => lowest):
DefinitionLBFactoryMulti.php:135
Wikimedia\Rdbms\LBFactory
DefinitionLBFactory.php:41
Wikimedia\Rdbms\LoadBalancer
DefinitionLoadBalancer.php:40
Wikimedia\Rdbms\ILBFactory\CLUSTER_MAIN_DEFAULT
const CLUSTER_MAIN_DEFAULT
Default main cluster name (do not change this)
DefinitionILBFactory.php:53
Wikimedia\Rdbms\ILoadBalancerForOwner
Internal interface for load balancer instances exposed to their owner.
DefinitionILoadBalancerForOwner.php:34
Wikimedia\Rdbms\ILoadBalancer
This class is a delegate to ILBFactory for a given database cluster.
DefinitionILoadBalancer.php:113
Wikimedia\Rdbms\ILoadBalancer\GROUP_GENERIC
const GROUP_GENERIC
The generic query group.
DefinitionILoadBalancer.php:128
Wikimedia\Rdbms
DefinitionChangedTablesTracker.php:3

[8]ページ先頭

©2009-2025 Movatter.jp