| 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 |
use LogicException; |
| 24 |
use UnexpectedValueException; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class LBFactoryMulti extends LBFactory { |
| 54 |
|
| 55 |
private $mainLBs = []; |
| 56 |
|
| 57 |
private $externalLBs = []; |
| 58 |
|
| 59 |
|
| 60 |
private $hostsByServerName; |
| 61 |
|
| 62 |
private $sectionsByDB; |
| 63 |
|
| 64 |
private $groupLoadsBySection; |
| 65 |
|
| 66 |
private $externalLoadsByCluster; |
| 67 |
|
| 68 |
private $serverTemplate; |
| 69 |
|
| 70 |
private $externalTemplateOverrides; |
| 71 |
|
| 72 |
private $templateOverridesBySection; |
| 73 |
|
| 74 |
private $templateOverridesByCluster; |
| 75 |
|
| 76 |
private $masterTemplateOverrides; |
| 77 |
|
| 78 |
private $templateOverridesByServer; |
| 79 |
|
| 80 |
private $readOnlyBySection; |
| 81 |
|
| 82 |
private $loadMonitorConfig; |
| 83 |
|
| 84 |
private $nonLocalDomainCache = []; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function __construct( array $conf ) { |
| 136 |
parent::__construct( $conf ); |
| 137 |
|
| 138 |
$this->hostsByServerName = $conf['hostsByName'] ?? []; |
| 139 |
$this->sectionsByDB = $conf['sectionsByDB']; |
| 140 |
$this->sectionsByDB += [ self::CLUSTER_MAIN_DEFAULT => self::CLUSTER_MAIN_DEFAULT ]; |
| 141 |
$this->groupLoadsBySection = $conf['groupLoadsBySection'] ?? []; |
| 142 |
foreach ( ( $conf['sectionLoads'] ?? [] ) as $section => $loadsByServerName ) { |
| 143 |
$this->groupLoadsBySection[$section][ILoadBalancer::GROUP_GENERIC] = $loadsByServerName; |
| 144 |
} |
| 145 |
$this->externalLoadsByCluster = $conf['externalLoads'] ?? []; |
| 146 |
$this->serverTemplate = $conf['serverTemplate'] ?? []; |
| 147 |
$this->externalTemplateOverrides = $conf['externalTemplateOverrides'] ?? []; |
| 148 |
$this->templateOverridesBySection = $conf['templateOverridesBySection'] ?? []; |
| 149 |
$this->templateOverridesByCluster = $conf['templateOverridesByCluster'] ?? []; |
| 150 |
$this->masterTemplateOverrides = $conf['masterTemplateOverrides'] ?? []; |
| 151 |
$this->templateOverridesByServer = $conf['templateOverridesByServer'] ?? []; |
| 152 |
$this->readOnlyBySection = $conf['readOnlyBySection'] ?? []; |
| 153 |
|
| 154 |
if ( isset( $conf['loadMonitor'] ) ) { |
| 155 |
$this->loadMonitorConfig = $conf['loadMonitor']; |
| 156 |
} elseif ( isset( $conf['loadMonitorClass'] ) ) { |
| 157 |
$this->loadMonitorConfig = [ 'class' => $conf['loadMonitorClass'] ]; |
| 158 |
} else { |
| 159 |
$this->loadMonitorConfig = [ 'class' => LoadMonitor::class ]; |
| 160 |
} |
| 161 |
|
| 162 |
foreach ( $this->externalLoadsByCluster as $cluster => $_ ) { |
| 163 |
if ( isset( $this->groupLoadsBySection[$cluster] ) ) { |
| 164 |
throw new LogicException( |
| 165 |
"External cluster '$cluster' has the same name as a main section/cluster" |
| 166 |
); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public function newMainLB( $domain = false ): ILoadBalancerForOwner { |
| 172 |
$domainInstance = $this->resolveDomainInstance( $domain ); |
| 173 |
$database = $domainInstance->getDatabase(); |
| 174 |
$section = $this->getSectionFromDatabase( $database ); |
| 175 |
|
| 176 |
if ( !isset( $this->groupLoadsBySection[$section][ILoadBalancer::GROUP_GENERIC] ) ) { |
| 177 |
throw new UnexpectedValueException( "Section '$section' has no hosts defined." ); |
| 178 |
} |
| 179 |
|
| 180 |
return $this->newLoadBalancer( |
| 181 |
$section, |
| 182 |
array_merge( |
| 183 |
$this->serverTemplate, |
| 184 |
$this->templateOverridesBySection[$section] ?? [] |
| 185 |
), |
| 186 |
$this->groupLoadsBySection[$section], |
| 187 |
|
| 188 |
is_string( $this->readOnlyReason ) |
| 189 |
? $this->readOnlyReason |
| 190 |
: ( $this->readOnlyBySection[$section] ?? false ) |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private function resolveDomainInstance( $domain ) { |
| 199 |
if ( $domain instanceof DatabaseDomain ) { |
| 200 |
return $domain; |
| 201 |
} elseif ( $domain === false || $domain === $this->localDomain->getId() ) { |
| 202 |
return $this->localDomain; |
| 203 |
} elseif ( isset( $this->domainAliases[$domain] ) ) { |
| 204 |
|
| 205 |
|
| 206 |
$this->domainAliases[$domain] = |
| 207 |
DatabaseDomain::newFromId( $this->domainAliases[$domain] ); |
| 208 |
|
| 209 |
return $this->domainAliases[$domain]; |
| 210 |
} |
| 211 |
|
| 212 |
$cachedDomain = $this->nonLocalDomainCache[$domain] ?? null; |
| 213 |
if ( $cachedDomain === null ) { |
| 214 |
$cachedDomain = DatabaseDomain::newFromId( $domain ); |
| 215 |
$this->nonLocalDomainCache = [ $domain => $cachedDomain ]; |
| 216 |
} |
| 217 |
|
| 218 |
return $cachedDomain; |
| 219 |
} |
| 220 |
|
| 221 |
public function getMainLB( $domain = false ): ILoadBalancer { |
| 222 |
$domainInstance = $this->resolveDomainInstance( $domain ); |
| 223 |
$section = $this->getSectionFromDatabase( $domainInstance->getDatabase() ); |
| 224 |
|
| 225 |
if ( !isset( $this->mainLBs[$section] ) ) { |
| 226 |
$this->mainLBs[$section] = $this->newMainLB( $domain ); |
| 227 |
} |
| 228 |
|
| 229 |
return $this->mainLBs[$section]; |
| 230 |
} |
| 231 |
|
| 232 |
public function newExternalLB( $cluster ): ILoadBalancerForOwner { |
| 233 |
if ( !isset( $this->externalLoadsByCluster[$cluster] ) ) { |
| 234 |
throw new InvalidArgumentException( "Unknown cluster '$cluster'" ); |
| 235 |
} |
| 236 |
return $this->newLoadBalancer( |
| 237 |
$cluster, |
| 238 |
array_merge( |
| 239 |
$this->serverTemplate, |
| 240 |
$this->externalTemplateOverrides, |
| 241 |
$this->templateOverridesByCluster[$cluster] ?? [] |
| 242 |
), |
| 243 |
[ ILoadBalancer::GROUP_GENERIC => $this->externalLoadsByCluster[$cluster] ], |
| 244 |
$this->readOnlyReason |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
public function getExternalLB( $cluster ): ILoadBalancer { |
| 249 |
if ( !isset( $this->externalLBs[$cluster] ) ) { |
| 250 |
$this->externalLBs[$cluster] = $this->newExternalLB( |
| 251 |
$cluster |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
return $this->externalLBs[$cluster]; |
| 256 |
} |
| 257 |
|
| 258 |
public function getAllMainLBs(): array { |
| 259 |
$lbs = []; |
| 260 |
foreach ( $this->sectionsByDB as $db => $section ) { |
| 261 |
if ( !isset( $lbs[$section] ) ) { |
| 262 |
$lbs[$section] = $this->getMainLB( $db ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return $lbs; |
| 267 |
} |
| 268 |
|
| 269 |
public function getAllExternalLBs(): array { |
| 270 |
$lbs = []; |
| 271 |
foreach ( $this->externalLoadsByCluster as $cluster => $unused ) { |
| 272 |
$lbs[$cluster] = $this->getExternalLB( $cluster ); |
| 273 |
} |
| 274 |
|
| 275 |
return $lbs; |
| 276 |
} |
| 277 |
|
| 278 |
protected function getLBsForOwner() { |
| 279 |
foreach ( $this->mainLBs as $lb ) { |
| 280 |
yield $lb; |
| 281 |
} |
| 282 |
foreach ( $this->externalLBs as $lb ) { |
| 283 |
yield $lb; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private function newLoadBalancer( |
| 297 |
string $clusterName, |
| 298 |
array $serverTemplate, |
| 299 |
array $groupLoads, |
| 300 |
$readOnlyReason |
| 301 |
) { |
| 302 |
$lb = new LoadBalancer( array_merge( |
| 303 |
$this->baseLoadBalancerParams(), |
| 304 |
[ |
| 305 |
'servers' => $this->makeServerConfigArrays( $serverTemplate, $groupLoads ), |
| 306 |
'loadMonitor' => $this->loadMonitorConfig, |
| 307 |
'readOnlyReason' => $readOnlyReason, |
| 308 |
'clusterName' => $clusterName |
| 309 |
] |
| 310 |
) ); |
| 311 |
$this->initLoadBalancer( $lb ); |
| 312 |
|
| 313 |
return $lb; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private function makeServerConfigArrays( array $serverTemplate, array $groupLoads ) { |
| 324 |
|
| 325 |
if ( !$groupLoads[ILoadBalancer::GROUP_GENERIC] ) { |
| 326 |
throw new UnexpectedValueException( "Empty generic load array; no primary DB defined." ); |
| 327 |
} |
| 328 |
$groupLoadsByServerName = []; |
| 329 |
foreach ( $groupLoads as $group => $loadByServerName ) { |
| 330 |
foreach ( $loadByServerName as $serverName => $load ) { |
| 331 |
$groupLoadsByServerName[$serverName][$group] = $load; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
$genericLoads = $groupLoads[ILoadBalancer::GROUP_GENERIC]; |
| 337 |
|
| 338 |
$genericLoads += array_fill_keys( array_keys( $groupLoadsByServerName ), 0 ); |
| 339 |
$servers = []; |
| 340 |
foreach ( $genericLoads as $serverName => $load ) { |
| 341 |
$servers[] = array_merge( |
| 342 |
$serverTemplate, |
| 343 |
$servers ? [] : $this->masterTemplateOverrides, |
| 344 |
$this->templateOverridesByServer[$serverName] ?? [], |
| 345 |
[ |
| 346 |
'host' => $this->hostsByServerName[$serverName] ?? $serverName, |
| 347 |
'serverName' => $serverName, |
| 348 |
'load' => $load, |
| 349 |
'groupLoads' => $groupLoadsByServerName[$serverName] ?? [] |
| 350 |
] |
| 351 |
); |
| 352 |
} |
| 353 |
|
| 354 |
return $servers; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
private function getSectionFromDatabase( $database ) { |
| 362 |
if ( $database !== null && isset( $this->sectionsByDB[$database] ) ) { |
| 363 |
return $this->sectionsByDB[$database]; |
| 364 |
} |
| 365 |
return $this->sectionsByDB[self::CLUSTER_MAIN_DEFAULT] |
| 366 |
?? self::CLUSTER_MAIN_DEFAULT; |
| 367 |
} |
| 368 |
|
| 369 |
public function reconfigure( array $conf ): void { |
| 370 |
if ( !$conf ) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
foreach ( $this->mainLBs as $lb ) { |
| 375 |
|
| 376 |
$groupLoads = $conf['groupLoadsBySection'][$lb->getClusterName()] ?? []; |
| 377 |
$groupLoads[ILoadBalancer::GROUP_GENERIC] = $conf['sectionLoads'][$lb->getClusterName()]; |
| 378 |
$config = [ |
| 379 |
'servers' => $this->makeServerConfigArrays( $conf['serverTemplate'] ?? [], $groupLoads ) |
| 380 |
]; |
| 381 |
$lb->reconfigure( $config ); |
| 382 |
|
| 383 |
} |
| 384 |
foreach ( $this->externalLBs as $lb ) { |
| 385 |
$groupLoads = [ |
| 386 |
ILoadBalancer::GROUP_GENERIC => $conf['externalLoads'][$lb->getClusterName()] |
| 387 |
]; |
| 388 |
$config = [ |
| 389 |
'servers' => $this->makeServerConfigArrays( $conf['serverTemplate'] ?? [], $groupLoads ) |
| 390 |
]; |
| 391 |
$lb->reconfigure( $config ); |
| 392 |
} |
| 393 |
} |
| 394 |
} |