summaryrefslogtreecommitdiff
path: root/includes/db/LBFactoryMulti.php
blob: bac96523422d805416c79de065fafa507c505a61 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/**
 * Advanced generator of database load balancing objects for wiki farms.
 *
 * 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
 */

/**
 * A multi-wiki, multi-master factory for Wikimedia and similar installations.
 * Ignores the old configuration globals
 *
 * Configuration:
 *     sectionsByDB                A map of database names to section names.
 *
 *     sectionLoads                A 2-d map. For each section, gives a map of server names to
 *                                 load ratios. For example:
 *                                 array(
 *                                     'section1' => array(
 *                                         'db1' => 100,
 *                                         'db2' => 100
 *                                     )
 *                                 )
 *
 *     serverTemplate              A server info associative array as documented for $wgDBservers.
 *                                 The host, hostName and load entries will be overridden.
 *
 *     groupLoadsBySection         A 3-d map giving server load ratios for each section and group.
 *                                 For example:
 *                                 array(
 *                                     'section1' => array(
 *                                         'group1' => array(
 *                                             'db1' => 100,
 *                                             'db2' => 100
 *                                         )
 *                                     )
 *                                 )
 *
 *     groupLoadsByDB              A 3-d map giving server load ratios by DB name.
 *
 *     hostsByName                 A map of hostname to IP address.
 *
 *     externalLoads               A map of external storage cluster name to server load map.
 *
 *     externalTemplateOverrides   A set of server info keys overriding serverTemplate for external
 *                                 storage.
 *
 *     templateOverridesByServer   A 2-d map overriding serverTemplate and
 *                                 externalTemplateOverrides on a server-by-server basis. Applies
 *                                 to both core and external storage.
 *
 *     templateOverridesByCluster  A 2-d map overriding the server info by external storage cluster.
 *
 *     masterTemplateOverrides     An override array for all master servers.
 *
 *     readOnlyBySection           A map of section name to read-only message.
 *                                 Missing or false for read/write.
 *
 * @ingroup Database
 */
class LBFactoryMulti extends LBFactory {
	// Required settings

	/** @var array A map of database names to section names */
	protected $sectionsByDB;

	/**
	 * @var array A 2-d map. For each section, gives a map of server names to
	 * load ratios
	 */
	protected $sectionLoads;

	/**
	 * @var array A server info associative array as documented for
	 * $wgDBservers. The host, hostName and load entries will be
	 * overridden
	 */
	protected $serverTemplate;

	// Optional settings

	/** @var array A 3-d map giving server load ratios for each section and group */
	protected $groupLoadsBySection = array();

	/** @var array A 3-d map giving server load ratios by DB name */
	protected $groupLoadsByDB = array();

	/** @var array A map of hostname to IP address */
	protected $hostsByName = array();

	/** @var array A map of external storage cluster name to server load map */
	protected $externalLoads = array();

	/**
	 * @var array A set of server info keys overriding serverTemplate for
	 * external storage
	 */
	protected $externalTemplateOverrides;

	/**
	 * @var array A 2-d map overriding serverTemplate and
	 * externalTemplateOverrides on a server-by-server basis. Applies to both
	 * core and external storage
	 */
	protected $templateOverridesByServer;

	/** @var array A 2-d map overriding the server info by external storage cluster */
	protected $templateOverridesByCluster;

	/** @var array An override array for all master servers */
	protected $masterTemplateOverrides;

	/**
	 * @var array|bool A map of section name to read-only message. Missing or
	 * false for read/write
	 */
	protected $readOnlyBySection = array();

	// Other stuff

	/** @var array Load balancer factory configuration */
	protected $conf;

	/** @var LoadBalancer[] */
	protected $mainLBs = array();

	/** @var LoadBalancer[] */
	protected $extLBs = array();

	/** @var string */
	protected $lastWiki;

	/** @var string */
	protected $lastSection;

