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 load ratios.
32 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
34 * serverTemplate A server info associative array as documented for $wgDBservers. The host,
35 * hostName and load entries will be overridden.
37 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
38 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
40 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
42 * hostsByName A map of hostname to IP address.
44 * externalLoads A map of external storage cluster name to server load map
46 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
48 * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
49 * server-by-server basis. Applies to both core and external storage.
51 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
53 * masterTemplateOverrides An override array for all master servers.
55 * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
59 class LBFactory_Multi
extends LBFactory
{
61 var $sectionsByDB, $sectionLoads, $serverTemplate;
63 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
64 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
65 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
67 var $conf, $mainLBs = array(), $extLBs = array();
68 var $lastWiki, $lastSection;
74 function __construct( $conf ) {
75 $this->chronProt
= new ChronologyProtector
;
77 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
78 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
79 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
80 'templateOverridesByCluster', 'masterTemplateOverrides',
81 'readOnlyBySection' );
83 foreach ( $required as $key ) {
84 if ( !isset( $conf[$key] ) ) {
85 throw new MWException( __CLASS__
. ": $key is required in configuration" );
87 $this->$key = $conf[$key];
90 foreach ( $optional as $key ) {
91 if ( isset( $conf[$key] ) ) {
92 $this->$key = $conf[$key];
96 // Check for read-only mode
97 $section = $this->getSectionForWiki();
98 if ( !empty( $this->readOnlyBySection
[$section] ) ) {
100 $wgReadOnly = $this->readOnlyBySection
[$section];
105 * @param $wiki bool|string
108 function getSectionForWiki( $wiki = false ) {
109 if ( $this->lastWiki
=== $wiki ) {
110 return $this->lastSection
;
112 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
113 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
114 $section = $this->sectionsByDB
[$dbName];
116 $section = 'DEFAULT';
118 $this->lastSection
= $section;
119 $this->lastWiki
= $wiki;
124 * @param $wiki bool|string
125 * @return LoadBalancer
127 function newMainLB( $wiki = false ) {
128 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
129 $section = $this->getSectionForWiki( $wiki );
130 $groupLoads = array();
131 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
132 $groupLoads = $this->groupLoadsByDB
[$dbName];
134 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
135 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
137 return $this->newLoadBalancer( $this->serverTemplate
, $this->sectionLoads
[$section], $groupLoads );
141 * @param $wiki bool|string
142 * @return LoadBalancer
144 function getMainLB( $wiki = false ) {
145 $section = $this->getSectionForWiki( $wiki );
146 if ( !isset( $this->mainLBs
[$section] ) ) {
147 $lb = $this->newMainLB( $wiki, $section );
148 $lb->parentInfo( array( 'id' => "main-$section" ) );
149 $this->chronProt
->initLB( $lb );
150 $this->mainLBs
[$section] = $lb;
152 return $this->mainLBs
[$section];
156 * @param string $cluster
158 * @throws MWException
159 * @return LoadBalancer
161 function newExternalLB( $cluster, $wiki = false ) {
162 if ( !isset( $this->externalLoads
[$cluster] ) ) {
163 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
165 $template = $this->serverTemplate
;
166 if ( isset( $this->externalTemplateOverrides
) ) {
167 $template = $this->externalTemplateOverrides +
$template;
169 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
170 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
172 return $this->newLoadBalancer( $template, $this->externalLoads
[$cluster], array() );
178 * @return LoadBalancer
180 function &getExternalLB( $cluster, $wiki = false ) {
181 if ( !isset( $this->extLBs
[$cluster] ) ) {
182 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
183 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
184 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
186 return $this->extLBs
[$cluster];
190 * Make a new load balancer object based on template and load array
193 * @param $loads array
195 * @return LoadBalancer
197 function newLoadBalancer( $template, $loads, $groupLoads ) {
198 global $wgMasterWaitTimeout;
199 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
200 $lb = new LoadBalancer( array(
201 'servers' => $servers,
202 'masterWaitTimeout' => $wgMasterWaitTimeout
208 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
211 * @param $loads array
215 function makeServerArray( $template, $loads, $groupLoads ) {
218 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
219 foreach ( $groupLoadsByServer as $server => $stuff ) {
220 if ( !isset( $loads[$server] ) ) {
224 foreach ( $loads as $serverName => $load ) {
225 $serverInfo = $template;
227 $serverInfo['master'] = true;
228 if ( isset( $this->masterTemplateOverrides
) ) {
229 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
233 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
234 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
236 if ( isset( $groupLoadsByServer[$serverName] ) ) {
237 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
239 if ( isset( $this->hostsByName
[$serverName] ) ) {
240 $serverInfo['host'] = $this->hostsByName
[$serverName];
242 $serverInfo['host'] = $serverName;
244 $serverInfo['hostName'] = $serverName;
245 $serverInfo['load'] = $load;
246 $servers[] = $serverInfo;
252 * Take a group load array indexed by group then server, and reindex it by server then group
256 function reindexGroupLoads( $groupLoads ) {
257 $reindexed = array();
258 foreach ( $groupLoads as $group => $loads ) {
259 foreach ( $loads as $server => $load ) {
260 $reindexed[$server][$group] = $load;
267 * Get the database name and prefix based on the wiki ID
271 function getDBNameAndPrefix( $wiki = false ) {
272 if ( $wiki === false ) {
273 global $wgDBname, $wgDBprefix;
274 return array( $wgDBname, $wgDBprefix );
276 return wfSplitWikiID( $wiki );
281 * Execute a function for each tracked load balancer
282 * The callback is called with the load balancer as the first parameter,
283 * and $params passed as the subsequent parameters.
285 * @param $params array
287 function forEachLB( $callback, $params = array() ) {
288 foreach ( $this->mainLBs
as $lb ) {
289 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
291 foreach ( $this->extLBs
as $lb ) {
292 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
296 function shutdown() {
297 foreach ( $this->mainLBs
as $lb ) {
298 $this->chronProt
->shutdownLB( $lb );
300 foreach ( $this->extLBs
as $extLB ) {
301 $this->chronProt
->shutdownLB( $extLB );
303 $this->chronProt
->shutdown();
304 $this->commitMasterChanges();