Gah, left this one too...
[mediawiki.git] / includes / LBFactory.php
blob6370f6506c009e324bcad963e16d4194a558fa79
1 <?php
3 /**
4 * An interface for generating database load balancers
5 */
6 abstract class LBFactory {
7 static $instance;
9 /**
10 * Get an LBFactory instance
12 static function &singleton() {
13 if ( is_null( self::$instance ) ) {
14 global $wgLBFactoryConf;
15 $class = $wgLBFactoryConf['class'];
16 self::$instance = new $class( $wgLBFactoryConf );
18 return self::$instance;
21 /**
22 * Destory the instance
23 * Actually used by maintenace/parserTests.inc to force to reopen connection
24 * when $wgDBprefix has changed
26 static function destroy(){
27 self::$instance = null;
30 /**
31 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
33 abstract function __construct( $conf );
35 /**
36 * Get a load balancer object.
38 * @param string $wiki Wiki ID, or false for the current wiki
39 * @return LoadBalancer
41 abstract function getMainLB( $wiki = false );
44 * Get a load balancer for external storage
46 * @param string $cluster External storage cluster, or false for core
47 * @param string $wiki Wiki ID, or false for the current wiki
49 abstract function &getExternalLB( $cluster, $wiki = false );
51 /**
52 * Execute a function for each tracked load balancer
53 * The callback is called with the load balancer as the first parameter,
54 * and $params passed as the subsequent parameters.
56 abstract function forEachLB( $callback, $params = array() );
58 /**
59 * Prepare all load balancers for shutdown
60 * STUB
62 function shutdown() {}
64 /**
65 * Call a method of each load balancer
67 function forEachLBCallMethod( $methodName, $args = array() ) {
68 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
71 /**
72 * Private helper for forEachLBCallMethod
74 function callMethod( $loadBalancer, $methodName, $args ) {
75 call_user_func_array( array( $loadBalancer, $methodName ), $args );
78 /**
79 * Commit changes on all master connections
81 function commitMasterChanges() {
82 $this->forEachLBCallMethod( 'commitMasterChanges' );
86 /**
87 * A simple single-master LBFactory that gets its configuration from the b/c globals
89 class LBFactory_Simple extends LBFactory {
90 var $mainLB;
91 var $extLBs = array();
93 # Chronology protector
94 var $chronProt;
96 function __construct( $conf ) {
97 $this->chronProt = new ChronologyProtector;
100 function getMainLB( $wiki = false ) {
101 if ( !isset( $this->mainLB ) ) {
102 global $wgDBservers, $wgMasterWaitTimeout;
103 if ( !$wgDBservers ) {
104 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
105 $wgDBservers = array(array(
106 'host' => $wgDBserver,
107 'user' => $wgDBuser,
108 'password' => $wgDBpassword,
109 'dbname' => $wgDBname,
110 'type' => $wgDBtype,
111 'load' => 1,
112 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
116 $this->mainLB = new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
117 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
118 $this->chronProt->initLB( $this->mainLB );
120 return $this->mainLB;
123 function &getExternalLB( $cluster, $wiki = false ) {
124 global $wgExternalServers;
125 if ( !isset( $this->extLBs[$cluster] ) ) {
126 if ( !isset( $wgExternalServers[$cluster] ) ) {
127 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
129 $this->extLBs[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
130 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
132 return $this->extLBs[$cluster];
136 * Execute a function for each tracked load balancer
137 * The callback is called with the load balancer as the first parameter,
138 * and $params passed as the subsequent parameters.
140 function forEachLB( $callback, $params = array() ) {
141 if ( isset( $this->mainLB ) ) {
142 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
144 foreach ( $this->extLBs as $lb ) {
145 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
149 function shutdown() {
150 if ( $this->mainLB ) {
151 $this->chronProt->shutdownLB( $this->mainLB );
153 $this->chronProt->shutdown();
154 $this->commitMasterChanges();
159 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
160 * Kind of like Hawking's [[Chronology Protection Agency]].
162 class ChronologyProtector {
163 var $startupPos;
164 var $shutdownPos = array();
167 * Initialise a LoadBalancer to give it appropriate chronology protection.
169 * @param LoadBalancer $lb
171 function initLB( $lb ) {
172 if ( $this->startupPos === null ) {
173 if ( !empty( $_SESSION[__CLASS__] ) ) {
174 $this->startupPos = $_SESSION[__CLASS__];
177 if ( !$this->startupPos ) {
178 return;
180 $masterName = $lb->getServerName( 0 );
182 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
183 $info = $lb->parentInfo();
184 $pos = $this->startupPos[$masterName];
185 wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
186 $lb->waitFor( $this->startupPos[$masterName] );
191 * Notify the ChronologyProtector that the LoadBalancer is about to shut
192 * down. Saves replication positions.
194 * @param LoadBalancer $lb
196 function shutdownLB( $lb ) {
197 if ( session_id() != '' && $lb->getServerCount() > 1 ) {
198 $masterName = $lb->getServerName( 0 );
199 if ( !isset( $this->shutdownPos[$masterName] ) ) {
200 $pos = $lb->getMasterPos();
201 $info = $lb->parentInfo();
202 wfDebug( __METHOD__.": LB " . $info['id'] . " has master pos $pos\n" );
203 $this->shutdownPos[$masterName] = $pos;
209 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
210 * May commit chronology data to persistent storage.
212 function shutdown() {
213 if ( session_id() != '' && count( $this->shutdownPos ) ) {
214 wfDebug( __METHOD__.": saving master pos for " .
215 count( $this->shutdownPos ) . " master(s)\n" );
216 $_SESSION[__CLASS__] = $this->shutdownPos;