MediaWiki  REL1_22
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             $lb->parentInfo( array( 'id' => "main-$section" ) );
00149             $this->chronProt->initLB( $lb );
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             $this->chronProt->initLB( $this->extLBs[$cluster] );
00185         }
00186         return $this->extLBs[$cluster];
00187     }
00188 
00197     function newLoadBalancer( $template, $loads, $groupLoads ) {
00198         global $wgMasterWaitTimeout;
00199         $servers = $this->makeServerArray( $template, $loads, $groupLoads );
00200         $lb = new LoadBalancer( array(
00201             'servers' => $servers,
00202             'masterWaitTimeout' => $wgMasterWaitTimeout
00203         ));
00204         return $lb;
00205     }
00206 
00215     function makeServerArray( $template, $loads, $groupLoads ) {
00216         $servers = array();
00217         $master = true;
00218         $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
00219         foreach ( $groupLoadsByServer as $server => $stuff ) {
00220             if ( !isset( $loads[$server] ) ) {
00221                 $loads[$server] = 0;
00222             }
00223         }
00224         foreach ( $loads as $serverName => $load ) {
00225             $serverInfo = $template;
00226             if ( $master ) {
00227                 $serverInfo['master'] = true;
00228                 if ( isset( $this->masterTemplateOverrides ) ) {
00229                     $serverInfo = $this->masterTemplateOverrides + $serverInfo;
00230                 }
00231                 $master = false;
00232             }
00233             if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
00234                 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
00235             }
00236             if ( isset( $groupLoadsByServer[$serverName] ) ) {
00237                 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
00238             }
00239             if ( isset( $this->hostsByName[$serverName] ) ) {
00240                 $serverInfo['host'] = $this->hostsByName[$serverName];
00241             } else {
00242                 $serverInfo['host'] = $serverName;
00243             }
00244             $serverInfo['hostName'] = $serverName;
00245             $serverInfo['load'] = $load;
00246             $servers[] = $serverInfo;
00247         }
00248         return $servers;
00249     }
00250 
00256     function reindexGroupLoads( $groupLoads ) {
00257         $reindexed = array();
00258         foreach ( $groupLoads as $group => $loads ) {
00259             foreach ( $loads as $server => $load ) {
00260                 $reindexed[$server][$group] = $load;
00261             }
00262         }
00263         return $reindexed;
00264     }
00265 
00271     function getDBNameAndPrefix( $wiki = false ) {
00272         if ( $wiki === false ) {
00273             global $wgDBname, $wgDBprefix;
00274             return array( $wgDBname, $wgDBprefix );
00275         } else {
00276             return wfSplitWikiID( $wiki );
00277         }
00278     }
00279 
00287     function forEachLB( $callback, $params = array() ) {
00288         foreach ( $this->mainLBs as $lb ) {
00289             call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00290         }
00291         foreach ( $this->extLBs as $lb ) {
00292             call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00293         }
00294     }
00295 
00296     function shutdown() {
00297         foreach ( $this->mainLBs as $lb ) {
00298             $this->chronProt->shutdownLB( $lb );
00299         }
00300         foreach ( $this->extLBs as $extLB ) {
00301             $this->chronProt->shutdownLB( $extLB );
00302         }
00303         $this->chronProt->shutdown();
00304         $this->commitMasterChanges();
00305     }
00306 }