MediaWiki REL1_35
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
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}
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 $loadMonitorConfig
Configuration for the LoadMonitor to use within LoadBalancer instances.
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:41
baseLoadBalancerParams( $owner)
Get parameters to ILoadBalancer::__construct()
initLoadBalancer(ILoadBalancer $lb)
Database connection, tracking, load balancing, and transaction manager for a cluster.