* Reorganised the includes directory, creating subdirectories db, parser and specials
[lhc/web/wiklou.git] / includes / db / LBFactory.php
1 <?php
2 /**
3 * @file
4 * @ingroup Database
5 */
6
7 /**
8 * An interface for generating database load balancers
9 * @ingroup Database
10 */
11 abstract class LBFactory {
12 static $instance;
13
14 /**
15 * Get an LBFactory instance
16 */
17 static function &singleton() {
18 if ( is_null( self::$instance ) ) {
19 global $wgLBFactoryConf;
20 $class = $wgLBFactoryConf['class'];
21 self::$instance = new $class( $wgLBFactoryConf );
22 }
23 return self::$instance;
24 }
25
26 /**
27 * Destory the instance
28 * Actually used by maintenace/parserTests.inc to force to reopen connection
29 * when $wgDBprefix has changed
30 */
31 static function destroy(){
32 self::$instance = null;
33 }
34
35 /**
36 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
37 */
38 abstract function __construct( $conf );
39
40 /**
41 * Get a load balancer object.
42 *
43 * @param string $wiki Wiki ID, or false for the current wiki
44 * @return LoadBalancer
45 */
46 abstract function getMainLB( $wiki = false );
47
48 /*
49 * Get a load balancer for external storage
50 *
51 * @param string $cluster External storage cluster, or false for core
52 * @param string $wiki Wiki ID, or false for the current wiki
53 */
54 abstract function &getExternalLB( $cluster, $wiki = false );
55
56 /**
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.
60 */
61 abstract function forEachLB( $callback, $params = array() );
62
63 /**
64 * Prepare all load balancers for shutdown
65 * STUB
66 */
67 function shutdown() {}
68
69 /**
70 * Call a method of each load balancer
71 */
72 function forEachLBCallMethod( $methodName, $args = array() ) {
73 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
74 }
75
76 /**
77 * Private helper for forEachLBCallMethod
78 */
79 function callMethod( $loadBalancer, $methodName, $args ) {
80 call_user_func_array( array( $loadBalancer, $methodName ), $args );
81 }
82
83 /**
84 * Commit changes on all master connections
85 */
86 function commitMasterChanges() {
87 $this->forEachLBCallMethod( 'commitMasterChanges' );
88 }
89 }
90
91 /**
92 * A simple single-master LBFactory that gets its configuration from the b/c globals
93 */
94 class LBFactory_Simple extends LBFactory {
95 var $mainLB;
96 var $extLBs = array();
97
98 # Chronology protector
99 var $chronProt;
100
101 function __construct( $conf ) {
102 $this->chronProt = new ChronologyProtector;
103 }
104
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,
112 'user' => $wgDBuser,
113 'password' => $wgDBpassword,
114 'dbname' => $wgDBname,
115 'type' => $wgDBtype,
116 'load' => 1,
117 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
118 ));
119 }
120
121 $this->mainLB = new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
122 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
123 $this->chronProt->initLB( $this->mainLB );
124 }
125 return $this->mainLB;
126 }
127
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\"" );
133 }
134 $this->extLBs[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
135 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
136 }
137 return $this->extLBs[$cluster];
138 }
139
140 /**
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.
144 */
145 function forEachLB( $callback, $params = array() ) {
146 if ( isset( $this->mainLB ) ) {
147 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
148 }
149 foreach ( $this->extLBs as $lb ) {
150 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
151 }
152 }
153
154 function shutdown() {
155 if ( $this->mainLB ) {
156 $this->chronProt->shutdownLB( $this->mainLB );
157 }
158 $this->chronProt->shutdown();
159 $this->commitMasterChanges();
160 }
161 }
162
163 /**
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]].
166 */
167 class ChronologyProtector {
168 var $startupPos;
169 var $shutdownPos = array();
170
171 /**
172 * Initialise a LoadBalancer to give it appropriate chronology protection.
173 *
174 * @param LoadBalancer $lb
175 */
176 function initLB( $lb ) {
177 if ( $this->startupPos === null ) {
178 if ( !empty( $_SESSION[__CLASS__] ) ) {
179 $this->startupPos = $_SESSION[__CLASS__];
180 }
181 }
182 if ( !$this->startupPos ) {
183 return;
184 }
185 $masterName = $lb->getServerName( 0 );
186
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] );
192 }
193 }
194
195 /**
196 * Notify the ChronologyProtector that the LoadBalancer is about to shut
197 * down. Saves replication positions.
198 *
199 * @param LoadBalancer $lb
200 */
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;
209 }
210 }
211 }
212
213 /**
214 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
215 * May commit chronology data to persistent storage.
216 */
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;
222 }
223 }
224 }