41 private $loadBalancer;
50 private $tableCache =
null;
65 private $normalizationCallback;
67 private $insertCallback;
90 LoggerInterface $logger,
94 callable $normalizationCallback =
null,
96 callable $insertCallback =
null
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;
115 private function getDBConnection( $index, $flags = 0 ) {
116 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
127 private function getCacheKey() {
128 return $this->cache->makeGlobalKey(
131 $this->loadBalancer->resolveDomainID( $this->domain )
139 private function normalizeName( $name ) {
140 if ( $this->normalizationCallback ===
null ) {
143 return call_user_func( $this->normalizationCallback, $name );
164 $name = $this->normalizeName( $name );
166 $table = $this->getTableFromCachesOrReplica();
167 $searchResult = array_search( $name, $table,
true );
168 if ( $searchResult ===
false ) {
169 $id = $this->store( $name );
170 if ( $id ===
null ) {
173 $table = $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
175 $searchResult = array_search( $name, $table,
true );
176 if ( $searchResult ===
false ) {
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 );
184 if ( isset( $table[$id] ) ) {
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.";
196 $this->logger->warning( $m );
204 $dbw->onTransactionPreCommitOrIdle(
function () {
205 $this->cache->delete( $this->getCacheKey(), WANObjectCache::HOLDOFF_TTL_NONE );
208 $this->tableCache = $table;
211 return $searchResult;
227 if ( $connFlags !== 0 && defined(
'MW_PHPUNIT_TEST' ) ) {
233 $dbw = $this->getDBConnection(
DB_PRIMARY, $connFlags );
234 $this->tableCache = $this->loadTable( $dbw );
235 $dbw->onTransactionPreCommitOrIdle(
function () {
236 $this->cache->delete( $this->getCacheKey() );
239 return $this->tableCache;
252 public function getId(
string $name ) {
253 $name = $this->normalizeName( $name );
255 $table = $this->getTableFromCachesOrReplica();
256 $searchResult = array_search( $name, $table,
true );
258 if ( $searchResult !==
false ) {
259 return $searchResult;
277 $table = $this->getTableFromCachesOrReplica();
278 if ( array_key_exists( $id, $table ) ) {
283 $table = $this->cache->getWithSetCallback(
284 $this->getCacheKey(),
286 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
288 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
290 $ttl = WANObjectCache::TTL_UNCACHEABLE;
299 $fname .
' falling back to primary select from ' .
300 $this->table .
' with id ' . $id
303 $db = $this->getDBConnection(
$source );
305 $table = $this->loadTable( $db );
306 if ( array_key_exists( $id, $table ) ) {
311 $setOpts += $cacheSetOpts;
318 $this->tableCache = $table;
320 if ( array_key_exists( $id, $table ) ) {
335 return $this->getTableFromCachesOrReplica();
341 private function getTableFromCachesOrReplica() {
342 if ( $this->tableCache !==
null ) {
343 return $this->tableCache;
346 $table = $this->cache->getWithSetCallback(
347 $this->getCacheKey(),
349 function ( $oldValue, &$ttl, &$setOpts ) {
352 return $this->loadTable( $dbr );
356 $this->tableCache = $table;
368 private function loadTable( IReadableDatabase $db ) {
369 $result = $db->newSelectQueryBuilder()
371 'id' => $this->idField,
372 'name' => $this->nameField
374 ->from( $this->table )
376 ->caller( __METHOD__ )->fetchResultSet();
379 foreach ( $result as $row ) {
380 $assocArray[$row->id] = $row->name;
392 private function store(
string $name ) {
393 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
399 $dbw->doAtomicSection(
401 function ( IDatabase $unused, $fname )
402 use ( $name, &$id, $dbw ) {
406 $dbw->newInsertQueryBuilder()
407 ->insertInto( $this->table )
409 ->row( $this->getFieldsToStore( $name ) )
410 ->caller( $fname )->execute();
412 if ( $dbw->affectedRows() === 0 ) {
414 'Tried to insert name into table ' . $this->table .
', but value already existed.'
420 $id = $dbw->insertId();
424 $dbw->onAtomicSectionCancel(
425 function ( $trigger, IDatabase $unused ) use ( $name, $id, $dbw ) {
426 $this->retryStore( $dbw, $name, $id );
430 IDatabase::ATOMIC_CANCELABLE
444 private function retryStore( IDatabase $dbw, $name, $id ) {
451 $dbw->doAtomicSection(
453 function ( IDatabase $unused, $fname ) use ( $name, $id, $dbw ) {
456 $dbw->newInsertQueryBuilder()
457 ->insertInto( $this->table )
458 ->row( $this->getFieldsToStore( $name, $id ) )
459 ->caller( $fname )->execute();
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.'
470 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
476 'Re-insert name into table ' . $this->table .
' after failed transaction.'
479 IDatabase::ATOMIC_CANCELABLE
481 }
catch ( TimeoutException $e ) {
483 }
catch ( Exception $ex ) {
484 $this->logger->error(
485 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
499 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
508 private function getFieldsToStore( $name, $id =
null ) {
511 $fields[$this->nameField] = $name;
513 if ( $id !==
null ) {
514 $fields[$this->idField] = $id;
517 if ( $this->insertCallback !==
null ) {
518 $fields = call_user_func( $this->insertCallback, $fields );