	/**
	 * @param array $conf
	 * @throws MWException
	 */
	function __construct( $conf ) {
		$this->chronProt = new ChronologyProtector;
		$this->conf = $conf;
		$required = array( 'sectionsByDB', 'sectionLoads', 'serverTemplate' );
		$optional = array( 'groupLoadsBySection', 'groupLoadsByDB', 'hostsByName',
			'externalLoads', 'externalTemplateOverrides', 'templateOverridesByServer',
			'templateOverridesByCluster', 'masterTemplateOverrides',
			'readOnlyBySection' );

		foreach ( $required as $key ) {
			if ( !isset( $conf[$key] ) ) {
				throw new MWException( __CLASS__ . ": $key is required in configuration" );
			}
			$this->$key = $conf[$key];
		}

		foreach ( $optional as $key ) {
			if ( isset( $conf[$key] ) ) {
				$this->$key = $conf[$key];
			}
		}

		// Check for read-only mode
		$section = $this->getSectionForWiki();
		if ( !empty( $this->readOnlyBySection[$section] ) ) {
			global $wgReadOnly;
			$wgReadOnly = $this->readOnlyBySection[$section];
		}
	}

	/**
	 * @param bool|string $wiki
	 * @return string
	 */
	function getSectionForWiki( $wiki = false ) {
		if ( $this->lastWiki === $wiki ) {
			return $this->lastSection;
		}
		list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
		if ( isset( $this->sectionsByDB[$dbName] ) ) {
			$section = $this->sectionsByDB[$dbName];
		} else {
			$section = 'DEFAULT';
		}
		$this->lastSection = $section;
		$this->lastWiki = $wiki;

		return $section;
	}

	/**
	 * @param bool|string $wiki
	 * @return LoadBalancer
	 */
	function newMainLB( $wiki = false ) {
		list( $dbName, ) = $this->getDBNameAndPrefix( $wiki );
		$section = $this->getSectionForWiki( $wiki );
		$groupLoads = array();
		if ( isset( $this->groupLoadsByDB[$dbName] ) ) {
			$groupLoads = $this->groupLoadsByDB[$dbName];
		}

		if ( isset( $this->groupLoadsBySection[$section] ) ) {
			$groupLoads = array_merge_recursive( $groupLoads, $this->groupLoadsBySection[$section] );
		}

		return $this->newLoadBalancer(
			$this->serverTemplate,
			$this->sectionLoads[$section],
			$groupLoads
		);
	}

	/**
	 * @param bool|string $wiki
	 * @return LoadBalancer
	 */
	function getMainLB( $wiki = false ) {
		$section = $this->getSectionForWiki( $wiki );
		if ( !isset( $this->mainLBs[$section] ) ) {
			$lb = $this->newMainLB( $wiki, $section );
			$lb->parentInfo( array( 'id' => "main-$section" ) );
			$this->chronProt->initLB( $lb );
			$this->mainLBs[$section] = $lb;
		}

		return $this->mainLBs[$section];
	}

	/**
	 * @param string $cluster
	 * @param bool|string $wiki
	 * @throws MWException
	 * @return LoadBalancer
	 */
	function newExternalLB( $cluster, $wiki = false ) {
		if ( !isset( $this->externalLoads[$cluster] ) ) {
			throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
		}
		$template = $this->serverTemplate;
		if ( isset( $this->externalTemplateOverrides ) ) {
			$template = $this->externalTemplateOverrides + $template;
		}
		if ( isset( $this->templateOverridesByCluster[$cluster] ) ) {
			$template = $this->templateOverridesByCluster[$cluster] + $template;
		}

		return $this->newLoadBalancer( $template, $this->externalLoads[$cluster], array() );
	}

