MediaWiki REL1_28
LBFactoryMulti.php
Go to the documentation of this file.
1<?php
33
39
45
46 // Optional settings
47
50
52 private $groupLoadsByDB = [];
53
55 private $hostsByName = [];
56
58 private $externalLoads = [];
59
65
72
75
78
81
86 private $readOnlyBySection = [];
87
89 private $conf;
90
92 private $mainLBs = [];
93
95 private $extLBs = [];
96
98 private $loadMonitorClass = 'LoadMonitor';
99
101 private $lastDomain;
102
105
162 public function __construct( array $conf ) {
163 parent::__construct( $conf );
164
165 $this->conf = $conf;
166 $required = [ 'sectionsByDB', 'sectionLoads', 'serverTemplate' ];
167 $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
168 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
169 'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
170 'readOnlyBySection', 'loadMonitorClass' ];
171
172 foreach ( $required as $key ) {
173 if ( !isset( $conf[$key] ) ) {
174 throw new InvalidArgumentException( __CLASS__ . ": $key is required." );
175 }
176 $this->$key = $conf[$key];
177 }
178
179 foreach ( $optional as $key ) {
180 if ( isset( $conf[$key] ) ) {
181 $this->$key = $conf[$key];
182 }
183 }
184 }
185
190 private function getSectionForDomain( $domain = false ) {
191 if ( $this->lastDomain === $domain ) {
192 return $this->lastSection;
193 }
194 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
195 if ( isset( $this->sectionsByDB[$dbName] ) ) {
196 $section = $this->sectionsByDB[$dbName];
197 } else {
198 $section = 'DEFAULT';
199 }
200 $this->lastSection = $section;
201 $this->lastDomain = $domain;
202
203 return $section;
204 }
205
210 public function newMainLB( $domain = false ) {
211 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
212 $section = $this->getSectionForDomain( $domain );
213 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
214 $groupLoads = $this->groupLoadsByDB[$dbName];
215 } else {
216 $groupLoads = [];
217 }
218
219 if ( isset( $this->groupLoadsBySection[$section] ) ) {
220 $groupLoads = array_merge_recursive(
221 $groupLoads, $this->groupLoadsBySection[$section] );
222 }
223
225 // Use the LB-specific read-only reason if everything isn't already read-only
226 if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
227 $readOnlyReason = $this->readOnlyBySection[$section];
228 }
229
231 if ( isset( $this->templateOverridesBySection[$section] ) ) {
232 $template = $this->templateOverridesBySection[$section] + $template;
233 }
234
235 return $this->newLoadBalancer(
236 $template,
237 $this->sectionLoads[$section],
238 $groupLoads,
240 );
241 }
242
247 public function getMainLB( $domain = false ) {
248 $section = $this->getSectionForDomain( $domain );
249 if ( !isset( $this->mainLBs[$section] ) ) {
250 $lb = $this->newMainLB( $domain );
251 $this->getChronologyProtector()->initLB( $lb );
252 $this->mainLBs[$section] = $lb;
253 }
254
255 return $this->mainLBs[$section];
256 }
257
258 public function newExternalLB( $cluster ) {
259 if ( !isset( $this->externalLoads[$cluster] ) ) {
260 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
261 }
263 if ( isset( $this->externalTemplateOverrides ) ) {
264 $template = $this->externalTemplateOverrides + $template;
265 }
266 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
267 $template = $this->templateOverridesByCluster[$cluster] + $template;
268 }
269
270 return $this->newLoadBalancer(
271 $template,
272 $this->externalLoads[$cluster],
273 [],
274 $this->readOnlyReason
275 );
276 }
277
278 public function getExternalLB( $cluster ) {
279 if ( !isset( $this->extLBs[$cluster] ) ) {
280 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
281 $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
282 }
283
284 return $this->extLBs[$cluster];
285 }
286
296 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
297 $lb = new LoadBalancer( array_merge(
298 $this->baseLoadBalancerParams(),
299 [
300 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
301 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
302 'readOnlyReason' => $readOnlyReason
303 ]
304 ) );
305 $this->initLoadBalancer( $lb );
306
307 return $lb;
308 }
309
318 private function makeServerArray( $template, $loads, $groupLoads ) {
319 $servers = [];
320 $master = true;
321 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
322 foreach ( $groupLoadsByServer as $server => $stuff ) {
323 if ( !isset( $loads[$server] ) ) {
324 $loads[$server] = 0;
325 }
326 }
327 foreach ( $loads as $serverName => $load ) {
328 $serverInfo = $template;
329 if ( $master ) {
330 $serverInfo['master'] = true;
331 if ( isset( $this->masterTemplateOverrides ) ) {
332 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
333 }
334 $master = false;
335 } else {
336 $serverInfo['replica'] = true;
337 }
338 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
339 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
340 }
341 if ( isset( $groupLoadsByServer[$serverName] ) ) {
342 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
343 }
344 if ( isset( $this->hostsByName[$serverName] ) ) {
345 $serverInfo['host'] = $this->hostsByName[$serverName];
346 } else {
347 $serverInfo['host'] = $serverName;
348 }
349 $serverInfo['hostName'] = $serverName;
350 $serverInfo['load'] = $load;
351 $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
352
353 $servers[] = $serverInfo;
354 }
355
356 return $servers;
357 }
358
364 private function reindexGroupLoads( $groupLoads ) {
365 $reindexed = [];
366 foreach ( $groupLoads as $group => $loads ) {
367 foreach ( $loads as $server => $load ) {
368 $reindexed[$server][$group] = $load;
369 }
370 }
371
372 return $reindexed;
373 }
374
379 private function getDBNameAndPrefix( $domain = false ) {
380 $domain = ( $domain === false )
381 ? $this->localDomain
382 : DatabaseDomain::newFromId( $domain );
383
384 return [ $domain->getDatabase(), $domain->getTablePrefix() ];
385 }
386
394 public function forEachLB( $callback, array $params = [] ) {
395 foreach ( $this->mainLBs as $lb ) {
396 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
397 }
398 foreach ( $this->extLBs as $lb ) {
399 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
400 }
401 }
402}
static newFromId( $domain)
A multi-database, multi-master factory for Wikimedia and similar installations.
getSectionForDomain( $domain=false)
array $templateOverridesByCluster
A 2-d map overriding the server info by external storage cluster.
LoadBalancer[] $extLBs
array $groupLoadsByDB
A 3-d map giving server load ratios by DB name.
getExternalLB( $cluster)
reindexGroupLoads( $groupLoads)
Take a group load array indexed by group then server, and reindex it by server then group.
array $masterTemplateOverrides
An override array for all master servers.
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
newMainLB( $domain=false)
array $externalTemplateOverrides
A set of server info keys overriding serverTemplate for external storage.
newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason)
Make a new load balancer object based on template and load array.
array $externalLoads
A map of external storage cluster name to server load map.
makeServerArray( $template, $loads, $groupLoads)
Make a server array as expected by LoadBalancer::__construct, using a template and load array.
getDBNameAndPrefix( $domain=false)
LoadBalancer[] $mainLBs
array $groupLoadsBySection
A 3-d map giving server load ratios for each section and group.
__construct(array $conf)
array $conf
Load balancer factory configuration.
array $templateOverridesByServer
A 2-d map overriding serverTemplate and externalTemplateOverrides on a server-by-server basis.
newExternalLB( $cluster)
array bool $readOnlyBySection
A map of section name to read-only message.
array $sectionsByDB
A map of database names to section names.
array[] $serverTemplate
Server info associative array.
array $sectionLoads
A 2-d map.
array $hostsByName
A map of hostname to IP address.
array $templateOverridesBySection
A 2-d map overriding the server info by section.
getMainLB( $domain=false)
An interface for generating database load balancers.
Definition LBFactory.php:31
initLoadBalancer(ILoadBalancer $lb)
baseLoadBalancerParams()
Base parameters to LoadBalancer::__construct()
getChronologyProtector()
string bool $readOnlyReason
Reason all LBs are read-only or false if not.
Definition LBFactory.php:67
Database connection, tracking, load balancing, and transaction manager for a cluster.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping $template
Definition hooks.txt:853
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:2901
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$params