MediaWiki
master
|
00001 <?php 00060 class LBFactory_Multi extends LBFactory { 00061 // Required settings 00062 var $sectionsByDB, $sectionLoads, $serverTemplate; 00063 // Optional settings 00064 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array(); 00065 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer; 00066 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array(); 00067 // Other stuff 00068 var $conf, $mainLBs = array(), $extLBs = array(); 00069 var $lastWiki, $lastSection; 00070 00075 function __construct( $conf ) { 00076 $this->chronProt = new ChronologyProtector; 00077 $this->conf = $conf; 00078 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' ); 00079 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName', 00080 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer', 00081 'templateOverridesByCluster', 'masterTemplateOverrides', 00082 'readOnlyBySection' ); 00083 00084 foreach ( $required as $key ) { 00085 if ( !isset( $conf[$key] ) ) { 00086 throw new MWException( __CLASS__.": $key is required in configuration" ); 00087 } 00088 $this->$key = $conf[$key]; 00089 } 00090 00091 foreach ( $optional as $key ) { 00092 if ( isset( $conf[$key] ) ) { 00093 $this->$key = $conf[$key]; 00094 } 00095 } 00096 00097 // Check for read-only mode 00098 $section = $this->getSectionForWiki(); 00099 if ( !empty( $this->readOnlyBySection[$section] ) ) { 00100 global $wgReadOnly; 00101 $wgReadOnly = $this->readOnlyBySection[$section]; 00102 } 00103 } 00104 00109 function getSectionForWiki( $wiki = false ) { 00110 if ( $this->lastWiki === $wiki ) { 00111 return $this->lastSection; 00112 } 00113 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); 00114 if ( isset( $this->sectionsByDB[$dbName] ) ) { 00115 $section = $this->sectionsByDB[$dbName]; 00116 } else { 00117 $section = 'DEFAULT'; 00118 } 00119 $this->lastSection = $section; 00120 $this->lastWiki = $wiki; 00121 return $section; 00122 } 00123 00128 function newMainLB( $wiki = false ) { 00129 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); 00130 $section = $this->getSectionForWiki( $wiki ); 00131 $groupLoads = array(); 00132 if ( isset( $this->groupLoadsByDB[$dbName] ) ) { 00133 $groupLoads = $this->groupLoadsByDB[$dbName]; 00134 } 00135 if ( isset( $this->groupLoadsBySection[$section] ) ) { 00136 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] ); 00137 } 00138 return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads ); 00139 } 00140 00145 function getMainLB( $wiki = false ) { 00146 $section = $this->getSectionForWiki( $wiki ); 00147 if ( !isset( $this->mainLBs[$section] ) ) { 00148 $lb = $this->newMainLB( $wiki, $section ); 00149 $this->chronProt->initLB( $lb ); 00150 $lb->parentInfo( array( 'id' => "main-$section" ) ); 00151 $this->mainLBs[$section] = $lb; 00152 } 00153 return $this->mainLBs[$section]; 00154 } 00155 00162 function newExternalLB( $cluster, $wiki = false ) { 00163 if ( !isset( $this->externalLoads[$cluster] ) ) { 00164 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" ); 00165 } 00166 $template = $this->serverTemplate; 00167 if ( isset( $this->externalTemplateOverrides ) ) { 00168 $template = $this->externalTemplateOverrides + $template; 00169 } 00170 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) { 00171 $template = $this->templateOverridesByCluster[$cluster] + $template; 00172 } 00173 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() ); 00174 } 00175 00181 function &getExternalLB( $cluster, $wiki = false ) { 00182 if ( !isset( $this->extLBs[$cluster] ) ) { 00183 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); 00184 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$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 $this->chronProt->shutdown(); 00301 $this->commitMasterChanges(); 00302 } 00303 }