MediaWiki  REL1_23
LBFactoryMulti.php
Go to the documentation of this file.
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         global $wgMasterWaitTimeout;
00290         $servers = $this->makeServerArray( $template, $loads, $groupLoads );
00291         $lb = new LoadBalancer( array(
00292             'servers' => $servers,
00293             'masterWaitTimeout' => $wgMasterWaitTimeout
00294         ) );
00295 
00296         return $lb;
00297     }
00298 
00307     function makeServerArray( $template, $loads, $groupLoads ) {
00308         $servers = array();
00309         $master = true;
00310         $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
00311         foreach ( $groupLoadsByServer as $server => $stuff ) {
00312             if ( !isset( $loads[$server] ) ) {
00313                 $loads[$server] = 0;
00314             }
00315         }
00316         foreach ( $loads as $serverName => $load ) {
00317             $serverInfo = $template;
00318             if ( $master ) {
00319                 $serverInfo['master'] = true;
00320                 if ( isset( $this->masterTemplateOverrides ) ) {
00321                     $serverInfo = $this->masterTemplateOverrides + $serverInfo;
00322                 }
00323                 $master = false;
00324             }
00325             if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
00326                 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
00327             }
00328             if ( isset( $groupLoadsByServer[$serverName] ) ) {
00329                 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
00330             }
00331             if ( isset( $this->hostsByName[$serverName] ) ) {
00332                 $serverInfo['host'] = $this->hostsByName[$serverName];
00333             } else {
00334                 $serverInfo['host'] = $serverName;
00335             }
00336             $serverInfo['hostName'] = $serverName;
00337             $serverInfo['load'] = $load;
00338             $servers[] = $serverInfo;
00339         }
00340 
00341         return $servers;
00342     }
00343 
00349     function reindexGroupLoads( $groupLoads ) {
00350         $reindexed = array();
00351         foreach ( $groupLoads as $group => $loads ) {
00352             foreach ( $loads as $server => $load ) {
00353                 $reindexed[$server][$group] = $load;
00354             }
00355         }
00356 
00357         return $reindexed;
00358     }
00359 
00365     function getDBNameAndPrefix( $wiki = false ) {
00366         if ( $wiki === false ) {
00367             global $wgDBname, $wgDBprefix;
00368 
00369             return array( $wgDBname, $wgDBprefix );
00370         } else {
00371             return wfSplitWikiID( $wiki );
00372         }
00373     }
00374 
00382     function forEachLB( $callback, $params = array() ) {
00383         foreach ( $this->mainLBs as $lb ) {
00384             call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00385         }
00386         foreach ( $this->extLBs as $lb ) {
00387             call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00388         }
00389     }
00390 
00391     function shutdown() {
00392         foreach ( $this->mainLBs as $lb ) {
00393             $this->chronProt->shutdownLB( $lb );
00394         }
00395         foreach ( $this->extLBs as $extLB ) {
00396             $this->chronProt->shutdownLB( $extLB );
00397         }
00398         $this->chronProt->shutdown();
00399         $this->commitMasterChanges();
00400     }
00401 }