f0c374fbee9d9e7808f921eb3d48f23a9428c23c
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactoryMulti.php
1 <?php
2 /**
3 * Advanced generator of database load balancing objects for wiki farms.
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 multi-wiki, multi-master factory for Wikimedia and similar installations.
26 * Ignores the old configuration globals
27 *
28 * Configuration:
29 * sectionsByDB A map of database names to section names.
30 *
31 * sectionLoads A 2-d map. For each section, gives a map of server names to
32 * load ratios. For example:
33 * array(
34 * 'section1' => array(
35 * 'db1' => 100,
36 * 'db2' => 100
37 * )
38 * )
39 *
40 * serverTemplate A server info associative array as documented for $wgDBservers.
41 * The host, hostName and load entries will be overridden.
42 *
43 * groupLoadsBySection A 3-d map giving server load ratios for each section and group.
44 * For example:
45 * array(
46 * 'section1' => array(
47 * 'group1' => array(
48 * 'db1' => 100,
49 * 'db2' => 100
50 * )
51 * )
52 * )
53 *
54 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
55 *
56 * hostsByName A map of hostname to IP address.
57 *
58 * externalLoads A map of external storage cluster name to server load map.
59 *
60 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external
61 * storage.
62 *
63 * templateOverridesByServer A 2-d map overriding serverTemplate and
64 * externalTemplateOverrides on a server-by-server basis. Applies
65 * to both core and external storage.
66 *
67 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster.
68 *
69 * masterTemplateOverrides An override array for all master servers.
70 *
71 * loadMonitorClass Name of the LoadMonitor class to always use.
72 *
73 * readOnlyBySection A map of section name to read-only message.
74 * Missing or false for read/write.
75 *
76 * @ingroup Database
77 */
78 class LBFactoryMulti extends LBFactory {
79 // Required settings
80
81 /** @var array A map of database names to section names */
82 private $sectionsByDB;
83
84 /**
85 * @var array A 2-d map. For each section, gives a map of server names to
86 * load ratios
87 */
88 private $sectionLoads;
89
90 /**
91 * @var array A server info associative array as documented for
92 * $wgDBservers. The host, hostName and load entries will be
93 * overridden
94 */
95 private $serverTemplate;
96
97 // Optional settings
98
99 /** @var array A 3-d map giving server load ratios for each section and group */
100 private $groupLoadsBySection = array();
101
102 /** @var array A 3-d map giving server load ratios by DB name */
103 private $groupLoadsByDB = array();
104
105 /** @var array A map of hostname to IP address */
106 private $hostsByName = array();
107
108 /** @var array A map of external storage cluster name to server load map */
109 private $externalLoads = array();
110
111 /**
112 * @var array A set of server info keys overriding serverTemplate for
113 * external storage
114 */
115 private $externalTemplateOverrides;
116
117 /**
118 * @var array A 2-d map overriding serverTemplate and
119 * externalTemplateOverrides on a server-by-server basis. Applies to both
120 * core and external storage
121 */
122 private $templateOverridesByServer;
123
124 /** @var array A 2-d map overriding the server info by external storage cluster */
125 private $templateOverridesByCluster;
126
127 /** @var array An override array for all master servers */
128 private $masterTemplateOverrides;
129
130 /**
131 * @var array|bool A map of section name to read-only message. Missing or
132 * false for read/write
133 */
134 private $readOnlyBySection = array();
135
136 // Other stuff
137
138 /** @var array Load balancer factory configuration */
139 private $conf;
140
141 /** @var LoadBalancer[] */
142 private $mainLBs = array();
143
144 /** @var LoadBalancer[] */
145 private $extLBs = array();
146
147 /** @var string */
148 private $loadMonitorClass;
149
150 /** @var string */
151 private $lastWiki;
152
153 /** @var string */
154 private $lastSection;
155
156 /**
157 * @param array $conf
158 * @throws MWException
159 */
160 public function __construct( array $conf ) {
161 $this->chronProt = new ChronologyProtector;
162 $this->conf = $conf;
163 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
164 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
165 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
166 'templateOverridesByCluster', 'masterTemplateOverrides',
167 'readOnlyBySection', 'loadMonitorClass' );
168
169 foreach ( $required as $key ) {
170 if ( !isset( $conf[$key] ) ) {
171 throw new MWException( __CLASS__ . ": $key is required in configuration" );
172 }
173 $this->$key = $conf[$key];
174 }
175
176 foreach ( $optional as $key ) {
177 if ( isset( $conf[$key] ) ) {
178 $this->$key = $conf[$key];
179 }
180 }
181
182 // Check for read-only mode
183 $section = $this->getSectionForWiki();
184 if ( !empty( $this->readOnlyBySection[$section] ) ) {
185 global $wgReadOnly;
186 $wgReadOnly = $this->readOnlyBySection[$section];
187 }
188 }
189
190 /**
191 * @param bool|string $wiki
192 * @return string
193 */
194 private function getSectionForWiki( $wiki = false ) {
195 if ( $this->lastWiki === $wiki ) {
196 return $this->lastSection;
197 }
198 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
199 if ( isset( $this->sectionsByDB[$dbName] ) ) {
200 $section = $this->sectionsByDB[$dbName];
201 } else {
202 $section = 'DEFAULT';
203 }
204 $this->lastSection = $section;
205 $this->lastWiki = $wiki;
206
207 return $section;
208 }
209
210 /**
211 * @param bool|string $wiki
212 * @return LoadBalancer
213 */
214 public function newMainLB( $wiki = false ) {
215 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
216 $section = $this->getSectionForWiki( $wiki );
217 $groupLoads = array();
218 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
219 $groupLoads = $this->groupLoadsByDB[$dbName];
220 }
221
222 if ( isset( $this->groupLoadsBySection[$section] ) ) {
223 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
224 }
225
226 return $this->newLoadBalancer(
227 $this->serverTemplate,
228 $this->sectionLoads[$section],
229 $groupLoads
230 );
231 }
232
233 /**
234 * @param bool|string $wiki
235 * @return LoadBalancer
236 */
237 public function getMainLB( $wiki = false ) {
238 $section = $this->getSectionForWiki( $wiki );
239 if ( !isset( $this->mainLBs[$section] ) ) {
240 $lb = $this->newMainLB( $wiki );
241 $lb->parentInfo( array( 'id' => "main-$section" ) );
242 $this->chronProt->initLB( $lb );
243 $this->mainLBs[$section] = $lb;
244 }
245
246 return $this->mainLBs[$section];
247 }
248
249 /**
250 * @param string $cluster
251 * @param bool|string $wiki
252 * @throws MWException
253 * @return LoadBalancer
254 */
255 protected function newExternalLB( $cluster, $wiki = false ) {
256 if ( !isset( $this->externalLoads[$cluster] ) ) {
257 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
258 }
259 $template = $this->serverTemplate;
260 if ( isset( $this->externalTemplateOverrides ) ) {
261 $template = $this->externalTemplateOverrides + $template;
262 }
263 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
264 $template = $this->templateOverridesByCluster[$cluster] + $template;
265 }
266
267 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
268 }
269
270 /**
271 * @param string $cluster External storage cluster, or false for core
272 * @param bool|string $wiki Wiki ID, or false for the current wiki
273 * @return LoadBalancer
274 */
275 public function &getExternalLB( $cluster, $wiki = false ) {
276 if ( !isset( $this->extLBs[$cluster] ) ) {
277 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
278 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
279 $this->chronProt->initLB( $this->extLBs[$cluster] );
280 }
281
282 return $this->extLBs[$cluster];
283 }
284
285 /**
286 * Make a new load balancer object based on template and load array
287 *
288 * @param array $template
289 * @param array $loads
290 * @param array $groupLoads
291 * @return LoadBalancer
292 */
293 private function newLoadBalancer( $template, $loads, $groupLoads ) {
294 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
295 $lb = new LoadBalancer( array(
296 'servers' => $servers,
297 'loadMonitor' => $this->loadMonitorClass
298 ) );
299
300 return $lb;
301 }
302
303 /**
304 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
305 *
306 * @param array $template
307 * @param array $loads
308 * @param array $groupLoads
309 * @return array
310 */
311 private function makeServerArray( $template, $loads, $groupLoads ) {
312 $servers = array();
313 $master = true;
314 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
315 foreach ( $groupLoadsByServer as $server => $stuff ) {
316 if ( !isset( $loads[$server] ) ) {
317 $loads[$server] = 0;
318 }
319 }
320 foreach ( $loads as $serverName => $load ) {
321 $serverInfo = $template;
322 if ( $master ) {
323 $serverInfo['master'] = true;
324 if ( isset( $this->masterTemplateOverrides ) ) {
325 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
326 }
327 $master = false;
328 } else {
329 $serverInfo['slave'] = true;
330 }
331 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
332 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
333 }
334 if ( isset( $groupLoadsByServer[$serverName] ) ) {
335 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
336 }
337 if ( isset( $this->hostsByName[$serverName] ) ) {
338 $serverInfo['host'] = $this->hostsByName[$serverName];
339 } else {
340 $serverInfo['host'] = $serverName;
341 }
342 $serverInfo['hostName'] = $serverName;
343 $serverInfo['load'] = $load;
344 $servers[] = $serverInfo;
345 }
346
347 return $servers;
348 }
349
350 /**
351 * Take a group load array indexed by group then server, and reindex it by server then group
352 * @param array $groupLoads
353 * @return array
354 */
355 private function reindexGroupLoads( $groupLoads ) {
356 $reindexed = array();
357 foreach ( $groupLoads as $group => $loads ) {
358 foreach ( $loads as $server => $load ) {
359 $reindexed[$server][$group] = $load;
360 }
361 }
362
363 return $reindexed;
364 }
365
366 /**
367 * Get the database name and prefix based on the wiki ID
368 * @param bool|string $wiki
369 * @return array
370 */
371 private function getDBNameAndPrefix( $wiki = false ) {
372 if ( $wiki === false ) {
373 global $wgDBname, $wgDBprefix;
374
375 return array( $wgDBname, $wgDBprefix );
376 } else {
377 return wfSplitWikiID( $wiki );
378 }
379 }
380
381 /**
382 * Execute a function for each tracked load balancer
383 * The callback is called with the load balancer as the first parameter,
384 * and $params passed as the subsequent parameters.
385 * @param callable $callback
386 * @param array $params
387 */
388 public function forEachLB( $callback, array $params = array() ) {
389 foreach ( $this->mainLBs as $lb ) {
390 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
391 }
392 foreach ( $this->extLBs as $lb ) {
393 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
394 }
395 }
396
397 public function shutdown() {
398 foreach ( $this->mainLBs as $lb ) {
399 $this->chronProt->shutdownLB( $lb );
400 }
401 foreach ( $this->extLBs as $extLB ) {
402 $this->chronProt->shutdownLB( $extLB );
403 }
404 $this->chronProt->shutdown();
405 $this->commitMasterChanges();
406 }
407 }