3 * Generator of database load balancing objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * A simple single-master LBFactory that gets its configuration from the b/c globals
27 class LBFactorySimple
extends LBFactory
{
28 /** @var LoadBalancer */
30 /** @var LoadBalancer[] */
33 /** @var array[] Map of (server index => server config) */
34 private $servers = [];
35 /** @var array[] Map of (cluster => (server index => server config)) */
36 private $externalClusters = [];
39 private $loadMonitorClass;
41 public function __construct( array $conf ) {
42 parent
::__construct( $conf );
44 $this->servers
= isset( $conf['servers'] ) ?
$conf['servers'] : [];
45 foreach ( $this->servers
as $i => $server ) {
47 $this->servers
[$i]['master'] = true;
49 $this->servers
[$i]['replica'] = true;
53 $this->externalClusters
= isset( $conf['externalClusters'] )
54 ?
$conf['externalClusters']
56 $this->loadMonitorClass
= isset( $conf['loadMonitorClass'] )
57 ?
$conf['loadMonitorClass']
62 * @param bool|string $domain
63 * @return LoadBalancer
65 public function newMainLB( $domain = false ) {
66 return $this->newLoadBalancer( $this->servers
);
70 * @param bool|string $domain
71 * @return LoadBalancer
73 public function getMainLB( $domain = false ) {
74 if ( !isset( $this->mainLB
) ) {
75 $this->mainLB
= $this->newMainLB( $domain );
76 $this->getChronologyProtector()->initLB( $this->mainLB
);
83 * @param string $cluster
84 * @param bool|string $domain
85 * @return LoadBalancer
86 * @throws InvalidArgumentException
88 protected function newExternalLB( $cluster, $domain = false ) {
89 if ( !isset( $this->externalClusters
[$cluster] ) ) {
90 throw new InvalidArgumentException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
93 return $this->newLoadBalancer( $this->externalClusters
[$cluster] );
97 * @param string $cluster
98 * @param bool|string $domain
101 public function getExternalLB( $cluster, $domain = false ) {
102 if ( !isset( $this->extLBs
[$cluster] ) ) {
103 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $domain );
104 $this->getChronologyProtector()->initLB( $this->extLBs
[$cluster] );
107 return $this->extLBs
[$cluster];
110 private function newLoadBalancer( array $servers ) {
111 $lb = new LoadBalancer( array_merge(
112 $this->baseLoadBalancerParams(),
114 'servers' => $servers,
115 'loadMonitor' => $this->loadMonitorClass
,
118 $this->initLoadBalancer( $lb );
124 * Execute a function for each tracked load balancer
125 * The callback is called with the load balancer as the first parameter,
126 * and $params passed as the subsequent parameters.
128 * @param callable $callback
129 * @param array $params
131 public function forEachLB( $callback, array $params = [] ) {
132 if ( isset( $this->mainLB
) ) {
133 call_user_func_array( $callback, array_merge( [ $this->mainLB
], $params ) );
135 foreach ( $this->extLBs
as $lb ) {
136 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );