3 * Advanced generator of database load balancing objects for wiki 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-wiki, multi-master factory for Wikimedia and similar installations.
26 * Ignores the old configuration globals
29 * sectionsByDB A map of database names to section names.
31 * sectionLoads A 2-d map. For each section, gives a map of server names to
32 * load ratios. For example:
34 * 'section1' => array(
40 * serverTemplate A server info associative array as documented for $wgDBservers.
41 * The host, hostName and load entries will be overridden.
43 * groupLoadsBySection A 3-d map giving server load ratios for each section and group.
46 * 'section1' => array(
54 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
56 * hostsByName A map of hostname to IP address.
58 * externalLoads A map of external storage cluster name to server load map.
60 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external
63 * templateOverridesByServer A 2-d map overriding serverTemplate and
64 * externalTemplateOverrides on a server-by-server basis. Applies
65 * to both core and external storage.
67 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster.
69 * masterTemplateOverrides An override array for all master servers.
71 * loadMonitorClass Name of the LoadMonitor class to always use.
73 * readOnlyBySection A map of section name to read-only message.
74 * Missing or false for read/write.
78 class LBFactoryMulti
extends LBFactory
{
79 /** @var array A map of database names to section names */
80 private $sectionsByDB;
83 * @var array A 2-d map. For each section, gives a map of server names to
86 private $sectionLoads;
89 * @var array A server info associative array as documented for
90 * $wgDBservers. The host, hostName and load entries will be
93 private $serverTemplate;
97 /** @var array A 3-d map giving server load ratios for each section and group */
98 private $groupLoadsBySection = array();
100 /** @var array A 3-d map giving server load ratios by DB name */
101 private $groupLoadsByDB = array();
103 /** @var array A map of hostname to IP address */
104 private $hostsByName = array();
106 /** @var array A map of external storage cluster name to server load map */
107 private $externalLoads = array();
110 * @var array A set of server info keys overriding serverTemplate for
113 private $externalTemplateOverrides;
116 * @var array A 2-d map overriding serverTemplate and
117 * externalTemplateOverrides on a server-by-server basis. Applies to both
118 * core and external storage
120 private $templateOverridesByServer;
122 /** @var array A 2-d map overriding the server info by external storage cluster */
123 private $templateOverridesByCluster;
125 /** @var array An override array for all master servers */
126 private $masterTemplateOverrides;
129 * @var array|bool A map of section name to read-only message. Missing or
130 * false for read/write
132 private $readOnlyBySection = array();
136 /** @var array Load balancer factory configuration */
139 /** @var LoadBalancer[] */
140 private $mainLBs = array();
142 /** @var LoadBalancer[] */
143 private $extLBs = array();
146 private $loadMonitorClass;
152 private $lastSection;
156 * @throws MWException
158 public function __construct( array $conf ) {
159 parent
::__construct( $conf );
162 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
163 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
164 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
165 'templateOverridesByCluster', 'masterTemplateOverrides',
166 'readOnlyBySection', 'loadMonitorClass' );
168 foreach ( $required as $key ) {
169 if ( !isset( $conf[$key] ) ) {
170 throw new MWException( __CLASS__
. ": $key is required in configuration" );
172 $this->$key = $conf[$key];
175 foreach ( $optional as $key ) {
176 if ( isset( $conf[$key] ) ) {
177 $this->$key = $conf[$key];
183 * @param bool|string $wiki
186 private function getSectionForWiki( $wiki = false ) {
187 if ( $this->lastWiki
=== $wiki ) {
188 return $this->lastSection
;
190 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
191 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
192 $section = $this->sectionsByDB
[$dbName];
194 $section = 'DEFAULT';
196 $this->lastSection
= $section;
197 $this->lastWiki
= $wiki;
203 * @param bool|string $wiki
204 * @return LoadBalancer
206 public function newMainLB( $wiki = false ) {
207 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
208 $section = $this->getSectionForWiki( $wiki );
209 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
210 $groupLoads = $this->groupLoadsByDB
[$dbName];
212 $groupLoads = array();
215 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
216 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
219 $readOnlyReason = $this->readOnlyReason
;
220 // Use the LB-specific read-only reason if everything isn't already read-only
221 if ( $readOnlyReason === false && isset( $this->readOnlyBySection
[$section] ) ) {
222 $readOnlyReason = $this->readOnlyBySection
[$section];
225 return $this->newLoadBalancer(
226 $this->serverTemplate
,
227 $this->sectionLoads
[$section],
234 * @param bool|string $wiki
235 * @return LoadBalancer
237 public function getMainLB( $wiki = false ) {
238 $section = $this->getSectionForWiki( $wiki );
239 if ( !isset( $this->mainLBs
[$section] ) ) {
240 $lb = $this->newMainLB( $wiki );
241 $lb->parentInfo( array( 'id' => "main-$section" ) );
242 $this->chronProt
->initLB( $lb );
243 $this->mainLBs
[$section] = $lb;
246 return $this->mainLBs
[$section];
250 * @param string $cluster
251 * @param bool|string $wiki
252 * @throws MWException
253 * @return LoadBalancer
255 protected function newExternalLB( $cluster, $wiki = false ) {
256 if ( !isset( $this->externalLoads
[$cluster] ) ) {
257 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
259 $template = $this->serverTemplate
;
260 if ( isset( $this->externalTemplateOverrides
) ) {
261 $template = $this->externalTemplateOverrides +
$template;
263 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
264 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
267 return $this->newLoadBalancer(
269 $this->externalLoads
[$cluster],
271 $this->readOnlyReason
276 * @param string $cluster External storage cluster, or false for core
277 * @param bool|string $wiki Wiki ID, or false for the current wiki
278 * @return LoadBalancer
280 public function &getExternalLB( $cluster, $wiki = false ) {
281 if ( !isset( $this->extLBs
[$cluster] ) ) {
282 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
283 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
284 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
287 return $this->extLBs
[$cluster];
291 * Make a new load balancer object based on template and load array
293 * @param array $template
294 * @param array $loads
295 * @param array $groupLoads
296 * @param string|bool $readOnlyReason
297 * @return LoadBalancer
299 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
300 return new LoadBalancer( array(
301 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
302 'loadMonitor' => $this->loadMonitorClass
,
303 'readOnlyReason' => $readOnlyReason
308 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
310 * @param array $template
311 * @param array $loads
312 * @param array $groupLoads
315 private function makeServerArray( $template, $loads, $groupLoads ) {
318 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
319 foreach ( $groupLoadsByServer as $server => $stuff ) {
320 if ( !isset( $loads[$server] ) ) {
324 foreach ( $loads as $serverName => $load ) {
325 $serverInfo = $template;
327 $serverInfo['master'] = true;
328 if ( isset( $this->masterTemplateOverrides
) ) {
329 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
333 $serverInfo['slave'] = true;
335 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
336 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
338 if ( isset( $groupLoadsByServer[$serverName] ) ) {
339 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
341 if ( isset( $this->hostsByName
[$serverName] ) ) {
342 $serverInfo['host'] = $this->hostsByName
[$serverName];
344 $serverInfo['host'] = $serverName;
346 $serverInfo['hostName'] = $serverName;
347 $serverInfo['load'] = $load;
348 $servers[] = $serverInfo;
355 * Take a group load array indexed by group then server, and reindex it by server then group
356 * @param array $groupLoads
359 private function reindexGroupLoads( $groupLoads ) {
360 $reindexed = array();
361 foreach ( $groupLoads as $group => $loads ) {
362 foreach ( $loads as $server => $load ) {
363 $reindexed[$server][$group] = $load;
371 * Get the database name and prefix based on the wiki ID
372 * @param bool|string $wiki
375 private function getDBNameAndPrefix( $wiki = false ) {
376 if ( $wiki === false ) {
377 global $wgDBname, $wgDBprefix;
379 return array( $wgDBname, $wgDBprefix );
381 return wfSplitWikiID( $wiki );
386 * Execute a function for each tracked load balancer
387 * The callback is called with the load balancer as the first parameter,
388 * and $params passed as the subsequent parameters.
389 * @param callable $callback
390 * @param array $params
392 public function forEachLB( $callback, array $params = array() ) {
393 foreach ( $this->mainLBs
as $lb ) {
394 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
396 foreach ( $this->extLBs
as $lb ) {
397 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
401 public function shutdown( $flags = 0 ) {
402 if ( !( $flags & self
::SHUTDOWN_NO_CHRONPROT
) ) {
403 $this->shutdownChronologyProtector( $this->chronProt
);
405 $this->commitMasterChanges( __METHOD__
); // sanity