Move LBFactorySimple to /libs/rdbms
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactorySimple.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * A simple single-master LBFactory that gets its configuration from the b/c globals
26 */
27 class LBFactorySimple extends LBFactory {
28 /** @var LoadBalancer */
29 private $mainLB;
30 /** @var LoadBalancer[] */
31 private $extLBs = [];
32
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 = [];
37
38 /** @var string */
39 private $loadMonitorClass;
40
41 public function __construct( array $conf ) {
42 parent::__construct( $conf );
43
44 $this->servers = isset( $conf['servers'] ) ? $conf['servers'] : [];
45 foreach ( $this->servers as $i => $server ) {
46 if ( $i == 0 ) {
47 $this->servers[$i]['master'] = true;
48 } else {
49 $this->servers[$i]['replica'] = true;
50 }
51 }
52
53 $this->externalClusters = isset( $conf['externalClusters'] )
54 ? $conf['externalClusters']
55 : [];
56 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
57 ? $conf['loadMonitorClass']
58 : null;
59 }
60
61 /**
62 * @param bool|string $domain
63 * @return LoadBalancer
64 */
65 public function newMainLB( $domain = false ) {
66 return $this->newLoadBalancer( $this->servers );
67 }
68
69 /**
70 * @param bool|string $domain
71 * @return LoadBalancer
72 */
73 public function getMainLB( $domain = false ) {
74 if ( !isset( $this->mainLB ) ) {
75 $this->mainLB = $this->newMainLB( $domain );
76 $this->getChronologyProtector()->initLB( $this->mainLB );
77 }
78
79 return $this->mainLB;
80 }
81
82 /**
83 * @param string $cluster
84 * @param bool|string $domain
85 * @return LoadBalancer
86 * @throws InvalidArgumentException
87 */
88 protected function newExternalLB( $cluster, $domain = false ) {
89 if ( !isset( $this->externalClusters[$cluster] ) ) {
90 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
91 }
92
93 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
94 }
95
96 /**
97 * @param string $cluster
98 * @param bool|string $domain
99 * @return array
100 */
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] );
105 }
106
107 return $this->extLBs[$cluster];
108 }
109
110 private function newLoadBalancer( array $servers ) {
111 $lb = new LoadBalancer( array_merge(
112 $this->baseLoadBalancerParams(),
113 [
114 'servers' => $servers,
115 'loadMonitor' => $this->loadMonitorClass,
116 ]
117 ) );
118 $this->initLoadBalancer( $lb );
119
120 return $lb;
121 }
122
123 /**
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.
127 *
128 * @param callable $callback
129 * @param array $params
130 */
131 public function forEachLB( $callback, array $params = [] ) {
132 if ( isset( $this->mainLB ) ) {
133 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
134 }
135 foreach ( $this->extLBs as $lb ) {
136 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
137 }
138 }
139 }