MediaWiki  REL1_21
LBFactory_Multi.php
Go to the documentation of this file.
00001 <?php
00059 class LBFactory_Multi extends LBFactory {
00060         // Required settings
00061         var $sectionsByDB, $sectionLoads, $serverTemplate;
00062         // Optional settings
00063         var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
00064         var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
00065         var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
00066         // Other stuff
00067         var $conf, $mainLBs = array(), $extLBs = array();
00068         var $lastWiki, $lastSection;
00069 
00074         function __construct( $conf ) {
00075                 $this->chronProt = new ChronologyProtector;
00076                 $this->conf = $conf;
00077                 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
00078                 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
00079                         'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
00080                         'templateOverridesByCluster', 'masterTemplateOverrides',
00081                         'readOnlyBySection' );
00082 
00083                 foreach ( $required as $key ) {
00084                         if ( !isset( $conf[$key] ) ) {
00085                                 throw new MWException( __CLASS__ . ": $key is required in configuration" );
00086                         }
00087                         $this->$key = $conf[$key];
00088                 }
00089 
00090                 foreach ( $optional as $key ) {
00091                         if ( isset( $conf[$key] ) ) {
00092                                 $this->$key = $conf[$key];
00093                         }
00094                 }
00095 
00096                 // Check for read-only mode
00097                 $section = $this->getSectionForWiki();
00098                 if ( !empty( $this->readOnlyBySection[$section] ) ) {
00099                         global $wgReadOnly;
00100                         $wgReadOnly = $this->readOnlyBySection[$section];
00101                 }
00102         }
00103 
00108         function getSectionForWiki( $wiki = false ) {
00109                 if ( $this->lastWiki === $wiki ) {
00110                         return $this->lastSection;
00111                 }
00112                 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
00113                 if ( isset( $this->sectionsByDB[$dbName] ) ) {
00114                         $section = $this->sectionsByDB[$dbName];
00115                 } else {
00116                         $section = 'DEFAULT';
00117                 }
00118                 $this->lastSection = $section;
00119                 $this->lastWiki = $wiki;
00120                 return $section;
00121         }
00122 
00127         function newMainLB( $wiki = false ) {
00128                 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
00129                 $section = $this->getSectionForWiki( $wiki );
00130                 $groupLoads = array();
00131                 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
00132                         $groupLoads = $this->groupLoadsByDB[$dbName];
00133                 }
00134                 if ( isset( $this->groupLoadsBySection[$section] ) ) {
00135                         $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
00136                 }
00137                 return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
00138         }
00139 
00144         function getMainLB( $wiki = false ) {
00145                 $section = $this->getSectionForWiki( $wiki );
00146                 if ( !isset( $this->mainLBs[$section] ) ) {
00147                         $lb = $this->newMainLB( $wiki, $section );
00148                         $this->chronProt->initLB( $lb );
00149                         $lb->parentInfo( array( 'id' => "main-$section" ) );
00150                         $this->mainLBs[$section] = $lb;
00151                 }
00152                 return $this->mainLBs[$section];
00153         }
00154 
00161         function newExternalLB( $cluster, $wiki = false ) {
00162                 if ( !isset( $this->externalLoads[$cluster] ) ) {
00163                         throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
00164                 }
00165                 $template = $this->serverTemplate;
00166                 if ( isset( $this->externalTemplateOverrides ) ) {
00167                         $template = $this->externalTemplateOverrides + $template;
00168                 }
00169                 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
00170                         $template = $this->templateOverridesByCluster[$cluster] + $template;
00171                 }
00172                 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
00173         }
00174 
00180         function &getExternalLB( $cluster, $wiki = false ) {
00181                 if ( !isset( $this->extLBs[$cluster] ) ) {
00182                         $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
00183                         $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
00184                 }
00185                 return $this->extLBs[$cluster];
00186         }
00187 
00196         function newLoadBalancer( $template, $loads, $groupLoads ) {
00197                 global $wgMasterWaitTimeout;
00198                 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
00199                 $lb = new LoadBalancer( array(
00200                         'servers' => $servers,
00201                         'masterWaitTimeout' => $wgMasterWaitTimeout
00202                 ));
00203                 return $lb;
00204         }
00205 
00214         function makeServerArray( $template, $loads, $groupLoads ) {
00215                 $servers = array();
00216                 $master = true;
00217                 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
00218                 foreach ( $groupLoadsByServer as $server => $stuff ) {
00219                         if ( !isset( $loads[$server] ) ) {
00220                                 $loads[$server] = 0;
00221                         }
00222                 }
00223                 foreach ( $loads as $serverName => $load ) {
00224                         $serverInfo = $template;
00225                         if ( $master ) {
00226                                 $serverInfo['master'] = true;
00227                                 if ( isset( $this->masterTemplateOverrides ) ) {
00228                                         $serverInfo = $this->masterTemplateOverrides + $serverInfo;
00229                                 }
00230                                 $master = false;
00231                         }
00232                         if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
00233                                 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
00234                         }
00235                         if ( isset( $groupLoadsByServer[$serverName] ) ) {
00236                                 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
00237                         }
00238                         if ( isset( $this->hostsByName[$serverName] ) ) {
00239                                 $serverInfo['host'] = $this->hostsByName[$serverName];
00240                         } else {
00241                                 $serverInfo['host'] = $serverName;
00242                         }
00243                         $serverInfo['hostName'] = $serverName;
00244                         $serverInfo['load'] = $load;
00245                         $servers[] = $serverInfo;
00246                 }
00247                 return $servers;
00248         }
00249 
00255         function reindexGroupLoads( $groupLoads ) {
00256                 $reindexed = array();
00257                 foreach ( $groupLoads as $group => $loads ) {
00258                         foreach ( $loads as $server => $load ) {
00259                                 $reindexed[$server][$group] = $load;
00260                         }
00261                 }
00262                 return $reindexed;
00263         }
00264 
00270         function getDBNameAndPrefix( $wiki = false ) {
00271                 if ( $wiki === false ) {
00272                         global $wgDBname, $wgDBprefix;
00273                         return array( $wgDBname, $wgDBprefix );
00274                 } else {
00275                         return wfSplitWikiID( $wiki );
00276                 }
00277         }
00278 
00286         function forEachLB( $callback, $params = array() ) {
00287                 foreach ( $this->mainLBs as $lb ) {
00288                         call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00289                 }
00290                 foreach ( $this->extLBs as $lb ) {
00291                         call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00292                 }
00293         }
00294 
00295         function shutdown() {
00296                 foreach ( $this->mainLBs as $lb ) {
00297                         $this->chronProt->shutdownLB( $lb );
00298                 }
00299                 $this->chronProt->shutdown();
00300                 $this->commitMasterChanges();
00301         }
00302 }