(bug 47070) check content model namespace on import.
[lhc/web/wiklou.git] / includes / db / LBFactory_Multi.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 * readOnlyBySection A map of section name to read-only message.
72 * Missing or false for read/write.
73 *
74 * @ingroup Database
75 */
76 class LBFactory_Multi extends LBFactory {
77 // Required settings
78 var $sectionsByDB, $sectionLoads, $serverTemplate;
79 // Optional settings
80 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
81 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
82 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
83 // Other stuff
84 var $conf, $mainLBs = array(), $extLBs = array();
85 var $lastWiki, $lastSection;
86
87 /**
88 * @param $conf array
89 * @throws MWException
90 */
91 function __construct( $conf ) {
92 $this->chronProt = new ChronologyProtector;
93 $this->conf = $conf;
94 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
95 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
96 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
97 'templateOverridesByCluster', 'masterTemplateOverrides',
98 'readOnlyBySection' );
99
100 foreach ( $required as $key ) {
101 if ( !isset( $conf[$key] ) ) {
102 throw new MWException( __CLASS__ . ": $key is required in configuration" );
103 }
104 $this->$key = $conf[$key];
105 }
106
107 foreach ( $optional as $key ) {
108 if ( isset( $conf[$key] ) ) {
109 $this->$key = $conf[$key];
110 }
111 }
112
113 // Check for read-only mode
114 $section = $this->getSectionForWiki();
115 if ( !empty( $this->readOnlyBySection[$section] ) ) {
116 global $wgReadOnly;
117 $wgReadOnly = $this->readOnlyBySection[$section];
118 }
119 }
120
121 /**
122 * @param $wiki bool|string
123 * @return string
124 */
125 function getSectionForWiki( $wiki = false ) {
126 if ( $this->lastWiki === $wiki ) {
127 return $this->lastSection;
128 }
129 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
130 if ( isset( $this->sectionsByDB[$dbName] ) ) {
131 $section = $this->sectionsByDB[$dbName];
132 } else {
133 $section = 'DEFAULT';
134 }
135 $this->lastSection = $section;
136 $this->lastWiki = $wiki;
137
138 return $section;
139 }
140
141 /**
142 * @param $wiki bool|string
143 * @return LoadBalancer
144 */
145 function newMainLB( $wiki = false ) {
146 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
147 $section = $this->getSectionForWiki( $wiki );
148 $groupLoads = array();
149 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
150 $groupLoads = $this->groupLoadsByDB[$dbName];
151 }
152
153 if ( isset( $this->groupLoadsBySection[$section] ) ) {
154 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
155 }
156
157 return $this->newLoadBalancer(
158 $this->serverTemplate,
159 $this->sectionLoads[$section],
160 $groupLoads
161 );
162 }
163
164 /**
165 * @param $wiki bool|string
166 * @return LoadBalancer
167 */
168 function getMainLB( $wiki = false ) {
169 $section = $this->getSectionForWiki( $wiki );
170 if ( !isset( $this->mainLBs[$section] ) ) {
171 $lb = $this->newMainLB( $wiki, $section );
172 $lb->parentInfo( array( 'id' => "main-$section" ) );
173 $this->chronProt->initLB( $lb );
174 $this->mainLBs[$section] = $lb;
175 }
176
177 return $this->mainLBs[$section];
178 }
179
180 /**
181 * @param string $cluster
182 * @param bool $wiki
183 * @throws MWException
184 * @return LoadBalancer
185 */
186 function newExternalLB( $cluster, $wiki = false ) {
187 if ( !isset( $this->externalLoads[$cluster] ) ) {
188 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
189 }
190 $template = $this->serverTemplate;
191 if ( isset( $this->externalTemplateOverrides ) ) {
192 $template = $this->externalTemplateOverrides + $template;
193 }
194 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
195 $template = $this->templateOverridesByCluster[$cluster] + $template;
196 }
197
198 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
199 }
200
201 /**
202 * @param $cluster
203 * @param $wiki
204 * @return LoadBalancer
205 */
206 function &getExternalLB( $cluster, $wiki = false ) {
207 if ( !isset( $this->extLBs[$cluster] ) ) {
208 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
209 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
210 $this->chronProt->initLB( $this->extLBs[$cluster] );
211 }
212
213 return $this->extLBs[$cluster];
214 }
215
216 /**
217 * Make a new load balancer object based on template and load array
218 *
219 * @param $template
220 * @param $loads array
221 * @param $groupLoads
222 * @return LoadBalancer
223 */
224 function newLoadBalancer( $template, $loads, $groupLoads ) {
225 global $wgMasterWaitTimeout;
226 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
227 $lb = new LoadBalancer( array(
228 'servers' => $servers,
229 'masterWaitTimeout' => $wgMasterWaitTimeout
230 ) );
231
232 return $lb;
233 }
234
235 /**
236 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
237 *
238 * @param $template
239 * @param $loads array
240 * @param $groupLoads
241 * @return array
242 */
243 function makeServerArray( $template, $loads, $groupLoads ) {
244 $servers = array();
245 $master = true;
246 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
247 foreach ( $groupLoadsByServer as $server => $stuff ) {
248 if ( !isset( $loads[$server] ) ) {
249 $loads[$server] = 0;
250 }
251 }
252 foreach ( $loads as $serverName => $load ) {
253 $serverInfo = $template;
254 if ( $master ) {
255 $serverInfo['master'] = true;
256 if ( isset( $this->masterTemplateOverrides ) ) {
257 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
258 }
259 $master = false;
260 }
261 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
262 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
263 }
264 if ( isset( $groupLoadsByServer[$serverName] ) ) {
265 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
266 }
267 if ( isset( $this->hostsByName[$serverName] ) ) {
268 $serverInfo['host'] = $this->hostsByName[$serverName];
269 } else {
270 $serverInfo['host'] = $serverName;
271 }
272 $serverInfo['hostName'] = $serverName;
273 $serverInfo['load'] = $load;
274 $servers[] = $serverInfo;
275 }
276
277 return $servers;
278 }
279
280 /**
281 * Take a group load array indexed by group then server, and reindex it by server then group
282 * @param $groupLoads
283 * @return array
284 */
285 function reindexGroupLoads( $groupLoads ) {
286 $reindexed = array();
287 foreach ( $groupLoads as $group => $loads ) {
288 foreach ( $loads as $server => $load ) {
289 $reindexed[$server][$group] = $load;
290 }
291 }
292
293 return $reindexed;
294 }
295
296 /**
297 * Get the database name and prefix based on the wiki ID
298 * @param $wiki bool
299 * @return array
300 */
301 function getDBNameAndPrefix( $wiki = false ) {
302 if ( $wiki === false ) {
303 global $wgDBname, $wgDBprefix;
304
305 return array( $wgDBname, $wgDBprefix );
306 } else {
307 return wfSplitWikiID( $wiki );
308 }
309 }
310
311 /**
312 * Execute a function for each tracked load balancer
313 * The callback is called with the load balancer as the first parameter,
314 * and $params passed as the subsequent parameters.
315 * @param $callback
316 * @param $params array
317 */
318 function forEachLB( $callback, $params = array() ) {
319 foreach ( $this->mainLBs as $lb ) {
320 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
321 }
322 foreach ( $this->extLBs as $lb ) {
323 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
324 }
325 }
326
327 function shutdown() {
328 foreach ( $this->mainLBs as $lb ) {
329 $this->chronProt->shutdownLB( $lb );
330 }
331 foreach ( $this->extLBs as $extLB ) {
332 $this->chronProt->shutdownLB( $extLB );
333 }
334 $this->chronProt->shutdown();
335 $this->commitMasterChanges();
336 }
337 }