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 * readOnlyBySection A map of section name to read-only message.
72 * Missing or false for read/write.
76 class LBFactory_Multi
extends LBFactory
{
78 var $sectionsByDB, $sectionLoads, $serverTemplate;
80 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
81 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
82 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
84 var $conf, $mainLBs = array(), $extLBs = array();
85 var $lastWiki, $lastSection;
91 function __construct( $conf ) {
92 $this->chronProt
= new ChronologyProtector
;
94 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
95 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
96 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
97 'templateOverridesByCluster', 'masterTemplateOverrides',
98 'readOnlyBySection' );
100 foreach ( $required as $key ) {
101 if ( !isset( $conf[$key] ) ) {
102 throw new MWException( __CLASS__
. ": $key is required in configuration" );
104 $this->$key = $conf[$key];
107 foreach ( $optional as $key ) {
108 if ( isset( $conf[$key] ) ) {
109 $this->$key = $conf[$key];
113 // Check for read-only mode
114 $section = $this->getSectionForWiki();
115 if ( !empty( $this->readOnlyBySection
[$section] ) ) {
117 $wgReadOnly = $this->readOnlyBySection
[$section];
122 * @param $wiki bool|string
125 function getSectionForWiki( $wiki = false ) {
126 if ( $this->lastWiki
=== $wiki ) {
127 return $this->lastSection
;
129 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
130 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
131 $section = $this->sectionsByDB
[$dbName];
133 $section = 'DEFAULT';
135 $this->lastSection
= $section;
136 $this->lastWiki
= $wiki;
142 * @param $wiki bool|string
143 * @return LoadBalancer
145 function newMainLB( $wiki = false ) {
146 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
147 $section = $this->getSectionForWiki( $wiki );
148 $groupLoads = array();
149 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
150 $groupLoads = $this->groupLoadsByDB
[$dbName];
153 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
154 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
157 return $this->newLoadBalancer(
158 $this->serverTemplate
,
159 $this->sectionLoads
[$section],
165 * @param $wiki bool|string
166 * @return LoadBalancer
168 function getMainLB( $wiki = false ) {
169 $section = $this->getSectionForWiki( $wiki );
170 if ( !isset( $this->mainLBs
[$section] ) ) {
171 $lb = $this->newMainLB( $wiki, $section );
172 $lb->parentInfo( array( 'id' => "main-$section" ) );
173 $this->chronProt
->initLB( $lb );
174 $this->mainLBs
[$section] = $lb;
177 return $this->mainLBs
[$section];
181 * @param string $cluster
183 * @throws MWException
184 * @return LoadBalancer
186 function newExternalLB( $cluster, $wiki = false ) {
187 if ( !isset( $this->externalLoads
[$cluster] ) ) {
188 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
190 $template = $this->serverTemplate
;
191 if ( isset( $this->externalTemplateOverrides
) ) {
192 $template = $this->externalTemplateOverrides +
$template;
194 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
195 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
198 return $this->newLoadBalancer( $template, $this->externalLoads
[$cluster], array() );
204 * @return LoadBalancer
206 function &getExternalLB( $cluster, $wiki = false ) {
207 if ( !isset( $this->extLBs
[$cluster] ) ) {
208 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
209 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
210 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
213 return $this->extLBs
[$cluster];
217 * Make a new load balancer object based on template and load array
220 * @param $loads array
222 * @return LoadBalancer
224 function newLoadBalancer( $template, $loads, $groupLoads ) {
225 global $wgMasterWaitTimeout;
226 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
227 $lb = new LoadBalancer( array(
228 'servers' => $servers,
229 'masterWaitTimeout' => $wgMasterWaitTimeout
236 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
239 * @param $loads array
243 function makeServerArray( $template, $loads, $groupLoads ) {
246 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
247 foreach ( $groupLoadsByServer as $server => $stuff ) {
248 if ( !isset( $loads[$server] ) ) {
252 foreach ( $loads as $serverName => $load ) {
253 $serverInfo = $template;
255 $serverInfo['master'] = true;
256 if ( isset( $this->masterTemplateOverrides
) ) {
257 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
261 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
262 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
264 if ( isset( $groupLoadsByServer[$serverName] ) ) {
265 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
267 if ( isset( $this->hostsByName
[$serverName] ) ) {
268 $serverInfo['host'] = $this->hostsByName
[$serverName];
270 $serverInfo['host'] = $serverName;
272 $serverInfo['hostName'] = $serverName;
273 $serverInfo['load'] = $load;
274 $servers[] = $serverInfo;
281 * Take a group load array indexed by group then server, and reindex it by server then group
285 function reindexGroupLoads( $groupLoads ) {
286 $reindexed = array();
287 foreach ( $groupLoads as $group => $loads ) {
288 foreach ( $loads as $server => $load ) {
289 $reindexed[$server][$group] = $load;
297 * Get the database name and prefix based on the wiki ID
301 function getDBNameAndPrefix( $wiki = false ) {
302 if ( $wiki === false ) {
303 global $wgDBname, $wgDBprefix;
305 return array( $wgDBname, $wgDBprefix );
307 return wfSplitWikiID( $wiki );
312 * Execute a function for each tracked load balancer
313 * The callback is called with the load balancer as the first parameter,
314 * and $params passed as the subsequent parameters.
316 * @param $params array
318 function forEachLB( $callback, $params = array() ) {
319 foreach ( $this->mainLBs
as $lb ) {
320 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
322 foreach ( $this->extLBs
as $lb ) {
323 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
327 function shutdown() {
328 foreach ( $this->mainLBs
as $lb ) {
329 $this->chronProt
->shutdownLB( $lb );
331 foreach ( $this->extLBs
as $extLB ) {
332 $this->chronProt
->shutdownLB( $extLB );
334 $this->chronProt
->shutdown();
335 $this->commitMasterChanges();