3 * Advanced generator of database load balancing objects for database farms.
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.
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.
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
25 * A multi-database, multi-master factory for Wikimedia and similar installations.
26 * Ignores the old configuration globals.
30 class LBFactoryMulti
extends LBFactory
{
31 /** @var array A map of database names to section names */
32 private $sectionsByDB;
35 * @var array A 2-d map. For each section, gives a map of server names to
38 private $sectionLoads;
41 * @var array[] Server info associative array
42 * @note The host, hostName and load entries will be overridden
44 private $serverTemplate;
48 /** @var array A 3-d map giving server load ratios for each section and group */
49 private $groupLoadsBySection = [];
51 /** @var array A 3-d map giving server load ratios by DB name */
52 private $groupLoadsByDB = [];
54 /** @var array A map of hostname to IP address */
55 private $hostsByName = [];
57 /** @var array A map of external storage cluster name to server load map */
58 private $externalLoads = [];
61 * @var array A set of server info keys overriding serverTemplate for
64 private $externalTemplateOverrides;
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
71 private $templateOverridesByServer;
73 /** @var array A 2-d map overriding the server info by section */
74 private $templateOverridesBySection;
76 /** @var array A 2-d map overriding the server info by external storage cluster */
77 private $templateOverridesByCluster;
79 /** @var array An override array for all master servers */
80 private $masterTemplateOverrides;
83 * @var array|bool A map of section name to read-only message. Missing or
84 * false for read/write
86 private $readOnlyBySection = [];
88 /** @var array Load balancer factory configuration */
91 /** @var LoadBalancer[] */
92 private $mainLBs = [];
94 /** @var LoadBalancer[] */
98 private $loadMonitorClass = 'LoadMonitor';
104 private $lastSection;
107 * @see LBFactory::__construct()
109 * Template override precedence (highest => lowest):
110 * - templateOverridesByServer
111 * - masterTemplateOverrides
112 * - templateOverridesBySection/templateOverridesByCluster
113 * - externalTemplateOverrides
115 * Overrides only work on top level keys (so nested values will not be merged).
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
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:
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.
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
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.
162 public function __construct( array $conf ) {
163 parent
::__construct( $conf );
166 $required = [ 'sectionsByDB', 'sectionLoads', 'serverTemplate' ];
167 $optional = [ 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
168 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
169 'templateOverridesByCluster', 'templateOverridesBySection', 'masterTemplateOverrides',
170 'readOnlyBySection', 'loadMonitorClass' ];
172 foreach ( $required as $key ) {
173 if ( !isset( $conf[$key] ) ) {
174 throw new InvalidArgumentException( __CLASS__
. ": $key is required." );
176 $this->$key = $conf[$key];
179 foreach ( $optional as $key ) {
180 if ( isset( $conf[$key] ) ) {
181 $this->$key = $conf[$key];
187 * @param bool|string $domain
190 private function getSectionForDomain( $domain = false ) {
191 if ( $this->lastDomain
=== $domain ) {
192 return $this->lastSection
;
194 list( $dbName, ) = $this->getDBNameAndPrefix( $domain );
195 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
196 $section = $this->sectionsByDB
[$dbName];
198 $section = 'DEFAULT';
200 $this->lastSection
= $section;
201 $this->lastDomain
= $domain;
207 * @param bool|string $domain
208 * @return LoadBalancer
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];
219 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
220 $groupLoads = array_merge_recursive(
221 $groupLoads, $this->groupLoadsBySection
[$section] );
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];
230 $template = $this->serverTemplate
;
231 if ( isset( $this->templateOverridesBySection
[$section] ) ) {
232 $template = $this->templateOverridesBySection
[$section] +
$template;
235 return $this->newLoadBalancer(
237 $this->sectionLoads
[$section],
244 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
245 * @return LoadBalancer
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;
255 return $this->mainLBs
[$section];
259 * @param string $cluster
260 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
261 * @throws InvalidArgumentException
262 * @return LoadBalancer
264 public function newExternalLB( $cluster, $domain = false ) {
265 if ( !isset( $this->externalLoads
[$cluster] ) ) {
266 throw new InvalidArgumentException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
268 $template = $this->serverTemplate
;
269 if ( isset( $this->externalTemplateOverrides
) ) {
270 $template = $this->externalTemplateOverrides +
$template;
272 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
273 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
276 return $this->newLoadBalancer(
278 $this->externalLoads
[$cluster],
280 $this->readOnlyReason
285 * @param string $cluster External storage cluster, or false for core
286 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
287 * @return LoadBalancer
289 public function getExternalLB( $cluster, $domain = false ) {
290 if ( !isset( $this->extLBs
[$cluster] ) ) {
291 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $domain );
292 $this->getChronologyProtector()->initLB( $this->extLBs
[$cluster] );
295 return $this->extLBs
[$cluster];
299 * Make a new load balancer object based on template and load array
301 * @param array $template
302 * @param array $loads
303 * @param array $groupLoads
304 * @param string|bool $readOnlyReason
305 * @return LoadBalancer
307 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
308 $lb = new LoadBalancer( array_merge(
309 $this->baseLoadBalancerParams(),
311 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
312 'loadMonitor' => [ 'class' => $this->loadMonitorClass
],
313 'readOnlyReason' => $readOnlyReason
316 $this->initLoadBalancer( $lb );
322 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
324 * @param array $template
325 * @param array $loads
326 * @param array $groupLoads
329 private function makeServerArray( $template, $loads, $groupLoads ) {
332 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
333 foreach ( $groupLoadsByServer as $server => $stuff ) {
334 if ( !isset( $loads[$server] ) ) {
338 foreach ( $loads as $serverName => $load ) {
339 $serverInfo = $template;
341 $serverInfo['master'] = true;
342 if ( isset( $this->masterTemplateOverrides
) ) {
343 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
347 $serverInfo['replica'] = true;
349 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
350 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
352 if ( isset( $groupLoadsByServer[$serverName] ) ) {
353 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
355 if ( isset( $this->hostsByName
[$serverName] ) ) {
356 $serverInfo['host'] = $this->hostsByName
[$serverName];
358 $serverInfo['host'] = $serverName;
360 $serverInfo['hostName'] = $serverName;
361 $serverInfo['load'] = $load;
362 $serverInfo +
= [ 'flags' => IDatabase
::DBO_DEFAULT
];
364 $servers[] = $serverInfo;
371 * Take a group load array indexed by group then server, and reindex it by server then group
372 * @param array $groupLoads
375 private function reindexGroupLoads( $groupLoads ) {
377 foreach ( $groupLoads as $group => $loads ) {
378 foreach ( $loads as $server => $load ) {
379 $reindexed[$server][$group] = $load;
387 * @param DatabaseDomain|string|bool $domain Domain ID, or false for the current domain
388 * @return array [database name, table prefix]
390 private function getDBNameAndPrefix( $domain = false ) {
391 $domain = ( $domain === false )
393 : DatabaseDomain
::newFromId( $domain );
395 return [ $domain->getDatabase(), $domain->getTablePrefix() ];
399 * Execute a function for each tracked load balancer
400 * The callback is called with the load balancer as the first parameter,
401 * and $params passed as the subsequent parameters.
402 * @param callable $callback
403 * @param array $params
405 public function forEachLB( $callback, array $params = [] ) {
406 foreach ( $this->mainLBs
as $lb ) {
407 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
409 foreach ( $this->extLBs
as $lb ) {
410 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );