353c47a47849ddae85cffb7a3caf85fa9b5d36ea
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactorySimple.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * A simple single-master LBFactory that gets its configuration from the b/c globals
26 */
27 class LBFactorySimple extends LBFactory {
28 /** @var LoadBalancer */
29 private $mainLB;
30 /** @var LoadBalancer[] */
31 private $extLBs = array();
32 /** @var ChronologyProtector */
33 private $chronProt;
34
35 /** @var string */
36 private $loadMonitorClass;
37
38 public function __construct( array $conf ) {
39 parent::__construct( $conf );
40
41 $this->chronProt = new ChronologyProtector;
42 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
43 ? $conf['loadMonitorClass']
44 : null;
45 }
46
47 /**
48 * @param bool|string $wiki
49 * @return LoadBalancer
50 */
51 public function newMainLB( $wiki = false ) {
52 global $wgDBservers;
53
54 if ( is_array( $wgDBservers ) ) {
55 $servers = $wgDBservers;
56 foreach ( $servers as $i => &$server ) {
57 if ( $i == 0 ) {
58 $server['master'] = true;
59 } else {
60 $server['slave'] = true;
61 }
62 }
63 } else {
64 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
65 global $wgDBssl, $wgDBcompress;
66
67 $flags = DBO_DEFAULT;
68 if ( $wgDebugDumpSql ) {
69 $flags |= DBO_DEBUG;
70 }
71 if ( $wgDBssl ) {
72 $flags |= DBO_SSL;
73 }
74 if ( $wgDBcompress ) {
75 $flags |= DBO_COMPRESS;
76 }
77
78 $servers = array( array(
79 'host' => $wgDBserver,
80 'user' => $wgDBuser,
81 'password' => $wgDBpassword,
82 'dbname' => $wgDBname,
83 'type' => $wgDBtype,
84 'load' => 1,
85 'flags' => $flags,
86 'master' => true
87 ) );
88 }
89
90 return new LoadBalancer( array(
91 'servers' => $servers,
92 'loadMonitor' => $this->loadMonitorClass,
93 'readOnlyReason' => $this->readOnlyReason
94 ) );
95 }
96
97 /**
98 * @param bool|string $wiki
99 * @return LoadBalancer
100 */
101 public function getMainLB( $wiki = false ) {
102 if ( !isset( $this->mainLB ) ) {
103 $this->mainLB = $this->newMainLB( $wiki );
104 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
105 $this->chronProt->initLB( $this->mainLB );
106 }
107
108 return $this->mainLB;
109 }
110
111 /**
112 * @throws MWException
113 * @param string $cluster
114 * @param bool|string $wiki
115 * @return LoadBalancer
116 */
117 protected function newExternalLB( $cluster, $wiki = false ) {
118 global $wgExternalServers;
119 if ( !isset( $wgExternalServers[$cluster] ) ) {
120 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
121 }
122
123 return new LoadBalancer( array(
124 'servers' => $wgExternalServers[$cluster],
125 'loadMonitor' => $this->loadMonitorClass,
126 'readOnlyReason' => $this->readOnlyReason
127 ) );
128 }
129
130 /**
131 * @param string $cluster
132 * @param bool|string $wiki
133 * @return array
134 */
135 public function &getExternalLB( $cluster, $wiki = false ) {
136 if ( !isset( $this->extLBs[$cluster] ) ) {
137 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
138 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
139 $this->chronProt->initLB( $this->extLBs[$cluster] );
140 }
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 * @param callable $callback
151 * @param array $params
152 */
153 public function forEachLB( $callback, array $params = array() ) {
154 if ( isset( $this->mainLB ) ) {
155 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
156 }
157 foreach ( $this->extLBs as $lb ) {
158 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
159 }
160 }
161
162 public function shutdown() {
163 if ( $this->mainLB ) {
164 $this->chronProt->shutdownLB( $this->mainLB );
165 }
166 foreach ( $this->extLBs as $extLB ) {
167 $this->chronProt->shutdownLB( $extLB );
168 }
169 $this->chronProt->shutdown();
170 $this->commitMasterChanges();
171 }
172 }