File: ExternalStoreDB.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (424 lines) | stat: -rw-r--r-- 12,691 bytes parent folder | download
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/**
 * 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
 */

use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\DBUnexpectedError;
use Wikimedia\Rdbms\ILoadBalancer;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Rdbms\Query;
use Wikimedia\Rdbms\ServerInfo;
use Wikimedia\ScopedCallback;

/**
 * External storage in a SQL database.
 *
 * In this system, each store "location" maps to a database "cluster".
 * The clusters must be defined in the normal LBFactory configuration.
 *
 * @see ExternalStoreAccess
 * @ingroup ExternalStorage
 */
class ExternalStoreDB extends ExternalStoreMedium {
	/** @var LBFactory */
	private $lbFactory;

	/**
	 * @see ExternalStoreMedium::__construct()
	 * @param array $params Additional parameters include:
	 *   - lbFactory: an LBFactory instance
	 */
	public function __construct( array $params ) {
		parent::__construct( $params );
		if ( !isset( $params['lbFactory'] ) || !( $params['lbFactory'] instanceof LBFactory ) ) {
			throw new InvalidArgumentException( "LBFactory required in 'lbFactory' field." );
		}
		$this->lbFactory = $params['lbFactory'];
	}

	/**
	 * Fetch data from given external store URL.
	 *
	 * The provided URL is in the form of `DB://cluster/id` or `DB://cluster/id/itemid`
	 * for concatenated storage if ConcatenatedGzipHistoryBlob was used.
	 *
	 * @param string $url
	 * @return string|false False if missing
	 * @see ExternalStoreMedium::fetchFromURL()
	 */
	public function fetchFromURL( $url ) {
		[ $cluster, $id, $itemID ] = $this->parseURL( $url );
		$ret = $this->fetchBlob( $cluster, $id, $itemID );

		if ( $itemID !== false && $ret !== false ) {
			return $ret->getItem( $itemID );
		}

		return $ret;
	}

	/**
	 * Fetch multiple URLs from given external store.
	 *
	 * The provided URLs are in the form of `DB://cluster/id`, or `DB://cluster/id/itemid`
	 * for concatenated storage if ConcatenatedGzipHistoryBlob was used.
	 *
	 * @param array $urls An array of external store URLs
	 * @return array A map from url to stored content. Failed results
	 *     are not represented.
	 */
	public function batchFetchFromURLs( array $urls ) {
		$batched = $inverseUrlMap = [];
		foreach ( $urls as $url ) {
			[ $cluster, $id, $itemID ] = $this->parseURL( $url );
			$batched[$cluster][$id][] = $itemID;
			// false $itemID gets cast to int, but should be ok
			// since we do === from the $itemID in $batched
			$inverseUrlMap[$cluster][$id][$itemID] = $url;
		}
		$ret = [];
		foreach ( $batched as $cluster => $batchByCluster ) {
			$res = $this->batchFetchBlobs( $cluster, $batchByCluster );
			/** @var HistoryBlob $blob */
			foreach ( $res as $id => $blob ) {
				foreach ( $batchByCluster[$id] as $itemID ) {
					$url = $inverseUrlMap[$cluster][$id][$itemID];
					if ( $itemID === false ) {
						$ret[$url] = $blob;
					} else {
						$ret[$url] = $blob->getItem( $itemID );
					}
				}
			}
		}

		return $ret;
	}

	/**
	 * @inheritDoc
	 */
	public function store( $location, $data ) {
		$blobsTable = $this->getTable( $location );

		$dbw = $this->getPrimary( $location );
		$dbw->newInsertQueryBuilder()
			->insertInto( $blobsTable )
			->row( [ 'blob_text' => $data ] )
			->caller( __METHOD__ )->execute();

		$id = $dbw->insertId();
		if ( !$id ) {
			throw new ExternalStoreException( __METHOD__ . ': no insert ID' );
		}

		return "DB://$location/$id";
	}

