Partial revert of r32982; old Title method is fine here
[lhc/web/wiklou.git] / includes / LBFactory.php
1 <?php
2
3 /**
4 * An interface for generating database load balancers
5 */
6 abstract class LBFactory {
7 static $instance;
8
9 /**
10 * Get an LBFactory instance
11 */
12 static function &singleton() {
13 if ( is_null( self::$instance ) ) {
14 global $wgLBFactoryConf;
15 $class = $wgLBFactoryConf['class'];
16 self::$instance = new $class( $wgLBFactoryConf );
17 }
18 return self::$instance;
19 }
20
21 /**
22 * Destory the instance
23 * Actually used by maintenace/parserTests.inc to force to reopen connection
24 * when $wgDBprefix has changed
25 */
26 static function destroy(){
27 self::$instance = null;
28 }
29
30 /**
31 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
32 */
33 abstract function __construct( $conf );
34
35 /**
36 * Get a load balancer object.
37 *
38 * @param string $wiki Wiki ID, or false for the current wiki
39 * @return LoadBalancer
40 */
41 abstract function getMainLB( $wiki = false );
42
43 /*
44 * Get a load balancer for external storage
45 *
46 * @param string $cluster External storage cluster, or false for core
47 * @param string $wiki Wiki ID, or false for the current wiki
48 */
49 abstract function &getExternalLB( $cluster, $wiki = false );
50
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.
55 */
56 abstract function forEachLB( $callback, $params = array() );
57
58 /**
59 * Prepare all load balancers for shutdown
60 * STUB
61 */
62 function shutdown() {}
63
64 /**
65 * Call a method of each load balancer
66 */
67 function forEachLBCallMethod( $methodName, $args = array() ) {
68 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
69 }
70
71 /**
72 * Private helper for forEachLBCallMethod
73 */
74 function callMethod( $loadBalancer, $methodName, $args ) {
75 call_user_func_array( array( $loadBalancer, $methodName ), $args );
76 }
77
78 /**
79 * Commit changes on all master connections
80 */
81 function commitMasterChanges() {
82 $this->forEachLBCallMethod( 'commitMasterChanges' );
83 }
84 }
85
86 /**
87 * A simple single-master LBFactory that gets its configuration from the b/c globals
88 */
89 class LBFactory_Simple extends LBFactory {
90 var $mainLB;
91 var $extLBs = array();
92
93 # Chronology protector
94 var $chronProt;
95
96 function __construct( $conf ) {
97 $this->chronProt = new ChronologyProtector;
98 }
99
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
113 ));
114 }
115
116 $this->mainLB = new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
117 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
118 $this->chronProt->initLB( $this->mainLB );
119 }
120 return $this->mainLB;
121 }
122
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\"" );
128 }
129 $this->extLBs[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
130 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
131 }
132 return $this->extLBs[$cluster];
133 }
134
135 /**
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.
139 */
140 function forEachLB( $callback, $params = array() ) {
141 if ( isset( $this->mainLB ) ) {
142 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
143 }
144 foreach ( $this->extLBs as $lb ) {
145 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
146 }
147 }
148
149 function shutdown() {
150 if ( $this->mainLB ) {
151 $this->chronProt->shutdownLB( $this->mainLB );
152 }
153 $this->chronProt->shutdown();
154 $this->commitMasterChanges();
155 }
156 }
157
158 /**
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]].
161 */
162 class ChronologyProtector {
163 var $startupPos;
164 var $shutdownPos = array();
165
166 /**
167 * Initialise a LoadBalancer to give it appropriate chronology protection.
168 *
169 * @param LoadBalancer $lb
170 */
171 function initLB( $lb ) {
172 if ( $this->startupPos === null ) {
173 if ( !empty( $_SESSION[__CLASS__] ) ) {
174 $this->startupPos = $_SESSION[__CLASS__];
175 }
176 }
177 if ( !$this->startupPos ) {
178 return;
179 }
180 $masterName = $lb->getServerName( 0 );
181
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] );
187 }
188 }
189
190 /**
191 * Notify the ChronologyProtector that the LoadBalancer is about to shut
192 * down. Saves replication positions.
193 *
194 * @param LoadBalancer $lb
195 */
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;
204 }
205 }
206 }
207
208 /**
209 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
210 * May commit chronology data to persistent storage.
211 */
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;
217 }
218 }
219 }
220