9 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
10 * Ignores the old configuration globals
13 * sectionsByDB A map of database names to section names
15 * sectionLoads A 2-d map. For each section, gives a map of server names to load ratios.
16 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
18 * serverTemplate A server info associative array as documented for $wgDBservers. The host,
19 * hostName and load entries will be overridden.
21 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
22 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
24 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
26 * hostsByName A map of hostname to IP address.
28 * externalLoads A map of external storage cluster name to server load map
30 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external storage
32 * templateOverridesByServer A 2-d map overriding serverTemplate and externalTemplateOverrides on a
33 * server-by-server basis. Applies to both core and external storage.
35 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster
37 * masterTemplateOverrides An override array for all master servers.
39 * readOnlyBySection A map of section name to read-only message. Missing or false for read/write.
43 class LBFactory_Multi
extends LBFactory
{
45 var $sectionsByDB, $sectionLoads, $serverTemplate;
47 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
48 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
49 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
51 var $conf, $mainLBs = array(), $extLBs = array();
52 var $lastWiki, $lastSection;
54 function __construct( $conf ) {
55 $this->chronProt
= new ChronologyProtector
;
57 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
58 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
59 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
60 'templateOverridesByCluster', 'masterTemplateOverrides',
61 'readOnlyBySection' );
63 foreach ( $required as $key ) {
64 if ( !isset( $conf[$key] ) ) {
65 throw new MWException( __CLASS__
.": $key is required in configuration" );
67 $this->$key = $conf[$key];
70 foreach ( $optional as $key ) {
71 if ( isset( $conf[$key] ) ) {
72 $this->$key = $conf[$key];
76 // Check for read-only mode
77 $section = $this->getSectionForWiki();
78 if ( !empty( $this->readOnlyBySection
[$section] ) ) {
80 $wgReadOnly = $this->readOnlyBySection
[$section];
84 function getSectionForWiki( $wiki = false ) {
85 if ( $this->lastWiki
=== $wiki ) {
86 return $this->lastSection
;
88 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
89 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
90 $section = $this->sectionsByDB
[$dbName];
94 $this->lastSection
= $section;
95 $this->lastWiki
= $wiki;
99 function newMainLB( $wiki = false ) {
100 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
101 $section = $this->getSectionForWiki( $wiki );
102 $groupLoads = array();
103 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
104 $groupLoads = $this->groupLoadsByDB
[$dbName];
106 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
107 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
109 return $this->newLoadBalancer( $this->serverTemplate
, $this->sectionLoads
[$section], $groupLoads );
112 function getMainLB( $wiki = false ) {
113 $section = $this->getSectionForWiki( $wiki );
114 if ( !isset( $this->mainLBs
[$section] ) ) {
115 $lb = $this->newMainLB( $wiki, $section );
116 $this->chronProt
->initLB( $lb );
117 $lb->parentInfo( array( 'id' => "main-$section" ) );
118 $this->mainLBs
[$section] = $lb;
120 return $this->mainLBs
[$section];
123 function newExternalLB( $cluster, $wiki = false ) {
124 if ( !isset( $this->externalLoads
[$cluster] ) ) {
125 throw new MWException( __METHOD__
.": Unknown cluster \"$cluster\"" );
127 $template = $this->serverTemplate
;
128 if ( isset( $this->externalTemplateOverrides
) ) {
129 $template = $this->externalTemplateOverrides +
$template;
131 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
132 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
134 return $this->newLoadBalancer( $template, $this->externalLoads
[$cluster], array() );
137 function &getExternalLB( $cluster, $wiki = false ) {
138 if ( !isset( $this->extLBs
[$cluster] ) ) {
139 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
140 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
142 return $this->extLBs
[$cluster];
146 * Make a new load balancer object based on template and load array
148 function newLoadBalancer( $template, $loads, $groupLoads ) {
149 global $wgMasterWaitTimeout;
150 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
151 $lb = new LoadBalancer( array(
152 'servers' => $servers,
153 'masterWaitTimeout' => $wgMasterWaitTimeout
159 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
161 function makeServerArray( $template, $loads, $groupLoads ) {
164 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
165 foreach ( $groupLoadsByServer as $server => $stuff ) {
166 if ( !isset( $loads[$server] ) ) {
170 foreach ( $loads as $serverName => $load ) {
171 $serverInfo = $template;
173 $serverInfo['master'] = true;
174 if ( isset( $this->masterTemplateOverrides
) ) {
175 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
179 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
180 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
182 if ( isset( $groupLoadsByServer[$serverName] ) ) {
183 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
185 if ( isset( $this->hostsByName
[$serverName] ) ) {
186 $serverInfo['host'] = $this->hostsByName
[$serverName];
188 $serverInfo['host'] = $serverName;
190 $serverInfo['hostName'] = $serverName;
191 $serverInfo['load'] = $load;
192 $servers[] = $serverInfo;
198 * Take a group load array indexed by group then server, and reindex it by server then group
200 function reindexGroupLoads( $groupLoads ) {
201 $reindexed = array();
202 foreach ( $groupLoads as $group => $loads ) {
203 foreach ( $loads as $server => $load ) {
204 $reindexed[$server][$group] = $load;
211 * Get the database name and prefix based on the wiki ID
213 function getDBNameAndPrefix( $wiki = false ) {
214 if ( $wiki === false ) {
215 global $wgDBname, $wgDBprefix;
216 return array( $wgDBname, $wgDBprefix );
218 return wfSplitWikiID( $wiki );
223 * Execute a function for each tracked load balancer
224 * The callback is called with the load balancer as the first parameter,
225 * and $params passed as the subsequent parameters.
227 function forEachLB( $callback, $params = array() ) {
228 foreach ( $this->mainLBs
as $lb ) {
229 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
231 foreach ( $this->extLBs
as $lb ) {
232 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
236 function shutdown() {
237 foreach ( $this->mainLBs
as $lb ) {
238 $this->chronProt
->shutdownLB( $lb );
240 $this->chronProt
->shutdown();
241 $this->commitMasterChanges();