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 */
31 /** @var LoadBalancer[] */
32 private $extLBs = array();
34 /** @var ChronologyProtector */
37 public function __construct( array $conf ) {
38 $this->chronProt
= new ChronologyProtector
;
42 * @param bool|string $wiki
43 * @return LoadBalancer
45 public function newMainLB( $wiki = false ) {
48 $servers = $wgDBservers;
50 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
51 global $wgDBssl, $wgDBcompress;
54 if ( $wgDebugDumpSql ) {
60 if ( $wgDBcompress ) {
61 $flags |
= DBO_COMPRESS
;
64 $servers = array( array(
65 'host' => $wgDBserver,
67 'password' => $wgDBpassword,
68 'dbname' => $wgDBname,
75 return new LoadBalancer( array(
76 'servers' => $servers,
81 * @param bool|string $wiki
82 * @return LoadBalancer
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
);
96 * @param string $cluster
97 * @param bool|string $wiki
98 * @return LoadBalancer
100 protected function newExternalLB( $cluster, $wiki = false ) {
101 global $wgExternalServers;
102 if ( !isset( $wgExternalServers[$cluster] ) ) {
103 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
106 return new LoadBalancer( array(
107 'servers' => $wgExternalServers[$cluster]
112 * @param string $cluster
113 * @param bool|string $wiki
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] );
123 return $this->extLBs
[$cluster];
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.
131 * @param callable $callback
132 * @param array $params
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 ) );
138 foreach ( $this->extLBs
as $lb ) {
139 call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
143 public function shutdown() {
144 if ( $this->mainLB
) {
145 $this->chronProt
->shutdownLB( $this->mainLB
);
147 foreach ( $this->extLBs
as $extLB ) {
148 $this->chronProt
->shutdownLB( $extLB );
150 $this->chronProt
->shutdown();
151 $this->commitMasterChanges();