8 * An interface for generating database load balancers
11 abstract class LBFactory
{
15 * Get an LBFactory instance
17 static function &singleton() {
18 if ( is_null( self
::$instance ) ) {
19 global $wgLBFactoryConf;
20 $class = $wgLBFactoryConf['class'];
21 self
::$instance = new $class( $wgLBFactoryConf );
23 return self
::$instance;
27 * Destory the instance
28 * Actually used by maintenace/parserTests.inc to force to reopen connection
29 * when $wgDBprefix has changed
31 static function destroy(){
32 self
::$instance = null;
36 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
38 abstract function __construct( $conf );
41 * Get a load balancer object.
43 * @param string $wiki Wiki ID, or false for the current wiki
44 * @return LoadBalancer
46 abstract function getMainLB( $wiki = false );
49 * Get a load balancer for external storage
51 * @param string $cluster External storage cluster, or false for core
52 * @param string $wiki Wiki ID, or false for the current wiki
54 abstract function &getExternalLB( $cluster, $wiki = false );
57 * Execute a function for each tracked load balancer
58 * The callback is called with the load balancer as the first parameter,
59 * and $params passed as the subsequent parameters.
61 abstract function forEachLB( $callback, $params = array() );
64 * Prepare all load balancers for shutdown
67 function shutdown() {}
70 * Call a method of each load balancer
72 function forEachLBCallMethod( $methodName, $args = array() ) {
73 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
77 * Private helper for forEachLBCallMethod
79 function callMethod( $loadBalancer, $methodName, $args ) {
80 call_user_func_array( array( $loadBalancer, $methodName ), $args );
84 * Commit changes on all master connections
86 function commitMasterChanges() {
87 $this->forEachLBCallMethod( 'commitMasterChanges' );
92 * A simple single-master LBFactory that gets its configuration from the b/c globals
94 class LBFactory_Simple
extends LBFactory
{
96 var $extLBs = array();
98 # Chronology protector
101 function __construct( $conf ) {
102 $this->chronProt
= new ChronologyProtector
;
105 function getMainLB( $wiki = false ) {
106 if ( !isset( $this->mainLB
) ) {
107 global $wgDBservers, $wgMasterWaitTimeout;
108 if ( !$wgDBservers ) {
109 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
110 $wgDBservers = array(array(
111 'host' => $wgDBserver,
113 'password' => $wgDBpassword,
114 'dbname' => $wgDBname,
117 'flags' => ($wgDebugDumpSql ? DBO_DEBUG
: 0) | DBO_DEFAULT
121 $this->mainLB
= new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
122 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
123 $this->chronProt
->initLB( $this->mainLB
);
125 return $this->mainLB
;
128 function &getExternalLB( $cluster, $wiki = false ) {
129 global $wgExternalServers;
130 if ( !isset( $this->extLBs
[$cluster] ) ) {
131 if ( !isset( $wgExternalServers[$cluster] ) ) {
132 throw new MWException( __METHOD__
.": Unknown cluster \"$cluster\"" );
134 $this->extLBs
[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
135 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
137 return $this->extLBs
[$cluster];
141 * Execute a function for each tracked load balancer
142 * The callback is called with the load balancer as the first parameter,
143 * and $params passed as the subsequent parameters.
145 function forEachLB( $callback, $params = array() ) {
146 if ( isset( $this->mainLB
) ) {
147 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
149 foreach ( $this->extLBs
as $lb ) {
150 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
154 function shutdown() {
155 if ( $this->mainLB
) {
156 $this->chronProt
->shutdownLB( $this->mainLB
);
158 $this->chronProt
->shutdown();
159 $this->commitMasterChanges();
164 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
165 * Kind of like Hawking's [[Chronology Protection Agency]].
167 class ChronologyProtector
{
169 var $shutdownPos = array();
172 * Initialise a LoadBalancer to give it appropriate chronology protection.
174 * @param LoadBalancer $lb
176 function initLB( $lb ) {
177 if ( $this->startupPos
=== null ) {
178 if ( !empty( $_SESSION[__CLASS__
] ) ) {
179 $this->startupPos
= $_SESSION[__CLASS__
];
182 if ( !$this->startupPos
) {
185 $masterName = $lb->getServerName( 0 );
187 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos
[$masterName] ) ) {
188 $info = $lb->parentInfo();
189 $pos = $this->startupPos
[$masterName];
190 wfDebug( __METHOD__
.": LB " . $info['id'] . " waiting for master pos $pos\n" );
191 $lb->waitFor( $this->startupPos
[$masterName] );
196 * Notify the ChronologyProtector that the LoadBalancer is about to shut
197 * down. Saves replication positions.
199 * @param LoadBalancer $lb
201 function shutdownLB( $lb ) {
202 if ( session_id() != '' && $lb->getServerCount() > 1 ) {
203 $masterName = $lb->getServerName( 0 );
204 if ( !isset( $this->shutdownPos
[$masterName] ) ) {
205 $pos = $lb->getMasterPos();
206 $info = $lb->parentInfo();
207 wfDebug( __METHOD__
.": LB " . $info['id'] . " has master pos $pos\n" );
208 $this->shutdownPos
[$masterName] = $pos;
214 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
215 * May commit chronology data to persistent storage.
217 function shutdown() {
218 if ( session_id() != '' && count( $this->shutdownPos
) ) {
219 wfDebug( __METHOD__
.": saving master pos for " .
220 count( $this->shutdownPos
) . " master(s)\n" );
221 $_SESSION[__CLASS__
] = $this->shutdownPos
;