MediaWiki  1.34.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 $externalLBs = [];
36 
38  private $mainServers = [];
41 
44 
57  public function __construct( array $conf ) {
58  parent::__construct( $conf );
59 
60  $this->mainServers = $conf['servers'] ?? [];
61  foreach ( $this->mainServers as $i => $server ) {
62  if ( $i == 0 ) {
63  $this->mainServers[$i]['master'] = true;
64  } else {
65  $this->mainServers[$i]['replica'] = true;
66  }
67  }
68 
69  foreach ( ( $conf['externalClusters'] ?? [] ) as $cluster => $servers ) {
70  foreach ( $servers as $index => $server ) {
71  $this->externalServersByCluster[$cluster][$index] = $server;
72  }
73  }
74 
75  $this->loadMonitorClass = $conf['loadMonitorClass'] ?? LoadMonitor::class;
76  }
77 
78  public function newMainLB( $domain = false, $owner = null ) {
79  return $this->newLoadBalancer( $this->mainServers, $owner );
80  }
81 
82  public function getMainLB( $domain = false ) {
83  if ( $this->mainLB === null ) {
84  $this->mainLB = $this->newMainLB( $domain, $this->getOwnershipId() );
85  }
86 
87  return $this->mainLB;
88  }
89 
90  public function newExternalLB( $cluster, $owner = null ) {
91  if ( !isset( $this->externalServersByCluster[$cluster] ) ) {
92  throw new InvalidArgumentException( "Unknown cluster '$cluster'." );
93  }
94 
95  return $this->newLoadBalancer( $this->externalServersByCluster[$cluster], $owner );
96  }
97 
98  public function getExternalLB( $cluster ) {
99  if ( !isset( $this->externalLBs[$cluster] ) ) {
100  $this->externalLBs[$cluster] = $this->newExternalLB( $cluster, $this->getOwnershipId() );
101  }
102 
103  return $this->externalLBs[$cluster];
104  }
105 
106  public function getAllMainLBs() {
107  return [ self::CLUSTER_MAIN_DEFAULT => $this->getMainLB() ];
108  }
109 
110  public function getAllExternalLBs() {
111  $lbs = [];
112  foreach ( array_keys( $this->externalServersByCluster ) as $cluster ) {
113  $lbs[$cluster] = $this->getExternalLB( $cluster );
114  }
115 
116  return $lbs;
117  }
118 
119  private function newLoadBalancer( array $servers, $owner ) {
120  $lb = new LoadBalancer( array_merge(
121  $this->baseLoadBalancerParams( $owner ),
122  [
123  'servers' => $servers,
124  'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
125  ]
126  ) );
127  $this->initLoadBalancer( $lb );
128 
129  return $lb;
130  }
131 
132  public function forEachLB( $callback, array $params = [] ) {
133  if ( $this->mainLB !== null ) {
134  $callback( $this->mainLB, ...$params );
135  }
136  foreach ( $this->externalLBs as $lb ) {
137  $callback( $lb, ...$params );
138  }
139  }
140 }
Wikimedia\Rdbms\LBFactorySimple\newMainLB
newMainLB( $domain=false, $owner=null)
Create a new load balancer object.
Definition: LBFactorySimple.php:78
Wikimedia\Rdbms\LBFactorySimple\getAllExternalLBs
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
Definition: LBFactorySimple.php:110
Wikimedia\Rdbms\LBFactorySimple\$loadMonitorClass
string $loadMonitorClass
Definition: LBFactorySimple.php:43
Wikimedia\Rdbms\LBFactorySimple\$externalServersByCluster
array[][] $externalServersByCluster
Map of (cluster => server index => server config map)
Definition: LBFactorySimple.php:40
Wikimedia\Rdbms\LBFactorySimple\__construct
__construct(array $conf)
Definition: LBFactorySimple.php:57
Wikimedia\Rdbms\LBFactory\initLoadBalancer
initLoadBalancer(ILoadBalancer $lb)
Definition: LBFactory.php:624
Wikimedia\Rdbms\LBFactory\getOwnershipId
getOwnershipId()
Definition: LBFactory.php:739
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\LBFactorySimple\getMainLB
getMainLB( $domain=false)
Get a cached (tracked) load balancer object.
Definition: LBFactorySimple.php:82
Wikimedia\Rdbms\LBFactorySimple
A simple single-master LBFactory that gets its configuration from the b/c globals.
Definition: LBFactorySimple.php:31
Wikimedia\Rdbms\LBFactorySimple\$externalLBs
LoadBalancer[] $externalLBs
Definition: LBFactorySimple.php:35
Wikimedia\Rdbms\LBFactorySimple\forEachLB
forEachLB( $callback, array $params=[])
Execute a function for each currently tracked (instantiated) load balancer.
Definition: LBFactorySimple.php:132
Wikimedia\Rdbms\LBFactory\baseLoadBalancerParams
baseLoadBalancerParams( $owner)
Get parameters to ILoadBalancer::__construct()
Definition: LBFactory.php:585
Wikimedia\Rdbms\LBFactorySimple\newLoadBalancer
newLoadBalancer(array $servers, $owner)
Definition: LBFactorySimple.php:119
Wikimedia\Rdbms\LoadBalancer
Database connection, tracking, load balancing, and transaction manager for a cluster.
Definition: LoadBalancer.php:42
Wikimedia\Rdbms\LBFactorySimple\getExternalLB
getExternalLB( $cluster)
Get a cached (tracked) load balancer for external storage.
Definition: LBFactorySimple.php:98
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:40
Wikimedia\Rdbms\LBFactorySimple\$mainServers
array[] $mainServers
Map of (server index => server config map)
Definition: LBFactorySimple.php:38
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:106
Wikimedia\Rdbms\LBFactorySimple\newExternalLB
newExternalLB( $cluster, $owner=null)
Create a new load balancer for external storage.
Definition: LBFactorySimple.php:90