MediaWiki
REL1_24
|
00001 <?php 00076 class LBFactoryMulti extends LBFactory { 00077 // Required settings 00078 00080 protected $sectionsByDB; 00081 00086 protected $sectionLoads; 00087 00093 protected $serverTemplate; 00094 00095 // Optional settings 00096 00098 protected $groupLoadsBySection = array(); 00099 00101 protected $groupLoadsByDB = array(); 00102 00104 protected $hostsByName = array(); 00105 00107 protected $externalLoads = array(); 00108 00113 protected $externalTemplateOverrides; 00114 00120 protected $templateOverridesByServer; 00121 00123 protected $templateOverridesByCluster; 00124 00126 protected $masterTemplateOverrides; 00127 00132 protected $readOnlyBySection = array(); 00133 00134 // Other stuff 00135 00137 protected $conf; 00138 00140 protected $mainLBs = array(); 00141 00143 protected $extLBs = array(); 00144 00146 protected $lastWiki; 00147 00149 protected $lastSection; 00150 00155 function __construct( $conf ) { 00156 $this->chronProt = new ChronologyProtector; 00157 $this->conf = $conf; 00158 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' ); 00159 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName', 00160 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer', 00161 'templateOverridesByCluster', 'masterTemplateOverrides', 00162 'readOnlyBySection' ); 00163 00164 foreach ( $required as $key ) { 00165 if ( !isset( $conf[$key] ) ) { 00166 throw new MWException( __CLASS__ . ": $key is required in configuration" ); 00167 } 00168 $this->$key = $conf[$key]; 00169 } 00170 00171 foreach ( $optional as $key ) { 00172 if ( isset( $conf[$key] ) ) { 00173 $this->$key = $conf[$key]; 00174 } 00175 } 00176 00177 // Check for read-only mode 00178 $section = $this->getSectionForWiki(); 00179 if ( !empty( $this->readOnlyBySection[$section] ) ) { 00180 global $wgReadOnly; 00181 $wgReadOnly = $this->readOnlyBySection[$section]; 00182 } 00183 } 00184 00189 function getSectionForWiki( $wiki = false ) { 00190 if ( $this->lastWiki === $wiki ) { 00191 return $this->lastSection; 00192 } 00193 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); 00194 if ( isset( $this->sectionsByDB[$dbName] ) ) { 00195 $section = $this->sectionsByDB[$dbName]; 00196 } else { 00197 $section = 'DEFAULT'; 00198 } 00199 $this->lastSection = $section; 00200 $this->lastWiki = $wiki; 00201 00202 return $section; 00203 } 00204 00209 function newMainLB( $wiki = false ) { 00210 list( $dbName, ) = $this->getDBNameAndPrefix( $wiki ); 00211 $section = $this->getSectionForWiki( $wiki ); 00212 $groupLoads = array(); 00213 if ( isset( $this->groupLoadsByDB[$dbName] ) ) { 00214 $groupLoads = $this->groupLoadsByDB[$dbName]; 00215 } 00216 00217 if ( isset( $this->groupLoadsBySection[$section] ) ) { 00218 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] ); 00219 } 00220 00221 return $this->newLoadBalancer( 00222 $this->serverTemplate, 00223 $this->sectionLoads[$section], 00224 $groupLoads 00225 ); 00226 } 00227 00232 function getMainLB( $wiki = false ) { 00233 $section = $this->getSectionForWiki( $wiki ); 00234 if ( !isset( $this->mainLBs[$section] ) ) { 00235 $lb = $this->newMainLB( $wiki, $section ); 00236 $lb->parentInfo( array( 'id' => "main-$section" ) ); 00237 $this->chronProt->initLB( $lb ); 00238 $this->mainLBs[$section] = $lb; 00239 } 00240 00241 return $this->mainLBs[$section]; 00242 } 00243 00250 function newExternalLB( $cluster, $wiki = false ) { 00251 if ( !isset( $this->externalLoads[$cluster] ) ) { 00252 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" ); 00253 } 00254 $template = $this->serverTemplate; 00255 if ( isset( $this->externalTemplateOverrides ) ) { 00256 $template = $this->externalTemplateOverrides + $template; 00257 } 00258 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) { 00259 $template = $this->templateOverridesByCluster[$cluster] + $template; 00260 } 00261 00262 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() ); 00263 } 00264 00270 function &getExternalLB( $cluster, $wiki = false ) { 00271 if ( !isset( $this->extLBs[$cluster] ) ) { 00272 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki ); 00273 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) ); 00274 $this->chronProt->initLB( $this->extLBs[$cluster] ); 00275 } 00276 00277 return $this->extLBs[$cluster]; 00278 } 00279 00288 function newLoadBalancer( $template, $loads, $groupLoads ) { 00289 $servers = $this->makeServerArray( $template, $loads, $groupLoads ); 00290 $lb = new LoadBalancer( array( 00291 'servers' => $servers, 00292 ) ); 00293 00294 return $lb; 00295 } 00296 00305 function makeServerArray( $template, $loads, $groupLoads ) { 00306 $servers = array(); 00307 $master = true; 00308 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads ); 00309 foreach ( $groupLoadsByServer as $server => $stuff ) { 00310 if ( !isset( $loads[$server] ) ) { 00311 $loads[$server] = 0; 00312 } 00313 } 00314 foreach ( $loads as $serverName => $load ) { 00315 $serverInfo = $template; 00316 if ( $master ) { 00317 $serverInfo['master'] = true; 00318 if ( isset( $this->masterTemplateOverrides ) ) { 00319 $serverInfo = $this->masterTemplateOverrides + $serverInfo; 00320 } 00321 $master = false; 00322 } 00323 if ( isset( $this->templateOverridesByServer[$serverName] ) ) { 00324 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo; 00325 } 00326 if ( isset( $groupLoadsByServer[$serverName] ) ) { 00327 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName]; 00328 } 00329 if ( isset( $this->hostsByName[$serverName] ) ) { 00330 $serverInfo['host'] = $this->hostsByName[$serverName]; 00331 } else { 00332 $serverInfo['host'] = $serverName; 00333 } 00334 $serverInfo['hostName'] = $serverName; 00335 $serverInfo['load'] = $load; 00336 $servers[] = $serverInfo; 00337 } 00338 00339 return $servers; 00340 } 00341 00347 function reindexGroupLoads( $groupLoads ) { 00348 $reindexed = array(); 00349 foreach ( $groupLoads as $group => $loads ) { 00350 foreach ( $loads as $server => $load ) { 00351 $reindexed[$server][$group] = $load; 00352 } 00353 } 00354 00355 return $reindexed; 00356 } 00357 00363 function getDBNameAndPrefix( $wiki = false ) { 00364 if ( $wiki === false ) { 00365 global $wgDBname, $wgDBprefix; 00366 00367 return array( $wgDBname, $wgDBprefix ); 00368 } else { 00369 return wfSplitWikiID( $wiki ); 00370 } 00371 } 00372 00380 function forEachLB( $callback, $params = array() ) { 00381 foreach ( $this->mainLBs as $lb ) { 00382 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00383 } 00384 foreach ( $this->extLBs as $lb ) { 00385 call_user_func_array( $callback, array_merge( array( $lb ), $params ) ); 00386 } 00387 } 00388 00389 function shutdown() { 00390 foreach ( $this->mainLBs as $lb ) { 00391 $this->chronProt->shutdownLB( $lb ); 00392 } 00393 foreach ( $this->extLBs as $extLB ) { 00394 $this->chronProt->shutdownLB( $extLB ); 00395 } 00396 $this->chronProt->shutdown(); 00397 $this->commitMasterChanges(); 00398 } 00399 }