3 * External storage in SQL database.
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.
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.
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
24 * DB accessable external objects.
26 * In this system, each store "location" maps to a database "cluster".
27 * The clusters must be defined in the normal LBFactory configuration.
29 * @ingroup ExternalStorage
31 class ExternalStoreDB
extends ExternalStoreMedium
{
33 * The provided URL is in the form of DB://cluster/id
34 * or DB://cluster/id/itemid for concatened storage.
37 * @return string|bool False if missing
38 * @see ExternalStoreMedium::fetchFromURL()
40 public function fetchFromURL( $url ) {
41 list( $cluster, $id, $itemID ) = $this->parseURL( $url );
42 $ret = $this->fetchBlob( $cluster, $id, $itemID );
44 if ( $itemID !== false && $ret !== false ) {
45 return $ret->getItem( $itemID );
52 * Fetch data from given external store URLs.
53 * The provided URLs are in the form of DB://cluster/id
54 * or DB://cluster/id/itemid for concatened storage.
56 * @param array $urls An array of external store URLs
57 * @return array A map from url to stored content. Failed results
58 * are not represented.
60 public function batchFetchFromURLs( array $urls ) {
61 $batched = $inverseUrlMap = [];
62 foreach ( $urls as $url ) {
63 list( $cluster, $id, $itemID ) = $this->parseURL( $url );
64 $batched[$cluster][$id][] = $itemID;
65 // false $itemID gets cast to int, but should be ok
66 // since we do === from the $itemID in $batched
67 $inverseUrlMap[$cluster][$id][$itemID] = $url;
70 foreach ( $batched as $cluster => $batchByCluster ) {
71 $res = $this->batchFetchBlobs( $cluster, $batchByCluster );
72 /** @var HistoryBlob $blob */
73 foreach ( $res as $id => $blob ) {
74 foreach ( $batchByCluster[$id] as $itemID ) {
75 $url = $inverseUrlMap[$cluster][$id][$itemID];
76 if ( $itemID === false ) {
79 $ret[$url] = $blob->getItem( $itemID );
88 public function store( $location, $data ) {
89 $dbw = $this->getMaster( $location );
90 $id = $dbw->nextSequenceValue( 'blob_blob_id_seq' );
91 $dbw->insert( $this->getTable( $dbw ),
92 [ 'blob_id' => $id, 'blob_text' => $data ],
94 $id = $dbw->insertId();
96 throw new MWException( __METHOD__
. ': no insert ID' );
99 return "DB://$location/$id";
103 * Get a LoadBalancer for the specified cluster
105 * @param string $cluster Cluster name
106 * @return LoadBalancer
108 function getLoadBalancer( $cluster ) {
109 $wiki = isset( $this->params
['wiki'] ) ?
$this->params
['wiki'] : false;
111 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
115 * Get a slave database connection for the specified cluster
117 * @param string $cluster Cluster name
120 function getSlave( $cluster ) {
121 global $wgDefaultExternalStore;
123 $wiki = isset( $this->params
['wiki'] ) ?
$this->params
['wiki'] : false;
124 $lb = $this->getLoadBalancer( $cluster );
126 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
127 wfDebug( "read only external store\n" );
128 $lb->allowLagged( true );
130 wfDebug( "writable external store\n" );
133 $db = $lb->getConnection( DB_SLAVE
, [], $wiki );
134 $db->clearFlag( DBO_TRX
); // sanity
140 * Get a master database connection for the specified cluster
142 * @param string $cluster Cluster name
145 function getMaster( $cluster ) {
146 $wiki = isset( $this->params
['wiki'] ) ?
$this->params
['wiki'] : false;
147 $lb = $this->getLoadBalancer( $cluster );
149 $db = $lb->getConnection( DB_MASTER
, [], $wiki );
150 $db->clearFlag( DBO_TRX
); // sanity
156 * Get the 'blobs' table name for this database
158 * @param IDatabase $db
159 * @return string Table name ('blobs' by default)
161 function getTable( $db ) {
162 $table = $db->getLBInfo( 'blobs table' );
163 if ( is_null( $table ) ) {
171 * Fetch a blob item out of the database; a cache of the last-loaded
172 * blob will be kept so that multiple loads out of a multi-item blob
173 * can avoid redundant database access and decompression.
174 * @param string $cluster
176 * @param string $itemID
177 * @return HistoryBlob|bool Returns false if missing
180 function fetchBlob( $cluster, $id, $itemID ) {
182 * One-step cache variable to hold base blobs; operations that
183 * pull multiple revisions may often pull multiple times from
184 * the same blob. By keeping the last-used one open, we avoid
185 * redundant unserialization and decompression overhead.
187 static $externalBlobCache = [];
189 $cacheID = ( $itemID === false ) ?
"$cluster/$id" : "$cluster/$id/";
190 if ( isset( $externalBlobCache[$cacheID] ) ) {
191 wfDebugLog( 'ExternalStoreDB-cache',
192 "ExternalStoreDB::fetchBlob cache hit on $cacheID" );
194 return $externalBlobCache[$cacheID];
197 wfDebugLog( 'ExternalStoreDB-cache',
198 "ExternalStoreDB::fetchBlob cache miss on $cacheID" );
200 $dbr = $this->getSlave( $cluster );
201 $ret = $dbr->selectField( $this->getTable( $dbr ),
202 'blob_text', [ 'blob_id' => $id ], __METHOD__
);
203 if ( $ret === false ) {
204 wfDebugLog( 'ExternalStoreDB',
205 "ExternalStoreDB::fetchBlob master fallback on $cacheID" );
207 $dbw = $this->getMaster( $cluster );
208 $ret = $dbw->selectField( $this->getTable( $dbw ),
209 'blob_text', [ 'blob_id' => $id ], __METHOD__
);
210 if ( $ret === false ) {
211 wfDebugLog( 'ExternalStoreDB',
212 "ExternalStoreDB::fetchBlob master failed to find $cacheID" );
215 if ( $itemID !== false && $ret !== false ) {
216 // Unserialise object; caller extracts item
217 $ret = unserialize( $ret );
220 $externalBlobCache = [ $cacheID => $ret ];
226 * Fetch multiple blob items out of the database
228 * @param string $cluster A cluster name valid for use with LBFactory
229 * @param array $ids A map from the blob_id's to look for to the requested itemIDs in the blobs
230 * @return array A map from the blob_id's requested to their content.
231 * Unlocated ids are not represented
233 function batchFetchBlobs( $cluster, array $ids ) {
234 $dbr = $this->getSlave( $cluster );
235 $res = $dbr->select( $this->getTable( $dbr ),
236 [ 'blob_id', 'blob_text' ], [ 'blob_id' => array_keys( $ids ) ], __METHOD__
);
238 if ( $res !== false ) {
239 $this->mergeBatchResult( $ret, $ids, $res );
242 wfDebugLog( __CLASS__
, __METHOD__
.
243 " master fallback on '$cluster' for: " .
244 implode( ',', array_keys( $ids ) ) );
246 $dbw = $this->getMaster( $cluster );
247 $res = $dbw->select( $this->getTable( $dbr ),
248 [ 'blob_id', 'blob_text' ],
249 [ 'blob_id' => array_keys( $ids ) ],
251 if ( $res === false ) {
252 wfDebugLog( __CLASS__
, __METHOD__
. " master failed on '$cluster'" );
254 $this->mergeBatchResult( $ret, $ids, $res );
258 wfDebugLog( __CLASS__
, __METHOD__
.
259 " master on '$cluster' failed locating items: " .
260 implode( ',', array_keys( $ids ) ) );
267 * Helper function for self::batchFetchBlobs for merging master/slave results
268 * @param array &$ret Current self::batchFetchBlobs return value
269 * @param array &$ids Map from blob_id to requested itemIDs
270 * @param mixed $res DB result from Database::select
272 private function mergeBatchResult( array &$ret, array &$ids, $res ) {
273 foreach ( $res as $row ) {
275 $itemIDs = $ids[$id];
276 unset( $ids[$id] ); // to track if everything is found
277 if ( count( $itemIDs ) === 1 && reset( $itemIDs ) === false ) {
278 // single result stored per blob
279 $ret[$id] = $row->blob_text
;
281 // multi result stored per blob
282 $ret[$id] = unserialize( $row->blob_text
);
291 protected function parseURL( $url ) {
292 $path = explode( '/', $url );
297 isset( $path[4] ) ?
$path[4] : false // itemID