	/**
	 * @inheritDoc
	 */
	public function isReadOnly( $location ) {
		if ( parent::isReadOnly( $location ) ) {
			return true;
		}

		return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !== false );
	}

	/**
	 * Get a LoadBalancer for the specified cluster
	 *
	 * @param string $cluster Cluster name
	 * @return ILoadBalancer
	 */
	private function getLoadBalancer( $cluster ) {
		return $this->lbFactory->getExternalLB( $cluster );
	}

	/**
	 * Get a replica DB connection for the specified cluster
	 *
	 * @since 1.34
	 * @param string $cluster Cluster name
	 * @return \Wikimedia\Rdbms\IReadableDatabase
	 */
	public function getReplica( $cluster ) {
		$lb = $this->getLoadBalancer( $cluster );

		return $lb->getConnection(
			DB_REPLICA,
			[],
			$this->getDomainId( $lb->getServerInfo( ServerInfo::WRITER_INDEX ) ),
			$lb::CONN_TRX_AUTOCOMMIT
		);
	}

	/**
	 * Get a primary database connection for the specified cluster
	 *
	 * @param string $cluster Cluster name
	 * @return \Wikimedia\Rdbms\IMaintainableDatabase
	 * @since 1.37
	 */
	public function getPrimary( $cluster ) {
		$lb = $this->getLoadBalancer( $cluster );

		return $lb->getMaintenanceConnectionRef(
			DB_PRIMARY,
			[],
			$this->getDomainId( $lb->getServerInfo( ServerInfo::WRITER_INDEX ) ),
			$lb::CONN_TRX_AUTOCOMMIT
		);
	}

	/**
	 * @param array $server Primary DB server configuration array for LoadBalancer
	 * @return string|false Database domain ID or false
	 */
	private function getDomainId( array $server ) {
		if ( $this->isDbDomainExplicit ) {
			return $this->dbDomain; // explicit foreign domain
		}

		if ( isset( $server['dbname'] ) ) {
			// T200471: for b/c, treat any "dbname" field as forcing which database to use.
			// MediaWiki/LoadBalancer previously did not enforce any concept of a local DB
			// domain, but rather assumed that the LB server configuration matched $wgDBname.
			// This check is useful when the external storage DB for this cluster does not use
			// the same name as the corresponding "main" DB(s) for wikis.
			$domain = new DatabaseDomain(
				$server['dbname'],
				$server['schema'] ?? null,
				$server['tablePrefix'] ?? ''
			);

			return $domain->getId();
		}

		return false; // local LB domain
	}

	/**
	 * Get the configured blobs table name for this database
	 *
	 * Typically, a suffix like "_clusterX" can be used to facilitate clean merging of
	 * read-only storage clusters by simply cloning tables to the new cluster servers.
	 *
	 * @param string $cluster Cluster name
	 * @return string Unqualified table name (e.g. "blobs_cluster32" or default "blobs")
	 * @internal Only for use within ExternalStoreDB and its core maintenance scripts
	 */
	public function getTable( string $cluster ) {
		$lb = $this->getLoadBalancer( $cluster );
		$info = $lb->getServerInfo( ServerInfo::WRITER_INDEX );

		return $info['blobs table'] ?? 'blobs';
	}

	/**
	 * Create the appropriate blobs table on this cluster
	 *
	 * @since 1.34
	 * @param string $cluster
	 */
	public function initializeTable( $cluster ) {
		global $IP;

		static $supportedTypes = [ 'mysql', 'sqlite' ];

		$dbw = $this->getPrimary( $cluster );
		if ( !in_array( $dbw->getType(), $supportedTypes, true ) ) {
			throw new DBUnexpectedError( $dbw, "RDBMS type '{$dbw->getType()}' not supported." );
		}

		$sqlFilePath = "$IP/maintenance/storage/blobs.sql";
		$sql = file_get_contents( $sqlFilePath );
		if ( $sql === false ) {
			throw new RuntimeException( "Failed to read '$sqlFilePath'." );
		}

		$blobsTable = $this->getTable( $cluster );
		$encTable = $dbw->tableName( $blobsTable );
		$sqlWithReplacedVars = str_replace(
			[ '/*$wgDBprefix*/blobs', '/*_*/blobs' ],
			[ $encTable, $encTable ],
			$sql
		);

		$dbw->query(
			new Query(
				$sqlWithReplacedVars,
				$dbw::QUERY_CHANGE_SCHEMA,
				'CREATE',
				$blobsTable,
				$sqlWithReplacedVars
			),
			__METHOD__
		);
	}

	/**
	 * Fetch a blob item out of the database; a cache of the last-loaded
	 * blob will be kept so that multiple loads out of a multi-item blob
	 * can avoid redundant database access and decompression.
	 * @param string $cluster
	 * @param string $id
	 * @param string $itemID
	 * @return HistoryBlob|false Returns false if missing
	 */
	private function fetchBlob( $cluster, $id, $itemID ) {
		/**
		 * One-step cache variable to hold base blobs; operations that
		 * pull multiple revisions may often pull multiple times from
		 * the same blob. By keeping the last-used one open, we avoid
		 * redundant unserialization and decompression overhead.
		 */
		static $externalBlobCache = [];

		$cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
		$cacheID = "$cacheID@{$this->dbDomain}";

		if ( isset( $externalBlobCache[$cacheID] ) ) {
			$this->logger->debug( __METHOD__ . ": cache hit on $cacheID" );

			return $externalBlobCache[$cacheID];
		}

		$this->logger->debug( __METHOD__ . ": cache miss on $cacheID" );

		$blobsTable = $this->getTable( $cluster );

		$dbr = $this->getReplica( $cluster );
		$ret = $dbr->newSelectQueryBuilder()
			->select( 'blob_text' )
			->from( $blobsTable )
			->where( [ 'blob_id' => $id ] )
			->caller( __METHOD__ )->fetchField();

		if ( $ret === false ) {
			// Try the primary DB
			$this->logger->warning( __METHOD__ . ": primary DB fallback on $cacheID" );
			$trxProfiler = $this->lbFactory->getTransactionProfiler();
			$scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY );
			$dbw = $this->getPrimary( $cluster );
			$ret = $dbw->newSelectQueryBuilder()
				->select( 'blob_text' )
				->from( $blobsTable )
				->where( [ 'blob_id' => $id ] )
				->caller( __METHOD__ )->fetchField();
			ScopedCallback::consume( $scope );
			if ( $ret === false ) {
				$this->logger->warning( __METHOD__ . ": primary DB failed to find $cacheID" );
			}
		}
		if ( $itemID !== false && $ret !== false ) {
			// Unserialise object; caller extracts item
			$ret = HistoryBlobUtils::unserialize( $ret );
		}

		$externalBlobCache = [ $cacheID => $ret ];

		return $ret;
	}

	/**
	 * Fetch multiple blob items out of the database
	 *
	 * @param string $cluster A cluster name valid for use with LBFactory
	 * @param array $ids A map from the blob_id's to look for to the requested itemIDs in the blobs
	 * @return array A map from the blob_id's requested to their content.
	 *   Unlocated ids are not represented
	 */
	private function batchFetchBlobs( $cluster, array $ids ) {
		$blobsTable = $this->getTable( $cluster );

		$dbr = $this->getReplica( $cluster );
		$res = $dbr->newSelectQueryBuilder()
			->select( [ 'blob_id', 'blob_text' ] )
			->from( $blobsTable )
			->where( [ 'blob_id' => array_keys( $ids ) ] )
			->caller( __METHOD__ )
			->fetchResultSet();

		$ret = [];
		$this->mergeBatchResult( $ret, $ids, $res );
		if ( $ids ) {
			// Try the primary
			$this->logger->info(
				__METHOD__ . ": primary fallback on '$cluster' for: " .
				implode( ',', array_keys( $ids ) )
			);
			$trxProfiler = $this->lbFactory->getTransactionProfiler();
			$scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY );
			$dbw = $this->getPrimary( $cluster );
			$res = $dbw->newSelectQueryBuilder()
				->select( [ 'blob_id', 'blob_text' ] )
				->from( $blobsTable )
				->where( [ 'blob_id' => array_keys( $ids ) ] )
				->caller( __METHOD__ )
				->fetchResultSet();
			ScopedCallback::consume( $scope );
			$this->mergeBatchResult( $ret, $ids, $res );
		}
		if ( $ids ) {
			$this->logger->error(
				__METHOD__ . ": primary on '$cluster' failed locating items: " .
				implode( ',', array_keys( $ids ) )
			);
		}

		return $ret;
	}

	/**
	 * Helper function for self::batchFetchBlobs for merging primary/replica DB results
	 * @param array &$ret Current self::batchFetchBlobs return value
	 * @param array &$ids Map from blob_id to requested itemIDs
	 * @param mixed $res DB result from Database::select
	 */
	private function mergeBatchResult( array &$ret, array &$ids, $res ) {
		foreach ( $res as $row ) {
			$id = $row->blob_id;
			$itemIDs = $ids[$id];
			unset( $ids[$id] ); // to track if everything is found
			if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) {
				// single result stored per blob
				$ret[$id] = $row->blob_text;
			} else {
				// multi result stored per blob
				$ret[$id] = HistoryBlobUtils::unserialize( $row->blob_text );
			}
		}
	}

	/**
	 * @param string $url
	 * @return array
	 */
	protected function parseURL( $url ) {
		$path = explode( '/', $url );

		return [
			$path[2], // cluster
			$path[3], // id
			$path[4] ?? false // itemID
		];
	}
}