Merge "Update formatting on database-related classes"
[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
121 return $section;
122 }
123
124 /**
125 * @param $wiki bool|string
126 * @return LoadBalancer
127 */
128 function newMainLB( $wiki = false ) {
129 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
130 $section = $this->getSectionForWiki( $wiki );
131 $groupLoads = array();
132 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
133 $groupLoads = $this->groupLoadsByDB[$dbName];
134 }
135 if ( isset( $this->groupLoadsBySection[$section] ) ) {
136 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
137 }
138
139 return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
140 }
141
142 /**
143 * @param $wiki bool|string
144 * @return LoadBalancer
145 */
146 function getMainLB( $wiki = false ) {
147 $section = $this->getSectionForWiki( $wiki );
148 if ( !isset( $this->mainLBs[$section] ) ) {
149 $lb = $this->newMainLB( $wiki, $section );
150 $lb->parentInfo( array( 'id' => "main-$section" ) );
151 $this->chronProt->initLB( $lb );
152 $this->mainLBs[$section] = $lb;
153 }
154
155 return $this->mainLBs[$section];
156 }
157
158 /**
159 * @param string $cluster
160 * @param bool $wiki
161 * @throws MWException
162 * @return LoadBalancer
163 */
164 function newExternalLB( $cluster, $wiki = false ) {
165 if ( !isset( $this->externalLoads[$cluster] ) ) {
166 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
167 }
168 $template = $this->serverTemplate;
169 if ( isset( $this->externalTemplateOverrides ) ) {
170 $template = $this->externalTemplateOverrides + $template;
171 }
172 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
173 $template = $this->templateOverridesByCluster[$cluster] + $template;
174 }
175
176 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
177 }
178
179 /**
180 * @param $cluster
181 * @param $wiki
182 * @return LoadBalancer
183 */
184 function &getExternalLB( $cluster, $wiki = false ) {
185 if ( !isset( $this->extLBs[$cluster] ) ) {
186 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
187 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
188 $this->chronProt->initLB( $this->extLBs[$cluster] );
189 }
190
191 return $this->extLBs[$cluster];
192 }
193
194 /**
195 * Make a new load balancer object based on template and load array
196 *
197 * @param $template
198 * @param $loads array
199 * @param $groupLoads
200 * @return LoadBalancer
201 */
202 function newLoadBalancer( $template, $loads, $groupLoads ) {
203 global $wgMasterWaitTimeout;
204 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
205 $lb = new LoadBalancer( array(
206 'servers' => $servers,
207 'masterWaitTimeout' => $wgMasterWaitTimeout
208 ) );
209
210 return $lb;
211 }
212
213 /**
214 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
215 *
216 * @param $template
217 * @param $loads array
218 * @param $groupLoads
219 * @return array
220 */
221 function makeServerArray( $template, $loads, $groupLoads ) {
222 $servers = array();
223 $master = true;
224 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
225 foreach ( $groupLoadsByServer as $server => $stuff ) {
226 if ( !isset( $loads[$server] ) ) {
227 $loads[$server] = 0;
228 }
229 }
230 foreach ( $loads as $serverName => $load ) {
231 $serverInfo = $template;
232 if ( $master ) {
233 $serverInfo['master'] = true;
234 if ( isset( $this->masterTemplateOverrides ) ) {
235 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
236 }
237 $master = false;
238 }
239 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
240 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
241 }
242 if ( isset( $groupLoadsByServer[$serverName] ) ) {
243 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
244 }
245 if ( isset( $this->hostsByName[$serverName] ) ) {
246 $serverInfo['host'] = $this->hostsByName[$serverName];
247 } else {
248 $serverInfo['host'] = $serverName;
249 }
250 $serverInfo['hostName'] = $serverName;
251 $serverInfo['load'] = $load;
252 $servers[] = $serverInfo;
253 }
254
255 return $servers;
256 }
257
258 /**
259 * Take a group load array indexed by group then server, and reindex it by server then group
260 * @param $groupLoads
261 * @return array
262 */
263 function reindexGroupLoads( $groupLoads ) {
264 $reindexed = array();
265 foreach ( $groupLoads as $group => $loads ) {
266 foreach ( $loads as $server => $load ) {
267 $reindexed[$server][$group] = $load;
268 }
269 }
270
271 return $reindexed;
272 }
273
274 /**
275 * Get the database name and prefix based on the wiki ID
276 * @param $wiki bool
277 * @return array
278 */
279 function getDBNameAndPrefix( $wiki = false ) {
280 if ( $wiki === false ) {
281 global $wgDBname, $wgDBprefix;
282
283 return array( $wgDBname, $wgDBprefix );
284 } else {
285 return wfSplitWikiID( $wiki );
286 }
287 }
288
289 /**
290 * Execute a function for each tracked load balancer
291 * The callback is called with the load balancer as the first parameter,
292 * and $params passed as the subsequent parameters.
293 * @param $callback
294 * @param $params array
295 */
296 function forEachLB( $callback, $params = array() ) {
297 foreach ( $this->mainLBs as $lb ) {
298 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
299 }
300 foreach ( $this->extLBs as $lb ) {
301 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
302 }
303 }
304
305 function shutdown() {
306 foreach ( $this->mainLBs as $lb ) {
307 $this->chronProt->shutdownLB( $lb );
308 }
309 foreach ( $this->extLBs as $extLB ) {
310 $this->chronProt->shutdownLB( $extLB );
311 }
312 $this->chronProt->shutdown();
313 $this->commitMasterChanges();
314 }
315 }