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