2005-05-21 10:15:39 +00:00
|
|
|
<?php
|
2012-05-20 15:56:43 +00:00
|
|
|
/**
|
|
|
|
|
* External storage in SQL database.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2005-08-14 07:18:34 +00:00
|
|
|
|
2018-02-25 09:05:07 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-02-18 00:26:47 +00:00
|
|
|
use Wikimedia\Rdbms\LoadBalancer;
|
2017-02-10 18:09:05 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2017-03-29 16:15:50 +00:00
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
|
|
|
|
use Wikimedia\Rdbms\MaintainableDBConnRef;
|
2017-02-18 00:26:47 +00:00
|
|
|
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2018-03-14 05:24:09 +00:00
|
|
|
* DB accessible external objects.
|
2013-01-21 22:40:15 +00:00
|
|
|
*
|
|
|
|
|
* In this system, each store "location" maps to a database "cluster".
|
|
|
|
|
* The clusters must be defined in the normal LBFactory configuration.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup ExternalStorage
|
|
|
|
|
*/
|
2013-01-21 22:40:15 +00:00
|
|
|
class ExternalStoreDB extends ExternalStoreMedium {
|
|
|
|
|
/**
|
2013-08-02 23:08:22 +00:00
|
|
|
* The provided URL is in the form of DB://cluster/id
|
2013-01-21 22:40:15 +00:00
|
|
|
* or DB://cluster/id/itemid for concatened storage.
|
|
|
|
|
*
|
2016-04-20 16:46:29 +00:00
|
|
|
* @param string $url
|
|
|
|
|
* @return string|bool False if missing
|
2013-01-21 22:40:15 +00:00
|
|
|
* @see ExternalStoreMedium::fetchFromURL()
|
|
|
|
|
*/
|
|
|
|
|
public function fetchFromURL( $url ) {
|
2013-08-02 23:08:22 +00:00
|
|
|
list( $cluster, $id, $itemID ) = $this->parseURL( $url );
|
2013-12-03 03:50:32 +00:00
|
|
|
$ret = $this->fetchBlob( $cluster, $id, $itemID );
|
2013-01-21 22:40:15 +00:00
|
|
|
|
|
|
|
|
if ( $itemID !== false && $ret !== false ) {
|
|
|
|
|
return $ret->getItem( $itemID );
|
|
|
|
|
}
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2013-01-21 22:40:15 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2005-08-14 07:18:34 +00:00
|
|
|
|
2013-08-02 23:08:22 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch data from given external store URLs.
|
|
|
|
|
* The provided URLs are in the form of DB://cluster/id
|
|
|
|
|
* or DB://cluster/id/itemid for concatened storage.
|
|
|
|
|
*
|
|
|
|
|
* @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 ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$batched = $inverseUrlMap = [];
|
2013-08-02 23:08:22 +00:00
|
|
|
foreach ( $urls as $url ) {
|
|
|
|
|
list( $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;
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [];
|
2013-08-02 23:08:22 +00:00
|
|
|
foreach ( $batched as $cluster => $batchByCluster ) {
|
|
|
|
|
$res = $this->batchFetchBlobs( $cluster, $batchByCluster );
|
2013-11-22 20:28:20 +00:00
|
|
|
/** @var HistoryBlob $blob */
|
2013-08-02 23:08:22 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2013-08-02 23:08:22 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-20 16:46:29 +00:00
|
|
|
public function store( $location, $data ) {
|
|
|
|
|
$dbw = $this->getMaster( $location );
|
2013-01-21 22:40:15 +00:00
|
|
|
$dbw->insert( $this->getTable( $dbw ),
|
2017-09-01 13:46:43 +00:00
|
|
|
[ 'blob_text' => $data ],
|
2013-01-21 22:40:15 +00:00
|
|
|
__METHOD__ );
|
|
|
|
|
$id = $dbw->insertId();
|
|
|
|
|
if ( !$id ) {
|
2013-04-13 11:36:24 +00:00
|
|
|
throw new MWException( __METHOD__ . ': no insert ID' );
|
2013-01-21 22:40:15 +00:00
|
|
|
}
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2016-04-20 16:46:29 +00:00
|
|
|
return "DB://$location/$id";
|
2009-03-26 13:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-22 08:27:14 +00:00
|
|
|
public function isReadOnly( $location ) {
|
|
|
|
|
return ( $this->getLoadBalancer( $location )->getReadOnlyReason() !== false );
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-08 21:35:25 +00:00
|
|
|
/**
|
|
|
|
|
* Get a LoadBalancer for the specified cluster
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $cluster Cluster name
|
|
|
|
|
* @return LoadBalancer
|
2010-01-08 21:35:25 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
private function getLoadBalancer( $cluster ) {
|
2018-02-25 09:05:07 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
|
return $lbFactory->getExternalLB( $cluster );
|
2005-08-14 07:18:34 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-01-08 21:35:25 +00:00
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Get a replica DB connection for the specified cluster
|
2010-01-08 21:35:25 +00:00
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $cluster Cluster name
|
2016-11-28 18:26:14 +00:00
|
|
|
* @return DBConnRef
|
2010-01-08 21:35:25 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
public function getSlave( $cluster ) {
|
2011-10-03 13:19:22 +00:00
|
|
|
global $wgDefaultExternalStore;
|
|
|
|
|
|
2017-10-06 22:17:58 +00:00
|
|
|
$wiki = $this->params['wiki'] ?? false;
|
2013-12-03 03:50:32 +00:00
|
|
|
$lb = $this->getLoadBalancer( $cluster );
|
2011-10-03 13:19:22 +00:00
|
|
|
|
2011-10-04 16:51:37 +00:00
|
|
|
if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
|
2014-03-29 10:52:07 +00:00
|
|
|
wfDebug( "read only external store\n" );
|
2013-02-03 18:47:42 +00:00
|
|
|
$lb->allowLagged( true );
|
2011-10-03 13:19:22 +00:00
|
|
|
} else {
|
2014-03-29 10:52:07 +00:00
|
|
|
wfDebug( "writable external store\n" );
|
2011-10-03 13:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
2016-09-21 07:58:14 +00:00
|
|
|
$db = $lb->getConnectionRef( DB_REPLICA, [], $wiki );
|
2014-09-12 18:43:34 +00:00
|
|
|
$db->clearFlag( DBO_TRX ); // sanity
|
|
|
|
|
|
|
|
|
|
return $db;
|
2005-08-14 07:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-08 21:35:25 +00:00
|
|
|
/**
|
|
|
|
|
* Get a master database connection for the specified cluster
|
|
|
|
|
*
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $cluster Cluster name
|
2016-11-28 18:26:14 +00:00
|
|
|
* @return MaintainableDBConnRef
|
2010-01-08 21:35:25 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
public function getMaster( $cluster ) {
|
2017-10-06 22:17:58 +00:00
|
|
|
$wiki = $this->params['wiki'] ?? false;
|
2013-12-03 03:50:32 +00:00
|
|
|
$lb = $this->getLoadBalancer( $cluster );
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2016-11-28 18:26:14 +00:00
|
|
|
$db = $lb->getMaintenanceConnectionRef( DB_MASTER, [], $wiki );
|
2014-09-12 18:43:34 +00:00
|
|
|
$db->clearFlag( DBO_TRX ); // sanity
|
|
|
|
|
|
|
|
|
|
return $db;
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2005-10-29 01:41:36 +00:00
|
|
|
|
2010-01-08 21:35:25 +00:00
|
|
|
/**
|
|
|
|
|
* Get the 'blobs' table name for this database
|
|
|
|
|
*
|
2015-10-04 09:07:25 +00:00
|
|
|
* @param IDatabase $db
|
2014-04-14 19:43:18 +00:00
|
|
|
* @return string Table name ('blobs' by default)
|
2010-01-08 21:35:25 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
public function getTable( $db ) {
|
2005-10-29 01:41:36 +00:00
|
|
|
$table = $db->getLBInfo( 'blobs table' );
|
|
|
|
|
if ( is_null( $table ) ) {
|
|
|
|
|
$table = 'blobs';
|
|
|
|
|
}
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2005-10-29 01:41:36 +00:00
|
|
|
return $table;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-09-16 12:00:23 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
2014-04-14 19:43:18 +00:00
|
|
|
* @param string $cluster
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @param string $itemID
|
2016-04-20 16:46:29 +00:00
|
|
|
* @return HistoryBlob|bool Returns false if missing
|
2005-09-16 12:00:23 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
private function fetchBlob( $cluster, $id, $itemID ) {
|
2010-08-12 18:25:04 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
static $externalBlobCache = [];
|
2010-08-12 18:25:04 +00:00
|
|
|
|
2005-09-16 12:00:23 +00:00
|
|
|
$cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( isset( $externalBlobCache[$cacheID] ) ) {
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( 'ExternalStoreDB-cache',
|
2014-02-04 21:16:13 +00:00
|
|
|
"ExternalStoreDB::fetchBlob cache hit on $cacheID" );
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2010-08-12 18:25:04 +00:00
|
|
|
return $externalBlobCache[$cacheID];
|
2005-09-16 12:00:23 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( 'ExternalStoreDB-cache',
|
2014-02-04 21:16:13 +00:00
|
|
|
"ExternalStoreDB::fetchBlob cache miss on $cacheID" );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-12-03 03:50:32 +00:00
|
|
|
$dbr = $this->getSlave( $cluster );
|
2013-08-08 02:26:28 +00:00
|
|
|
$ret = $dbr->selectField( $this->getTable( $dbr ),
|
2016-02-17 09:09:32 +00:00
|
|
|
'blob_text', [ 'blob_id' => $id ], __METHOD__ );
|
2006-01-19 03:55:03 +00:00
|
|
|
if ( $ret === false ) {
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( 'ExternalStoreDB',
|
2014-02-04 21:16:13 +00:00
|
|
|
"ExternalStoreDB::fetchBlob master fallback on $cacheID" );
|
2006-01-19 03:55:03 +00:00
|
|
|
// Try the master
|
2013-12-03 03:50:32 +00:00
|
|
|
$dbw = $this->getMaster( $cluster );
|
2013-08-08 02:26:28 +00:00
|
|
|
$ret = $dbw->selectField( $this->getTable( $dbw ),
|
2016-02-17 09:09:32 +00:00
|
|
|
'blob_text', [ 'blob_id' => $id ], __METHOD__ );
|
2013-04-27 12:02:08 +00:00
|
|
|
if ( $ret === false ) {
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( 'ExternalStoreDB',
|
2014-02-04 21:16:13 +00:00
|
|
|
"ExternalStoreDB::fetchBlob master failed to find $cacheID" );
|
2006-03-14 00:47:58 +00:00
|
|
|
}
|
2006-01-19 03:55:03 +00:00
|
|
|
}
|
2013-04-20 20:28:52 +00:00
|
|
|
if ( $itemID !== false && $ret !== false ) {
|
2005-09-16 12:00:23 +00:00
|
|
|
// Unserialise object; caller extracts item
|
|
|
|
|
$ret = unserialize( $ret );
|
2005-08-14 07:18:34 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$externalBlobCache = [ $cacheID => $ret ];
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2005-05-21 10:15:39 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2013-08-02 23:08:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2013-11-22 20:28:20 +00:00
|
|
|
* @return array A map from the blob_id's requested to their content.
|
|
|
|
|
* Unlocated ids are not represented
|
2013-08-02 23:08:22 +00:00
|
|
|
*/
|
2016-11-28 18:26:14 +00:00
|
|
|
private function batchFetchBlobs( $cluster, array $ids ) {
|
2013-08-02 23:08:22 +00:00
|
|
|
$dbr = $this->getSlave( $cluster );
|
2013-08-08 02:26:28 +00:00
|
|
|
$res = $dbr->select( $this->getTable( $dbr ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'blob_id', 'blob_text' ], [ 'blob_id' => array_keys( $ids ) ], __METHOD__ );
|
|
|
|
|
$ret = [];
|
2013-08-02 23:08:22 +00:00
|
|
|
if ( $res !== false ) {
|
|
|
|
|
$this->mergeBatchResult( $ret, $ids, $res );
|
|
|
|
|
}
|
|
|
|
|
if ( $ids ) {
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( __CLASS__, __METHOD__ .
|
|
|
|
|
" master fallback on '$cluster' for: " .
|
2014-02-04 21:16:13 +00:00
|
|
|
implode( ',', array_keys( $ids ) ) );
|
2013-08-02 23:08:22 +00:00
|
|
|
// Try the master
|
|
|
|
|
$dbw = $this->getMaster( $cluster );
|
2013-08-08 02:26:28 +00:00
|
|
|
$res = $dbw->select( $this->getTable( $dbr ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'blob_id', 'blob_text' ],
|
|
|
|
|
[ 'blob_id' => array_keys( $ids ) ],
|
2013-08-08 02:26:28 +00:00
|
|
|
__METHOD__ );
|
2013-08-02 23:08:22 +00:00
|
|
|
if ( $res === false ) {
|
2014-02-04 21:16:13 +00:00
|
|
|
wfDebugLog( __CLASS__, __METHOD__ . " master failed on '$cluster'" );
|
2013-08-02 23:08:22 +00:00
|
|
|
} else {
|
|
|
|
|
$this->mergeBatchResult( $ret, $ids, $res );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $ids ) {
|
2013-08-08 02:26:28 +00:00
|
|
|
wfDebugLog( __CLASS__, __METHOD__ .
|
|
|
|
|
" master on '$cluster' failed locating items: " .
|
2014-02-04 21:16:13 +00:00
|
|
|
implode( ',', array_keys( $ids ) ) );
|
2013-08-02 23:08:22 +00:00
|
|
|
}
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2013-08-02 23:08:22 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-09-05 20:21:26 +00:00
|
|
|
* Helper function for self::batchFetchBlobs for merging master/replica DB results
|
2013-08-02 23:08:22 +00:00
|
|
|
* @param array &$ret Current self::batchFetchBlobs return value
|
|
|
|
|
* @param array &$ids Map from blob_id to requested itemIDs
|
2015-10-06 05:39:37 +00:00
|
|
|
* @param mixed $res DB result from Database::select
|
2013-08-02 23:08:22 +00:00
|
|
|
*/
|
|
|
|
|
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] = unserialize( $row->blob_text );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-12 18:43:34 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2013-08-02 23:08:22 +00:00
|
|
|
protected function parseURL( $url ) {
|
|
|
|
|
$path = explode( '/', $url );
|
2013-11-22 20:28:20 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2013-08-02 23:08:22 +00:00
|
|
|
$path[2], // cluster
|
|
|
|
|
$path[3], // id
|
2017-10-06 22:17:58 +00:00
|
|
|
$path[4] ?? false // itemID
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-08-02 23:08:22 +00:00
|
|
|
}
|
2005-05-21 10:15:39 +00:00
|
|
|
}
|