Merge "jquery.makeCollapsible: Clean up issues caused by wrong nesting"
[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 load ratios.
32 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
33 *
34 * serverTemplate A server info associative array as documented for $wgDBservers. The host,
35 * hostName and load entries will be overridden.
36 *
37 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
38 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
39 *
40 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
41 *
42 * hostsByName A map of hostname to IP address.
43 *
44 * externalLoads A map of external storage cluster name to server load map
45 *
46 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
47 *
48 * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
49 * server-by-server basis. Applies to both core and external storage.
50 *
51 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
52 *
53 * masterTemplateOverrides An override array for all master servers.
54 *
55 * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
56 *
57 * @ingroup Database
58 */
59 class LBFactory_Multi extends LBFactory {
60 // Required settings
61 var $sectionsByDB, $sectionLoads, $serverTemplate;
62 // Optional settings
63 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
64 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
65 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
66 // Other stuff
67 var $conf, $mainLBs = array(), $extLBs = array();
68 var $lastWiki, $lastSection;
69
70 /**
71 * @param $conf array
72 * @throws MWException
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 string $cluster
157 * @param bool $wiki
158 * @throws MWException
159 * @return LoadBalancer
160 */
161 function newExternalLB( $cluster, $wiki = false ) {
162 if ( !isset( $this->externalLoads[$cluster] ) ) {
163 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
164 }
165 $template = $this->serverTemplate;
166 if ( isset( $this->externalTemplateOverrides ) ) {
167 $template = $this->externalTemplateOverrides + $template;
168 }
169 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
170 $template = $this->templateOverridesByCluster[$cluster] + $template;
171 }
172 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
173 }
174
175 /**
176 * @param $cluster
177 * @param $wiki
178 * @return LoadBalancer
179 */
180 function &getExternalLB( $cluster, $wiki = false ) {
181 if ( !isset( $this->extLBs[$cluster] ) ) {
182 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
183 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
184 }
185 return $this->extLBs[$cluster];
186 }
187
188 /**
189 * Make a new load balancer object based on template and load array
190 *
191 * @param $template
192 * @param $loads array
193 * @param $groupLoads
194 * @return LoadBalancer
195 */
196 function newLoadBalancer( $template, $loads, $groupLoads ) {
197 global $wgMasterWaitTimeout;
198 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
199 $lb = new LoadBalancer( array(
200 'servers' => $servers,
201 'masterWaitTimeout' => $wgMasterWaitTimeout
202 ));
203 return $lb;
204 }
205
206 /**
207 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
208 *
209 * @param $template
210 * @param $loads array
211 * @param $groupLoads
212 * @return array
213 */
214 function makeServerArray( $template, $loads, $groupLoads ) {
215 $servers = array();
216 $master = true;
217 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
218 foreach ( $groupLoadsByServer as $server => $stuff ) {
219 if ( !isset( $loads[$server] ) ) {
220 $loads[$server] = 0;
221 }
222 }
223 foreach ( $loads as $serverName => $load ) {
224 $serverInfo = $template;
225 if ( $master ) {
226 $serverInfo['master'] = true;
227 if ( isset( $this->masterTemplateOverrides ) ) {
228 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
229 }
230 $master = false;
231 }
232 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
233 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
234 }
235 if ( isset( $groupLoadsByServer[$serverName] ) ) {
236 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
237 }
238 if ( isset( $this->hostsByName[$serverName] ) ) {
239 $serverInfo['host'] = $this->hostsByName[$serverName];
240 } else {
241 $serverInfo['host'] = $serverName;
242 }
243 $serverInfo['hostName'] = $serverName;
244 $serverInfo['load'] = $load;
245 $servers[] = $serverInfo;
246 }
247 return $servers;
248 }
249
250 /**
251 * Take a group load array indexed by group then server, and reindex it by server then group
252 * @param $groupLoads
253 * @return array
254 */
255 function reindexGroupLoads( $groupLoads ) {
256 $reindexed = array();
257 foreach ( $groupLoads as $group => $loads ) {
258 foreach ( $loads as $server => $load ) {
259 $reindexed[$server][$group] = $load;
260 }
261 }
262 return $reindexed;
263 }
264
265 /**
266 * Get the database name and prefix based on the wiki ID
267 * @param $wiki bool
268 * @return array
269 */
270 function getDBNameAndPrefix( $wiki = false ) {
271 if ( $wiki === false ) {
272 global $wgDBname, $wgDBprefix;
273 return array( $wgDBname, $wgDBprefix );
274 } else {
275 return wfSplitWikiID( $wiki );
276 }
277 }
278
279 /**
280 * Execute a function for each tracked load balancer
281 * The callback is called with the load balancer as the first parameter,
282 * and $params passed as the subsequent parameters.
283 * @param $callback
284 * @param $params array
285 */
286 function forEachLB( $callback, $params = array() ) {
287 foreach ( $this->mainLBs as $lb ) {
288 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
289 }
290 foreach ( $this->extLBs as $lb ) {
291 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
292 }
293 }
294
295 function shutdown() {
296 foreach ( $this->mainLBs as $lb ) {
297 $this->chronProt->shutdownLB( $lb );
298 }
299 $this->chronProt->shutdown();
300 $this->commitMasterChanges();
301 }
302 }