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