Fixed dependencies for jquery.collapsibleTabs
[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 /**
26 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
27 * Ignores the old configuration globals
28 *
29 * Configuration:
30 * sectionsByDB A map of database names to section names
31 *
32 * sectionLoads A 2-d map. For each section, gives a map of server names to load ratios.
33 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
34 *
35 * serverTemplate A server info associative array as documented for $wgDBservers. The host,
36 * hostName and load entries will be overridden.
37 *
38 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
39 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
40 *
41 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
42 *
43 * hostsByName A map of hostname to IP address.
44 *
45 * externalLoads A map of external storage cluster name to server load map
46 *
47 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
48 *
49 * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
50 * server-by-server basis. Applies to both core and external storage.
51 *
52 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
53 *
54 * masterTemplateOverrides An override array for all master servers.
55 *
56 * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
57 *
58 * @ingroup Database
59 */
60 class LBFactory_Multi extends LBFactory {
61 // Required settings
62 var $sectionsByDB, $sectionLoads, $serverTemplate;
63 // Optional settings
64 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
65 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
66 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
67 // Other stuff
68 var $conf, $mainLBs = array(), $extLBs = array();
69 var $lastWiki, $lastSection;
70
71 /**
72 * @param $conf array
73 */
74 function __construct( $conf ) {
75 $this->chronProt = new ChronologyProtector;
76 $this->conf = $conf;
77 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
78 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
79 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
80 'templateOverridesByCluster', 'masterTemplateOverrides',
81 'readOnlyBySection' );
82
83 foreach ( $required as $key ) {
84 if ( !isset( $conf[$key] ) ) {
85 throw new MWException( __CLASS__.": $key is required in configuration" );
86 }
87 $this->$key = $conf[$key];
88 }
89
90 foreach ( $optional as $key ) {
91 if ( isset( $conf[$key] ) ) {
92 $this->$key = $conf[$key];
93 }
94 }
95
96 // Check for read-only mode
97 $section = $this->getSectionForWiki();
98 if ( !empty( $this->readOnlyBySection[$section] ) ) {
99 global $wgReadOnly;
100 $wgReadOnly = $this->readOnlyBySection[$section];
101 }
102 }
103
104 /**
105 * @param $wiki bool|string
106 * @return string
107 */
108 function getSectionForWiki( $wiki = false ) {
109 if ( $this->lastWiki === $wiki ) {
110 return $this->lastSection;
111 }
112 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
113 if ( isset( $this->sectionsByDB[$dbName] ) ) {
114 $section = $this->sectionsByDB[$dbName];
115 } else {
116 $section = 'DEFAULT';
117 }
118 $this->lastSection = $section;
119 $this->lastWiki = $wiki;
120 return $section;
121 }
122
123 /**
124 * @param $wiki bool|string
125 * @return LoadBalancer
126 */
127 function newMainLB( $wiki = false ) {
128 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
129 $section = $this->getSectionForWiki( $wiki );
130 $groupLoads = array();
131 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
132 $groupLoads = $this->groupLoadsByDB[$dbName];
133 }
134 if ( isset( $this->groupLoadsBySection[$section] ) ) {
135 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
136 }
137 return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
138 }
139
140 /**
141 * @param $wiki bool|string
142 * @return LoadBalancer
143 */
144 function getMainLB( $wiki = false ) {
145 $section = $this->getSectionForWiki( $wiki );
146 if ( !isset( $this->mainLBs[$section] ) ) {
147 $lb = $this->newMainLB( $wiki, $section );
148 $this->chronProt->initLB( $lb );
149 $lb->parentInfo( array( 'id' => "main-$section" ) );
150 $this->mainLBs[$section] = $lb;
151 }
152 return $this->mainLBs[$section];
153 }
154
155 /**
156 * @param $cluster
157 * @param $wiki
158 * @return LoadBalancer
159 */
160 function newExternalLB( $cluster, $wiki = false ) {
161 if ( !isset( $this->externalLoads[$cluster] ) ) {
162 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
163 }
164 $template = $this->serverTemplate;
165 if ( isset( $this->externalTemplateOverrides ) ) {
166 $template = $this->externalTemplateOverrides + $template;
167 }
168 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
169 $template = $this->templateOverridesByCluster[$cluster] + $template;
170 }
171 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
172 }
173
174 /**
175 * @param $cluster
176 * @param $wiki
177 * @return LoadBalancer
178 */
179 function &getExternalLB( $cluster, $wiki = false ) {
180 if ( !isset( $this->extLBs[$cluster] ) ) {
181 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
182 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
183 }
184 return $this->extLBs[$cluster];
185 }
186
187 /**
188 * Make a new load balancer object based on template and load array
189 *
190 * @param $template
191 * @param $loads array
192 * @param $groupLoads
193 * @return LoadBalancer
194 */
195 function newLoadBalancer( $template, $loads, $groupLoads ) {
196 global $wgMasterWaitTimeout;
197 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
198 $lb = new LoadBalancer( array(
199 'servers' => $servers,
200 'masterWaitTimeout' => $wgMasterWaitTimeout
201 ));
202 return $lb;
203 }
204
205 /**
206 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
207 *
208 * @param $template
209 * @param $loads array
210 * @param $groupLoads
211 * @return array
212 */
213 function makeServerArray( $template, $loads, $groupLoads ) {
214 $servers = array();
215 $master = true;
216 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
217 foreach ( $groupLoadsByServer as $server => $stuff ) {
218 if ( !isset( $loads[$server] ) ) {
219 $loads[$server] = 0;
220 }
221 }
222 foreach ( $loads as $serverName => $load ) {
223 $serverInfo = $template;
224 if ( $master ) {
225 $serverInfo['master'] = true;
226 if ( isset( $this->masterTemplateOverrides ) ) {
227 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
228 }
229 $master = false;
230 }
231 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
232 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
233 }
234 if ( isset( $groupLoadsByServer[$serverName] ) ) {
235 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
236 }
237 if ( isset( $this->hostsByName[$serverName] ) ) {
238 $serverInfo['host'] = $this->hostsByName[$serverName];
239 } else {
240 $serverInfo['host'] = $serverName;
241 }
242 $serverInfo['hostName'] = $serverName;
243 $serverInfo['load'] = $load;
244 $servers[] = $serverInfo;
245 }
246 return $servers;
247 }
248
249 /**
250 * Take a group load array indexed by group then server, and reindex it by server then group
251 * @param $groupLoads
252 * @return array
253 */
254 function reindexGroupLoads( $groupLoads ) {
255 $reindexed = array();
256 foreach ( $groupLoads as $group => $loads ) {
257 foreach ( $loads as $server => $load ) {
258 $reindexed[$server][$group] = $load;
259 }
260 }
261 return $reindexed;
262 }
263
264 /**
265 * Get the database name and prefix based on the wiki ID
266 * @param $wiki bool
267 * @return array
268 */
269 function getDBNameAndPrefix( $wiki = false ) {
270 if ( $wiki === false ) {
271 global $wgDBname, $wgDBprefix;
272 return array( $wgDBname, $wgDBprefix );
273 } else {
274 return wfSplitWikiID( $wiki );
275 }
276 }
277
278 /**
279 * Execute a function for each tracked load balancer
280 * The callback is called with the load balancer as the first parameter,
281 * and $params passed as the subsequent parameters.
282 * @param $callback
283 * @param $params array
284 */
285 function forEachLB( $callback, $params = array() ) {
286 foreach ( $this->mainLBs as $lb ) {
287 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
288 }
289 foreach ( $this->extLBs as $lb ) {
290 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
291 }
292 }
293
294 function shutdown() {
295 foreach ( $this->mainLBs as $lb ) {
296 $this->chronProt->shutdownLB( $lb );
297 }
298 $this->chronProt->shutdown();
299 $this->commitMasterChanges();
300 }
301 }