MediaWiki REL1_28
ExternalStoreDB.php
Go to the documentation of this file.
1<?php
40 public function fetchFromURL( $url ) {
41 list( $cluster, $id, $itemID ) = $this->parseURL( $url );
42 $ret = $this->fetchBlob( $cluster, $id, $itemID );
43
44 if ( $itemID !== false && $ret !== false ) {
45 return $ret->getItem( $itemID );
46 }
47
48 return $ret;
49 }
50
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;
68 }
69 $ret = [];
70 foreach ( $batched as $cluster => $batchByCluster ) {
71 $res = $this->batchFetchBlobs( $cluster, $batchByCluster );
73 foreach ( $res as $id => $blob ) {
74 foreach ( $batchByCluster[$id] as $itemID ) {
75 $url = $inverseUrlMap[$cluster][$id][$itemID];
76 if ( $itemID === false ) {
77 $ret[$url] = $blob;
78 } else {
79 $ret[$url] = $blob->getItem( $itemID );
80 }
81 }
82 }
83 }
84
85 return $ret;
86 }
87
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 ],
93 __METHOD__ );
94 $id = $dbw->insertId();
95 if ( !$id ) {
96 throw new MWException( __METHOD__ . ': no insert ID' );
97 }
98
99 return "DB://$location/$id";
100 }
101
108 function getLoadBalancer( $cluster ) {
109 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
110
111 return wfGetLBFactory()->getExternalLB( $cluster, $wiki );
112 }
113
120 function getSlave( $cluster ) {
122
123 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
124 $lb = $this->getLoadBalancer( $cluster );
125
126 if ( !in_array( "DB://" . $cluster, (array)$wgDefaultExternalStore ) ) {
127 wfDebug( "read only external store\n" );
128 $lb->allowLagged( true );
129 } else {
130 wfDebug( "writable external store\n" );
131 }
132
133 $db = $lb->getConnectionRef( DB_REPLICA, [], $wiki );
134 $db->clearFlag( DBO_TRX ); // sanity
135
136 return $db;
137 }
138
145 function getMaster( $cluster ) {
146 $wiki = isset( $this->params['wiki'] ) ? $this->params['wiki'] : false;
147 $lb = $this->getLoadBalancer( $cluster );
148
149 $db = $lb->getConnectionRef( DB_MASTER, [], $wiki );
150 $db->clearFlag( DBO_TRX ); // sanity
151
152 return $db;
153 }
154
161 function getTable( $db ) {
162 $table = $db->getLBInfo( 'blobs table' );
163 if ( is_null( $table ) ) {
164 $table = 'blobs';
165 }
166
167 return $table;
168 }
169
180 function fetchBlob( $cluster, $id, $itemID ) {
187 static $externalBlobCache = [];
188
189 $cacheID = ( $itemID === false ) ? "$cluster/$id" : "$cluster/$id/";
190 if ( isset( $externalBlobCache[$cacheID] ) ) {
191 wfDebugLog( 'ExternalStoreDB-cache',
192 "ExternalStoreDB::fetchBlob cache hit on $cacheID" );
193
194 return $externalBlobCache[$cacheID];
195 }
196
197 wfDebugLog( 'ExternalStoreDB-cache',
198 "ExternalStoreDB::fetchBlob cache miss on $cacheID" );
199
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" );
206 // Try the master
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" );
213 }
214 }
215 if ( $itemID !== false && $ret !== false ) {
216 // Unserialise object; caller extracts item
217 $ret = unserialize( $ret );
218 }
219
220 $externalBlobCache = [ $cacheID => $ret ];
221
222 return $ret;
223 }
224
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__ );
237 $ret = [];
238 if ( $res !== false ) {
239 $this->mergeBatchResult( $ret, $ids, $res );
240 }
241 if ( $ids ) {
242 wfDebugLog( __CLASS__, __METHOD__ .
243 " master fallback on '$cluster' for: " .
244 implode( ',', array_keys( $ids ) ) );
245 // Try the master
246 $dbw = $this->getMaster( $cluster );
247 $res = $dbw->select( $this->getTable( $dbr ),
248 [ 'blob_id', 'blob_text' ],
249 [ 'blob_id' => array_keys( $ids ) ],
250 __METHOD__ );
251 if ( $res === false ) {
252 wfDebugLog( __CLASS__, __METHOD__ . " master failed on '$cluster'" );
253 } else {
254 $this->mergeBatchResult( $ret, $ids, $res );
255 }
256 }
257 if ( $ids ) {
258 wfDebugLog( __CLASS__, __METHOD__ .
259 " master on '$cluster' failed locating items: " .
260 implode( ',', array_keys( $ids ) ) );
261 }
262
263 return $ret;
264 }
265
272 private function mergeBatchResult( array &$ret, array &$ids, $res ) {
273 foreach ( $res as $row ) {
274 $id = $row->blob_id;
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;
280 } else {
281 // multi result stored per blob
282 $ret[$id] = unserialize( $row->blob_text );
283 }
284 }
285 }
286
291 protected function parseURL( $url ) {
292 $path = explode( '/', $url );
293
294 return [
295 $path[2], // cluster
296 $path[3], // id
297 isset( $path[4] ) ? $path[4] : false // itemID
298 ];
299 }
300}
unserialize( $serialized)
array $wgDefaultExternalStore
The place to put new revisions, false to put them in the local text table.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
wfGetLBFactory()
Get the load balancer factory object.
DB accessable external objects.
getSlave( $cluster)
Get a replica DB connection for the specified cluster.
batchFetchBlobs( $cluster, array $ids)
Fetch multiple blob items out of the database.
mergeBatchResult(array &$ret, array &$ids, $res)
Helper function for self::batchFetchBlobs for merging master/replica DB results.
getMaster( $cluster)
Get a master database connection for the specified cluster.
fetchBlob( $cluster, $id, $itemID)
Fetch a blob item out of the database; a cache of the last-loaded blob will be kept so that multiple ...
getTable( $db)
Get the 'blobs' table name for this database.
fetchFromURL( $url)
The provided URL is in the form of DB://cluster/id or DB://cluster/id/itemid for concatened storage.
store( $location, $data)
Insert a data item into a given location.
batchFetchFromURLs(array $urls)
Fetch data from given external store URLs.
getLoadBalancer( $cluster)
Get a LoadBalancer for the specified cluster.
Accessable external objects in a particular storage medium.
MediaWiki exception.
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1949
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
const DB_REPLICA
Definition defines.php:22
const DB_MASTER
Definition defines.php:23
const DBO_TRX
Definition defines.php:9