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(
79  self::CLUSTER_MAIN_DEFAULT,
80  $this->mainServers,
81  $owner
82  );
83  }
84 
85  public function getMainLB( $domain = false ) {
86  if ( $this->mainLB === null ) {
87  $this->mainLB = $this->newMainLB( $domain, $this->getOwnershipId() );
88  }
89 
90  return $this->mainLB;
91  }
92 
93  public function newExternalLB( $cluster, $owner = null ) {
94  if ( !isset( $this->externalServersByCluster[$cluster] ) ) {
95  throw new InvalidArgumentException( "Unknown cluster '$cluster'." );
96  }
97 
98  return $this->newLoadBalancer(
99  $cluster,
100  $this->externalServersByCluster[$cluster],
101  $owner
102  );
103  }
104 
105  public function getExternalLB( $cluster ) {
106  if ( !isset( $this->externalLBs[$cluster] ) ) {
107  $this->externalLBs[$cluster] = $this->newExternalLB(
108  $cluster,
109  $this->getOwnershipId()
110  );
111  }
112 
113  return $this->externalLBs[$cluster];
114  }
115 
116  public function getAllMainLBs() {
117  return [ self::CLUSTER_MAIN_DEFAULT => $this->getMainLB() ];
118  }
119 
120  public function getAllExternalLBs() {
121  $lbs = [];
122  foreach ( array_keys( $this->externalServersByCluster ) as $cluster ) {
123  $lbs[$cluster] = $this->getExternalLB( $cluster );
124  }
125 
126  return $lbs;
127  }
128 
129  private function newLoadBalancer( string $clusterName, array $servers, $owner ) {
130  $lb = new LoadBalancer( array_merge(
131  $this->baseLoadBalancerParams( $owner ),
132  [
133  'servers' => $servers,
134  'loadMonitor' => $this->loadMonitorConfig,
135  'clusterName' => $clusterName
136  ]
137  ) );
138  $this->initLoadBalancer( $lb );
139 
140  return $lb;
141  }
142 
143  public function forEachLB( $callback, array $params = [] ) {
144  if ( $this->mainLB !== null ) {
145  $callback( $this->mainLB, ...$params );
146  }
147  foreach ( $this->externalLBs as $lb ) {
148  $callback( $lb, ...$params );
149  }
150  }
151 }
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:120
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:796
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\LBFactorySimple\getMainLB
getMainLB( $domain=false)
Get a cached (tracked) load balancer object.
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\$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:143
Wikimedia\Rdbms\LBFactory\baseLoadBalancerParams
baseLoadBalancerParams( $owner)
Get parameters to ILoadBalancer::__construct()
Definition: LBFactory.php:624
Wikimedia\Rdbms\LBFactorySimple\newLoadBalancer
newLoadBalancer(string $clusterName, array $servers, $owner)
Definition: LBFactorySimple.php:129
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:105
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:116
Wikimedia\Rdbms\LBFactorySimple\newExternalLB
newExternalLB( $cluster, $owner=null)
Create a new load balancer for external storage.
Definition: LBFactorySimple.php:93