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