00001 <?php
00011 abstract class LBFactory {
00012 static $instance;
00013
00017 static function &singleton() {
00018 if ( is_null( self::$instance ) ) {
00019 global $wgLBFactoryConf;
00020 $class = $wgLBFactoryConf['class'];
00021 self::$instance = new $class( $wgLBFactoryConf );
00022 }
00023 return self::$instance;
00024 }
00025
00030 static function destroyInstance() {
00031 if ( self::$instance ) {
00032 self::$instance->shutdown();
00033 self::$instance->forEachLBCallMethod( 'closeAll' );
00034 self::$instance = null;
00035 }
00036 }
00037
00041 abstract function __construct( $conf );
00042
00050 abstract function newMainLB( $wiki = false );
00051
00058 abstract function getMainLB( $wiki = false );
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 abstract function newExternalLB( $cluster, $wiki = false );
00069
00070
00071
00072
00073
00074
00075
00076 abstract function &getExternalLB( $cluster, $wiki = false );
00077
00083 abstract function forEachLB( $callback, $params = array() );
00084
00089 function shutdown() {}
00090
00094 function forEachLBCallMethod( $methodName, $args = array() ) {
00095 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
00096 }
00097
00101 function callMethod( $loadBalancer, $methodName, $args ) {
00102 call_user_func_array( array( $loadBalancer, $methodName ), $args );
00103 }
00104
00108 function commitMasterChanges() {
00109 $this->forEachLBCallMethod( 'commitMasterChanges' );
00110 }
00111 }
00112
00116 class LBFactory_Simple extends LBFactory {
00117 var $mainLB;
00118 var $extLBs = array();
00119
00120 # Chronology protector
00121 var $chronProt;
00122
00123 function __construct( $conf ) {
00124 $this->chronProt = new ChronologyProtector;
00125 }
00126
00127 function newMainLB( $wiki = false ) {
00128 global $wgDBservers, $wgMasterWaitTimeout;
00129 if ( $wgDBservers ) {
00130 $servers = $wgDBservers;
00131 } else {
00132 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
00133 $servers = array(array(
00134 'host' => $wgDBserver,
00135 'user' => $wgDBuser,
00136 'password' => $wgDBpassword,
00137 'dbname' => $wgDBname,
00138 'type' => $wgDBtype,
00139 'load' => 1,
00140 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
00141 ));
00142 }
00143
00144 return new LoadBalancer( array(
00145 'servers' => $servers,
00146 'masterWaitTimeout' => $wgMasterWaitTimeout
00147 ));
00148 }
00149
00150 function getMainLB( $wiki = false ) {
00151 if ( !isset( $this->mainLB ) ) {
00152 $this->mainLB = $this->newMainLB( $wiki );
00153 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
00154 $this->chronProt->initLB( $this->mainLB );
00155 }
00156 return $this->mainLB;
00157 }
00158
00159 function newExternalLB( $cluster, $wiki = false ) {
00160 global $wgExternalServers;
00161 if ( !isset( $wgExternalServers[$cluster] ) ) {
00162 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
00163 }
00164 return new LoadBalancer( array(
00165 'servers' => $wgExternalServers[$cluster]
00166 ));
00167 }
00168
00169 function &getExternalLB( $cluster, $wiki = false ) {
00170 if ( !isset( $this->extLBs[$cluster] ) ) {
00171 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
00172 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
00173 }
00174 return $this->extLBs[$cluster];
00175 }
00176
00182 function forEachLB( $callback, $params = array() ) {
00183 if ( isset( $this->mainLB ) ) {
00184 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
00185 }
00186 foreach ( $this->extLBs as $lb ) {
00187 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
00188 }
00189 }
00190
00191 function shutdown() {
00192 if ( $this->mainLB ) {
00193 $this->chronProt->shutdownLB( $this->mainLB );
00194 }
00195 $this->chronProt->shutdown();
00196 $this->commitMasterChanges();
00197 }
00198 }
00199
00204 class ChronologyProtector {
00205 var $startupPos;
00206 var $shutdownPos = array();
00207
00213 function initLB( $lb ) {
00214 if ( $this->startupPos === null ) {
00215 if ( !empty( $_SESSION[__CLASS__] ) ) {
00216 $this->startupPos = $_SESSION[__CLASS__];
00217 }
00218 }
00219 if ( !$this->startupPos ) {
00220 return;
00221 }
00222 $masterName = $lb->getServerName( 0 );
00223
00224 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
00225 $info = $lb->parentInfo();
00226 $pos = $this->startupPos[$masterName];
00227 wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
00228 $lb->waitFor( $this->startupPos[$masterName] );
00229 }
00230 }
00231
00238 function shutdownLB( $lb ) {
00239
00240 if ( strval( session_id() ) == '' || $lb->getServerCount() <= 1 ) {
00241 return;
00242 }
00243 $masterName = $lb->getServerName( 0 );
00244 if ( isset( $this->shutdownPos[$masterName] ) ) {
00245
00246 return;
00247 }
00248
00249 $db = $lb->getAnyOpenConnection( 0 );
00250 $info = $lb->parentInfo();
00251 if ( !$db || !$db->doneWrites() ) {
00252 wfDebug( __METHOD__.": LB {$info['id']}, no writes done\n" );
00253 return;
00254 }
00255 $pos = $db->getMasterPos();
00256 wfDebug( __METHOD__.": LB {$info['id']} has master pos $pos\n" );
00257 $this->shutdownPos[$masterName] = $pos;
00258 }
00259
00264 function shutdown() {
00265 if ( session_id() != '' && count( $this->shutdownPos ) ) {
00266 wfDebug( __METHOD__.": saving master pos for " .
00267 count( $this->shutdownPos ) . " master(s)\n" );
00268 $_SESSION[__CLASS__] = $this->shutdownPos;
00269 }
00270 }
00271 }