MediaWiki  1.27.3
LBFactoryMulti.php
Go to the documentation of this file.
1 <?php
86 class LBFactoryMulti extends LBFactory {
88  private $sectionsByDB;
89 
94  private $sectionLoads;
95 
102 
103  // Optional settings
104 
106  private $groupLoadsBySection = [];
107 
109  private $groupLoadsByDB = [];
110 
112  private $hostsByName = [];
113 
115  private $externalLoads = [];
116 
122 
129 
132 
135 
138 
143  private $readOnlyBySection = [];
144 
145  // Other stuff
146 
148  private $conf;
149 
151  private $mainLBs = [];
152 
154  private $extLBs = [];
155 
158 
160  private $lastWiki;
161 
163  private $lastSection;
164 
169  public function __construct( array $conf ) {
170  parent::__construct( $conf );
171 
172  $this->conf = $conf;
173  $required = [ 'sectionsByDB', 'sectionLoads', 'serverTemplate' ];
174  $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
175  'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
176  'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
177  'readOnlyBySection', 'loadMonitorClass' ];
178 
179  foreach ( $required as $key ) {
180  if ( !isset( $conf[$key] ) ) {
181  throw new MWException( __CLASS__ . ": $key is required in configuration" );
182  }
183  $this->$key = $conf[$key];
184  }
185 
186  foreach ( $optional as $key ) {
187  if ( isset( $conf[$key] ) ) {
188  $this->$key = $conf[$key];
189  }
190  }
191  }
192 
197  private function getSectionForWiki( $wiki = false ) {
198  if ( $this->lastWiki === $wiki ) {
199  return $this->lastSection;
200  }
201  list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
202  if ( isset( $this->sectionsByDB[$dbName] ) ) {
203  $section = $this->sectionsByDB[$dbName];
204  } else {
205  $section = 'DEFAULT';
206  }
207  $this->lastSection = $section;
208  $this->lastWiki = $wiki;
209 
210  return $section;
211  }
212 
217  public function newMainLB( $wiki = false ) {
218  list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
219  $section = $this->getSectionForWiki( $wiki );
220  if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
221  $groupLoads = $this->groupLoadsByDB[$dbName];
222  } else {
223  $groupLoads = [];
224  }
225 
226  if ( isset( $this->groupLoadsBySection[$section] ) ) {
227  $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
228  }
229 
231  // Use the LB-specific read-only reason if everything isn't already read-only
232  if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
233  $readOnlyReason = $this->readOnlyBySection[$section];
234  }
235 
237  if ( isset( $this->templateOverridesBySection[$section] ) ) {
238  $template = $this->templateOverridesBySection[$section] + $template;
239  }
240 
241  return $this->newLoadBalancer(
242  $template,
243  $this->sectionLoads[$section],
244  $groupLoads,
246  );
247  }
248 
253  public function getMainLB( $wiki = false ) {
254  $section = $this->getSectionForWiki( $wiki );
255  if ( !isset( $this->mainLBs[$section] ) ) {
256  $lb = $this->newMainLB( $wiki );
257  $lb->parentInfo( [ 'id' => "main-$section" ] );
258  $this->chronProt->initLB( $lb );
259  $this->mainLBs[$section] = $lb;
260  }
261 
262  return $this->mainLBs[$section];
263  }
264 
271  protected function newExternalLB( $cluster, $wiki = false ) {
272  if ( !isset( $this->externalLoads[$cluster] ) ) {
273  throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
274  }
276  if ( isset( $this->externalTemplateOverrides ) ) {
277  $template = $this->externalTemplateOverrides + $template;
278  }
279  if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
280  $template = $this->templateOverridesByCluster[$cluster] + $template;
281  }
282 
283  return $this->newLoadBalancer(
284  $template,
285  $this->externalLoads[$cluster],
286  [],
287  $this->readOnlyReason
288  );
289  }
290 
296  public function &getExternalLB( $cluster, $wiki = false ) {
297  if ( !isset( $this->extLBs[$cluster] ) ) {
298  $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
299  $this->extLBs[$cluster]->parentInfo( [ 'id' => "ext-$cluster" ] );
300  $this->chronProt->initLB( $this->extLBs[$cluster] );
301  }
302 
303  return $this->extLBs[$cluster];
304  }
305 
315  private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
316  return new LoadBalancer( [
317  'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
318  'loadMonitor' => $this->loadMonitorClass,
319  'readOnlyReason' => $readOnlyReason,
320  'trxProfiler' => $this->trxProfiler
321  ] );
322  }
323 
332  private function makeServerArray( $template, $loads, $groupLoads ) {
333  $servers = [];
334  $master = true;
335  $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
336  foreach ( $groupLoadsByServer as $server => $stuff ) {
337  if ( !isset( $loads[$server] ) ) {
338  $loads[$server] = 0;
339  }
340  }
341  foreach ( $loads as $serverName => $load ) {
342  $serverInfo = $template;
343  if ( $master ) {
344  $serverInfo['master'] = true;
345  if ( isset( $this->masterTemplateOverrides ) ) {
346  $serverInfo = $this->masterTemplateOverrides + $serverInfo;
347  }
348  $master = false;
349  } else {
350  $serverInfo['slave'] = true;
351  }
352  if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
353  $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
354  }
355  if ( isset( $groupLoadsByServer[$serverName] ) ) {
356  $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
357  }
358  if ( isset( $this->hostsByName[$serverName] ) ) {
359  $serverInfo['host'] = $this->hostsByName[$serverName];
360  } else {
361  $serverInfo['host'] = $serverName;
362  }
363  $serverInfo['hostName'] = $serverName;
364  $serverInfo['load'] = $load;
365  $servers[] = $serverInfo;
366  }
367 
368  return $servers;
369  }
370 
376  private function reindexGroupLoads( $groupLoads ) {
377  $reindexed = [];
378  foreach ( $groupLoads as $group => $loads ) {
379  foreach ( $loads as $server => $load ) {
380  $reindexed[$server][$group] = $load;
381  }
382  }
383 
384  return $reindexed;
385  }
386 
392  private function getDBNameAndPrefix( $wiki = false ) {
393  if ( $wiki === false ) {
395 
396  return [ $wgDBname, $wgDBprefix ];
397  } else {
398  return wfSplitWikiID( $wiki );
399  }
400  }
401 
409  public function forEachLB( $callback, array $params = [] ) {
410  foreach ( $this->mainLBs as $lb ) {
411  call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
412  }
413  foreach ( $this->extLBs as $lb ) {
414  call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
415  }
416  }
417 
418  public function shutdown( $flags = 0 ) {
419  if ( !( $flags & self::SHUTDOWN_NO_CHRONPROT ) ) {
420  $this->shutdownChronologyProtector( $this->chronProt );
421  }
422  $this->commitMasterChanges( __METHOD__ ); // sanity
423  }
424 }
LoadBalancer[] $mainLBs
array $groupLoadsBySection
A 3-d map giving server load ratios for each section and group.
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
__construct(array $conf)
the array() calling protocol came about after MediaWiki 1.4rc1.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2325
getMainLB($wiki=false)
shutdownChronologyProtector(ChronologyProtector $cp)
Definition: LBFactory.php:447
array $serverTemplate
A server info associative array as documented for $wgDBservers.
array $templateOverridesBySection
A 2-d map overriding the server info by section.
newLoadBalancer($template, $loads, $groupLoads, $readOnlyReason)
Make a new load balancer object based on template and load array.
getSectionForWiki($wiki=false)
newExternalLB($cluster, $wiki=false)
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2552
A multi-wiki, multi-master factory for Wikimedia and similar installations.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
array $templateOverridesByServer
A 2-d map overriding serverTemplate and externalTemplateOverrides on a server-by-server basis...
commitMasterChanges($fname=__METHOD__, array $options=[])
Commit changes on all master connections.
Definition: LBFactory.php:234
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:766
Database load balancing object.
An interface for generating database load balancers.
Definition: LBFactory.php:31
array $templateOverridesByCluster
A 2-d map overriding the server info by external storage cluster.
array $sectionLoads
A 2-d map.
string $loadMonitorClass
$params
string bool $readOnlyReason
Reason all LBs are read-only or false if not.
Definition: LBFactory.php:45
getDBNameAndPrefix($wiki=false)
Get the database name and prefix based on the wiki ID.
array $externalTemplateOverrides
A set of server info keys overriding serverTemplate for external storage.
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
Definition: distributors.txt:9
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:2719
array $sectionsByDB
A map of database names to section names.
array $conf
Load balancer factory configuration.
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:35
LoadBalancer[] $extLBs
makeServerArray($template, $loads, $groupLoads)
Make a server array as expected by LoadBalancer::__construct, using a template and load array...
array $hostsByName
A map of hostname to IP address.
array $masterTemplateOverrides
An override array for all master servers.
newMainLB($wiki=false)
array bool $readOnlyBySection
A map of section name to read-only message.
$wgDBprefix
Table name prefix.
shutdown($flags=0)
array $externalLoads
A map of external storage cluster name to server load map.
wfSplitWikiID($wiki)
Split a wiki ID into DB name and table prefix.
controlled by $wgMainCacheType controlled by $wgParserCacheType controlled by $wgMessageCacheType If you set CACHE_NONE to one of the three control default value for MediaWiki still create a but requests to it are no ops and we always fall through to the database If the cache daemon can t be it should also disable itself fairly smoothly By $wgMemc is used but when it is $parserMemc or $messageMemc this is mentioned $wgDBname
Definition: memcached.txt:96
& getExternalLB($cluster, $wiki=false)
reindexGroupLoads($groupLoads)
Take a group load array indexed by group then server, and reindex it by server then group...
forEachLB($callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
array $groupLoadsByDB
A 3-d map giving server load ratios by DB name.