MediaWiki  1.23.2
LBFactoryMulti.php
Go to the documentation of this file.
1 <?php
76 class LBFactoryMulti extends LBFactory {
77  // Required settings
78 
80  protected $sectionsByDB;
81 
86  protected $sectionLoads;
87 
93  protected $serverTemplate;
94 
95  // Optional settings
96 
98  protected $groupLoadsBySection = array();
99 
101  protected $groupLoadsByDB = array();
102 
104  protected $hostsByName = array();
105 
107  protected $externalLoads = array();
108 
114 
120  protected $templateOverridesByServer;
121 
123  protected $templateOverridesByCluster;
124 
126  protected $masterTemplateOverrides;
127 
132  protected $readOnlyBySection = array();
133 
134  // Other stuff
135 
137  protected $conf;
138 
140  protected $mainLBs = array();
141 
143  protected $extLBs = array();
144 
146  protected $lastWiki;
147 
149  protected $lastSection;
150 
155  function __construct( $conf ) {
156  $this->chronProt = new ChronologyProtector;
157  $this->conf = $conf;
158  $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
159  $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
160  'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
161  'templateOverridesByCluster', 'masterTemplateOverrides',
162  'readOnlyBySection' );
163 
164  foreach ( $required as $key ) {
165  if ( !isset( $conf[$key] ) ) {
166  throw new MWException( __CLASS__ . ": $key is required in configuration" );
167  }
168  $this->$key = $conf[$key];
169  }
170 
171  foreach ( $optional as $key ) {
172  if ( isset( $conf[$key] ) ) {
173  $this->$key = $conf[$key];
174  }
175  }
176 
177  // Check for read-only mode
178  $section = $this->getSectionForWiki();
179  if ( !empty( $this->readOnlyBySection[$section] ) ) {
180  global $wgReadOnly;
181  $wgReadOnly = $this->readOnlyBySection[$section];
182  }
183  }
184 
189  function getSectionForWiki( $wiki = false ) {
190  if ( $this->lastWiki === $wiki ) {
191  return $this->lastSection;
192  }
193  list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
194  if ( isset( $this->sectionsByDB[$dbName] ) ) {
195  $section = $this->sectionsByDB[$dbName];
196  } else {
197  $section = 'DEFAULT';
198  }
199  $this->lastSection = $section;
200  $this->lastWiki = $wiki;
201 
202  return $section;
203  }
204 
209  function newMainLB( $wiki = false ) {
210  list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
211  $section = $this->getSectionForWiki( $wiki );
212  $groupLoads = array();
213  if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
214  $groupLoads = $this->groupLoadsByDB[$dbName];
215  }
216 
217  if ( isset( $this->groupLoadsBySection[$section] ) ) {
218  $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
219  }
220 
221  return $this->newLoadBalancer(
222  $this->serverTemplate,
223  $this->sectionLoads[$section],
224  $groupLoads
225  );
226  }
227 
232  function getMainLB( $wiki = false ) {
233  $section = $this->getSectionForWiki( $wiki );
234  if ( !isset( $this->mainLBs[$section] ) ) {
235  $lb = $this->newMainLB( $wiki, $section );
236  $lb->parentInfo( array( 'id' => "main-$section" ) );
237  $this->chronProt->initLB( $lb );
238  $this->mainLBs[$section] = $lb;
239  }
240 
241  return $this->mainLBs[$section];
242  }
243 
250  function newExternalLB( $cluster, $wiki = false ) {
251  if ( !isset( $this->externalLoads[$cluster] ) ) {
252  throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
253  }
255  if ( isset( $this->externalTemplateOverrides ) ) {
256  $template = $this->externalTemplateOverrides + $template;
257  }
258  if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
259  $template = $this->templateOverridesByCluster[$cluster] + $template;
260  }
261 
262  return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
263  }
264 
270  function &getExternalLB( $cluster, $wiki = false ) {
271  if ( !isset( $this->extLBs[$cluster] ) ) {
272  $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
273  $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
274  $this->chronProt->initLB( $this->extLBs[$cluster] );
275  }
276 
277  return $this->extLBs[$cluster];
278  }
279 
288  function newLoadBalancer( $template, $loads, $groupLoads ) {
289  global $wgMasterWaitTimeout;
290  $servers = $this->makeServerArray( $template, $loads, $groupLoads );
292  'servers' => $servers,
293  'masterWaitTimeout' => $wgMasterWaitTimeout
294  ) );
295 
296  return $lb;
297  }
298 
307  function makeServerArray( $template, $loads, $groupLoads ) {
308  $servers = array();
309  $master = true;
310  $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
311  foreach ( $groupLoadsByServer as $server => $stuff ) {
312  if ( !isset( $loads[$server] ) ) {
313  $loads[$server] = 0;
314  }
315  }
316  foreach ( $loads as $serverName => $load ) {
317  $serverInfo = $template;
318  if ( $master ) {
319  $serverInfo['master'] = true;
320  if ( isset( $this->masterTemplateOverrides ) ) {
321  $serverInfo = $this->masterTemplateOverrides + $serverInfo;
322  }
323  $master = false;
324  }
325  if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
326  $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
327  }
328  if ( isset( $groupLoadsByServer[$serverName] ) ) {
329  $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
330  }
331  if ( isset( $this->hostsByName[$serverName] ) ) {
332  $serverInfo['host'] = $this->hostsByName[$serverName];
333  } else {
334  $serverInfo['host'] = $serverName;
335  }
336  $serverInfo['hostName'] = $serverName;
337  $serverInfo['load'] = $load;
338  $servers[] = $serverInfo;
339  }
340 
341  return $servers;
342  }
343 
349  function reindexGroupLoads( $groupLoads ) {
350  $reindexed = array();
351  foreach ( $groupLoads as $group => $loads ) {
352  foreach ( $loads as $server => $load ) {
353  $reindexed[$server][$group] = $load;
354  }
355  }
356 
357  return $reindexed;
358  }
359 
365  function getDBNameAndPrefix( $wiki = false ) {
366  if ( $wiki === false ) {
367  global $wgDBname, $wgDBprefix;
368 
369  return array( $wgDBname, $wgDBprefix );
370  } else {
371  return wfSplitWikiID( $wiki );
372  }
373  }
374 
382  function forEachLB( $callback, $params = array() ) {
383  foreach ( $this->mainLBs as $lb ) {
384  call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
385  }
386  foreach ( $this->extLBs as $lb ) {
387  call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
388  }
389  }
390 
391  function shutdown() {
392  foreach ( $this->mainLBs as $lb ) {
393  $this->chronProt->shutdownLB( $lb );
394  }
395  foreach ( $this->extLBs as $extLB ) {
396  $this->chronProt->shutdownLB( $extLB );
397  }
398  $this->chronProt->shutdown();
399  $this->commitMasterChanges();
400  }
401 }
LBFactoryMulti\$readOnlyBySection
$readOnlyBySection
Definition: LBFactoryMulti.php:121
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LBFactoryMulti\__construct
__construct( $conf)
Definition: LBFactoryMulti.php:139
LBFactoryMulti\$masterTemplateOverrides
array $masterTemplateOverrides
An override array for all master servers *.
Definition: LBFactoryMulti.php:115
LBFactoryMulti\$lastSection
string $lastSection
Definition: LBFactoryMulti.php:133
LBFactoryMulti\$externalLoads
array $externalLoads
A map of external storage cluster name to server load map *.
Definition: LBFactoryMulti.php:100
LBFactoryMulti\getDBNameAndPrefix
getDBNameAndPrefix( $wiki=false)
Get the database name and prefix based on the wiki ID.
Definition: LBFactoryMulti.php:349
LBFactoryMulti\newExternalLB
newExternalLB( $cluster, $wiki=false)
Definition: LBFactoryMulti.php:234
$params
$params
Definition: styleTest.css.php:40
wfSplitWikiID
wfSplitWikiID( $wiki)
Split a wiki ID into DB name and table prefix.
Definition: GlobalFunctions.php:3620
LBFactoryMulti\forEachLB
forEachLB( $callback, $params=array())
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
Definition: LBFactoryMulti.php:366
ChronologyProtector
Class for ensuring a consistent ordering of events as seen by the user, despite replication.
Definition: ChronologyProtector.php:28
LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:28
LBFactoryMulti\newMainLB
newMainLB( $wiki=false)
Definition: LBFactoryMulti.php:193
LBFactoryMulti\$hostsByName
array $hostsByName
A map of hostname to IP address *.
Definition: LBFactoryMulti.php:98
LBFactoryMulti\$mainLBs
LoadBalancer[] $mainLBs
Definition: LBFactoryMulti.php:127
LBFactoryMulti\reindexGroupLoads
reindexGroupLoads( $groupLoads)
Take a group load array indexed by group then server, and reindex it by server then group.
Definition: LBFactoryMulti.php:333
LBFactoryMulti\$sectionsByDB
array $sectionsByDB
A map of database names to section names *.
Definition: LBFactoryMulti.php:79
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
MWException
MediaWiki exception.
Definition: MWException.php:26
$wgDBname
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
LBFactoryMulti\$groupLoadsByDB
array $groupLoadsByDB
A 3-d map giving server load ratios by DB name *.
Definition: LBFactoryMulti.php:96
LBFactoryMulti\$groupLoadsBySection
array $groupLoadsBySection
A 3-d map giving server load ratios for each section and group *.
Definition: LBFactoryMulti.php:94
LBFactoryMulti\newLoadBalancer
newLoadBalancer( $template, $loads, $groupLoads)
Make a new load balancer object based on template and load array.
Definition: LBFactoryMulti.php:272
LBFactoryMulti\$templateOverridesByCluster
array $templateOverridesByCluster
A 2-d map overriding the server info by external storage cluster *.
Definition: LBFactoryMulti.php:113
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
list
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
LBFactoryMulti\$lastWiki
string $lastWiki
Definition: LBFactoryMulti.php:131
LoadBalancer
Database load balancing object.
Definition: LoadBalancer.php:30
$section
$section
Definition: Utf8Test.php:88
LBFactoryMulti\getExternalLB
& getExternalLB( $cluster, $wiki=false)
Definition: LBFactoryMulti.php:254
LBFactory\commitMasterChanges
commitMasterChanges()
Commit changes on all master connections.
Definition: LBFactory.php:191
LBFactoryMulti\$sectionLoads
array $sectionLoads
A 2-d map.
Definition: LBFactoryMulti.php:84
LBFactoryMulti\$serverTemplate
array $serverTemplate
A server info associative array as documented for $wgDBservers.
Definition: LBFactoryMulti.php:90
LBFactoryMulti\$externalTemplateOverrides
array $externalTemplateOverrides
A set of server info keys overriding serverTemplate for external storage.
Definition: LBFactoryMulti.php:105
as
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
LBFactoryMulti
A multi-wiki, multi-master factory for Wikimedia and similar installations.
Definition: LBFactoryMulti.php:76
LBFactoryMulti\$templateOverridesByServer
array $templateOverridesByServer
A 2-d map overriding serverTemplate and externalTemplateOverrides on a server-by-server basis.
Definition: LBFactoryMulti.php:111
LBFactoryMulti\$conf
array $conf
Load balancer factory configuration *.
Definition: LBFactoryMulti.php:125
LBFactoryMulti\makeServerArray
makeServerArray( $template, $loads, $groupLoads)
Make a server array as expected by LoadBalancer::__construct, using a template and load array.
Definition: LBFactoryMulti.php:291
LBFactoryMulti\$extLBs
LoadBalancer[] $extLBs
Definition: LBFactoryMulti.php:129
LBFactoryMulti\getSectionForWiki
getSectionForWiki( $wiki=false)
Definition: LBFactoryMulti.php:173
LBFactoryMulti\shutdown
shutdown()
Prepare all tracked load balancers for shutdown STUB.
Definition: LBFactoryMulti.php:375
LBFactoryMulti\getMainLB
getMainLB( $wiki=false)
Definition: LBFactoryMulti.php:216