MediaWiki REL1_33
LBFactoryMulti.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
37
43
49
50 // Optional settings
51
54
56 private $groupLoadsByDB = [];
57
59 private $hostsByName = [];
60
62 private $externalLoads = [];
63
69
76
79
82
85
90 private $readOnlyBySection = [];
91
93 private $mainLBs = [];
94
96 private $extLBs = [];
97
99 private $loadMonitorClass = 'LoadMonitor';
100
102 private $lastDomain;
103
106
163 public function __construct( array $conf ) {
164 parent::__construct( $conf );
165
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 $section = $this->sectionsByDB[$dbName] ?? 'DEFAULT';
196 $this->lastSection = $section;
197 $this->lastDomain = $domain;
198
199 return $section;
200 }
201
206 public function newMainLB( $domain = false ) {
207 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
208 $section = $this->getSectionForDomain( $domain );
209 $groupLoads = $this->groupLoadsByDB[$dbName] ?? [];
210
211 if ( isset( $this->groupLoadsBySection[$section] ) ) {
212 $groupLoads = array_merge_recursive(
213 $groupLoads, $this->groupLoadsBySection[$section] );
214 }
215
217 // Use the LB-specific read-only reason if everything isn't already read-only
218 if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
219 $readOnlyReason = $this->readOnlyBySection[$section];
220 }
221
223 if ( isset( $this->templateOverridesBySection[$section] ) ) {
224 $template = $this->templateOverridesBySection[$section] + $template;
225 }
226
227 return $this->newLoadBalancer(
228 $template,
229 $this->sectionLoads[$section],
230 $groupLoads,
232 );
233 }
234
239 public function getMainLB( $domain = false ) {
240 $section = $this->getSectionForDomain( $domain );
241 if ( !isset( $this->mainLBs[$section] ) ) {
242 $this->mainLBs[$section] = $this->newMainLB( $domain );
243 }
244
245 return $this->mainLBs[$section];
246 }
247
248 public function newExternalLB( $cluster ) {
249 if ( !isset( $this->externalLoads[$cluster] ) ) {
250 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
251 }
253 if ( $this->externalTemplateOverrides ) {
254 $template = $this->externalTemplateOverrides + $template;
255 }
256 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
257 $template = $this->templateOverridesByCluster[$cluster] + $template;
258 }
259
260 return $this->newLoadBalancer(
261 $template,
262 $this->externalLoads[$cluster],
263 [],
264 $this->readOnlyReason
265 );
266 }
267
268 public function getExternalLB( $cluster ) {
269 if ( !isset( $this->extLBs[$cluster] ) ) {
270 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
271 }
272
273 return $this->extLBs[$cluster];
274 }
275
276 public function getAllMainLBs() {
277 $lbs = [];
278 foreach ( $this->sectionsByDB as $db => $section ) {
279 if ( !isset( $lbs[$section] ) ) {
280 $lbs[$section] = $this->getMainLB( $db );
281 }
282 }
283
284 return $lbs;
285 }
286
287 public function getAllExternalLBs() {
288 $lbs = [];
289 foreach ( $this->externalLoads as $cluster => $unused ) {
290 $lbs[$cluster] = $this->getExternalLB( $cluster );
291 }
292
293 return $lbs;
294 }
295
305 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
306 $lb = new LoadBalancer( array_merge(
307 $this->baseLoadBalancerParams(),
308 [
309 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
310 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
311 'readOnlyReason' => $readOnlyReason
312 ]
313 ) );
314 $this->initLoadBalancer( $lb );
315
316 return $lb;
317 }
318
327 private function makeServerArray( $template, $loads, $groupLoads ) {
328 $servers = [];
329 $master = true;
330 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
331 foreach ( $groupLoadsByServer as $server => $stuff ) {
332 if ( !isset( $loads[$server] ) ) {
333 $loads[$server] = 0;
334 }
335 }
336 foreach ( $loads as $serverName => $load ) {
337 $serverInfo = $template;
338 if ( $master ) {
339 $serverInfo['master'] = true;
340 if ( $this->masterTemplateOverrides ) {
341 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
342 }
343 $master = false;
344 } else {
345 $serverInfo['replica'] = true;
346 }
347 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
348 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
349 }
350 if ( isset( $groupLoadsByServer[$serverName] ) ) {
351 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
352 }
353 $serverInfo['host'] = $this->hostsByName[$serverName] ?? $serverName;
354 $serverInfo['hostName'] = $serverName;
355 $serverInfo['load'] = $load;
356 $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
357
358 $servers[] = $serverInfo;
359 }
360
361 return $servers;
362 }
363
369 private function reindexGroupLoads( $groupLoads ) {
370 $reindexed = [];
371 foreach ( $groupLoads as $group => $loads ) {
372 foreach ( $loads as $server => $load ) {
373 $reindexed[$server][$group] = $load;
374 }
375 }
376
377 return $reindexed;
378 }
379
384 private function getDBNameAndPrefix( $domain = false ) {
385 $domain = ( $domain === false )
386 ? $this->localDomain
387 : DatabaseDomain::newFromId( $domain );
388
389 return [ $domain->getDatabase(), $domain->getTablePrefix() ];
390 }
391
399 public function forEachLB( $callback, array $params = [] ) {
400 foreach ( $this->mainLBs as $lb ) {
401 $callback( $lb, ...$params );
402 }
403 foreach ( $this->extLBs as $lb ) {
404 $callback( $lb, ...$params );
405 }
406 }
407}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
A multi-database, multi-master factory for Wikimedia and similar installations.
array $sectionsByDB
A map of database names to section names.
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
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.
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
reindexGroupLoads( $groupLoads)
Take a group load array indexed by group then server, and reindex it by server then group.
array $externalTemplateOverrides
A set of server info keys overriding serverTemplate for external storage.
array $masterTemplateOverrides
An override array for all master servers.
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.
array $templateOverridesByServer
A 2-d map overriding serverTemplate and externalTemplateOverrides on a server-by-server basis.
array[] $serverTemplate
Server info associative array.
array bool $readOnlyBySection
A map of section name to read-only message.
array $groupLoadsByDB
A 3-d map giving server load ratios by DB name.
array $groupLoadsBySection
A 3-d map giving server load ratios for each section and group.
array $hostsByName
A map of hostname to IP address.
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.
An interface for generating database load balancers.
Definition LBFactory.php:39
initLoadBalancer(ILoadBalancer $lb)
string bool $readOnlyReason
Reason all LBs are read-only or false if not.
Definition LBFactory.php:93
baseLoadBalancerParams()
Base parameters to ILoadBalancer::__construct()
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
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:822
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:3070
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params