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