3 * Advanced generator of database load balancing objects for wiki farms
11 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
12 * Ignores the old configuration globals
15 * sectionsByDB A map of database names to section names
17 * sectionLoads A 2-d map. For each section, gives a map of server names to load ratios.
18 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
20 * serverTemplate A server info associative array as documented for $wgDBservers. The host,
21 * hostName and load entries will be overridden.
23 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
24 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
26 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
28 * hostsByName A map of hostname to IP address.
30 * externalLoads A map of external storage cluster name to server load map
32 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
34 * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
35 * server-by-server basis. Applies to both core and external storage.
37 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
39 * masterTemplateOverrides An override array for all master servers.
41 * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
45 class LBFactory_Multi
extends LBFactory
{
47 var $sectionsByDB, $sectionLoads, $serverTemplate;
49 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
50 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
51 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
53 var $conf, $mainLBs = array(), $extLBs = array();
54 var $lastWiki, $lastSection;
56 function __construct( $conf ) {
57 $this->chronProt
= new ChronologyProtector
;
59 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
60 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
61 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
62 'templateOverridesByCluster', 'masterTemplateOverrides',
63 'readOnlyBySection' );
65 foreach ( $required as $key ) {
66 if ( !isset( $conf[$key] ) ) {
67 throw new MWException( __CLASS__
.": $key is required in configuration" );
69 $this->$key = $conf[$key];
72 foreach ( $optional as $key ) {
73 if ( isset( $conf[$key] ) ) {
74 $this->$key = $conf[$key];
78 // Check for read-only mode
79 $section = $this->getSectionForWiki();
80 if ( !empty( $this->readOnlyBySection
[$section] ) ) {
82 $wgReadOnly = $this->readOnlyBySection
[$section];
86 function getSectionForWiki( $wiki = false ) {
87 if ( $this->lastWiki
=== $wiki ) {
88 return $this->lastSection
;
90 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
91 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
92 $section = $this->sectionsByDB
[$dbName];
96 $this->lastSection
= $section;
97 $this->lastWiki
= $wiki;
102 * @param $wiki string
103 * @return LoadBalancer
105 function newMainLB( $wiki = false ) {
106 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
107 $section = $this->getSectionForWiki( $wiki );
108 $groupLoads = array();
109 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
110 $groupLoads = $this->groupLoadsByDB
[$dbName];
112 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
113 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
115 return $this->newLoadBalancer( $this->serverTemplate
, $this->sectionLoads
[$section], $groupLoads );
120 * @return LoadBalancer
122 function getMainLB( $wiki = false ) {
123 $section = $this->getSectionForWiki( $wiki );
124 if ( !isset( $this->mainLBs
[$section] ) ) {
125 $lb = $this->newMainLB( $wiki, $section );
126 $this->chronProt
->initLB( $lb );
127 $lb->parentInfo( array( 'id' => "main-$section" ) );
128 $this->mainLBs
[$section] = $lb;
130 return $this->mainLBs
[$section];
136 * @return LoadBalancer
138 function newExternalLB( $cluster, $wiki = false ) {
139 if ( !isset( $this->externalLoads
[$cluster] ) ) {
140 throw new MWException( __METHOD__
.": Unknown cluster \"$cluster\"" );
142 $template = $this->serverTemplate
;
143 if ( isset( $this->externalTemplateOverrides
) ) {
144 $template = $this->externalTemplateOverrides +
$template;
146 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
147 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
149 return $this->newLoadBalancer( $template, $this->externalLoads
[$cluster], array() );
155 * @return LoadBalancer
157 function &getExternalLB( $cluster, $wiki = false ) {
158 if ( !isset( $this->extLBs
[$cluster] ) ) {
159 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
160 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
162 return $this->extLBs
[$cluster];
166 * Make a new load balancer object based on template and load array
168 * @return LoadBalancer
170 function newLoadBalancer( $template, $loads, $groupLoads ) {
171 global $wgMasterWaitTimeout;
172 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
173 $lb = new LoadBalancer( array(
174 'servers' => $servers,
175 'masterWaitTimeout' => $wgMasterWaitTimeout
181 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
185 function makeServerArray( $template, $loads, $groupLoads ) {
188 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
189 foreach ( $groupLoadsByServer as $server => $stuff ) {
190 if ( !isset( $loads[$server] ) ) {
194 foreach ( $loads as $serverName => $load ) {
195 $serverInfo = $template;
197 $serverInfo['master'] = true;
198 if ( isset( $this->masterTemplateOverrides
) ) {
199 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
203 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
204 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
206 if ( isset( $groupLoadsByServer[$serverName] ) ) {
207 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
209 if ( isset( $this->hostsByName
[$serverName] ) ) {
210 $serverInfo['host'] = $this->hostsByName
[$serverName];
212 $serverInfo['host'] = $serverName;
214 $serverInfo['hostName'] = $serverName;
215 $serverInfo['load'] = $load;
216 $servers[] = $serverInfo;
222 * Take a group load array indexed by group then server, and reindex it by server then group
224 function reindexGroupLoads( $groupLoads ) {
225 $reindexed = array();
226 foreach ( $groupLoads as $group => $loads ) {
227 foreach ( $loads as $server => $load ) {
228 $reindexed[$server][$group] = $load;
235 * Get the database name and prefix based on the wiki ID
237 function getDBNameAndPrefix( $wiki = false ) {
238 if ( $wiki === false ) {
239 global $wgDBname, $wgDBprefix;
240 return array( $wgDBname, $wgDBprefix );
242 return wfSplitWikiID( $wiki );
247 * Execute a function for each tracked load balancer
248 * The callback is called with the load balancer as the first parameter,
249 * and $params passed as the subsequent parameters.
251 function forEachLB( $callback, $params = array() ) {
252 foreach ( $this->mainLBs
as $lb ) {
253 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
255 foreach ( $this->extLBs
as $lb ) {
256 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
260 function shutdown() {
261 foreach ( $this->mainLBs
as $lb ) {
262 $this->chronProt
->shutdownLB( $lb );
264 $this->chronProt
->shutdown();
265 $this->commitMasterChanges();