3 * Generator of database load balancing objects.
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.
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.
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
25 * A simple single-master LBFactory that gets its configuration from the b/c globals
27 class LBFactorySimple
extends LBFactory
{
28 /** @var LoadBalancer */
30 /** @var LoadBalancer[] */
31 private $extLBs = array();
32 /** @var ChronologyProtector */
36 private $loadMonitorClass;
38 public function __construct( array $conf ) {
39 parent
::__construct( $conf );
41 $this->chronProt
= new ChronologyProtector
;
42 $this->loadMonitorClass
= isset( $conf['loadMonitorClass'] )
43 ?
$conf['loadMonitorClass']
48 * @param bool|string $wiki
49 * @return LoadBalancer
51 public function newMainLB( $wiki = false ) {
54 if ( is_array( $wgDBservers ) ) {
55 $servers = $wgDBservers;
56 foreach ( $servers as $i => &$server ) {
58 $server['master'] = true;
60 $server['slave'] = true;
64 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
65 global $wgDBssl, $wgDBcompress;
68 if ( $wgDebugDumpSql ) {
74 if ( $wgDBcompress ) {
75 $flags |
= DBO_COMPRESS
;
78 $servers = array( array(
79 'host' => $wgDBserver,
81 'password' => $wgDBpassword,
82 'dbname' => $wgDBname,
90 return new LoadBalancer( array(
91 'servers' => $servers,
92 'loadMonitor' => $this->loadMonitorClass
,
93 'readOnlyReason' => $this->readOnlyReason
98 * @param bool|string $wiki
99 * @return LoadBalancer
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
);
108 return $this->mainLB
;
112 * @throws MWException
113 * @param string $cluster
114 * @param bool|string $wiki
115 * @return LoadBalancer
117 protected function newExternalLB( $cluster, $wiki = false ) {
118 global $wgExternalServers;
119 if ( !isset( $wgExternalServers[$cluster] ) ) {
120 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
123 return new LoadBalancer( array(
124 'servers' => $wgExternalServers[$cluster],
125 'loadMonitor' => $this->loadMonitorClass
,
126 'readOnlyReason' => $this->readOnlyReason
131 * @param string $cluster
132 * @param bool|string $wiki
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] );
142 return $this->extLBs
[$cluster];
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.
150 * @param callable $callback
151 * @param array $params
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 ) );
157 foreach ( $this->extLBs
as $lb ) {
158 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
162 public function shutdown() {
163 if ( $this->mainLB
) {
164 $this->chronProt
->shutdownLB( $this->mainLB
);
166 foreach ( $this->extLBs
as $extLB ) {
167 $this->chronProt
->shutdownLB( $extLB );
169 $this->chronProt
->shutdown();
170 $this->commitMasterChanges();