Merge "Handle edge case in WikiPage::lock()"
[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 ) );
94 }
95
96 /**
97 * @param bool|string $wiki
98 * @return LoadBalancer
99 */
100 public function getMainLB( $wiki = false ) {
101 if ( !isset( $this->mainLB ) ) {
102 $this->mainLB = $this->newMainLB( $wiki );
103 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
104 $this->chronProt->initLB( $this->mainLB );
105 }
106
107 return $this->mainLB;
108 }
109
110 /**
111 * @throws MWException
112 * @param string $cluster
113 * @param bool|string $wiki
114 * @return LoadBalancer
115 */
116 protected function newExternalLB( $cluster, $wiki = false ) {
117 global $wgExternalServers;
118 if ( !isset( $wgExternalServers[$cluster] ) ) {
119 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
120 }
121
122 return new LoadBalancer( array(
123 'servers' => $wgExternalServers[$cluster],
124 'loadMonitor' => $this->loadMonitorClass
125 ) );
126 }
127
128 /**
129 * @param string $cluster
130 * @param bool|string $wiki
131 * @return array
132 */
133 public function &getExternalLB( $cluster, $wiki = false ) {
134 if ( !isset( $this->extLBs[$cluster] ) ) {
135 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
136 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
137 $this->chronProt->initLB( $this->extLBs[$cluster] );
138 }
139
140 return $this->extLBs[$cluster];
141 }
142
143 /**
144 * Execute a function for each tracked load balancer
145 * The callback is called with the load balancer as the first parameter,
146 * and $params passed as the subsequent parameters.
147 *
148 * @param callable $callback
149 * @param array $params
150 */
151 public function forEachLB( $callback, array $params = array() ) {
152 if ( isset( $this->mainLB ) ) {
153 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
154 }
155 foreach ( $this->extLBs as $lb ) {
156 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
157 }
158 }
159
160 public function shutdown() {
161 if ( $this->mainLB ) {
162 $this->chronProt->shutdownLB( $this->mainLB );
163 }
164 foreach ( $this->extLBs as $extLB ) {
165 $this->chronProt->shutdownLB( $extLB );
166 }
167 $this->chronProt->shutdown();
168 $this->commitMasterChanges();
169 }
170 }