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
24 namespace Wikimedia\Rdbms
;
26 use InvalidArgumentException
;
29 * A simple single-master LBFactory that gets its configuration from the b/c globals
31 class LBFactorySimple
extends LBFactory
{
32 /** @var LoadBalancer */
34 /** @var LoadBalancer[] */
37 /** @var array[] Map of (server index => server config) */
38 private $servers = [];
39 /** @var array[] Map of (cluster => (server index => server config)) */
40 private $externalClusters = [];
43 private $loadMonitorClass;
46 * @see LBFactory::__construct()
47 * @param array $conf Parameters of LBFactory::__construct() as well as:
48 * - servers : list of server configuration maps to Database::factory().
49 * Additionally, the server maps should have a 'load' key, which is used to decide
50 * how often clients connect to one server verses the others. A 'max lag' key should
51 * also be set on server maps, indicating how stale the data can be before the load
52 * balancer tries to avoid using it. The map can have 'is static' set to disable blocking
53 * replication sync checks (intended for archive servers with unchanging data).
54 * - externalClusters : map of cluster names to server arrays. The servers arrays have the
55 * same format as "servers" above.
57 public function __construct( array $conf ) {
58 parent
::__construct( $conf );
60 $this->servers
= isset( $conf['servers'] ) ?
$conf['servers'] : [];
61 foreach ( $this->servers
as $i => $server ) {
63 $this->servers
[$i]['master'] = true;
65 $this->servers
[$i]['replica'] = true;
69 $this->externalClusters
= isset( $conf['externalClusters'] )
70 ?
$conf['externalClusters']
72 $this->loadMonitorClass
= isset( $conf['loadMonitorClass'] )
73 ?
$conf['loadMonitorClass']
78 * @param bool|string $domain
79 * @return LoadBalancer
81 public function newMainLB( $domain = false ) {
82 return $this->newLoadBalancer( $this->servers
);
86 * @param bool|string $domain
87 * @return LoadBalancer
89 public function getMainLB( $domain = false ) {
90 if ( !isset( $this->mainLB
) ) {
91 $this->mainLB
= $this->newMainLB( $domain );
92 $this->getChronologyProtector()->initLB( $this->mainLB
);
98 public function newExternalLB( $cluster ) {
99 if ( !isset( $this->externalClusters
[$cluster] ) ) {
100 throw new InvalidArgumentException( __METHOD__
. ": Unknown cluster \"$cluster\"." );
103 return $this->newLoadBalancer( $this->externalClusters
[$cluster] );
106 public function getExternalLB( $cluster ) {
107 if ( !isset( $this->extLBs
[$cluster] ) ) {
108 $this->extLBs
[$cluster] = $this->newExternalLB( $cluster );
109 $this->getChronologyProtector()->initLB( $this->extLBs
[$cluster] );
112 return $this->extLBs
[$cluster];
115 public function getAllMainLBs() {
116 return [ 'DEFAULT' => $this->getMainLB() ];
119 public function getAllExternalLBs() {
121 foreach ( $this->externalClusters
as $cluster => $unused ) {
122 $lbs[$cluster] = $this->getExternalLB( $cluster );
128 private function newLoadBalancer( array $servers ) {
129 $lb = new LoadBalancer( array_merge(
130 $this->baseLoadBalancerParams(),
132 'servers' => $servers,
133 'loadMonitor' => [ 'class' => $this->loadMonitorClass
],
136 $this->initLoadBalancer( $lb );
142 * Execute a function for each tracked load balancer
143 * The callback is called with the load balancer as the first parameter,
144 * and $params passed as the subsequent parameters.
146 * @param callable $callback
147 * @param array $params
149 public function forEachLB( $callback, array $params = [] ) {
150 if ( isset( $this->mainLB
) ) {
151 call_user_func_array( $callback, array_merge( [ $this->mainLB
], $params ) );
153 foreach ( $this->extLBs
as $lb ) {
154 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );