* Don't show hist link if hidden
[lhc/web/wiklou.git] / includes / LBFactory_Multi.php
1 <?php
2
3 /**
4 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
5 * Ignores the old configuration globals
6 *
7 * Configuration:
8 * sectionsByDB A map of database names to section names
9 *
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 ) )
12 *
13 * mainTemplate A server info associative array as documented for $wgDBservers. The host,
14 * hostName and load entries will be overridden.
15 *
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 ) ) )
18 *
19 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
20 *
21 * hostsByName A map of hostname to IP address.
22 *
23 * externalLoads A map of external storage cluster name to server load map
24 *
25 * externalTemplate A server info structure used for external storage servers
26 *
27 * templateOverridesByServer A 2-d map overriding mainTemplate or externalTemplate on a
28 * server-by-server basis.
29 *
30 * templateOverridesByCluster A 2-d map overriding externalTemplate by cluster
31 *
32 * masterTemplateOverrides An override array for mainTemplate and externalTemplate for all
33 * master servers.
34 *
35 */
36 class LBFactory_Multi extends LBFactory {
37 // Required settings
38 var $sectionsByDB, $sectionLoads, $mainTemplate;
39 // Optional settings
40 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
41 var $externalLoads = array(), $externalTemplate, $templateOverridesByServer;
42 var $templateOverridesByCluster, $masterTemplateOverrides;
43 // Other stuff
44 var $conf, $mainLBs = array(), $extLBs = array();
45 var $localSection = null;
46
47 function __construct( $conf ) {
48 $this->chronProt = new ChronologyProtector;
49 $this->conf = $conf;
50 $required = array( 'sectionsByDB', 'sectionLoads', 'mainTemplate' );
51 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
52 'externalLoads', 'externalTemplate', 'templateOverridesByServer',
53 'templateOverridesByCluster', 'masterTemplateOverrides' );
54
55 foreach ( $required as $key ) {
56 if ( !isset( $conf[$key] ) ) {
57 throw new MWException( __CLASS__.": $key is required in configuration" );
58 }
59 $this->$key = $conf[$key];
60 }
61
62 foreach ( $optional as $key ) {
63 if ( isset( $conf[$key] ) ) {
64 $this->$key = $conf[$key];
65 }
66 }
67 }
68
69 function getSectionForWiki( $wiki ) {
70 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
71 if ( isset( $this->sectionsByDB[$dbName] ) ) {
72 return $this->sectionsByDB[$dbName];
73 } else {
74 return 'DEFAULT';
75 }
76 }
77
78 function getMainLB( $wiki = false ) {
79 // Determine section
80 if ( $wiki === false ) {
81 if ( $this->localSection === null ) {
82 $this->localSection = $this->getSectionForWiki( $wiki );
83 }
84 $section = $this->localSection;
85 } else {
86 $section = $this->getSectionForWiki( $wiki );
87 }
88
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];
94 }
95 if ( isset( $this->groupLoadsBySection[$section] ) ) {
96 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
97 }
98 $this->mainLBs[$section] = $this->newLoadBalancer( $this->mainTemplate,
99 $this->sectionLoads[$section], $groupLoads, "main-$section" );
100 $this->chronProt->initLB( $this->mainLBs[$section] );
101 }
102 return $this->mainLBs[$section];
103 }
104
105 function &getExternalLB( $cluster, $wiki = false ) {
106 if ( !isset( $this->extLBs[$cluster] ) ) {
107 if ( !isset( $this->externalLoads[$cluster] ) ) {
108 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
109 }
110 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
111 $template = $this->templateOverridesByCluster[$cluster];
112 } elseif ( isset( $this->externalTemplate ) ) {
113 $template = $this->externalTemplate;
114 } else {
115 $template = $this->mainTemplate;
116 }
117 $this->extLBs[$cluster] = $this->newLoadBalancer( $template,
118 $this->externalLoads[$cluster], array(), "ext-$cluster" );
119 }
120 return $this->extLBs[$cluster];
121 }
122
123 /**
124 * Make a new load balancer object based on template and load array
125 */
126 function newLoadBalancer( $template, $loads, $groupLoads, $id ) {
127 global $wgMasterWaitTimeout;
128 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
129 $lb = new LoadBalancer( $servers, false, $wgMasterWaitTimeout );
130 $lb->parentInfo( array( 'id' => $id ) );
131 return $lb;
132 }
133
134 /**
135 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
136 */
137 function makeServerArray( $template, $loads, $groupLoads ) {
138 $servers = array();
139 $master = true;
140 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
141 foreach ( $groupLoadsByServer as $server => $stuff ) {
142 if ( !isset( $loads[$server] ) ) {
143 $loads[$server] = 0;
144 }
145 }
146 foreach ( $loads as $serverName => $load ) {
147 $serverInfo = $template;
148 if ( $master ) {
149 $serverInfo['master'] = true;
150 if ( isset( $this->masterTemplateOverrides ) ) {
151 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
152 }
153 $master = false;
154 }
155 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
156 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
157 }
158 if ( isset( $groupLoadsByServer[$serverName] ) ) {
159 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
160 }
161 if ( isset( $this->hostsByName[$serverName] ) ) {
162 $serverInfo['host'] = $this->hostsByName[$serverName];
163 } else {
164 $serverInfo['host'] = $serverName;
165 }
166 $serverInfo['hostName'] = $serverName;
167 $serverInfo['load'] = $load;
168 $servers[] = $serverInfo;
169 }
170 return $servers;
171 }
172
173 /**
174 * Take a group load array indexed by group then server, and reindex it by server then group
175 */
176 function reindexGroupLoads( $groupLoads ) {
177 $reindexed = array();
178 foreach ( $groupLoads as $group => $loads ) {
179 foreach ( $loads as $server => $load ) {
180 $reindexed[$server][$group] = $load;
181 }
182 }
183 return $reindexed;
184 }
185
186 /**
187 * Get the database name and prefix based on the wiki ID
188 */
189 function getDBNameAndPrefix( $wiki = false ) {
190 if ( $wiki === false ) {
191 global $wgDBname, $wgDBprefix;
192 return array( $wgDBname, $wgDBprefix );
193 } else {
194 return wfSplitWikiID( $wiki );
195 }
196 }
197
198 /**
199 * Execute a function for each tracked load balancer
200 * The callback is called with the load balancer as the first parameter,
201 * and $params passed as the subsequent parameters.
202 */
203 function forEachLB( $callback, $params = array() ) {
204 foreach ( $this->mainLBs as $lb ) {
205 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
206 }
207 foreach ( $this->extLBs as $lb ) {
208 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
209 }
210 }
211
212 function shutdown() {
213 foreach ( $this->mainLBs as $lb ) {
214 $this->chronProt->shutdownLB( $lb );
215 }
216 $this->chronProt->shutdown();
217 $this->commitMasterChanges();
218 }
219 }
220