Merge "Add GENDER support to protection null revision edit summary"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactoryMulti.php
1 <?php
2 /**
3 * Advanced generator of database load balancing objects for database 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-database, multi-master factory for Wikimedia and similar installations.
26 * Ignores the old configuration globals.
27 *
28 * @ingroup Database
29 */
30 class LBFactoryMulti extends LBFactory {
31 /** @var array A map of database names to section names */
32 private $sectionsByDB;
33
34 /**
35 * @var array A 2-d map. For each section, gives a map of server names to
36 * load ratios
37 */
38 private $sectionLoads;
39
40 /**
41 * @var array[] Server info associative array
42 * @note The host, hostName and load entries will be overridden
43 */
44 private $serverTemplate;
45
46 // Optional settings
47
48 /** @var array A 3-d map giving server load ratios for each section and group */
49 private $groupLoadsBySection = [];
50
51 /** @var array A 3-d map giving server load ratios by DB name */
52 private $groupLoadsByDB = [];
53
54 /** @var array A map of hostname to IP address */
55 private $hostsByName = [];
56
57 /** @var array A map of external storage cluster name to server load map */
58 private $externalLoads = [];
59
60 /**
61 * @var array A set of server info keys overriding serverTemplate for
62 * external storage
63 */
64 private $externalTemplateOverrides;
65
66 /**
67 * @var array A 2-d map overriding serverTemplate and
68 * externalTemplateOverrides on a server-by-server basis. Applies to both
69 * core and external storage
70 */
71 private $templateOverridesByServer;
72
73 /** @var array A 2-d map overriding the server info by section */
74 private $templateOverridesBySection;
75
76 /** @var array A 2-d map overriding the server info by external storage cluster */
77 private $templateOverridesByCluster;
78
79 /** @var array An override array for all master servers */
80 private $masterTemplateOverrides;
81
82 /**
83 * @var array|bool A map of section name to read-only message. Missing or
84 * false for read/write
85 */
86 private $readOnlyBySection = [];
87
88 /** @var array Load balancer factory configuration */
89 private $conf;
90
91 /** @var LoadBalancer[] */
92 private $mainLBs = [];
93
94 /** @var LoadBalancer[] */
95 private $extLBs = [];
96
97 /** @var string */
98 private $loadMonitorClass = 'LoadMonitor';
99
100 /** @var string */
101 private $lastDomain;
102
103 /** @var string */
104 private $lastSection;
105
106 /**
107 * @see LBFactory::__construct()
108 *
109 * Template override precedence (highest => lowest):
110 * - templateOverridesByServer
111 * - masterTemplateOverrides
112 * - templateOverridesBySection/templateOverridesByCluster
113 * - externalTemplateOverrides
114 * - serverTemplate
115 * Overrides only work on top level keys (so nested values will not be merged).
116 *
117 * Server configuration maps should be of the format Database::factory() requires.
118 * Additionally, a 'max lag' key should also be set on server maps, indicating how stale the
119 * data can be before the load balancer tries to avoid using it. The map can have 'is static'
120 * set to disable blocking replication sync checks (intended for archive servers with
121 * unchanging data).
122 *
123 * @param array $conf Parameters of LBFactory::__construct() as well as:
124 * - sectionsByDB Map of database names to section names.
125 * - sectionLoads 2-d map. For each section, gives a map of server names to
126 * load ratios. For example:
127 * [
128 * 'section1' => [
129 * 'db1' => 100,
130 * 'db2' => 100
131 * ]
132 * ]
133 * - serverTemplate Server configuration map intended for Database::factory().
134 * Note that "host", "hostName" and "load" entries will be
135 * overridden by "sectionLoads" and "hostsByName".
136 * - groupLoadsBySection 3-d map giving server load ratios for each section/group.
137 * For example:
138 * [
139 * 'section1' => [
140 * 'group1' => [
141 * 'db1' => 100,
142 * 'db2' => 100
143 * ]
144 * ]
145 * ]
146 * - groupLoadsByDB 3-d map giving server load ratios by DB name.
147 * - hostsByName Map of hostname to IP address.
148 * - externalLoads Map of external storage cluster name to server load map.
149 * - externalTemplateOverrides Set of server configuration maps overriding
150 * "serverTemplate" for external storage.
151 * - templateOverridesByServer 2-d map overriding "serverTemplate" and
152 * "externalTemplateOverrides" on a server-by-server basis.
153 * Applies to both core and external storage.
154 * - templateOverridesBySection 2-d map overriding the server configuration maps by section.
155 * - templateOverridesByCluster 2-d map overriding the server configuration maps by external
156 * storage cluster.
157 * - masterTemplateOverrides Server configuration map overrides for all master servers.
158 * - loadMonitorClass Name of the LoadMonitor class to always use.
159 * - readOnlyBySection A map of section name to read-only message.
160 * Missing or false for read/write.
161 */
162 public function __construct( array $conf ) {
163 parent::__construct( $conf );
164
165 $this->conf = $conf;
166 $required = [ 'sectionsByDB', 'sectionLoads', 'serverTemplate' ];
167 $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
168 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
169 'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
170 'readOnlyBySection', 'loadMonitorClass' ];
171
172 foreach ( $required as $key ) {
173 if ( !isset( $conf[$key] ) ) {
174 throw new InvalidArgumentException( __CLASS__ . ": $key is required." );
175 }
176 $this->$key = $conf[$key];
177 }
178
179 foreach ( $optional as $key ) {
180 if ( isset( $conf[$key] ) ) {
181 $this->$key = $conf[$key];
182 }
183 }
184 }
185
186 /**
187 * @param bool|string $domain
188 * @return string
189 */
190 private function getSectionForDomain( $domain = false ) {
191 if ( $this->lastDomain === $domain ) {
192 return $this->lastSection;
193 }
194 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
195 if ( isset( $this->sectionsByDB[$dbName] ) ) {
196 $section = $this->sectionsByDB[$dbName];
197 } else {
198 $section = 'DEFAULT';
199 }
200 $this->lastSection = $section;
201 $this->lastDomain = $domain;
202
203 return $section;
204 }
205
206 /**
207 * @param bool|string $domain
208 * @return LoadBalancer
209 */
210 public function newMainLB( $domain = false ) {
211 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
212 $section = $this->getSectionForDomain( $domain );
213 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
214 $groupLoads = $this->groupLoadsByDB[$dbName];
215 } else {
216 $groupLoads = [];
217 }
218
219 if ( isset( $this->groupLoadsBySection[$section] ) ) {
220 $groupLoads = array_merge_recursive(
221 $groupLoads, $this->groupLoadsBySection[$section] );
222 }
223
224 $readOnlyReason = $this->readOnlyReason;
225 // Use the LB-specific read-only reason if everything isn't already read-only
226 if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
227 $readOnlyReason = $this->readOnlyBySection[$section];
228 }
229
230 $template = $this->serverTemplate;
231 if ( isset( $this->templateOverridesBySection[$section] ) ) {
232 $template = $this->templateOverridesBySection[$section] + $template;
233 }
234
235 return $this->newLoadBalancer(
236 $template,
237 $this->sectionLoads[$section],
238 $groupLoads,
239 $readOnlyReason
240 );
241 }
242
243 /**
244 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
245 * @return LoadBalancer
246 */
247 public function getMainLB( $domain = false ) {
248 $section = $this->getSectionForDomain( $domain );
249 if ( !isset( $this->mainLBs[$section] ) ) {
250 $lb = $this->newMainLB( $domain );
251 $this->getChronologyProtector()->initLB( $lb );
252 $this->mainLBs[$section] = $lb;
253 }
254
255 return $this->mainLBs[$section];
256 }
257
258 public function newExternalLB( $cluster ) {
259 if ( !isset( $this->externalLoads[$cluster] ) ) {
260 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
261 }
262 $template = $this->serverTemplate;
263 if ( isset( $this->externalTemplateOverrides ) ) {
264 $template = $this->externalTemplateOverrides + $template;
265 }
266 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
267 $template = $this->templateOverridesByCluster[$cluster] + $template;
268 }
269
270 return $this->newLoadBalancer(
271 $template,
272 $this->externalLoads[$cluster],
273 [],
274 $this->readOnlyReason
275 );
276 }
277
278 public function getExternalLB( $cluster ) {
279 if ( !isset( $this->extLBs[$cluster] ) ) {
280 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
281 $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
282 }
283
284 return $this->extLBs[$cluster];
285 }
286
287 /**
288 * Make a new load balancer object based on template and load array
289 *
290 * @param array $template
291 * @param array $loads
292 * @param array $groupLoads
293 * @param string|bool $readOnlyReason
294 * @return LoadBalancer
295 */
296 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
297 $lb = new LoadBalancer( array_merge(
298 $this->baseLoadBalancerParams(),
299 [
300 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
301 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
302 'readOnlyReason' => $readOnlyReason
303 ]
304 ) );
305 $this->initLoadBalancer( $lb );
306
307 return $lb;
308 }
309
310 /**
311 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
312 *
313 * @param array $template
314 * @param array $loads
315 * @param array $groupLoads
316 * @return array
317 */
318 private function makeServerArray( $template, $loads, $groupLoads ) {
319 $servers = [];
320 $master = true;
321 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
322 foreach ( $groupLoadsByServer as $server => $stuff ) {
323 if ( !isset( $loads[$server] ) ) {
324 $loads[$server] = 0;
325 }
326 }
327 foreach ( $loads as $serverName => $load ) {
328 $serverInfo = $template;
329 if ( $master ) {
330 $serverInfo['master'] = true;
331 if ( isset( $this->masterTemplateOverrides ) ) {
332 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
333 }
334 $master = false;
335 } else {
336 $serverInfo['replica'] = true;
337 }
338 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
339 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
340 }
341 if ( isset( $groupLoadsByServer[$serverName] ) ) {
342 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
343 }
344 if ( isset( $this->hostsByName[$serverName] ) ) {
345 $serverInfo['host'] = $this->hostsByName[$serverName];
346 } else {
347 $serverInfo['host'] = $serverName;
348 }
349 $serverInfo['hostName'] = $serverName;
350 $serverInfo['load'] = $load;
351 $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
352
353 $servers[] = $serverInfo;
354 }
355
356 return $servers;
357 }
358
359 /**
360 * Take a group load array indexed by group then server, and reindex it by server then group
361 * @param array $groupLoads
362 * @return array
363 */
364 private function reindexGroupLoads( $groupLoads ) {
365 $reindexed = [];
366 foreach ( $groupLoads as $group => $loads ) {
367 foreach ( $loads as $server => $load ) {
368 $reindexed[$server][$group] = $load;
369 }
370 }
371
372 return $reindexed;
373 }
374
375 /**
376 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
377 * @return array [database name, table prefix]
378 */
379 private function getDBNameAndPrefix( $domain = false ) {
380 $domain = ( $domain === false )
381 ? $this->localDomain
382 : DatabaseDomain::newFromId( $domain );
383
384 return [ $domain->getDatabase(), $domain->getTablePrefix() ];
385 }
386
387 /**
388 * Execute a function for each tracked load balancer
389 * The callback is called with the load balancer as the first parameter,
390 * and $params passed as the subsequent parameters.
391 * @param callable $callback
392 * @param array $params
393 */
394 public function forEachLB( $callback, array $params = [] ) {
395 foreach ( $this->mainLBs as $lb ) {
396 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
397 }
398 foreach ( $this->extLBs as $lb ) {
399 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
400 }
401 }
402 }