MediaWiki master
NameTableStore.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Storage;
22
23use Exception;
24use Psr\Log\LoggerInterface;
26use Wikimedia\Assert\Assert;
32use Wikimedia\RequestTimeout\TimeoutException;
33
39
41 private $loadBalancer;
42
44 private $cache;
45
47 private $logger;
48
50 private $tableCache = null;
51
53 private $domain;
54
56 private $cacheTTL;
57
59 private $table;
61 private $idField;
63 private $nameField;
65 private $normalizationCallback;
67 private $insertCallback;
68
87 public function __construct(
88 ILoadBalancer $dbLoadBalancer,
89 WANObjectCache $cache,
90 LoggerInterface $logger,
91 $table,
92 $idField,
93 $nameField,
94 callable $normalizationCallback = null,
95 $dbDomain = false,
96 callable $insertCallback = null
97 ) {
98 $this->loadBalancer = $dbLoadBalancer;
99 $this->cache = $cache;
100 $this->logger = $logger;
101 $this->table = $table;
102 $this->idField = $idField;
103 $this->nameField = $nameField;
104 $this->normalizationCallback = $normalizationCallback;
105 $this->domain = $dbDomain;
106 $this->cacheTTL = ExpirationAwareness::TTL_MONTH;
107 $this->insertCallback = $insertCallback;
108 }
109
115 private function getDBConnection( $index, $flags = 0 ) {
116 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
117 }
118
127 private function getCacheKey() {
128 return $this->cache->makeGlobalKey(
129 'NameTableSqlStore',
130 $this->table,
131 $this->loadBalancer->resolveDomainID( $this->domain )
132 );
133 }
134
139 private function normalizeName( $name ) {
140 if ( $this->normalizationCallback === null ) {
141 return $name;
142 }
143 return call_user_func( $this->normalizationCallback, $name );
144 }
145
163 public function acquireId( string $name ) {
164 $name = $this->normalizeName( $name );
165
166 $table = $this->getTableFromCachesOrReplica();
167 $searchResult = array_search( $name, $table, true );
168 if ( $searchResult === false ) {
169 $id = $this->store( $name );
170 if ( $id === null ) {
171 // RACE: $name was already in the db, probably just inserted, so load from primary DB.
172 // Use DBO_TRX to avoid missing inserts due to other threads or REPEATABLE-READs.
173 $table = $this->reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
174
175 $searchResult = array_search( $name, $table, true );
176 if ( $searchResult === false ) {
177 // Insert failed due to IGNORE flag, but DB_PRIMARY didn't give us the data
178 $m = "No insert possible but primary DB didn't give us a record for " .
179 "'{$name}' in '{$this->table}'";
180 $this->logger->error( $m );
181 throw new NameTableAccessException( $m );
182 }
183 } else {
184 if ( isset( $table[$id] ) ) {
185 // This can happen when a transaction is rolled back and acquireId is called in
186 // an onTransactionResolution() callback, which gets executed before retryStore()
187 // has a chance to run. The right thing to do in this case is to discard the old
188 // value. According to the contract of acquireId, the caller should not have
189 // used it outside the transaction, so it should not be persisted anywhere after
190 // the rollback.
191 $m = "Got ID $id for '$name' from insert"
192 . " into '{$this->table}', but ID $id was previously associated with"
193 . " the name '{$table[$id]}'. Overriding the old value, which presumably"
194 . " has been removed from the database due to a transaction rollback.";
195
196 $this->logger->warning( $m );
197 }
198
199 $table[$id] = $name;
200 $searchResult = $id;
201
202 // As store returned an ID we know we inserted so delete from WAN cache
203 $dbw = $this->getDBConnection( DB_PRIMARY );
204 $dbw->onTransactionPreCommitOrIdle( function () {
205 $this->cache->delete( $this->getCacheKey(), WANObjectCache::HOLDOFF_TTL_NONE );
206 }, __METHOD__ );
207 }
208 $this->tableCache = $table;
209 }
210
211 return $searchResult;
212 }
213
226 public function reloadMap( $connFlags = 0 ) {
227 if ( $connFlags !== 0 && defined( 'MW_PHPUNIT_TEST' ) ) {
228 // HACK: We can't use $connFlags while doing PHPUnit tests, because the
229 // fake database tables are bound to a single connection.
230 $connFlags = 0;
231 }
232
233 $dbw = $this->getDBConnection( DB_PRIMARY, $connFlags );
234 $this->tableCache = $this->loadTable( $dbw );
235 $dbw->onTransactionPreCommitOrIdle( function () {
236 $this->cache->delete( $this->getCacheKey() );
237 }, __METHOD__ );
238
239 return $this->tableCache;
240 }
241
252 public function getId( string $name ) {
253 $name = $this->normalizeName( $name );
254
255 $table = $this->getTableFromCachesOrReplica();
256 $searchResult = array_search( $name, $table, true );
257
258 if ( $searchResult !== false ) {
259 return $searchResult;
260 }
261
262 throw NameTableAccessException::newFromDetails( $this->table, 'name', $name );
263 }
264
276 public function getName( int $id ) {
277 $table = $this->getTableFromCachesOrReplica();
278 if ( array_key_exists( $id, $table ) ) {
279 return $table[$id];
280 }
281 $fname = __METHOD__;
282
283 $table = $this->cache->getWithSetCallback(
284 $this->getCacheKey(),
285 $this->cacheTTL,
286 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
287 // Check if cached value is up-to-date enough to have $id
288 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
289 // Completely leave the cache key alone
290 $ttl = WANObjectCache::TTL_UNCACHEABLE;
291 // Use the old value
292 return $oldValue;
293 }
294 // Regenerate from replica DB, and primary DB if needed
295 foreach ( [ DB_REPLICA, DB_PRIMARY ] as $source ) {
296 // Log a fallback to primary
297 if ( $source === DB_PRIMARY ) {
298 $this->logger->info(
299 $fname . ' falling back to primary select from ' .
300 $this->table . ' with id ' . $id
301 );
302 }
303 $db = $this->getDBConnection( $source );
304 $cacheSetOpts = Database::getCacheSetOptions( $db );
305 $table = $this->loadTable( $db );
306 if ( array_key_exists( $id, $table ) ) {
307 break; // found it
308 }
309 }
310 // Use the value from last source checked
311 $setOpts += $cacheSetOpts;
312
313 return $table;
314 },
315 [ 'minAsOf' => INF ] // force callback run
316 );
317
318 $this->tableCache = $table;
319
320 if ( array_key_exists( $id, $table ) ) {
321 return $table[$id];
322 }
323
324 throw NameTableAccessException::newFromDetails( $this->table, 'id', $id );
325 }
326
334 public function getMap() {
335 return $this->getTableFromCachesOrReplica();
336 }
337
341 private function getTableFromCachesOrReplica() {
342 if ( $this->tableCache !== null ) {
343 return $this->tableCache;
344 }
345
346 $table = $this->cache->getWithSetCallback(
347 $this->getCacheKey(),
348 $this->cacheTTL,
349 function ( $oldValue, &$ttl, &$setOpts ) {
350 $dbr = $this->getDBConnection( DB_REPLICA );
351 $setOpts += Database::getCacheSetOptions( $dbr );
352 return $this->loadTable( $dbr );
353 }
354 );
355
356 $this->tableCache = $table;
357
358 return $table;
359 }
360
368 private function loadTable( IReadableDatabase $db ) {
369 $result = $db->newSelectQueryBuilder()
370 ->select( [
371 'id' => $this->idField,
372 'name' => $this->nameField
373 ] )
374 ->from( $this->table )
375 ->orderBy( 'id' )
376 ->caller( __METHOD__ )->fetchResultSet();
377
378 $assocArray = [];
379 foreach ( $result as $row ) {
380 $assocArray[$row->id] = $row->name;
381 }
382
383 return $assocArray;
384 }
385
392 private function store( string $name ) {
393 Assert::parameter( $name !== '', '$name', 'should not be an empty string' );
394 // Note: this is only called internally so normalization of $name has already occurred.
395
396 $dbw = $this->getDBConnection( DB_PRIMARY );
397
398 $id = null;
399 $dbw->doAtomicSection(
400 __METHOD__,
401 function ( IDatabase $unused, $fname )
402 use ( $name, &$id, $dbw ) {
403 // NOTE: use IDatabase from the parent scope here, not the function parameter.
404 // If $dbw is a wrapper around the actual DB, we need to call the wrapper here,
405 // not the inner instance.
406 $dbw->newInsertQueryBuilder()
407 ->insertInto( $this->table )
408 ->ignore()
409 ->row( $this->getFieldsToStore( $name ) )
410 ->caller( $fname )->execute();
411
412 if ( $dbw->affectedRows() === 0 ) {
413 $this->logger->info(
414 'Tried to insert name into table ' . $this->table . ', but value already existed.'
415 );
416
417 return;
418 }
419
420 $id = $dbw->insertId();
421
422 // Any open transaction may still be rolled back. If that happens, we have to re-try the
423 // insertion and restore a consistent state of the cached table.
424 $dbw->onAtomicSectionCancel(
425 function ( $trigger, IDatabase $unused ) use ( $name, $id, $dbw ) {
426 $this->retryStore( $dbw, $name, $id );
427 },
428 $fname );
429 },
430 IDatabase::ATOMIC_CANCELABLE
431 );
432
433 return $id;
434 }
435
444 private function retryStore( IDatabase $dbw, $name, $id ) {
445 // NOTE: in the closure below, use the IDatabase from the original method call,
446 // not the one passed to the closure as a parameter.
447 // If $dbw is a wrapper around the actual DB, we need to call the wrapper,
448 // not the inner instance.
449
450 try {
451 $dbw->doAtomicSection(
452 __METHOD__,
453 function ( IDatabase $unused, $fname ) use ( $name, $id, $dbw ) {
454 // Try to insert a row with the ID we originally got.
455 // If that fails (because of a key conflict), we will just try to get another ID again later.
456 $dbw->newInsertQueryBuilder()
457 ->insertInto( $this->table )
458 ->row( $this->getFieldsToStore( $name, $id ) )
459 ->caller( $fname )->execute();
460
461 // Make sure we re-load the map in case this gets rolled back again.
462 // We could re-try once more, but that bears the risk of an infinite loop.
463 // So let's just give up on the ID.
464 $dbw->onAtomicSectionCancel(
465 function ( $trigger, IDatabase $unused ) {
466 $this->logger->warning(
467 'Re-insertion of name into table ' . $this->table
468 . ' was rolled back. Giving up and reloading the cache.'
469 );
470 $this->reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
471 },
472 $fname
473 );
474
475 $this->logger->info(
476 'Re-insert name into table ' . $this->table . ' after failed transaction.'
477 );
478 },
479 IDatabase::ATOMIC_CANCELABLE
480 );
481 } catch ( TimeoutException $e ) {
482 throw $e;
483 } catch ( Exception $ex ) {
484 $this->logger->error(
485 'Re-insertion of name into table ' . $this->table . ' failed: ' . $ex->getMessage()
486 );
487 } finally {
488 // NOTE: we reload regardless of whether the above insert succeeded. There is
489 // only three possibilities: the insert succeeded, so the new map will have
490 // the desired $id/$name mapping. Or the insert failed because another
491 // process already inserted that same $id/$name mapping, in which case the
492 // new map will also have it. Or another process grabbed the desired ID for
493 // another name, or the database refuses to insert the given ID into the
494 // auto increment field - in that case, the new map will not have a mapping
495 // for $name (or has a different mapping for $name). In that last case, we can
496 // only hope that the ID produced within the failed transaction has not been
497 // used outside that transaction.
498
499 $this->reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
500 }
501 }
502
508 private function getFieldsToStore( $name, $id = null ) {
509 $fields = [];
510
511 $fields[$this->nameField] = $name;
512
513 if ( $id !== null ) {
514 $fields[$this->idField] = $id;
515 }
516
517 if ( $this->insertCallback !== null ) {
518 $fields = call_user_func( $this->insertCallback, $fields );
519 }
520 return $fields;
521 }
522
523}
Exception representing a failure to look up a row from a name table.
static newFromDetails( $tableName, $accessType, $accessValue)
acquireId(string $name)
Acquire the id of the given name.
getId(string $name)
Get the id of the given name.
getMap()
Get the whole table, in no particular order as a map of ids to names.
getName(int $id)
Get the name of the given id.
reloadMap( $connFlags=0)
Reloads the name table from the primary database, and purges the WAN cache entry.
__construct(ILoadBalancer $dbLoadBalancer, WANObjectCache $cache, LoggerInterface $logger, $table, $idField, $nameField, callable $normalizationCallback=null, $dbDomain=false, callable $insertCallback=null)
Multi-datacenter aware caching interface.
static getCacheSetOptions(?IReadableDatabase ... $dbs)
Merge the result of getSessionLagStatus() for several DBs using the most pessimistic values to estima...
Generic interface providing Time-To-Live constants for expirable object storage.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
This class is a delegate to ILBFactory for a given database cluster.
A database connection without write operations.
$source
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28