summaryrefslogtreecommitdiff
path: root/www/wiki/includes/libs/rdbms/lbfactory/LBFactorySimple.php
blob: 9a6aa3a74d4ce5c250f017e7d98c6b3dac1e5b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
 * Generator of database load balancing objects.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Database
 */

namespace Wikimedia\Rdbms;

use InvalidArgumentException;

/**
 * A simple single-master LBFactory that gets its configuration from the b/c globals
 */
class LBFactorySimple extends LBFactory {
	/** @var LoadBalancer */
	private $mainLB;
	/** @var LoadBalancer[] */
	private $extLBs = [];

	/** @var array[] Map of (server index => server config) */
	private $servers = [];
	/** @var array[] Map of (cluster => (server index => server config)) */
	private $externalClusters = [];

	/** @var string */
	private $loadMonitorClass;
	/** @var int */
	private $maxLag;

	/** @var int Default 'maxLag' when unspecified */
	const MAX_LAG_DEFAULT = 10;

	/**
	 * @see LBFactory::__construct()
	 * @param array $conf Parameters of LBFactory::__construct() as well as:
	 *   - servers : list of server configuration maps to Database::factory().
	 *      Additionally, the server maps should have a 'load' key, which is used to decide
	 *      how often clients connect to one server verses the others. A 'max lag' key should
	 *      also be set on server maps, indicating how stale the data can be before the load
	 *      balancer tries to avoid using it. The map can have 'is static' set to disable blocking
	 *      replication sync checks (intended for archive servers with unchanging data).
	 *   - externalClusters : map of cluster names to server arrays. The servers arrays have the
	 *      same format as "servers" above.
	 */
	public function __construct( array $conf ) {
		parent::__construct( $conf );

		$this->servers = isset( $conf['servers'] ) ? $conf['servers'] : [];
		foreach ( $this->servers as $i => $server ) {
			if ( $i == 0 ) {
				$this->servers[$i]['master'] = true;
			} else {
				$this->servers[$i]['replica'] = true;
			}
		}

		$this->externalClusters = isset( $conf['externalClusters'] )
			? $conf['externalClusters']
			: [];
		$this->loadMonitorClass = isset( $conf['loadMonitorClass'] )
			? $conf['loadMonitorClass']
			: 'LoadMonitor';
		$this->maxLag = isset( $conf['maxLag'] ) ? $conf['maxLag'] : self::MAX_LAG_DEFAULT;
	}

	/**
	 * @param bool|string $domain
	 * @return LoadBalancer
	 */
	public function newMainLB( $domain = false ) {
		return $this->newLoadBalancer( $this->servers );
	}

	/**
	 * @param bool|string $domain
	 * @return LoadBalancer
	 */
	public function getMainLB( $domain = false ) {
		if ( !isset( $this->mainLB ) ) {
			$this->mainLB = $this->newMainLB( $domain );
		}

		return $this->mainLB;
	}

	public function newExternalLB( $cluster ) {
		if ( !isset( $this->externalClusters[$cluster] ) ) {
			throw new InvalidArgumentException( __METHOD__ . ": Unknown cluster \"$cluster\"." );
		}

		return $this->newLoadBalancer( $this->externalClusters[$cluster] );
	}

	public function getExternalLB( $cluster ) {
		if ( !isset( $this->extLBs[$cluster] ) ) {
			$this->extLBs[$cluster] = $this->newExternalLB( $cluster );
		}

		return $this->extLBs[$cluster];
	}

	public function getAllMainLBs() {
		return [ 'DEFAULT' => $this->getMainLB() ];
	}

	public function getAllExternalLBs() {
		$lbs = [];
		foreach ( $this->externalClusters as $cluster => $unused ) {
			$lbs[$cluster] = $this->getExternalLB( $cluster );
		}

		return $lbs;
	}

	private function newLoadBalancer( array $servers ) {
		$lb = new LoadBalancer( array_merge(
			$this->baseLoadBalancerParams(),
			[
				'servers' => $servers,
				'maxLag' => $this->maxLag,
				'loadMonitor' => [ 'class' => $this->loadMonitorClass ],
			]
		) );
		$this->initLoadBalancer( $lb );

		return $lb;
	}

	/**
	 * Execute a function for each tracked load balancer
	 * The callback is called with the load balancer as the first parameter,
	 * and $params passed as the subsequent parameters.
	 *
	 * @param callable $callback
	 * @param array $params
	 */
	public function forEachLB( $callback, array $params = [] ) {
		if ( isset( $this->mainLB ) ) {
			call_user_func_array( $callback, array_merge( [ $this->mainLB ], $params ) );
		}
		foreach ( $this->extLBs as $lb ) {
			call_user_func_array( $callback, array_merge( [ $lb ], $params ) );
		}
	}
}