7e73e5bdcf6dafd61c49a4708082bba647be0fac
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[] */
35 private $externalLBs = [];
37 /** @var array[] Map of (server index => server config map) */
38 private $mainServers = [];
39 /** @var array[][] Map of (cluster => server index => server config map) */
40 private $externalServersByCluster = [];
43 private $loadMonitorClass;
46 * @see LBFactory::__construct()
47 * @param array $conf Additional parameters include:
48 * - servers : list of server config 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->mainServers
= $conf['servers'] ??
[];
61 foreach ( $this->mainServers
as $i => $server ) {
63 $this->mainServers
[$i]['master'] = true;
65 $this->mainServers
[$i]['replica'] = true;
69 foreach ( ( $conf['externalClusters'] ??
[] ) as $cluster => $servers ) {
70 foreach ( $servers as $index => $server ) {
71 $this->externalServersByCluster
[$cluster][$index] = $server;
75 $this->loadMonitorClass
= $conf['loadMonitorClass'] ?? LoadMonitor
::class;
78 public function newMainLB( $domain = false ) {
79 return $this->newLoadBalancer( $this->mainServers
);
82 public function getMainLB( $domain = false ) {
83 if ( $this->mainLB
=== null ) {
84 $this->mainLB
= $this->newMainLB( $domain );
90 public function newExternalLB( $cluster ) {
91 if ( !isset( $this->externalServersByCluster
[$cluster] ) ) {
92 throw new InvalidArgumentException( "Unknown cluster '$cluster'." );
95 return $this->newLoadBalancer( $this->externalServersByCluster
[$cluster] );
98 public function getExternalLB( $cluster ) {
99 if ( !isset( $this->externalLBs
[$cluster] ) ) {
100 $this->externalLBs
[$cluster] = $this->newExternalLB( $cluster );
103 return $this->externalLBs
[$cluster];
106 public function getAllMainLBs() {
107 return [ self
::CLUSTER_MAIN_DEFAULT
=> $this->getMainLB() ];
110 public function getAllExternalLBs() {
112 foreach ( array_keys( $this->externalServersByCluster
) as $cluster ) {
113 $lbs[$cluster] = $this->getExternalLB( $cluster );
119 private function newLoadBalancer( array $servers ) {
120 $lb = new LoadBalancer( array_merge(
121 $this->baseLoadBalancerParams(),
123 'servers' => $servers,
124 'loadMonitor' => [ 'class' => $this->loadMonitorClass
],
127 $this->initLoadBalancer( $lb );
132 public function forEachLB( $callback, array $params = [] ) {
133 if ( $this->mainLB
!== null ) {
134 $callback( $this->mainLB
, ...$params );
136 foreach ( $this->externalLBs
as $lb ) {
137 $callback( $lb, ...$params );