Some fixes to the new load balancement system:
[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 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
23 */
24 abstract function __construct( $conf );
25
26 /**
27 * Get a load balancer object.
28 *
29 * @param string $wiki Wiki ID, or false for the current wiki
30 * @return LoadBalancer
31 */
32 abstract function getMainLB( $wiki = false );
33
34 /*
35 * Get a load balancer for external storage
36 *
37 * @param string $cluster External storage cluster, or false for core
38 * @param string $wiki Wiki ID, or false for the current wiki
39 */
40 abstract function &getExternalLB( $cluster, $wiki = false );
41
42 /**
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.
46 */
47 abstract function forEachLB( $callback, $params = array() );
48
49 /**
50 * Prepare all load balancers for shutdown
51 * STUB
52 */
53 function shutdown() {}
54
55 /**
56 * Call a method of each load balancer
57 */
58 function forEachLBCallMethod( $methodName, $args = array() ) {
59 $this->forEachLB( array( $this, 'callMethod' ), array( $methodName, $args ) );
60 }
61
62 /**
63 * Private helper for forEachLBCallMethod
64 */
65 function callMethod( $loadBalancer, $methodName, $args ) {
66 call_user_func_array( array( $loadBalancer, $methodName ), $args );
67 }
68
69 /**
70 * Commit changes on all master connections
71 */
72 function commitMasterChanges() {
73 $this->forEachLBCallMethod( 'commitMasterChanges' );
74 }
75 }
76
77 /**
78 * A simple single-master LBFactory that gets its configuration from the b/c globals
79 */
80 class LBFactory_Simple extends LBFactory {
81 var $mainLB;
82 var $extLBs = array();
83
84 # Chronology protector
85 var $chronProt;
86
87 function __construct( $conf ) {
88 $this->chronProt = new ChronologyProtector;
89 }
90
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,
98 'user' => $wgDBuser,
99 'password' => $wgDBpassword,
100 'dbname' => $wgDBname,
101 'type' => $wgDBtype,
102 'load' => 1,
103 'flags' => ($wgDebugDumpSql ? DBO_DEBUG : 0) | DBO_DEFAULT
104 ));
105 }
106
107 $this->mainLB = new LoadBalancer( $wgDBservers, false, $wgMasterWaitTimeout, true );
108 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
109 $this->chronProt->initLB( $this->mainLB );
110 }
111 return $this->mainLB;
112 }
113
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\"" );
119 }
120 $this->extLBs[$cluster] = new LoadBalancer( $wgExternalServers[$cluster] );
121 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
122 }
123 return $this->extLBs[$cluster];
124 }
125
126 /**
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.
130 */
131 function forEachLB( $callback, $params = array() ) {
132 if ( isset( $this->mainLB ) ) {
133 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
134 }
135 foreach ( $this->extLBs as $lb ) {
136 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
137 }
138 }
139
140 function shutdown() {
141 if ( $this->mainLB ) {
142 $this->chronProt->shutdownLB( $this->mainLB );
143 }
144 $this->chronProt->shutdown();
145 $this->commitMasterChanges();
146 }
147 }
148
149 /**
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]].
152 */
153 class ChronologyProtector {
154 var $startupPos;
155 var $shutdownPos = array();
156
157 /**
158 * Initialise a LoadBalancer to give it appropriate chronology protection.
159 *
160 * @param LoadBalancer $lb
161 */
162 function initLB( $lb ) {
163 if ( $this->startupPos === null ) {
164 if ( !empty( $_SESSION[__CLASS__] ) ) {
165 $this->startupPos = $_SESSION[__CLASS__];
166 }
167 }
168 if ( !$this->startupPos ) {
169 return;
170 }
171 $masterName = $lb->getServerName( 0 );
172
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] );
178 }
179 }
180
181 /**
182 * Notify the ChronologyProtector that the LoadBalancer is about to shut
183 * down. Saves replication positions.
184 *
185 * @param LoadBalancer $lb
186 */
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;
195 }
196 }
197 }
198
199 /**
200 * Notify the ChronologyProtector that the LBFactory is done calling shutdownLB() for now.
201 * May commit chronology data to persistent storage.
202 */
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;
208 }
209 }
210 }
211