Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 LoadBalancer[] */
93 private $mainLBs = [];
94
95 /** @var LoadBalancer[] */
96 private $extLBs = [];
97
98 /** @var string */
99 private $loadMonitorClass = 'LoadMonitor';
100
101 /** @var string */
102 private $lastDomain;
103
104 /** @var string */
105 private $lastSection;
106
107 /**
108 * @see LBFactory::__construct()
109 *
110 * Template override precedence (highest => lowest):
111 * - templateOverridesByServer
112 * - masterTemplateOverrides
113 * - templateOverridesBySection/templateOverridesByCluster
114 * - externalTemplateOverrides
115 * - serverTemplate
116 * Overrides only work on top level keys (so nested values will not be merged).
117 *
118 * Server configuration maps should be of the format Database::factory() requires.
119 * Additionally, a 'max lag' key should also be set on server maps, indicating how stale the
120 * data can be before the load balancer tries to avoid using it. The map can have 'is static'
121 * set to disable blocking replication sync checks (intended for archive servers with
122 * unchanging data).
123 *
124 * @param array $conf Parameters of LBFactory::__construct() as well as:
125 * - sectionsByDB Map of database names to section names.
126 * - sectionLoads 2-d map. For each section, gives a map of server names to
127 * load ratios. For example:
128 * [
129 * 'section1' => [
130 * 'db1' => 100,
131 * 'db2' => 100
132 * ]
133 * ]
134 * - serverTemplate Server configuration map intended for Database::factory().
135 * Note that "host", "hostName" and "load" entries will be
136 * overridden by "sectionLoads" and "hostsByName".
137 * - groupLoadsBySection 3-d map giving server load ratios for each section/group.
138 * For example:
139 * [
140 * 'section1' => [
141 * 'group1' => [
142 * 'db1' => 100,
143 * 'db2' => 100
144 * ]
145 * ]
146 * ]
147 * - groupLoadsByDB 3-d map giving server load ratios by DB name.
148 * - hostsByName Map of hostname to IP address.
149 * - externalLoads Map of external storage cluster name to server load map.
150 * - externalTemplateOverrides Set of server configuration maps overriding
151 * "serverTemplate" for external storage.
152 * - templateOverridesByServer 2-d map overriding "serverTemplate" and
153 * "externalTemplateOverrides" on a server-by-server basis.
154 * Applies to both core and external storage.
155 * - templateOverridesBySection 2-d map overriding the server configuration maps by section.
156 * - templateOverridesByCluster 2-d map overriding the server configuration maps by external
157 * storage cluster.
158 * - masterTemplateOverrides Server configuration map overrides for all master servers.
159 * - loadMonitorClass Name of the LoadMonitor class to always use.
160 * - readOnlyBySection A map of section name to read-only message.
161 * Missing or false for read/write.
162 */
163 public function __construct( array $conf ) {
164 parent::__construct( $conf );
165
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 $section = $this->sectionsByDB[$dbName] ?? 'DEFAULT';
196 $this->lastSection = $section;
197 $this->lastDomain = $domain;
198
199 return $section;
200 }
201
202 /**
203 * @param bool|string $domain
204 * @return LoadBalancer
205 */
206 public function newMainLB( $domain = false ) {
207 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
208 $section = $this->getSectionForDomain( $domain );
209 $groupLoads = $this->groupLoadsByDB[$dbName] ?? [];
210
211 if ( isset( $this->groupLoadsBySection[$section] ) ) {
212 $groupLoads = array_merge_recursive(
213 $groupLoads, $this->groupLoadsBySection[$section] );
214 }
215
216 $readOnlyReason = $this->readOnlyReason;
217 // Use the LB-specific read-only reason if everything isn't already read-only
218 if ( $readOnlyReason === false && isset( $this->readOnlyBySection[$section] ) ) {
219 $readOnlyReason = $this->readOnlyBySection[$section];
220 }
221
222 $template = $this->serverTemplate;
223 if ( isset( $this->templateOverridesBySection[$section] ) ) {
224 $template = $this->templateOverridesBySection[$section] + $template;
225 }
226
227 return $this->newLoadBalancer(
228 $template,
229 $this->sectionLoads[$section],
230 $groupLoads,
231 $readOnlyReason
232 );
233 }
234
235 /**
236 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
237 * @return LoadBalancer
238 */
239 public function getMainLB( $domain = false ) {
240 $section = $this->getSectionForDomain( $domain );
241 if ( !isset( $this->mainLBs[$section] ) ) {
242 $this->mainLBs[$section] = $this->newMainLB( $domain );
243 }
244
245 return $this->mainLBs[$section];
246 }
247
248 public function newExternalLB( $cluster ) {
249 if ( !isset( $this->externalLoads[$cluster] ) ) {
250 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
251 }
252 $template = $this->serverTemplate;
253 if ( $this->externalTemplateOverrides ) {
254 $template = $this->externalTemplateOverrides + $template;
255 }
256 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
257 $template = $this->templateOverridesByCluster[$cluster] + $template;
258 }
259
260 return $this->newLoadBalancer(
261 $template,
262 $this->externalLoads[$cluster],
263 [],
264 $this->readOnlyReason
265 );
266 }
267
268 public function getExternalLB( $cluster ) {
269 if ( !isset( $this->extLBs[$cluster] ) ) {
270 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
271 }
272
273 return $this->extLBs[$cluster];
274 }
275
276 public function getAllMainLBs() {
277 $lbs = [];
278 foreach ( $this->sectionsByDB as $db => $section ) {
279 if ( !isset( $lbs[$section] ) ) {
280 $lbs[$section] = $this->getMainLB( $db );
281 }
282 }
283
284 return $lbs;
285 }
286
287 public function getAllExternalLBs() {
288 $lbs = [];
289 foreach ( $this->externalLoads as $cluster => $unused ) {
290 $lbs[$cluster] = $this->getExternalLB( $cluster );
291 }
292
293 return $lbs;
294 }
295
296 /**
297 * Make a new load balancer object based on template and load array
298 *
299 * @param array $template
300 * @param array $loads
301 * @param array $groupLoads
302 * @param string|bool $readOnlyReason
303 * @return LoadBalancer
304 */
305 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
306 $lb = new LoadBalancer( array_merge(
307 $this->baseLoadBalancerParams(),
308 [
309 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
310 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
311 'readOnlyReason' => $readOnlyReason
312 ]
313 ) );
314 $this->initLoadBalancer( $lb );
315
316 return $lb;
317 }
318
319 /**
320 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
321 *
322 * @param array $template
323 * @param array $loads
324 * @param array $groupLoads
325 * @return array
326 */
327 private function makeServerArray( $template, $loads, $groupLoads ) {
328 $servers = [];
329 $master = true;
330 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
331 foreach ( $groupLoadsByServer as $server => $stuff ) {
332 if ( !isset( $loads[$server] ) ) {
333 $loads[$server] = 0;
334 }
335 }
336 foreach ( $loads as $serverName => $load ) {
337 $serverInfo = $template;
338 if ( $master ) {
339 $serverInfo['master'] = true;
340 if ( $this->masterTemplateOverrides ) {
341 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
342 }
343 $master = false;
344 } else {
345 $serverInfo['replica'] = true;
346 }
347 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
348 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
349 }
350 if ( isset( $groupLoadsByServer[$serverName] ) ) {
351 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
352 }
353 $serverInfo['host'] = $this->hostsByName[$serverName] ?? $serverName;
354 $serverInfo['hostName'] = $serverName;
355 $serverInfo['load'] = $load;
356 $serverInfo += [ 'flags' => IDatabase::DBO_DEFAULT ];
357
358 $servers[] = $serverInfo;
359 }
360
361 return $servers;
362 }
363
364 /**
365 * Take a group load array indexed by group then server, and reindex it by server then group
366 * @param array $groupLoads
367 * @return array
368 */
369 private function reindexGroupLoads( $groupLoads ) {
370 $reindexed = [];
371 foreach ( $groupLoads as $group => $loads ) {
372 foreach ( $loads as $server => $load ) {
373 $reindexed[$server][$group] = $load;
374 }
375 }
376
377 return $reindexed;
378 }
379
380 /**
381 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
382 * @return array [database name, table prefix]
383 */
384 private function getDBNameAndPrefix( $domain = false ) {
385 $domain = ( $domain === false )
386 ? $this->localDomain
387 : DatabaseDomain::newFromId( $domain );
388
389 return [ $domain->getDatabase(), $domain->getTablePrefix() ];
390 }
391
392 /**
393 * Execute a function for each tracked load balancer
394 * The callback is called with the load balancer as the first parameter,
395 * and $params passed as the subsequent parameters.
396 * @param callable $callback
397 * @param array $params
398 */
399 public function forEachLB( $callback, array $params = [] ) {
400 foreach ( $this->mainLBs as $lb ) {
401 $callback( $lb, ...$params );
402 }
403 foreach ( $this->extLBs as $lb ) {
404 $callback( $lb, ...$params );
405 }
406 }
407 }