90c33b0c08d0bc0b81631c25f8402f5646840e66
[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 $this->chronProt = new ChronologyProtector;
40 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
41 ? $conf['loadMonitorClass']
42 : null;
43 }
44
45 /**
46 * @param bool|string $wiki
47 * @return LoadBalancer
48 */
49 public function newMainLB( $wiki = false ) {
50 global $wgDBservers;
51
52 if ( is_array( $wgDBservers ) ) {
53 $servers = $wgDBservers;
54 foreach ( $servers as $i => &$server ) {
55 if ( $i == 0 ) {
56 $server['master'] = true;
57 } else {
58 $server['slave'] = true;
59 }
60 }
61 } else {
62 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
63 global $wgDBssl, $wgDBcompress;
64
65 $flags = DBO_DEFAULT;
66 if ( $wgDebugDumpSql ) {
67 $flags |= DBO_DEBUG;
68 }
69 if ( $wgDBssl ) {
70 $flags |= DBO_SSL;
71 }
72 if ( $wgDBcompress ) {
73 $flags |= DBO_COMPRESS;
74 }
75
76 $servers = array( array(
77 'host' => $wgDBserver,
78 'user' => $wgDBuser,
79 'password' => $wgDBpassword,
80 'dbname' => $wgDBname,
81 'type' => $wgDBtype,
82 'load' => 1,
83 'flags' => $flags,
84 'master' => true
85 ) );
86 }
87
88 return new LoadBalancer( array(
89 'servers' => $servers,
90 'loadMonitor' => $this->loadMonitorClass
91 ) );
92 }
93
94 /**
95 * @param bool|string $wiki
96 * @return LoadBalancer
97 */
98 public function getMainLB( $wiki = false ) {
99 if ( !isset( $this->mainLB ) ) {
100 $this->mainLB = $this->newMainLB( $wiki );
101 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
102 $this->chronProt->initLB( $this->mainLB );
103 }
104
105 return $this->mainLB;
106 }
107
108 /**
109 * @throws MWException
110 * @param string $cluster
111 * @param bool|string $wiki
112 * @return LoadBalancer
113 */
114 protected function newExternalLB( $cluster, $wiki = false ) {
115 global $wgExternalServers;
116 if ( !isset( $wgExternalServers[$cluster] ) ) {
117 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
118 }
119
120 return new LoadBalancer( array(
121 'servers' => $wgExternalServers[$cluster],
122 'loadMonitor' => $this->loadMonitorClass
123 ) );
124 }
125
126 /**
127 * @param string $cluster
128 * @param bool|string $wiki
129 * @return array
130 */
131 public function &getExternalLB( $cluster, $wiki = false ) {
132 if ( !isset( $this->extLBs[$cluster] ) ) {
133 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
134 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
135 $this->chronProt->initLB( $this->extLBs[$cluster] );
136 }
137
138 return $this->extLBs[$cluster];
139 }
140
141 /**
142 * Execute a function for each tracked load balancer
143 * The callback is called with the load balancer as the first parameter,
144 * and $params passed as the subsequent parameters.
145 *
146 * @param callable $callback
147 * @param array $params
148 */
149 public function forEachLB( $callback, array $params = array() ) {
150 if ( isset( $this->mainLB ) ) {
151 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
152 }
153 foreach ( $this->extLBs as $lb ) {
154 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
155 }
156 }
157
158 public function shutdown() {
159 if ( $this->mainLB ) {
160 $this->chronProt->shutdownLB( $this->mainLB );
161 }
162 foreach ( $this->extLBs as $extLB ) {
163 $this->chronProt->shutdownLB( $extLB );
164 }
165 $this->chronProt->shutdown();
166 $this->commitMasterChanges();
167 }
168 }