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
97 * @param bool|string $wiki
98 * @return LoadBalancer
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
);
107 return $this->mainLB
;
111 * @throws MWException
112 * @param string $cluster
113 * @param bool|string $wiki
114 * @return LoadBalancer
116 protected function newExternalLB( $cluster, $wiki = false ) {
117 global $wgExternalServers;
118 if ( !isset( $wgExternalServers[$cluster] ) ) {
119 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
122 return new LoadBalancer( array(
123 'servers' => $wgExternalServers[$cluster],
124 'loadMonitor' => $this->loadMonitorClass
129 * @param string $cluster
130 * @param bool|string $wiki
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] );
140 return $this->extLBs
[$cluster];
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.
148 * @param callable $callback
149 * @param array $params
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 ) );
155 foreach ( $this->extLBs
as $lb ) {
156 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
160 public function shutdown() {
161 if ( $this->mainLB
) {
162 $this->chronProt
->shutdownLB( $this->mainLB
);
164 foreach ( $this->extLBs
as $extLB ) {
165 $this->chronProt
->shutdownLB( $extLB );
167 $this->chronProt
->shutdown();
168 $this->commitMasterChanges();