4 * An interface for generating database load balancers
6 abstract class LBFactory
{
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;
22 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
24 abstract function __construct( $conf );
27 * Get a load balancer object.
29 * @param string $wiki Wiki ID, or false for the current wiki
30 * @return LoadBalancer
32 abstract function getMainLB( $wiki = false );
35 * Get a load balancer for external storage
37 * @param string $cluster External storage cluster, or false for core
38 * @param string $wiki Wiki ID, or false for the current wiki
40 abstract function &getExternalLB( $cluster, $wiki = false );
43 * Execute a function for each tracked load balancer
44 * The callback is called with the load balancer as the first parameter,
45 * and $params passed as the subsequent parameters.
47 abstract function forEachLB( $callback, $params = array() );
50 * Prepare all load balancers for shutdown
53 function shutdown() {}
56 * Call a method of each load balancer
58 function forEachLBCallMethod( $methodName, $args = array() ) {
59 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
63 * Private helper for forEachLBCallMethod
65 function callMethod( $loadBalancer, $methodName, $args ) {
66 call_user_func_array( array( $loadBalancer, $methodName ), $args );
70 * Commit changes on all master connections
72 function commitMasterChanges() {
73 $this->forEachLBCallMethod( 'commitMasterChanges' );
78 * A simple single-master LBFactory that gets its configuration from the b/c globals
80 class LBFactory_Simple
extends LBFactory
{
82 var $extLBs = array();
84 # Chronology protector
87 function __construct( $conf ) {
88 $this->chronProt
= new ChronologyProtector
;
91 function getMainLB( $wiki = false ) {
92 if ( !isset( $this->mainLB
) ) {
93 global $wgDBservers, $wgMasterWaitTimeout;
94 if ( !$wgDBservers ) {
95 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
96 $wgDBservers = array(array(
97 'host' => $wgDBserver,
99 'password' => $wgDBpassword,
100 'dbname' => $wgDBname,
103 'flags' => ($wgDebugDumpSql ? DBO_DEBUG
: 0) | DBO_DEFAULT
107 $this->mainLB
= new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
108 $this->mainLB
->parentInfo( array( 'id' => 'main' ) );
109 $this->chronProt
->initLB( $this->mainLB
);
111 return $this->mainLB
;
114 function &getExternalLB( $cluster, $wiki = false ) {
115 global $wgExternalServers;
116 if ( !isset( $this->extLBs
[$cluster] ) ) {
117 if ( !isset( $wgExternalServers[$cluster] ) ) {
118 throw new MWException( __METHOD__
.": Unknown cluster \"$cluster\"" );
120 $this->extLBs
[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
121 $this->extLBs
[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
123 return $this->extLBs
[$cluster];
127 * Execute a function for each tracked load balancer
128 * The callback is called with the load balancer as the first parameter,
129 * and $params passed as the subsequent parameters.
131 function forEachLB( $callback, $params = array() ) {
132 if ( isset( $this->mainLB
) ) {
133 call_user_func_array( $callback, array_merge( array( $this->mainLB
), $params ) );
135 foreach ( $this->extLBs
as $lb ) {
136 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
140 function shutdown() {
141 if ( $this->mainLB
) {
142 $this->chronProt
->shutdownLB( $this->mainLB
);
144 $this->chronProt
->shutdown();
145 $this->commitMasterChanges();
150 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
151 * Kind of like Hawking's [[Chronology Protection Agency]].
153 class ChronologyProtector
{
155 var $shutdownPos = array();
158 * Initialise a LoadBalancer to give it appropriate chronology protection.
160 * @param LoadBalancer $lb
162 function initLB( $lb ) {
163 if ( $this->startupPos
=== null ) {
164 if ( !empty( $_SESSION[__CLASS__
] ) ) {
165 $this->startupPos
= $_SESSION[__CLASS__
];
168 if ( !$this->startupPos
) {
171 $masterName = $lb->getServerName( 0 );
173 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos
[$masterName] ) ) {
174 $info = $lb->parentInfo();
175 $pos = $this->startupPos
[$masterName];
176 wfDebug( __METHOD__
.": LB " . $info['id'] . " waiting for master pos $pos\n" );
177 $lb->waitFor( $this->startupPos
[$masterName] );
182 * Notify the ChronologyProtector that the LoadBalancer is about to shut
183 * down. Saves replication positions.
185 * @param LoadBalancer $lb
187 function shutdownLB( $lb ) {
188 if ( session_id() != '' && $lb->getServerCount() > 1 ) {
189 $masterName = $lb->getServerName( 0 );
190 if ( !isset( $this->shutdownPos
[$masterName] ) ) {
191 $pos = $lb->getMasterPos();
192 $info = $lb->parentInfo();
193 wfDebug( __METHOD__
.": LB " . $info['id'] . " has master pos $pos\n" );
194 $this->shutdownPos
[$masterName] = $pos;
200 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
201 * May commit chronology data to persistent storage.
203 function shutdown() {
204 if ( session_id() != '' && count( $this->shutdownPos
) ) {
205 wfDebug( __METHOD__
.": saving master pos for " .
206 count( $this->shutdownPos
) . " master(s)\n" );
207 $_SESSION[__CLASS__
] = $this->shutdownPos
;