00001 <?php
00043 class LBFactory_Multi extends LBFactory {
00044
00045 var $sectionsByDB, $sectionLoads, $serverTemplate;
00046
00047 var $groupLoadsBySection = array(), $groupLoadsByDB = array(), $hostsByName = array();
00048 var $externalLoads = array(), $externalTemplateOverrides, $templateOverridesByServer;
00049 var $templateOverridesByCluster, $masterTemplateOverrides, $readOnlyBySection = array();
00050
00051 var $conf, $mainLBs = array(), $extLBs = array();
00052 var $lastWiki, $lastSection;
00053
00054 function __construct( $conf ) {
00055 $this->chronProt = new ChronologyProtector;
00056 $this->conf = $conf;
00057 $required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
00058 $optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
00059 'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
00060 'templateOverridesByCluster', 'masterTemplateOverrides',
00061 'readOnlyBySection' );
00062
00063 foreach ( $required as $key ) {
00064 if ( !isset( $conf[$key] ) ) {
00065 throw new MWException( __CLASS__.": $key is required in configuration" );
00066 }
00067 $this->$key = $conf[$key];
00068 }
00069
00070 foreach ( $optional as $key ) {
00071 if ( isset( $conf[$key] ) ) {
00072 $this->$key = $conf[$key];
00073 }
00074 }
00075
00076
00077 $section = $this->getSectionForWiki();
00078 if ( !empty( $this->readOnlyBySection[$section] ) ) {
00079 global $wgReadOnly;
00080 $wgReadOnly = $this->readOnlyBySection[$section];
00081 }
00082 }
00083
00084 function getSectionForWiki( $wiki = false ) {
00085 if ( $this->lastWiki === $wiki ) {
00086 return $this->lastSection;
00087 }
00088 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
00089 if ( isset( $this->sectionsByDB[$dbName] ) ) {
00090 $section = $this->sectionsByDB[$dbName];
00091 } else {
00092 $section = 'DEFAULT';
00093 }
00094 $this->lastSection = $section;
00095 $this->lastWiki = $wiki;
00096 return $section;
00097 }
00098
00099 function newMainLB( $wiki = false ) {
00100 list( $dbName, $prefix ) = $this->getDBNameAndPrefix( $wiki );
00101 $section = $this->getSectionForWiki( $wiki );
00102 $groupLoads = array();
00103 if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
00104 $groupLoads = $this->groupLoadsByDB[$dbName];
00105 }
00106 if ( isset( $this->groupLoadsBySection[$section] ) ) {
00107 $groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
00108 }
00109 return $this->newLoadBalancer( $this->serverTemplate, $this->sectionLoads[$section], $groupLoads );
00110 }
00111
00112 function getMainLB( $wiki = false ) {
00113 $section = $this->getSectionForWiki( $wiki );
00114 if ( !isset( $this->mainLBs[$section] ) ) {
00115 $lb = $this->newMainLB( $wiki, $section );
00116 $this->chronProt->initLB( $lb );
00117 $lb->parentInfo( array( 'id' => "main-$section" ) );
00118 $this->mainLBs[$section] = $lb;
00119 }
00120 return $this->mainLBs[$section];
00121 }
00122
00123 function newExternalLB( $cluster, $wiki = false ) {
00124 if ( !isset( $this->externalLoads[$cluster] ) ) {
00125 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
00126 }
00127 $template = $this->serverTemplate;
00128 if ( isset( $this->externalTemplateOverrides ) ) {
00129 $template = $this->externalTemplateOverrides + $template;
00130 }
00131 if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
00132 $template = $this->templateOverridesByCluster[$cluster] + $template;
00133 }
00134 return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
00135 }
00136
00137 function &getExternalLB( $cluster, $wiki = false ) {
00138 if ( !isset( $this->extLBs[$cluster] ) ) {
00139 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
00140 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
00141 }
00142 return $this->extLBs[$cluster];
00143 }
00144
00148 function newLoadBalancer( $template, $loads, $groupLoads ) {
00149 global $wgMasterWaitTimeout;
00150 $servers = $this->makeServerArray( $template, $loads, $groupLoads );
00151 $lb = new LoadBalancer( array(
00152 'servers' => $servers,
00153 'masterWaitTimeout' => $wgMasterWaitTimeout
00154 ));
00155 return $lb;
00156 }
00157
00161 function makeServerArray( $template, $loads, $groupLoads ) {
00162 $servers = array();
00163 $master = true;
00164 $groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
00165 foreach ( $groupLoadsByServer as $server => $stuff ) {
00166 if ( !isset( $loads[$server] ) ) {
00167 $loads[$server] = 0;
00168 }
00169 }
00170 foreach ( $loads as $serverName => $load ) {
00171 $serverInfo = $template;
00172 if ( $master ) {
00173 $serverInfo['master'] = true;
00174 if ( isset( $this->masterTemplateOverrides ) ) {
00175 $serverInfo = $this->masterTemplateOverrides + $serverInfo;
00176 }
00177 $master = false;
00178 }
00179 if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
00180 $serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
00181 }
00182 if ( isset( $groupLoadsByServer[$serverName] ) ) {
00183 $serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
00184 }
00185 if ( isset( $this->hostsByName[$serverName] ) ) {
00186 $serverInfo['host'] = $this->hostsByName[$serverName];
00187 } else {
00188 $serverInfo['host'] = $serverName;
00189 }
00190 $serverInfo['hostName'] = $serverName;
00191 $serverInfo['load'] = $load;
00192 $servers[] = $serverInfo;
00193 }
00194 return $servers;
00195 }
00196
00200 function reindexGroupLoads( $groupLoads ) {
00201 $reindexed = array();
00202 foreach ( $groupLoads as $group => $loads ) {
00203 foreach ( $loads as $server => $load ) {
00204 $reindexed[$server][$group] = $load;
00205 }
00206 }
00207 return $reindexed;
00208 }
00209
00213 function getDBNameAndPrefix( $wiki = false ) {
00214 if ( $wiki === false ) {
00215 global $wgDBname, $wgDBprefix;
00216 return array( $wgDBname, $wgDBprefix );
00217 } else {
00218 return wfSplitWikiID( $wiki );
00219 }
00220 }
00221
00227 function forEachLB( $callback, $params = array() ) {
00228 foreach ( $this->mainLBs as $lb ) {
00229 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00230 }
00231 foreach ( $this->extLBs as $lb ) {
00232 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00233 }
00234 }
00235
00236 function shutdown() {
00237 foreach ( $this->mainLBs as $lb ) {
00238 $this->chronProt->shutdownLB( $lb );
00239 }
00240 $this->chronProt->shutdown();
00241 $this->commitMasterChanges();
00242 }
00243 }