Merge "registration: Vary the loaded queue cache on $wgVersion"
[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
31 /** @var LoadBalancer[] */
32 private $extLBs = array();
33
34 /** @var ChronologyProtector */
35 private $chronProt;
36
37 public function __construct( array $conf ) {
38 $this->chronProt = new ChronologyProtector;
39 }
40
41 /**
42 * @param bool|string $wiki
43 * @return LoadBalancer
44 */
45 public function newMainLB( $wiki = false ) {
46 global $wgDBservers;
47 if ( $wgDBservers ) {
48 $servers = $wgDBservers;
49 } else {
50 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
51 global $wgDBssl, $wgDBcompress;
52
53 $flags = DBO_DEFAULT;
54 if ( $wgDebugDumpSql ) {
55 $flags |= DBO_DEBUG;
56 }
57 if ( $wgDBssl ) {
58 $flags |= DBO_SSL;
59 }
60 if ( $wgDBcompress ) {
61 $flags |= DBO_COMPRESS;
62 }
63
64 $servers = array( array(
65 'host' => $wgDBserver,
66 'user' => $wgDBuser,
67 'password' => $wgDBpassword,
68 'dbname' => $wgDBname,
69 'type' => $wgDBtype,
70 'load' => 1,
71 'flags' => $flags
72 ) );
73 }
74
75 return new LoadBalancer( array(
76 'servers' => $servers,
77 ) );
78 }
79
80 /**
81 * @param bool|string $wiki
82 * @return LoadBalancer
83 */
84 public function getMainLB( $wiki = false ) {
85 if ( !isset( $this->mainLB ) ) {
86 $this->mainLB = $this->newMainLB( $wiki );
87 $this->mainLB->parentInfo( array( 'id' => 'main' ) );
88 $this->chronProt->initLB( $this->mainLB );
89 }
90
91 return $this->mainLB;
92 }
93
94 /**
95 * @throws MWException
96 * @param string $cluster
97 * @param bool|string $wiki
98 * @return LoadBalancer
99 */
100 protected function newExternalLB( $cluster, $wiki = false ) {
101 global $wgExternalServers;
102 if ( !isset( $wgExternalServers[$cluster] ) ) {
103 throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
104 }
105
106 return new LoadBalancer( array(
107 'servers' => $wgExternalServers[$cluster]
108 ) );
109 }
110
111 /**
112 * @param string $cluster
113 * @param bool|string $wiki
114 * @return array
115 */
116 public function &getExternalLB( $cluster, $wiki = false ) {
117 if ( !isset( $this->extLBs[$cluster] ) ) {
118 $this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
119 $this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
120 $this->chronProt->initLB( $this->extLBs[$cluster] );
121 }
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 * @param callable $callback
132 * @param array $params
133 */
134 public function forEachLB( $callback, array $params = array() ) {
135 if ( isset( $this->mainLB ) ) {
136 call_user_func_array( $callback, array_merge( array( $this->mainLB ), $params ) );
137 }
138 foreach ( $this->extLBs as $lb ) {
139 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
140 }
141 }
142
143 public function shutdown() {
144 if ( $this->mainLB ) {
145 $this->chronProt->shutdownLB( $this->mainLB );
146 }
147 foreach ( $this->extLBs as $extLB ) {
148 $this->chronProt->shutdownLB( $extLB );
149 }
150 $this->chronProt->shutdown();
151 $this->commitMasterChanges();
152 }
153 }