	/**
	 * @param string $cluster External storage cluster, or false for core
	 * @param bool|string $wiki Wiki ID, or false for the current wiki
	 * @return LoadBalancer
	 */
	function &getExternalLB( $cluster, $wiki = false ) {
		if ( !isset( $this->extLBs[$cluster] ) ) {
			$this->extLBs[$cluster] = $this->newExternalLB( $cluster, $wiki );
			$this->extLBs[$cluster]->parentInfo( array( 'id' => "ext-$cluster" ) );
			$this->chronProt->initLB( $this->extLBs[$cluster] );
		}

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

	/**
	 * Make a new load balancer object based on template and load array
	 *
	 * @param array $template
	 * @param array $loads
	 * @param array $groupLoads
	 * @return LoadBalancer
	 */
	function newLoadBalancer( $template, $loads, $groupLoads ) {
		$servers = $this->makeServerArray( $template, $loads, $groupLoads );
		$lb = new LoadBalancer( array(
			'servers' => $servers,
		) );

		return $lb;
	}

	/**
	 * Make a server array as expected by LoadBalancer::__construct, using a template and load array
	 *
	 * @param array $template
	 * @param array $loads
	 * @param array $groupLoads
	 * @return array
	 */
	function makeServerArray( $template, $loads, $groupLoads ) {
		$servers = array();
		$master = true;
		$groupLoadsByServer = $this->reindexGroupLoads( $groupLoads );
		foreach ( $groupLoadsByServer as $server => $stuff ) {
			if ( !isset( $loads[$server] ) ) {
				$loads[$server] = 0;
			}
		}
		foreach ( $loads as $serverName => $load ) {
			$serverInfo = $template;
			if ( $master ) {
				$serverInfo['master'] = true;
				if ( isset( $this->masterTemplateOverrides ) ) {
					$serverInfo = $this->masterTemplateOverrides + $serverInfo;
				}
				$master = false;
			}
			if ( isset( $this->templateOverridesByServer[$serverName] ) ) {
				$serverInfo = $this->templateOverridesByServer[$serverName] + $serverInfo;
			}
			if ( isset( $groupLoadsByServer[$serverName] ) ) {
				$serverInfo['groupLoads'] = $groupLoadsByServer[$serverName];
			}
			if ( isset( $this->hostsByName[$serverName] ) ) {
				$serverInfo['host'] = $this->hostsByName[$serverName];
			} else {
				$serverInfo['host'] = $serverName;
			}
			$serverInfo['hostName'] = $serverName;
			$serverInfo['load'] = $load;
			$servers[] = $serverInfo;
		}

		return $servers;
	}

	/**
	 * Take a group load array indexed by group then server, and reindex it by server then group
	 * @param array $groupLoads
	 * @return array
	 */
	function reindexGroupLoads( $groupLoads ) {
		$reindexed = array();
		foreach ( $groupLoads as $group => $loads ) {
			foreach ( $loads as $server => $load ) {
				$reindexed[$server][$group] = $load;
			}
		}

		return $reindexed;
	}

	/**
	 * Get the database name and prefix based on the wiki ID
	 * @param bool|string $wiki
	 * @return array
	 */
	function getDBNameAndPrefix( $wiki = false ) {
		if ( $wiki === false ) {
			global $wgDBname, $wgDBprefix;

			return array( $wgDBname, $wgDBprefix );
		} else {
			return wfSplitWikiID( $wiki );
		}
	}

	/**
	 * 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
	 */
	function forEachLB( $callback, $params = array() ) {
		foreach ( $this->mainLBs as $lb ) {
			call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
		}
		foreach ( $this->extLBs as $lb ) {
			call_user_func_array( $callback, array_merge( array( $lb ), $params ) );
		}
	}

	function shutdown() {
		foreach ( $this->mainLBs as $lb ) {
			$this->chronProt->shutdownLB( $lb );
		}
		foreach ( $this->extLBs as $extLB ) {
			$this->chronProt->shutdownLB( $extLB );
		}
		$this->chronProt->shutdown();
		$this->commitMasterChanges();
	}
}