MediaWiki
REL1_20
|
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 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 00160 function newExternalLB( $cluster, $wiki = false ) { 00161 if ( !isset( $this->externalLoads[$cluster] ) ) { 00162 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" ); 00163 } 00164 $template = $this->serverTemplate; 00165 if ( isset( $this->externalTemplateOverrides ) ) { 00166 $template = $this->externalTemplateOverrides + $template; 00167 } 00168 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) { 00169 $template = $this->templateOverridesByCluster[$cluster] + $template; 00170 } 00171 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() ); 00172 } 00173 00179 function &getExternalLB( $cluster, $wiki = false ) { 00180 if ( !isset( $this->extLBs[$cluster] ) ) { 00181 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); 00182 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); 00183 } 00184 return $this->extLBs[$cluster]; 00185 } 00186 00195 function newLoadBalancer( $template, $loads, $groupLoads ) { 00196 global $wgMasterWaitTimeout; 00197 $servers = $this->makeServerArray( $template, $loads, $groupLoads ); 00198 $lb = new LoadBalancer( array( 00199 'servers' => $servers, 00200 'masterWaitTimeout' => $wgMasterWaitTimeout 00201 )); 00202 return $lb; 00203 } 00204 00213 function makeServerArray( $template, $loads, $groupLoads ) { 00214 $servers = array(); 00215 $master = true; 00216 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads ); 00217 foreach ( $groupLoadsByServer as $server => $stuff ) { 00218 if ( !isset( $loads[$server] ) ) { 00219 $loads[$server] = 0; 00220 } 00221 } 00222 foreach ( $loads as $serverName => $load ) { 00223 $serverInfo = $template; 00224 if ( $master ) { 00225 $serverInfo['master'] = true; 00226 if ( isset( $this->masterTemplateOverrides ) ) { 00227 $serverInfo = $this->masterTemplateOverrides + $serverInfo; 00228 } 00229 $master = false; 00230 } 00231 if ( isset( $this->templateOverridesByServer[$serverName] ) ) { 00232 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo; 00233 } 00234 if ( isset( $groupLoadsByServer[$serverName] ) ) { 00235 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName]; 00236 } 00237 if ( isset( $this->hostsByName[$serverName] ) ) { 00238 $serverInfo['host'] = $this->hostsByName[$serverName]; 00239 } else { 00240 $serverInfo['host'] = $serverName; 00241 } 00242 $serverInfo['hostName'] = $serverName; 00243 $serverInfo['load'] = $load; 00244 $servers[] = $serverInfo; 00245 } 00246 return $servers; 00247 } 00248 00254 function reindexGroupLoads( $groupLoads ) { 00255 $reindexed = array(); 00256 foreach ( $groupLoads as $group => $loads ) { 00257 foreach ( $loads as $server => $load ) { 00258 $reindexed[$server][$group] = $load; 00259 } 00260 } 00261 return $reindexed; 00262 } 00263 00269 function getDBNameAndPrefix( $wiki = false ) { 00270 if ( $wiki === false ) { 00271 global $wgDBname, $wgDBprefix; 00272 return array( $wgDBname, $wgDBprefix ); 00273 } else { 00274 return wfSplitWikiID( $wiki ); 00275 } 00276 } 00277 00285 function forEachLB( $callback, $params = array() ) { 00286 foreach ( $this->mainLBs as $lb ) { 00287 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00288 } 00289 foreach ( $this->extLBs as $lb ) { 00290 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00291 } 00292 } 00293 00294 function shutdown() { 00295 foreach ( $this->mainLBs as $lb ) { 00296 $this->chronProt->shutdownLB( $lb ); 00297 } 00298 $this->chronProt->shutdown(); 00299 $this->commitMasterChanges(); 00300 } 00301 }