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