Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactorySimple.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 namespace Wikimedia\Rdbms;
25
26 use InvalidArgumentException;
27
28 /**
29 * A simple single-master LBFactory that gets its configuration from the b/c globals
30 */
31 class LBFactorySimple extends LBFactory {
32 /** @var LoadBalancer */
33 private $mainLB;
34 /** @var LoadBalancer[] */
35 private $externalLBs = [];
36
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 = [];
41
42 /** @var string */
43 private $loadMonitorClass;
44
45 /**
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.
56 */
57 public function __construct( array $conf ) {
58 parent::__construct( $conf );
59
60 $this->mainServers = $conf['servers'] ?? [];
61 foreach ( $this->mainServers as $i => $server ) {
62 if ( $i == 0 ) {
63 $this->mainServers[$i]['master'] = true;
64 } else {
65 $this->mainServers[$i]['replica'] = true;
66 }
67 }
68
69 foreach ( ( $conf['externalClusters'] ?? [] ) as $cluster => $servers ) {
70 foreach ( $servers as $index => $server ) {
71 $this->externalServersByCluster[$cluster][$index] = $server;
72 }
73 }
74
75 $this->loadMonitorClass = $conf['loadMonitorClass'] ?? LoadMonitor::class;
76 }
77
78 public function newMainLB( $domain = false, $owner = null ) {
79 return $this->newLoadBalancer( $this->mainServers, $owner );
80 }
81
82 public function getMainLB( $domain = false ) {
83 if ( $this->mainLB === null ) {
84 $this->mainLB = $this->newMainLB( $domain, $this->getOwnershipId() );
85 }
86
87 return $this->mainLB;
88 }
89
90 public function newExternalLB( $cluster, $owner = null ) {
91 if ( !isset( $this->externalServersByCluster[$cluster] ) ) {
92 throw new InvalidArgumentException( "Unknown cluster '$cluster'." );
93 }
94
95 return $this->newLoadBalancer( $this->externalServersByCluster[$cluster], $owner );
96 }
97
98 public function getExternalLB( $cluster ) {
99 if ( !isset( $this->externalLBs[$cluster] ) ) {
100 $this->externalLBs[$cluster] = $this->newExternalLB( $cluster, $this->getOwnershipId() );
101 }
102
103 return $this->externalLBs[$cluster];
104 }
105
106 public function getAllMainLBs() {
107 return [ self::CLUSTER_MAIN_DEFAULT => $this->getMainLB() ];
108 }
109
110 public function getAllExternalLBs() {
111 $lbs = [];
112 foreach ( array_keys( $this->externalServersByCluster ) as $cluster ) {
113 $lbs[$cluster] = $this->getExternalLB( $cluster );
114 }
115
116 return $lbs;
117 }
118
119 private function newLoadBalancer( array $servers, $owner ) {
120 $lb = new LoadBalancer( array_merge(
121 $this->baseLoadBalancerParams( $owner ),
122 [
123 'servers' => $servers,
124 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
125 ]
126 ) );
127 $this->initLoadBalancer( $lb );
128
129 return $lb;
130 }
131
132 public function forEachLB( $callback, array $params = [] ) {
133 if ( $this->mainLB !== null ) {
134 $callback( $this->mainLB, ...$params );
135 }
136 foreach ( $this->externalLBs as $lb ) {
137 $callback( $lb, ...$params );
138 }
139 }
140 }