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