MediaWiki REL1_30
LBFactorySimple.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
33 private $mainLB;
35 private $extLBs = [];
36
38 private $servers = [];
40 private $externalClusters = [];
41
44
57 public function __construct( array $conf ) {
58 parent::__construct( $conf );
59
60 $this->servers = isset( $conf['servers'] ) ? $conf['servers'] : [];
61 foreach ( $this->servers as $i => $server ) {
62 if ( $i == 0 ) {
63 $this->servers[$i]['master'] = true;
64 } else {
65 $this->servers[$i]['replica'] = true;
66 }
67 }
68
69 $this->externalClusters = isset( $conf['externalClusters'] )
70 ? $conf['externalClusters']
71 : [];
72 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
73 ? $conf['loadMonitorClass']
74 : 'LoadMonitor';
75 }
76
81 public function newMainLB( $domain = false ) {
82 return $this->newLoadBalancer( $this->servers );
83 }
84
89 public function getMainLB( $domain = false ) {
90 if ( !isset( $this->mainLB ) ) {
91 $this->mainLB = $this->newMainLB( $domain );
92 }
93
94 return $this->mainLB;
95 }
96
97 public function newExternalLB( $cluster ) {
98 if ( !isset( $this->externalClusters[$cluster] ) ) {
99 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
100 }
101
102 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
103 }
104
105 public function getExternalLB( $cluster ) {
106 if ( !isset( $this->extLBs[$cluster] ) ) {
107 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
108 }
109
110 return $this->extLBs[$cluster];
111 }
112
113 public function getAllMainLBs() {
114 return [ 'DEFAULT' => $this->getMainLB() ];
115 }
116
117 public function getAllExternalLBs() {
118 $lbs = [];
119 foreach ( $this->externalClusters as $cluster => $unused ) {
120 $lbs[$cluster] = $this->getExternalLB( $cluster );
121 }
122
123 return $lbs;
124 }
125
126 private function newLoadBalancer( array $servers ) {
127 $lb = new LoadBalancer( array_merge(
128 $this->baseLoadBalancerParams(),
129 [
130 'servers' => $servers,
131 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
132 ]
133 ) );
134 $this->initLoadBalancer( $lb );
135
136 return $lb;
137 }
138
147 public function forEachLB( $callback, array $params = [] ) {
148 if ( isset( $this->mainLB ) ) {
149 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
150 }
151 foreach ( $this->extLBs as $lb ) {
152 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
153 }
154 }
155}
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.
getAllExternalLBs()
Get cached (tracked) load balancers for all external database clusters.
array[] $servers
Map of (server index => server config)
array[] $externalClusters
Map of (cluster => (server index => server config))
forEachLB( $callback, array $params=[])
Execute a function for each tracked load balancer The callback is called with the load balancer as th...
An interface for generating database load balancers.
Definition LBFactory.php:38
initLoadBalancer(ILoadBalancer $lb)
baseLoadBalancerParams()
Base parameters to LoadBalancer::__construct()
Database connection, tracking, load balancing, and transaction manager for a cluster.
storage can be distributed across multiple servers
Definition memcached.txt:33
$params