MediaWiki  master
LBFactoryMulti.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
27 
34 class LBFactoryMulti extends LBFactory {
36  private $sectionsByDB;
41  private $sectionLoads;
46  private $serverTemplate;
47 
49  private $groupLoadsBySection = [];
51  private $groupLoadsByDB = [];
53  private $hostsByName = [];
55  private $externalLoads = [];
77  private $readOnlyBySection = [];
78 
80  private $mainLBs = [];
82  private $extLBs = [];
84  private $loadMonitorClass = 'LoadMonitor';
86  private $lastDomain;
88  private $lastSection;
89 
146  public function __construct( array $conf ) {
147  parent::__construct( $conf );
148 
149  $required = [ 'sectionsByDB', 'sectionLoads', 'serverTemplate' ];
150  $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
151  'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
152  'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
153  'readOnlyBySection', 'loadMonitorClass' ];
154 
155  foreach ( $required as $key ) {
156  if ( !isset( $conf[$key] ) ) {
157  throw new InvalidArgumentException( __CLASS__ . ": $key is required." );
158  }
159  $this->$key = $conf[$key];
160  }
161 
162  foreach ( $optional as $key ) {
163  if ( isset( $conf[$key] ) ) {
164  $this->$key = $conf[$key];
165  }
166  }
167  }
168 
173  private function getSectionForDomain( $domain = false ) {
174  if ( $this->lastDomain === $domain ) {
175  return $this->lastSection;
176  }
177 
178  $database = $this->getDatabaseFromDomain( $domain );
179  $section = $this->sectionsByDB[$database] ?? 'DEFAULT';
180  $this->lastSection = $section;
181  $this->lastDomain = $domain;
182 
183  return $section;
184  }
185 
186  public function newMainLB( $domain = false ) {
187  $database = $this->getDatabaseFromDomain( $domain );
188  $section = $this->getSectionForDomain( $domain );
189  $groupLoads = $this->groupLoadsByDB[$database] ?? [];
190 
191  if ( isset( $this->groupLoadsBySection[$section] ) ) {
192  $groupLoads = array_merge_recursive(
193  $groupLoads, $this->groupLoadsBySection[$section] );
194  }
195 
197  // Use the LB-specific read-only reason if everything isn't already read-only
198  if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
199  $readOnlyReason = $this->readOnlyBySection[$section];
200  }
201 
203  if ( isset( $this->templateOverridesBySection[$section] ) ) {
204  $template = $this->templateOverridesBySection[$section] + $template;
205  }
206 
207  return $this->newLoadBalancer(
208  $template,
209  $this->sectionLoads[$section],
210  $groupLoads,
212  );
213  }
214 
215  public function getMainLB( $domain = false ) {
216  $section = $this->getSectionForDomain( $domain );
217  if ( !isset( $this->mainLBs[$section] ) ) {
218  $this->mainLBs[$section] = $this->newMainLB( $domain );
219  }
220 
221  return $this->mainLBs[$section];
222  }
223 
224  public function newExternalLB( $cluster ) {
225  if ( !isset( $this->externalLoads[$cluster] ) ) {
226  throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
227  }
229  if ( $this->externalTemplateOverrides ) {
230  $template = $this->externalTemplateOverrides + $template;
231  }
232  if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
233  $template = $this->templateOverridesByCluster[$cluster] + $template;
234  }
235 
236  return $this->newLoadBalancer(
237  $template,
238  $this->externalLoads[$cluster],
239  [],
240  $this->readOnlyReason
241  );
242  }
243 
244  public function getExternalLB( $cluster ) {
245  if ( !isset( $this->extLBs[$cluster] ) ) {
246  $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
247  }
248 
249  return $this->extLBs[$cluster];
250  }
251 
252  public function getAllMainLBs() {
253  $lbs = [];
254  foreach ( $this->sectionsByDB as $db => $section ) {
255  if ( !isset( $lbs[$section] ) ) {
256  $lbs[$section] = $this->getMainLB( $db );
257  }
258  }
259 
260  return $lbs;
261  }
262 
263  public function getAllExternalLBs() {
264  $lbs = [];
265  foreach ( $this->externalLoads as $cluster => $unused ) {
266  $lbs[$cluster] = $this->getExternalLB( $cluster );
267  }
268 
269  return $lbs;
270  }
271 
281  private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
282  $lb = new LoadBalancer( array_merge(
283  $this->baseLoadBalancerParams(),
284  [
285  'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
286  'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
287  'readOnlyReason' => $readOnlyReason
288  ]
289  ) );
290  $this->initLoadBalancer( $lb );
291 
292  return $lb;
293  }
294 
303  private function makeServerArray( $template, $loads, $groupLoads ) {
304  $servers = [];
305  $master = true;
306  $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
307  foreach ( $groupLoadsByServer as $server => $stuff ) {
308  if ( !isset( $loads[$server] ) ) {
309  $loads[$server] = 0;
310  }
311  }
312  foreach ( $loads as $serverName => $load ) {
313  $serverInfo = $template;
314  if ( $master ) {
315  $serverInfo['master'] = true;
316  if ( $this->masterTemplateOverrides ) {
317  $serverInfo = $this->masterTemplateOverrides + $serverInfo;
318  }
319  $master = false;
320  } else {
321  $serverInfo['replica'] = true;
322  }
323  if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
324  $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
325  }
326  if ( isset( $groupLoadsByServer[$serverName] ) ) {
327  $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
328  }
329  $serverInfo['host'] = $this->hostsByName[$serverName] ?? $serverName;
330  $serverInfo['hostName'] = $serverName;
331  $serverInfo['load'] = $load;
332  $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
333 
334  $servers[] = $serverInfo;
335  }
336 
337  return $servers;
338  }
339 
345  private function reindexGroupLoads( $groupLoads ) {
346  $reindexed = [];
347  foreach ( $groupLoads as $group => $loads ) {
348  foreach ( $loads as $server => $load ) {
349  $reindexed[$server][$group] = $load;
350  }
351  }
352 
353  return $reindexed;
354  }
355 
360  private function getDatabaseFromDomain( $domain = false ) {
361  return ( $domain === false )
362  ? $this->localDomain->getDatabase()
363  : DatabaseDomain::newFromId( $domain )->getDatabase();
364  }
365 
366  public function forEachLB( $callback, array $params = [] ) {
367  foreach ( $this->mainLBs as $lb ) {
368  $callback( $lb, ...$params );
369  }
370  foreach ( $this->extLBs as $lb ) {
371  $callback( $lb, ...$params );
372  }
373  }
374 }
forEachLB( $callback, array $params=[])
Execute a function for each currently tracked (instantiated) load balancer.
getMainLB( $domain=false)
Get a cached (tracked) load balancer object.
string bool $readOnlyReason
Reason all LBs are read-only or false if not.
Definition: LBFactory.php:98
reindexGroupLoads( $groupLoads)
Take a group load array indexed by group then server, and reindex it by server then group...
An interface for generating database load balancers.
Definition: LBFactory.php:40
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
array $templateOverridesByCluster
A 2-d map overriding the server info by external storage cluster.
array $templateOverridesBySection
A 2-d map overriding the server info by section.
getExternalLB( $cluster)
Get a cached (tracked) load balancer for external storage.
A multi-database, multi-master factory for Wikimedia and similar installations.
newExternalLB( $cluster)
Create a new load balancer for external storage.
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
makeServerArray( $template, $loads, $groupLoads)
Make a server array as expected by LoadBalancer::__construct, using a template and load array...
array $sectionsByDB
A map of database names to section names.
initLoadBalancer(ILoadBalancer $lb)
Definition: LBFactory.php:637
array $externalTemplateOverrides
A set of server info keys overriding serverTemplate for external storage.
array $groupLoadsByDB
A 3-d map giving server load ratios by DB name.
$params
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
newMainLB( $domain=false)
Create a new load balancer object.
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:3045
array $groupLoadsBySection
A 3-d map giving server load ratios for each section and group.
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
const DBO_DEFAULT
Definition: defines.php:13
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:773
array $hostsByName
A map of hostname to IP address.
array $templateOverridesByServer
A 2-d map overriding serverTemplate and externalTemplateOverrides on a server-by-server basis...
newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason)
Make a new load balancer object based on template and load array.
array $masterTemplateOverrides
An override array for all master servers.
array $externalLoads
A map of external storage cluster name to server load map.
baseLoadBalancerParams()
Base parameters to ILoadBalancer::__construct()
Definition: LBFactory.php:598
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
Database connection, tracking, load balancing, and transaction manager for a cluster.
array bool $readOnlyBySection
A map of section name to read-only message.
array [] $serverTemplate
Server info associative array.