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['replica'] = true;
59 $server +
= [ 'flags' => DBO_DEFAULT
];
62 global $wgDBserver, $wgDBuser, $wgDBpassword, $wgDBname, $wgDBtype, $wgDebugDumpSql;
63 global $wgDBssl, $wgDBcompress;
66 if ( $wgDebugDumpSql ) {
72 if ( $wgDBcompress ) {
73 $flags |
= DBO_COMPRESS
;
77 'host' => $wgDBserver,
79 'password' => $wgDBpassword,
80 'dbname' => $wgDBname,
88 return $this->newLoadBalancer( $servers );
92 * @param bool|string $wiki
93 * @return LoadBalancer
95 public function getMainLB( $wiki = false ) {
96 if ( !isset( $this->mainLB
) ) {
97 $this->mainLB
= $this->newMainLB( $wiki );
98 $this->chronProt
->initLB( $this->mainLB
);
101 return $this->mainLB
;
105 * @throws MWException
106 * @param string $cluster
107 * @param bool|string $wiki
108 * @return LoadBalancer
110 protected function newExternalLB( $cluster, $wiki = false ) {
111 global $wgExternalServers;
112 if ( !isset( $wgExternalServers[$cluster] ) ) {
113 throw new MWException( __METHOD__
. ": Unknown cluster \"$cluster\"" );
116 return $this->newLoadBalancer( $wgExternalServers[$cluster] );
120 * @param string $cluster
121 * @param bool|string $wiki
124 public function getExternalLB( $cluster, $wiki = false ) {
125 if ( !isset( $this->extLBs
[$cluster] ) ) {
126 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster, $wiki );
127 $this->chronProt
->initLB( $this->extLBs
[$cluster] );
130 return $this->extLBs
[$cluster];
133 private function newLoadBalancer( array $servers ) {
134 $lb = new LoadBalancer( [
135 'servers' => $servers,
136 'loadMonitor' => $this->loadMonitorClass
,
137 'readOnlyReason' => $this->readOnlyReason
,
138 'trxProfiler' => $this->trxProfiler
,
139 'srvCache' => $this->srvCache
,
140 'wanCache' => $this->wanCache
143 $this->initLoadBalancer( $lb );
149 * Execute a function for each tracked load balancer
150 * The callback is called with the load balancer as the first parameter,
151 * and $params passed as the subsequent parameters.
153 * @param callable $callback
154 * @param array $params
156 public function forEachLB( $callback, array $params = [] ) {
157 if ( isset( $this->mainLB
) ) {
158 call_user_func_array( $callback, array_merge( [ $this->mainLB
], $params ) );
160 foreach ( $this->extLBs
as $lb ) {
161 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );