MediaWiki REL1_34
LBFactorySimple.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
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}
A simple single-master LBFactory that gets its configuration from the b/c globals.
getAllMainLBs()
Get cached (tracked) load balancers for all main database clusters.
getMainLB( $domain=false)
Get a cached (tracked) load balancer object.
array[][] $externalServersByCluster
Map of (cluster => server index => server config map)
newLoadBalancer(array $servers, $owner)
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
getExternalLB( $cluster)
Get a cached (tracked) load balancer for external storage.
array[] $mainServers
Map of (server index => server config map)
newExternalLB( $cluster, $owner=null)
Create a new load balancer for external storage.
newMainLB( $domain=false, $owner=null)
Create a new load balancer object.
forEachLB( $callback, array $params=[])
Execute a function for each currently tracked (instantiated) load balancer.
An interface for generating database load balancers.
Definition LBFactory.php:40
baseLoadBalancerParams( $owner)
Get parameters to ILoadBalancer::__construct()
initLoadBalancer(ILoadBalancer $lb)
Database connection, tracking, load balancing, and transaction manager for a cluster.