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