MediaWiki  1.32.5
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 
45  private $maxLag;
46 
48  const MAX_LAG_DEFAULT = 10;
49 
62  public function __construct( array $conf ) {
63  parent::__construct( $conf );
64 
65  $this->servers = $conf['servers'] ?? [];
66  foreach ( $this->servers as $i => $server ) {
67  if ( $i == 0 ) {
68  $this->servers[$i]['master'] = true;
69  } else {
70  $this->servers[$i]['replica'] = true;
71  }
72  }
73 
74  $this->externalClusters = $conf['externalClusters'] ?? [];
75  $this->loadMonitorClass = $conf['loadMonitorClass'] ?? 'LoadMonitor';
76  $this->maxLag = $conf['maxLag'] ?? self::MAX_LAG_DEFAULT;
77  }
78 
83  public function newMainLB( $domain = false ) {
84  return $this->newLoadBalancer( $this->servers );
85  }
86 
91  public function getMainLB( $domain = false ) {
92  if ( !isset( $this->mainLB ) ) {
93  $this->mainLB = $this->newMainLB( $domain );
94  }
95 
96  return $this->mainLB;
97  }
98 
99  public function newExternalLB( $cluster ) {
100  if ( !isset( $this->externalClusters[$cluster] ) ) {
101  throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
102  }
103 
104  return $this->newLoadBalancer( $this->externalClusters[$cluster] );
105  }
106 
107  public function getExternalLB( $cluster ) {
108  if ( !isset( $this->extLBs[$cluster] ) ) {
109  $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
110  }
111 
112  return $this->extLBs[$cluster];
113  }
114 
115  public function getAllMainLBs() {
116  return [ 'DEFAULT' => $this->getMainLB() ];
117  }
118 
119  public function getAllExternalLBs() {
120  $lbs = [];
121  foreach ( $this->externalClusters as $cluster => $unused ) {
122  $lbs[$cluster] = $this->getExternalLB( $cluster );
123  }
124 
125  return $lbs;
126  }
127 
128  private function newLoadBalancer( array $servers ) {
129  $lb = new LoadBalancer( array_merge(
130  $this->baseLoadBalancerParams(),
131  [
132  'servers' => $servers,
133  'maxLag' => $this->maxLag,
134  'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
135  ]
136  ) );
137  $this->initLoadBalancer( $lb );
138 
139  return $lb;
140  }
141 
150  public function forEachLB( $callback, array $params = [] ) {
151  if ( isset( $this->mainLB ) ) {
152  $callback( $this->mainLB, ...$params );
153  }
154  foreach ( $this->extLBs as $lb ) {
155  $callback( $lb, ...$params );
156  }
157  }
158 }
Wikimedia\Rdbms\LBFactorySimple\newLoadBalancer
newLoadBalancer(array $servers)
Definition: LBFactorySimple.php:128
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:119
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:83
Wikimedia\Rdbms\LBFactorySimple\$maxLag
int $maxLag
Definition: LBFactorySimple.php:45
Wikimedia\Rdbms\LBFactorySimple\__construct
__construct(array $conf)
Definition: LBFactorySimple.php:62
Wikimedia\Rdbms\LBFactory\initLoadBalancer
initLoadBalancer(ILoadBalancer $lb)
Definition: LBFactory.php:604
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:567
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:91
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:150
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:99
Wikimedia\Rdbms\LBFactorySimple\getExternalLB
getExternalLB( $cluster)
Definition: LBFactorySimple.php:107
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:115