* Split off DB load monitoring logic into a LoadMonitor class hierarchy, to allow...
[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( array(
122 'servers' => $wgDBservers,
123 'masterWaitTimeout' => $wgMasterWaitTimeout
124 ));
125 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
126 $this->chronProt->initLB( $this->mainLB );
127 }
128 return $this->mainLB;
129 }
130
131 function &getExternalLB( $cluster, $wiki = false ) {
132 global $wgExternalServers;
133 if ( !isset( $this->extLBs[$cluster] ) ) {
134 if ( !isset( $wgExternalServers[$cluster] ) ) {
135 throw new MWException( __METHOD__.": Unknown cluster \"$cluster\"" );
136 }
137 $this->extLBs[$cluster] = new LoadBalancer( array(
138 'servers' => $wgExternalServers[$cluster]
139 ));
140 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
141 }
142 return $this->extLBs[$cluster];
143 }
144
145 /**
146 * Execute a function for each tracked load balancer
147 * The callback is called with the load balancer as the first parameter,
148 * and $params passed as the subsequent parameters.
149 */
150 function forEachLB( $callback, $params = array() ) {
151 if ( isset( $this->mainLB ) ) {
152 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
153 }
154 foreach ( $this->extLBs as $lb ) {
155 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
156 }
157 }
158
159 function shutdown() {
160 if ( $this->mainLB ) {
161 $this->chronProt->shutdownLB( $this->mainLB );
162 }
163 $this->chronProt->shutdown();
164 $this->commitMasterChanges();
165 }
166 }
167
168 /**
169 * Class for ensuring a consistent ordering of events as seen by the user, despite replication.
170 * Kind of like Hawking's [[Chronology Protection Agency]].
171 */
172 class ChronologyProtector {
173 var $startupPos;
174 var $shutdownPos = array();
175
176 /**
177 * Initialise a LoadBalancer to give it appropriate chronology protection.
178 *
179 * @param LoadBalancer $lb
180 */
181 function initLB( $lb ) {
182 if ( $this->startupPos === null ) {
183 if ( !empty( $_SESSION[__CLASS__] ) ) {
184 $this->startupPos = $_SESSION[__CLASS__];
185 }
186 }
187 if ( !$this->startupPos ) {
188 return;
189 }
190 $masterName = $lb->getServerName( 0 );
191
192 if ( $lb->getServerCount() > 1 && !empty( $this->startupPos[$masterName] ) ) {
193 $info = $lb->parentInfo();
194 $pos = $this->startupPos[$masterName];
195 wfDebug( __METHOD__.": LB " . $info['id'] . " waiting for master pos $pos\n" );
196 $lb->waitFor( $this->startupPos[$masterName] );
197 }
198 }
199
200 /**
201 * Notify the ChronologyProtector that the LoadBalancer is about to shut
202 * down. Saves replication positions.
203 *
204 * @param LoadBalancer $lb
205 */
206 function shutdownLB( $lb ) {
207 if ( session_id() != '' && $lb->getServerCount() > 1 ) {
208 $masterName = $lb->getServerName( 0 );
209 if ( !isset( $this->shutdownPos[$masterName] ) ) {
210 $pos = $lb->getMasterPos();
211 $info = $lb->parentInfo();
212 wfDebug( __METHOD__.": LB " . $info['id'] . " has master pos $pos\n" );
213 $this->shutdownPos[$masterName] = $pos;
214 }
215 }
216 }
217
218 /**
219 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
220 * May commit chronology data to persistent storage.
221 */
222 function shutdown() {
223 if ( session_id() != '' && count( $this->shutdownPos ) ) {
224 wfDebug( __METHOD__.": saving master pos for " .
225 count( $this->shutdownPos ) . " master(s)\n" );
226 $_SESSION[__CLASS__] = $this->shutdownPos;
227 }
228 }
229 }