| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms; |
| 21 |
|
| 22 |
use InvalidArgumentException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class LBFactorySimple extends LBFactory { |
| 37 |
|
| 38 |
private $mainLB; |
| 39 |
|
| 40 |
private $externalLBs = []; |
| 41 |
|
| 42 |
|
| 43 |
private $loadMonitorConfig; |
| 44 |
|
| 45 |
|
| 46 |
private $mainServers; |
| 47 |
|
| 48 |
private $externalServersByCluster = []; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( array $conf ) { |
| 64 |
parent::__construct( $conf ); |
| 65 |
|
| 66 |
$this->mainServers = $conf['servers'] ?? []; |
| 67 |
foreach ( ( $conf['externalClusters'] ?? [] ) as $cluster => $servers ) { |
| 68 |
foreach ( $servers as $index => $server ) { |
| 69 |
$this->externalServersByCluster[$cluster][$index] = $server; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if ( isset( $conf['loadMonitor'] ) ) { |
| 74 |
$this->loadMonitorConfig = $conf['loadMonitor']; |
| 75 |
} elseif ( isset( $conf['loadMonitorClass'] ) ) { |
| 76 |
$this->loadMonitorConfig = [ 'class' => $conf['loadMonitorClass'] ]; |
| 77 |
} else { |
| 78 |
$this->loadMonitorConfig = [ 'class' => LoadMonitor::class ]; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public function newMainLB( $domain = false ): ILoadBalancerForOwner { |
| 83 |
return $this->newLoadBalancer( |
| 84 |
self::CLUSTER_MAIN_DEFAULT, |
| 85 |
$this->mainServers |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
public function getMainLB( $domain = false ): ILoadBalancer { |
| 90 |
$this->mainLB ??= $this->newMainLB( $domain ); |
| 91 |
|
| 92 |
return $this->mainLB; |
| 93 |
} |
| 94 |
|
| 95 |
public function newExternalLB( $cluster ): ILoadBalancerForOwner { |
| 96 |
if ( !isset( $this->externalServersByCluster[$cluster] ) ) { |
| 97 |
throw new InvalidArgumentException( "Unknown cluster '$cluster'." ); |
| 98 |
} |
| 99 |
|
| 100 |
return $this->newLoadBalancer( |
| 101 |
$cluster, |
| 102 |
$this->externalServersByCluster[$cluster] |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
public function getExternalLB( $cluster ): ILoadBalancer { |
| 107 |
if ( !isset( $this->externalLBs[$cluster] ) ) { |
| 108 |
$this->externalLBs[$cluster] = $this->newExternalLB( $cluster ); |
| 109 |
} |
| 110 |
|
| 111 |
return $this->externalLBs[$cluster]; |
| 112 |
} |
| 113 |
|
| 114 |
public function getAllMainLBs(): array { |
| 115 |
return [ self::CLUSTER_MAIN_DEFAULT => $this->getMainLB() ]; |
| 116 |
} |
| 117 |
|
| 118 |
public function getAllExternalLBs(): array { |
| 119 |
$lbs = []; |
| 120 |
foreach ( $this->externalServersByCluster as $cluster => $_ ) { |
| 121 |
$lbs[$cluster] = $this->getExternalLB( $cluster ); |
| 122 |
} |
| 123 |
|
| 124 |
return $lbs; |
| 125 |
} |
| 126 |
|
| 127 |
private function newLoadBalancer( string $clusterName, array $servers ) { |
| 128 |
$lb = new LoadBalancer( array_merge( |
| 129 |
$this->baseLoadBalancerParams(), |
| 130 |
[ |
| 131 |
'servers' => $servers, |
| 132 |
'loadMonitor' => $this->loadMonitorConfig, |
| 133 |
'clusterName' => $clusterName |
| 134 |
] |
| 135 |
) ); |
| 136 |
$this->initLoadBalancer( $lb ); |
| 137 |
|
| 138 |
return $lb; |
| 139 |
} |
| 140 |
|
| 141 |
protected function getLBsForOwner() { |
| 142 |
if ( $this->mainLB !== null ) { |
| 143 |
yield $this->mainLB; |
| 144 |
} |
| 145 |
foreach ( $this->externalLBs as $lb ) { |
| 146 |
yield $lb; |
| 147 |
} |
| 148 |
} |
| 149 |
} |