MediaWiki  master
LBFactorySimple.php
Go to the documentation of this file.
1 <?php
24 namespace Wikimedia\Rdbms;
25 
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 }
newExternalLB( $cluster)
Create a new load balancer for external storage.
array [] $externalClusters
Map of (cluster => (server index => server config))
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
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
initLoadBalancer(ILoadBalancer $lb)
Definition: LBFactory.php:619
array [] $servers
Map of (server index => server config)
getExternalLB( $cluster)
Get a cached (tracked) load balancer for external storage.
$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
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
storage can be distributed across multiple servers
Definition: memcached.txt:33
baseLoadBalancerParams()
Base parameters to ILoadBalancer::__construct()
Definition: LBFactory.php:580
Database connection, tracking, load balancing, and transaction manager for a cluster.
A simple single-master LBFactory that gets its configuration from the b/c globals.
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.