3 * Advanced generator of database load balancing objects for wiki farms.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
26 * Ignores the old configuration globals
29 * sectionsByDB A map of database names to section names.
31 * sectionLoads A 2-d map. For each section, gives a map of server names to
32 * load ratios. For example:
34 * 'section1' => array(
40 * serverTemplate A server info associative array as documented for $wgDBservers.
41 * The host, hostName and load entries will be overridden.
43 * groupLoadsBySection A 3-d map giving server load ratios for each section and group.
46 * 'section1' => array(
54 * groupLoadsByDB A 3-d map giving server load ratios by DB name.
56 * hostsByName A map of hostname to IP address.
58 * externalLoads A map of external storage cluster name to server load map.
60 * externalTemplateOverrides A set of server info keys overriding serverTemplate for external
63 * templateOverridesByServer A 2-d map overriding serverTemplate and
64 * externalTemplateOverrides on a server-by-server basis. Applies
65 * to both core and external storage.
67 * templateOverridesByCluster A 2-d map overriding the server info by external storage cluster.
69 * masterTemplateOverrides An override array for all master servers.
71 * loadMonitorClass Name of the LoadMonitor class to always use.
73 * readOnlyBySection A map of section name to read-only message.
74 * Missing or false for read/write.
78 class LBFactoryMulti
extends LBFactory
{
81 /** @var array A map of database names to section names */
82 private $sectionsByDB;
85 * @var array A 2-d map. For each section, gives a map of server names to
88 private $sectionLoads;
91 * @var array A server info associative array as documented for
92 * $wgDBservers. The host, hostName and load entries will be
95 private $serverTemplate;
99 /** @var array A 3-d map giving server load ratios for each section and group */
100 private $groupLoadsBySection = array();
102 /** @var array A 3-d map giving server load ratios by DB name */
103 private $groupLoadsByDB = array();
105 /** @var array A map of hostname to IP address */
106 private $hostsByName = array();
108 /** @var array A map of external storage cluster name to server load map */
109 private $externalLoads = array();
112 * @var array A set of server info keys overriding serverTemplate for
115 private $externalTemplateOverrides;
118 * @var array A 2-d map overriding serverTemplate and
119 * externalTemplateOverrides on a server-by-server basis. Applies to both
120 * core and external storage
122 private $templateOverridesByServer;
124 /** @var array A 2-d map overriding the server info by external storage cluster */
125 private $templateOverridesByCluster;
127 /** @var array An override array for all master servers */
128 private $masterTemplateOverrides;
131 * @var array|bool A map of section name to read-only message. Missing or
132 * false for read/write
134 private $readOnlyBySection = array();
138 /** @var array Load balancer factory configuration */
141 /** @var LoadBalancer[] */
142 private $mainLBs = array();
144 /** @var LoadBalancer[] */
145 private $extLBs = array();
148 private $loadMonitorClass;
154 private $lastSection;
158 * @throws MWException
160 public function __construct( array $conf ) {
161 parent
::__construct( $conf );
163 $this->chronProt
= new ChronologyProtector
;
165 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
166 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
167 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
168 'templateOverridesByCluster', 'masterTemplateOverrides',
169 'readOnlyBySection', 'loadMonitorClass' );
171 foreach ( $required as $key ) {
172 if ( !isset( $conf[$key] ) ) {
173 throw new MWException( __CLASS__
. ": $key is required in configuration" );
175 $this->$key = $conf[$key];
178 foreach ( $optional as $key ) {
179 if ( isset( $conf[$key] ) ) {
180 $this->$key = $conf[$key];
186 * @param bool|string $wiki
189 private function getSectionForWiki( $wiki = false ) {
190 if ( $this->lastWiki
=== $wiki ) {
191 return $this->lastSection
;
193 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
194 if ( isset( $this->sectionsByDB
[$dbName] ) ) {
195 $section = $this->sectionsByDB
[$dbName];
197 $section = 'DEFAULT';
199 $this->lastSection
= $section;
200 $this->lastWiki
= $wiki;
206 * @param bool|string $wiki
207 * @return LoadBalancer
209 public function newMainLB( $wiki = false ) {
210 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
211 $section = $this->getSectionForWiki( $wiki );
212 if ( isset( $this->groupLoadsByDB
[$dbName] ) ) {
213 $groupLoads = $this->groupLoadsByDB
[$dbName];
215 $groupLoads = array();
218 if ( isset( $this->groupLoadsBySection
[$section] ) ) {
219 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection
[$section] );
222 $readOnlyReason = $this->readOnlyReason
;
223 // Use the LB-specific read-only reason if everything isn't already read-only
224 if ( $readOnlyReason === false && isset( $this->readOnlyBySection
[$section] ) ) {
225 $readOnlyReason = $this->readOnlyBySection
[$section];
228 return $this->newLoadBalancer(
229 $this->serverTemplate
,
230 $this->sectionLoads
[$section],
237 * @param bool|string $wiki
238 * @return LoadBalancer
240 public function getMainLB( $wiki = false ) {
241 $section = $this->getSectionForWiki( $wiki );
242 if ( !isset( $this->mainLBs
[$section] ) ) {
243 $lb = $this->newMainLB( $wiki );
244 $lb->parentInfo( array( 'id' => "main-$section" ) );
245 $this->chronProt
->initLB( $lb );
246 $this->mainLBs
[$section] = $lb;
249 return $this->mainLBs
[$section];
253 * @param string $cluster
254 * @param bool|string $wiki
255 * @throws MWException
256 * @return LoadBalancer
258 protected function newExternalLB( $cluster, $wiki = false ) {
259 if ( !isset( $this->externalLoads
[$cluster] ) ) {
260 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
262 $template = $this->serverTemplate
;
263 if ( isset( $this->externalTemplateOverrides
) ) {
264 $template = $this->externalTemplateOverrides +
$template;
266 if ( isset( $this->templateOverridesByCluster
[$cluster] ) ) {
267 $template = $this->templateOverridesByCluster
[$cluster] +
$template;
270 return $this->newLoadBalancer(
272 $this->externalLoads
[$cluster],
274 $this->readOnlyReason
279 * @param string $cluster External storage cluster, or false for core
280 * @param bool|string $wiki Wiki ID, or false for the current wiki
281 * @return LoadBalancer
283 public function &getExternalLB( $cluster, $wiki = false ) {
284 if ( !isset( $this->extLBs
[$cluster] ) ) {
285 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
286 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
287 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
290 return $this->extLBs
[$cluster];
294 * Make a new load balancer object based on template and load array
296 * @param array $template
297 * @param array $loads
298 * @param array $groupLoads
299 * @param string|bool $readOnlyReason
300 * @return LoadBalancer
302 private function newLoadBalancer( $template, $loads, $groupLoads, $readOnlyReason ) {
303 return new LoadBalancer( array(
304 'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
305 'loadMonitor' => $this->loadMonitorClass
,
306 'readOnlyReason' => $readOnlyReason
311 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
313 * @param array $template
314 * @param array $loads
315 * @param array $groupLoads
318 private function makeServerArray( $template, $loads, $groupLoads ) {
321 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
322 foreach ( $groupLoadsByServer as $server => $stuff ) {
323 if ( !isset( $loads[$server] ) ) {
327 foreach ( $loads as $serverName => $load ) {
328 $serverInfo = $template;
330 $serverInfo['master'] = true;
331 if ( isset( $this->masterTemplateOverrides
) ) {
332 $serverInfo = $this->masterTemplateOverrides +
$serverInfo;
336 $serverInfo['slave'] = true;
338 if ( isset( $this->templateOverridesByServer
[$serverName] ) ) {
339 $serverInfo = $this->templateOverridesByServer
[$serverName] +
$serverInfo;
341 if ( isset( $groupLoadsByServer[$serverName] ) ) {
342 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
344 if ( isset( $this->hostsByName
[$serverName] ) ) {
345 $serverInfo['host'] = $this->hostsByName
[$serverName];
347 $serverInfo['host'] = $serverName;
349 $serverInfo['hostName'] = $serverName;
350 $serverInfo['load'] = $load;
351 $servers[] = $serverInfo;
358 * Take a group load array indexed by group then server, and reindex it by server then group
359 * @param array $groupLoads
362 private function reindexGroupLoads( $groupLoads ) {
363 $reindexed = array();
364 foreach ( $groupLoads as $group => $loads ) {
365 foreach ( $loads as $server => $load ) {
366 $reindexed[$server][$group] = $load;
374 * Get the database name and prefix based on the wiki ID
375 * @param bool|string $wiki
378 private function getDBNameAndPrefix( $wiki = false ) {
379 if ( $wiki === false ) {
380 global $wgDBname, $wgDBprefix;
382 return array( $wgDBname, $wgDBprefix );
384 return wfSplitWikiID( $wiki );
389 * Execute a function for each tracked load balancer
390 * The callback is called with the load balancer as the first parameter,
391 * and $params passed as the subsequent parameters.
392 * @param callable $callback
393 * @param array $params
395 public function forEachLB( $callback, array $params = array() ) {
396 foreach ( $this->mainLBs
as $lb ) {
397 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
399 foreach ( $this->extLBs
as $lb ) {
400 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
404 public function shutdown() {
405 foreach ( $this->mainLBs
as $lb ) {
406 $this->chronProt
->shutdownLB( $lb );
408 foreach ( $this->extLBs
as $extLB ) {
409 $this->chronProt
->shutdownLB( $extLB );
411 $this->chronProt
->shutdown();
412 $this->commitMasterChanges();