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[] */
34 private $loadMonitorClass;
36 public function __construct( array $conf ) {
37 parent
::__construct( $conf );
39 $this->loadMonitorClass
= isset( $conf['loadMonitorClass'] )
40 ?
$conf['loadMonitorClass']
45 * @param bool|string $wiki
46 * @return LoadBalancer
48 public function newMainLB( $wiki = false ) {
51 if ( is_array( $wgDBservers ) ) {
52 $servers = $wgDBservers;
53 foreach ( $servers as $i => &$server ) {
55 $server['master'] = true;
57 $server['slave'] = true;
61 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
62 global $wgDBssl, $wgDBcompress;
65 if ( $wgDebugDumpSql ) {
71 if ( $wgDBcompress ) {
72 $flags |
= DBO_COMPRESS
;
76 'host' => $wgDBserver,
78 'password' => $wgDBpassword,
79 'dbname' => $wgDBname,
87 return new LoadBalancer( [
88 'servers' => $servers,
89 'loadMonitor' => $this->loadMonitorClass
,
90 'readOnlyReason' => $this->readOnlyReason
,
91 'trxProfiler' => $this->trxProfiler
96 * @param bool|string $wiki
97 * @return LoadBalancer
99 public function getMainLB( $wiki = false ) {
100 if ( !isset( $this->mainLB
) ) {
101 $this->mainLB
= $this->newMainLB( $wiki );
102 $this->mainLB
->parentInfo( [ 'id' => 'main' ] );
103 $this->chronProt
->initLB( $this->mainLB
);
106 return $this->mainLB
;
110 * @throws MWException
111 * @param string $cluster
112 * @param bool|string $wiki
113 * @return LoadBalancer
115 protected function newExternalLB( $cluster, $wiki = false ) {
116 global $wgExternalServers;
117 if ( !isset( $wgExternalServers[$cluster] ) ) {
118 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
121 return new LoadBalancer( [
122 'servers' => $wgExternalServers[$cluster],
123 'loadMonitor' => $this->loadMonitorClass
,
124 'readOnlyReason' => $this->readOnlyReason
,
125 'trxProfiler' => $this->trxProfiler
130 * @param string $cluster
131 * @param bool|string $wiki
134 public function &getExternalLB( $cluster, $wiki = false ) {
135 if ( !isset( $this->extLBs
[$cluster] ) ) {
136 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
137 $this->extLBs
[$cluster]->parentInfo( [ 'id' => "ext-$cluster" ] );
138 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
141 return $this->extLBs
[$cluster];
145 * Execute a function for each tracked load balancer
146 * The callback is called with the load balancer as the first parameter,
147 * and $params passed as the subsequent parameters.
149 * @param callable $callback
150 * @param array $params
152 public function forEachLB( $callback, array $params = [] ) {
153 if ( isset( $this->mainLB
) ) {
154 call_user_func_array( $callback, array_merge( [ $this->mainLB
], $params ) );
156 foreach ( $this->extLBs
as $lb ) {
157 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
161 public function shutdown( $flags = 0 ) {
162 if ( !( $flags & self
::SHUTDOWN_NO_CHRONPROT
) ) {
163 $this->shutdownChronologyProtector( $this->chronProt
);
165 $this->commitMasterChanges( __METHOD__
); // sanity