40 private $loadBalancer;
49 private $tableCache =
null;
64 private $normalizationCallback;
66 private $insertCallback;
89 LoggerInterface $logger,
93 callable $normalizationCallback =
null,
95 callable $insertCallback =
null
97 $this->loadBalancer = $dbLoadBalancer;
98 $this->cache = $cache;
99 $this->logger = $logger;
100 $this->table = $table;
101 $this->idField = $idField;
102 $this->nameField = $nameField;
103 $this->normalizationCallback = $normalizationCallback;
104 $this->domain = $dbDomain;
105 $this->cacheTTL = IExpiringStore::TTL_MONTH;
106 $this->insertCallback = $insertCallback;
114 private function getDBConnection( $index, $flags = 0 ) {
115 return $this->loadBalancer->getConnection( $index, [], $this->domain, $flags );
126 private function getCacheKey() {
127 return $this->cache->makeGlobalKey(
130 $this->loadBalancer->resolveDomainID( $this->domain )
138 private function normalizeName( $name ) {
139 if ( $this->normalizationCallback ===
null ) {
142 return call_user_func( $this->normalizationCallback, $name );
163 $name = $this->normalizeName( $name );
165 $table = $this->getTableFromCachesOrReplica();
166 $searchResult = array_search( $name, $table,
true );
167 if ( $searchResult ===
false ) {
168 $id = $this->store( $name );
169 if ( $id ===
null ) {
172 $table = $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
174 $searchResult = array_search( $name, $table,
true );
175 if ( $searchResult ===
false ) {
177 $m =
"No insert possible but primary DB didn't give us a record for " .
178 "'{$name}' in '{$this->table}'";
179 $this->logger->error( $m );
183 if ( isset( $table[$id] ) ) {
190 $m =
"Got ID $id for '$name' from insert"
191 .
" into '{$this->table}', but ID $id was previously associated with"
192 .
" the name '{$table[$id]}'. Overriding the old value, which presumably"
193 .
" has been removed from the database due to a transaction rollback.";
195 $this->logger->warning( $m );
203 $dbw->onTransactionPreCommitOrIdle(
function () {
204 $this->cache->delete( $this->getCacheKey(), WANObjectCache::HOLDOFF_TTL_NONE );
207 $this->tableCache = $table;
210 return $searchResult;
226 if ( $connFlags !== 0 && defined(
'MW_PHPUNIT_TEST' ) ) {
232 $dbw = $this->getDBConnection(
DB_PRIMARY, $connFlags );
233 $this->tableCache = $this->loadTable( $dbw );
234 $dbw->onTransactionPreCommitOrIdle(
function () {
235 $this->cache->delete( $this->getCacheKey() );
238 return $this->tableCache;
251 public function getId(
string $name ) {
252 $name = $this->normalizeName( $name );
254 $table = $this->getTableFromCachesOrReplica();
255 $searchResult = array_search( $name, $table,
true );
257 if ( $searchResult !==
false ) {
258 return $searchResult;
276 $table = $this->getTableFromCachesOrReplica();
277 if ( array_key_exists( $id, $table ) ) {
282 $table = $this->cache->getWithSetCallback(
283 $this->getCacheKey(),
285 function ( $oldValue, &$ttl, &$setOpts ) use ( $id, $fname ) {
287 if ( is_array( $oldValue ) && array_key_exists( $id, $oldValue ) ) {
289 $ttl = WANObjectCache::TTL_UNCACHEABLE;
298 $fname .
' falling back to primary select from ' .
299 $this->table .
' with id ' . $id
302 $db = $this->getDBConnection(
$source );
304 $table = $this->loadTable( $db );
305 if ( array_key_exists( $id, $table ) ) {
310 $setOpts += $cacheSetOpts;
317 $this->tableCache = $table;
319 if ( array_key_exists( $id, $table ) ) {
334 return $this->getTableFromCachesOrReplica();
340 private function getTableFromCachesOrReplica() {
341 if ( $this->tableCache !==
null ) {
342 return $this->tableCache;
345 $table = $this->cache->getWithSetCallback(
346 $this->getCacheKey(),
348 function ( $oldValue, &$ttl, &$setOpts ) {
351 return $this->loadTable(
$dbr );
355 $this->tableCache = $table;
367 private function loadTable( IDatabase $db ) {
368 $result = $db->newSelectQueryBuilder()
370 'id' => $this->idField,
371 'name' => $this->nameField
373 ->from( $this->table )
375 ->caller( __METHOD__ )->fetchResultSet();
378 foreach ( $result as $row ) {
379 $assocArray[$row->id] = $row->name;
391 private function store(
string $name ) {
392 Assert::parameter( $name !==
'',
'$name',
'should not be an empty string' );
398 $dbw->doAtomicSection(
400 function ( IDatabase $unused, $fname )
401 use ( $name, &$id, $dbw ) {
407 $this->getFieldsToStore( $name ),
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 ) {
458 $this->getFieldsToStore( $name, $id ),
465 $dbw->onAtomicSectionCancel(
466 function ( $trigger, IDatabase $unused ) {
467 $this->logger->warning(
468 'Re-insertion of name into table ' . $this->table
469 .
' was rolled back. Giving up and reloading the cache.'
471 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
477 'Re-insert name into table ' . $this->table .
' after failed transaction.'
480 IDatabase::ATOMIC_CANCELABLE
482 }
catch ( TimeoutException $e ) {
484 }
catch ( Exception $ex ) {
485 $this->logger->error(
486 'Re-insertion of name into table ' . $this->table .
' failed: ' . $ex->getMessage()
500 $this->
reloadMap( ILoadBalancer::CONN_TRX_AUTOCOMMIT );
509 private function getFieldsToStore( $name, $id =
null ) {
512 $fields[$this->nameField] = $name;
514 if ( $id !==
null ) {
515 $fields[$this->idField] = $id;
518 if ( $this->insertCallback !==
null ) {
519 $fields = call_user_func( $this->insertCallback, $fields );