4 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
5 * Ignores the old configuration globals
8 * sectionsByDB A map of database names to section names
10 * sectionLoads A 2-d map. For each section, gives a map of server names to load ratios.
11 * For example: array( 'section1' => array( 'db1' => 100, 'db2' => 100 ) )
13 * mainTemplate A server info associative array as documented for $wgDBservers. The host,
14 * hostName and load entries will be overridden.
16 * groupLoadsBySection A 3-d map giving server load ratios for each section and group. For example:
17 * array( 'section1' => array( 'group1' => array( 'db1' => 100, 'db2' => 100 ) ) )
19 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
21 * hostsByName A map of hostname to IP address.
23 * externalLoads A map of external storage cluster name to server load map
25 * externalTemplate A server info structure used for external storage servers
27 * templateOverridesByServer A 2-d map overriding mainTemplate or externalTemplate on a
28 * server-by-server basis.
30 * templateOverridesByCluster A 2-d map overriding externalTemplate by cluster
32 * masterTemplateOverrides An override array for mainTemplate and externalTemplate for all
36 class LBFactory_Multi
extends LBFactory
{
38 var $sectionsByDB, $sectionLoads, $mainTemplate;
40 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
41 var $externalLoads = array(), $externalTemplate, $templateOverridesByServer;
42 var $templateOverridesByCluster, $masterTemplateOverrides;
44 var $conf, $mainLBs = array(), $extLBs = array();
45 var $localSection = null;
47 function __construct( $conf ) {
48 $this->chronProt
= new ChronologyProtector
;
50 $required = array( 'sectionsByDB', 'sectionLoads', 'mainTemplate' );
51 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
52 'externalLoads', 'externalTemplate', 'templateOverridesByServer',
53 'templateOverridesByCluster', 'masterTemplateOverrides' );
55 foreach ( $required as $key ) {
56 if ( !isset( $conf[$key] ) ) {
57 throw new MWException( __CLASS__
.": $key is required in configuration" );
59 $this->$key = $conf[$key];
62 foreach ( $optional as $key ) {
63 if ( isset( $conf[$key] ) ) {
64 $this->$key = $conf[$key];
69 function getSectionForWiki( $wiki ) {
70 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
71 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
72 return $this->sectionsByDB
[$dbName];
78 function getMainLB( $wiki = false ) {
80 if ( $wiki === false ) {
81 if ( $this->localSection
=== null ) {
82 $this->localSection
= $this->getSectionForWiki( $wiki );
84 $section = $this->localSection
;
86 $section = $this->getSectionForWiki( $wiki );
89 if ( !isset( $this->mainLBs
[$section] ) ) {
90 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
91 $groupLoads = array();
92 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
93 $groupLoads = $this->groupLoadsByDB
[$dbName];
95 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
96 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
98 $this->mainLBs
[$section] = $this->newLoadBalancer( $this->mainTemplate
,
99 $this->sectionLoads
[$section], $groupLoads, "main-$section" );
100 $this->chronProt
->initLB( $this->mainLBs
[$section] );
102 return $this->mainLBs
[$section];
105 function getExternalLB( $cluster, $wiki = false ) {
106 global $wgExternalServers;
107 if ( !isset( $this->extLBs
[$cluster] ) ) {
108 if ( !isset( $this->externalLoads
[$cluster] ) ) {
109 throw new MWException( __METHOD__
.": Unknown cluster \"$cluster\"" );
111 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
112 $template = $this->templateOverridesByCluster
[$cluster];
113 } elseif ( isset( $this->externalTemplate
) ) {
114 $template = $this->externalTemplate
;
116 $template = $this->mainTemplate
;
118 $this->extLBs
[$cluster] = $this->newLoadBalancer( $template,
119 $this->externalLoads
[$cluster], array(), "ext-$cluster" );
121 return $this->extLBs
[$cluster];
125 * Make a new load balancer object based on template and load array
127 function newLoadBalancer( $template, $loads, $groupLoads, $id ) {
128 global $wgMasterWaitTimeout;
129 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
130 $lb = new LoadBalancer( $servers, false, $wgMasterWaitTimeout );
131 $lb->parentInfo( array( 'id' => $id ) );
136 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
138 function makeServerArray( $template, $loads, $groupLoads ) {
141 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
142 foreach ( $groupLoadsByServer as $server => $stuff ) {
143 if ( !isset( $loads[$server] ) ) {
147 foreach ( $loads as $serverName => $load ) {
148 $serverInfo = $template;
150 $serverInfo['master'] = true;
151 if ( isset( $this->masterTemplateOverrides
) ) {
152 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
156 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
157 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
159 if ( isset( $groupLoadsByServer[$serverName] ) ) {
160 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
162 if ( isset( $this->hostsByName
[$serverName] ) ) {
163 $serverInfo['host'] = $this->hostsByName
[$serverName];
165 $serverInfo['host'] = $serverName;
167 $serverInfo['hostName'] = $serverName;
168 $serverInfo['load'] = $load;
169 $servers[] = $serverInfo;
175 * Take a group load array indexed by group then server, and reindex it by server then group
177 function reindexGroupLoads( $groupLoads ) {
178 $reindexed = array();
179 foreach ( $groupLoads as $group => $loads ) {
180 foreach ( $loads as $server => $load ) {
181 $reindexed[$server][$group] = $load;
188 * Get the database name and prefix based on the wiki ID
190 function getDBNameAndPrefix( $wiki = false ) {
191 if ( $wiki === false ) {
192 global $wgDBname, $wgDBprefix;
193 return array( $wgDBname, $wgDBprefix );
195 return wfSplitWikiID( $wiki );
200 * Execute a function for each tracked load balancer
201 * The callback is called with the load balancer as the first parameter,
202 * and $params passed as the subsequent parameters.
204 function forEachLB( $callback, $params = array() ) {
205 foreach ( $this->mainLBs
as $lb ) {
206 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
208 foreach ( $this->extLBs
as $lb ) {
209 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
213 function shutdown() {
214 foreach ( $this->mainLBs
as $lb ) {
215 $this->chronProt
->shutdownLB( $lb );
217 $this->chronProt
->shutdown();
218 $this->commitMasterChanges();