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