MediaWiki  1.33.0
LBFactorySimple.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
26 use InvalidArgumentException;
27 
31 class LBFactorySimple extends LBFactory {
33  private $mainLB;
35  private $extLBs = [];
36 
38  private $servers = [];
40  private $externalClusters = [];
41 
44 
57  public function __construct( array $conf ) {
58  parent::__construct( $conf );
59 
60  $this->servers = $conf['servers'] ?? [];
61  foreach ( $this->servers as $i => $server ) {
62  if ( $i == 0 ) {
63  $this->servers[$i]['master'] = true;
64  } else {
65  $this->servers[$i]['replica'] = true;
66  }
67  }
68 
69  $this->externalClusters = $conf['externalClusters'] ?? [];
70  $this->loadMonitorClass = $conf['loadMonitorClass'] ?? 'LoadMonitor';
71  }
72 
77  public function newMainLB( $domain = false ) {
78  return $this->newLoadBalancer( $this->servers );
79  }
80 
85  public function getMainLB( $domain = false ) {
86  if ( !isset( $this->mainLB ) ) {
87  $this->mainLB = $this->newMainLB( $domain );
88  }
89 
90  return $this->mainLB;
91  }
92 
93  public function newExternalLB( $cluster ) {
94  if ( !isset( $this->externalClusters[$cluster] ) ) {
95  throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
96  }
97 
98  return $this->newLoadBalancer( $this->externalClusters[$cluster] );
99  }
100 
101  public function getExternalLB( $cluster ) {
102  if ( !isset( $this->extLBs[$cluster] ) ) {
103  $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
104  }
105 
106  return $this->extLBs[$cluster];
107  }
108 
109  public function getAllMainLBs() {
110  return [ 'DEFAULT' => $this->getMainLB() ];
111  }
112 
113  public function getAllExternalLBs() {
114  $lbs = [];
115  foreach ( $this->externalClusters as $cluster => $unused ) {
116  $lbs[$cluster] = $this->getExternalLB( $cluster );
117  }
118 
119  return $lbs;
120  }
121 
122  private function newLoadBalancer( array $servers ) {
123  $lb = new LoadBalancer( array_merge(
124  $this->baseLoadBalancerParams(),
125  [
126  'servers' => $servers,
127  'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
128  ]
129  ) );
130  $this->initLoadBalancer( $lb );
131 
132  return $lb;
133  }
134 
143  public function forEachLB( $callback, array $params = [] ) {
144  if ( isset( $this->mainLB ) ) {
145  $callback( $this->mainLB, ...$params );
146  }
147  foreach ( $this->extLBs as $lb ) {
148  $callback( $lb, ...$params );
149  }
150  }
151 }
Wikimedia\Rdbms\LBFactorySimple\newLoadBalancer
newLoadBalancer(array $servers)
Definition: LBFactorySimple.php:122
servers
storage can be distributed across multiple servers
Definition: memcached.txt:33
Wikimedia\Rdbms\LBFactorySimple\getAllExternalLBs
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
Definition: LBFactorySimple.php:113
Wikimedia\Rdbms\LBFactorySimple\$loadMonitorClass
string $loadMonitorClass
Definition: LBFactorySimple.php:43
Wikimedia\Rdbms\LBFactorySimple\$externalClusters
array[] $externalClusters
Map of (cluster => (server index => server config))
Definition: LBFactorySimple.php:40
Wikimedia\Rdbms\LBFactorySimple\newMainLB
newMainLB( $domain=false)
Definition: LBFactorySimple.php:77
Wikimedia\Rdbms\LBFactorySimple\__construct
__construct(array $conf)
Definition: LBFactorySimple.php:57
Wikimedia\Rdbms\LBFactory\initLoadBalancer
initLoadBalancer(ILoadBalancer $lb)
Definition: LBFactory.php:609
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
$params
$params
Definition: styleTest.css.php:44
Wikimedia\Rdbms\LBFactory\baseLoadBalancerParams
baseLoadBalancerParams()
Base parameters to ILoadBalancer::__construct()
Definition: LBFactory.php:571
php
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
Wikimedia\Rdbms\LBFactorySimple\getMainLB
getMainLB( $domain=false)
Definition: LBFactorySimple.php:85
Wikimedia\Rdbms\LBFactorySimple
A simple single-master LBFactory that gets its configuration from the b/c globals.
Definition: LBFactorySimple.php:31
Wikimedia\Rdbms\LBFactorySimple\$extLBs
LoadBalancer[] $extLBs
Definition: LBFactorySimple.php:35
Wikimedia\Rdbms\LBFactorySimple\$servers
array[] $servers
Map of (server index => server config)
Definition: LBFactorySimple.php:38
Wikimedia\Rdbms\LBFactorySimple\forEachLB
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
Definition: LBFactorySimple.php:143
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
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))
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:41
Wikimedia\Rdbms\LBFactorySimple\newExternalLB
newExternalLB( $cluster)
Definition: LBFactorySimple.php:93
Wikimedia\Rdbms\LBFactorySimple\getExternalLB
getExternalLB( $cluster)
Definition: LBFactorySimple.php:101
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:39
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
Wikimedia\Rdbms\LBFactorySimple\$mainLB
LoadBalancer $mainLB
Definition: LBFactorySimple.php:33
Wikimedia\Rdbms\LBFactorySimple\getAllMainLBs
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
Definition: LBFactorySimple.php:109