Move LBFactory to Rdbms namespace
[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 LoadBalancer;
27 use InvalidArgumentException;
28
29 /**
30 * A simple single-master LBFactory that gets its configuration from the b/c globals
31 */
32 class LBFactorySimple extends LBFactory {
33 /** @var LoadBalancer */
34 private $mainLB;
35 /** @var LoadBalancer[] */
36 private $extLBs = [];
37
38 /** @var array[] Map of (server index => server config) */
39 private $servers = [];
40 /** @var array[] Map of (cluster => (server index => server config)) */
41 private $externalClusters = [];
42
43 /** @var string */
44 private $loadMonitorClass;
45
46 /**
47 * @see LBFactory::__construct()
48 * @param array $conf Parameters of LBFactory::__construct() as well as:
49 * - servers : list of server configuration maps to Database::factory().
50 * Additionally, the server maps should have a 'load' key, which is used to decide
51 * how often clients connect to one server verses the others. A 'max lag' key should
52 * also be set on server maps, indicating how stale the data can be before the load
53 * balancer tries to avoid using it. The map can have 'is static' set to disable blocking
54 * replication sync checks (intended for archive servers with unchanging data).
55 * - externalClusters : map of cluster names to server arrays. The servers arrays have the
56 * same format as "servers" above.
57 */
58 public function __construct( array $conf ) {
59 parent::__construct( $conf );
60
61 $this->servers = isset( $conf['servers'] ) ? $conf['servers'] : [];
62 foreach ( $this->servers as $i => $server ) {
63 if ( $i == 0 ) {
64 $this->servers[$i]['master'] = true;
65 } else {
66 $this->servers[$i]['replica'] = true;
67 }
68 }
69
70 $this->externalClusters = isset( $conf['externalClusters'] )
71 ? $conf['externalClusters']
72 : [];
73 $this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
74 ? $conf['loadMonitorClass']
75 : 'LoadMonitor';
76 }
77
78 /**
79 * @param bool|string $domain
80 * @return LoadBalancer
81 */
82 public function newMainLB( $domain = false ) {
83 return $this->newLoadBalancer( $this->servers );
84 }
85
86 /**
87 * @param bool|string $domain
88 * @return LoadBalancer
89 */
90 public function getMainLB( $domain = false ) {
91 if ( !isset( $this->mainLB ) ) {
92 $this->mainLB = $this->newMainLB( $domain );
93 $this->getChronologyProtector()->initLB( $this->mainLB );
94 }
95
96 return $this->mainLB;
97 }
98
99 public function newExternalLB( $cluster ) {
100 if ( !isset( $this->externalClusters[$cluster] ) ) {
101 throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
102 }
103
104 return $this->newLoadBalancer( $this->externalClusters[$cluster] );
105 }
106
107 public function getExternalLB( $cluster ) {
108 if ( !isset( $this->extLBs[$cluster] ) ) {
109 $this->extLBs[$cluster] = $this->newExternalLB( $cluster );
110 $this->getChronologyProtector()->initLB( $this->extLBs[$cluster] );
111 }
112
113 return $this->extLBs[$cluster];
114 }
115
116 public function getAllMainLBs() {
117 return [ 'DEFAULT' => $this->getMainLB() ];
118 }
119
120 public function getAllExternalLBs() {
121 $lbs = [];
122 foreach ( $this->externalClusters as $cluster => $unused ) {
123 $lbs[$cluster] = $this->getExternalLB( $cluster );
124 }
125
126 return $lbs;
127 }
128
129 private function newLoadBalancer( array $servers ) {
130 $lb = new LoadBalancer( array_merge(
131 $this->baseLoadBalancerParams(),
132 [
133 'servers' => $servers,
134 'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
135 ]
136 ) );
137 $this->initLoadBalancer( $lb );
138
139 return $lb;
140 }
141
142 /**
143 * Execute a function for each tracked load balancer
144 * The callback is called with the load balancer as the first parameter,
145 * and $params passed as the subsequent parameters.
146 *
147 * @param callable $callback
148 * @param array $params
149 */
150 public function forEachLB( $callback, array $params = [] ) {
151 if ( isset( $this->mainLB ) ) {
152 call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
153 }
154 foreach ( $this->extLBs as $lb ) {
155 call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
156 }
157 }
158 }