Merge "Dependency inject $wgSharedTables into DatabaseBase"
[lhc/web/wiklou.git] / includes / db / loadbalancer / 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 LBFactoryMW {
28 /** @var LoadBalancer */
29 private $mainLB;
30 /** @var LoadBalancer[] */
31 private $extLBs = [];
32
33 /** @var string */
34 private $loadMonitorClass;
35
36 public function __construct( array $conf ) {
37 parent::__construct( $conf );
38
39 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
40 ? $conf['loadMonitorClass']
41 : null;
42 }
43
44 /**
45 * @param bool|string $wiki
46 * @return LoadBalancer
47 */
48 public function newMainLB( $wiki = false ) {
49 global $wgDBservers, $wgDBprefix, $wgDBmwschema;
50
51 if ( is_array( $wgDBservers ) ) {
52 $servers = $wgDBservers;
53 foreach ( $servers as $i => &$server ) {
54 if ( $i == 0 ) {
55 $server['master'] = true;
56 } else {
57 $server['replica'] = true;
58 }
59 $server += [
60 'schema' => $wgDBmwschema,
61 'tablePrefix' => $wgDBprefix,
62 'flags' => DBO_DEFAULT
63 ];
64 }
65 } else {
66 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
67 global $wgDBssl, $wgDBcompress;
68
69 $flags = DBO_DEFAULT;
70 if ( $wgDebugDumpSql ) {
71 $flags |= DBO_DEBUG;
72 }
73 if ( $wgDBssl ) {
74 $flags |= DBO_SSL;
75 }
76 if ( $wgDBcompress ) {
77 $flags |= DBO_COMPRESS;
78 }
79
80 $servers = [ [
81 'host' => $wgDBserver,
82 'user' => $wgDBuser,
83 'password' => $wgDBpassword,
84 'dbname' => $wgDBname,
85 'schema' => $wgDBmwschema,
86 'tablePrefix' => $wgDBprefix,
87 'type' => $wgDBtype,
88 'load' => 1,
89 'flags' => $flags,
90 'master' => true
91 ] ];
92 }
93
94 return $this->newLoadBalancer( $servers );
95 }
96
97 /**
98 * @param bool|string $wiki
99 * @return LoadBalancer
100 */
101 public function getMainLB( $wiki = false ) {
102 if ( !isset( $this->mainLB ) ) {
103 $this->mainLB = $this->newMainLB( $wiki );
104 $this->chronProt->initLB( $this->mainLB );
105 }
106
107 return $this->mainLB;
108 }
109
110 /**
111 * @param string $cluster
112 * @param bool|string $wiki
113 * @return LoadBalancer
114 * @throws InvalidArgumentException
115 */
116 protected function newExternalLB( $cluster, $wiki = false ) {
117 global $wgExternalServers;
118 if ( !isset( $wgExternalServers[$cluster] ) ) {
119 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
120 }
121
122 return $this->newLoadBalancer( $wgExternalServers[$cluster] );
123 }
124
125 /**
126 * @param string $cluster
127 * @param bool|string $wiki
128 * @return array
129 */
130 public function getExternalLB( $cluster, $wiki = false ) {
131 if ( !isset( $this->extLBs[$cluster] ) ) {
132 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
133 $this->chronProt->initLB( $this->extLBs[$cluster] );
134 }
135
136 return $this->extLBs[$cluster];
137 }
138
139 private function newLoadBalancer( array $servers ) {
140 $lb = new LoadBalancer( array_merge(
141 $this->baseLoadBalancerParams(),
142 [
143 'servers' => $servers,
144 'loadMonitor' => $this->loadMonitorClass,
145 ]
146 ) );
147 $this->initLoadBalancer( $lb );
148
149 return $lb;
150 }
151
152 /**
153 * Execute a function for each tracked load balancer
154 * The callback is called with the load balancer as the first parameter,
155 * and $params passed as the subsequent parameters.
156 *
157 * @param callable $callback
158 * @param array $params
159 */
160 public function forEachLB( $callback, array $params = [] ) {
161 if ( isset( $this->mainLB ) ) {
162 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
163 }
164 foreach ( $this->extLBs as $lb ) {
165 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
166 }
167 }
168 }