Replace getMasterDB methods with getPrimaryDB

Bug: T254646
Change-Id: I68198bc39b174ea1920b4acc2617cb6c6ce406e9
This commit is contained in:
James D. Forrester 2021-04-18 18:32:42 -07:00 committed by Aaron Schulz
parent 342759f9e0
commit 597376e12c
17 changed files with 100 additions and 60 deletions

View file

@ -353,6 +353,9 @@ because of Phabricator reports.
Use getPage() and setPage() instead.
* Title::isWatchable() has been deprecated. Use WatchlistManager::isWatchable()
instead.
* The 'getMasterDB()' methods for LocalRepo (and so ForeignDBRepo), JobQueueDB,
ForeignDBViaLBRepo, and DBFileJournal have been deprecated in favour of a new
method, getPrimaryDB().
* wfIncrStats(), deprecated in 1.36, now emits deprecation warnings.
* wfCanIPUseHTTPS() is now deprecated, and always returns true.
* The UserLoadFromDatabase hook has been deprecated. It had no known uses.

View file

@ -56,7 +56,7 @@ class DBFileJournal extends FileJournal {
$status = StatusValue::newGood();
try {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
} catch ( DBError $e ) {
$status->fatal( 'filejournal-fail-dbconnect', $this->backend );
@ -99,7 +99,7 @@ class DBFileJournal extends FileJournal {
* @return int|false The value from the field, or false on failure.
*/
protected function doGetCurrentPosition() {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
return $dbw->selectField( 'filejournal', 'MAX(fj_id)',
[ 'fj_backend' => $this->backend ],
@ -113,7 +113,7 @@ class DBFileJournal extends FileJournal {
* @return int|false The value from the field, or false on failure.
*/
protected function doGetPositionAtTime( $time ) {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
$encTimestamp = $dbw->addQuotes( $dbw->timestamp( $time ) );
@ -131,7 +131,7 @@ class DBFileJournal extends FileJournal {
* @return array[]
*/
protected function doGetChangeEntries( $start, $limit ) {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
$res = $dbw->select( 'filejournal', '*',
[
@ -165,7 +165,7 @@ class DBFileJournal extends FileJournal {
return $status; // nothing to do
}
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
$dbCutoff = $dbw->timestamp( ConvertibleTimestamp::time() - 86400 * $this->ttlDays );
$dbw->delete( 'filejournal',
@ -177,14 +177,26 @@ class DBFileJournal extends FileJournal {
}
/**
* Get a master connection to the logging DB
* Get a primary connection to the logging DB
*
* @return IDatabase
* @throws DBError
*/
protected function getMasterDB() {
protected function getPrimaryDB() {
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
return $lb->getConnectionRef( DB_PRIMARY, [], $this->domain, $lb::CONN_TRX_AUTOCOMMIT );
}
/**
* Get a primary connection to the logging DB
*
* @deprecated since 1.37
* @return IDatabase
* @throws DBError
*/
protected function getMasterDB() {
// wfDeprecated( __METHOD__, '1.37' );
return $this->getPrimaryDB();
}
}

View file

@ -81,7 +81,7 @@ class ForeignDBRepo extends LocalRepo {
$this->dbDomain = $dbDomain->getId();
}
public function getMasterDB() {
public function getPrimaryDB() {
if ( !isset( $this->dbConn ) ) {
$func = $this->getDBFactory();
$this->dbConn = $func( DB_PRIMARY );
@ -91,7 +91,7 @@ class ForeignDBRepo extends LocalRepo {
}
public function getReplicaDB() {
return $this->getMasterDB();
return $this->getPrimaryDB();
}
/**

View file

@ -46,10 +46,15 @@ class ForeignDBViaLBRepo extends LocalRepo {
$this->hasAccessibleSharedCache = $info['hasSharedCache'];
}
public function getMasterDB() {
public function getPrimaryDB() {
return $this->getDBLoadBalancer()->getConnectionRef( DB_PRIMARY, [], $this->dbDomain );
}
public function getMasterDB() {
// wfDeprecated( __METHOD__, '1.37' );
return $this->getPrimaryDB();
}
public function getReplicaDB() {
return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->dbDomain );
}

View file

@ -149,7 +149,7 @@ class LocalRepo extends FileRepo {
$backend = $this->backend; // convenience
$root = $this->getZonePath( 'deleted' );
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
$status = $this->newGood();
$storageKeys = array_unique( $storageKeys );
foreach ( $storageKeys as $key ) {
@ -187,7 +187,7 @@ class LocalRepo extends FileRepo {
protected function deletedFileHasKey( $key, $lock = null ) {
$options = ( $lock === 'lock' ) ? [ 'FOR UPDATE' ] : [];
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
return (bool)$dbw->selectField( 'filearchive', '1',
[ 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ],
@ -208,7 +208,7 @@ class LocalRepo extends FileRepo {
$sha1 = self::getHashFromKey( $key );
$ext = File::normalizeExtension( substr( $key, strcspn( $key, '.' ) + 1 ) );
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
return (bool)$dbw->selectField( 'oldimage', '1',
[ 'oi_sha1' => $sha1,
@ -523,10 +523,20 @@ class LocalRepo extends FileRepo {
* Get a connection to the primary DB
* @return IDatabase
*/
public function getMasterDB() {
public function getPrimaryDB() {
return wfGetDB( DB_PRIMARY );
}
/**
* Get a connection to the primary DB
* @deprecated since 1.37
* @return IDatabase
*/
public function getMasterDB() {
// wfDeprecated( __METHOD__, '1.37' );
return $this->getPrimaryDB();
}
/**
* Get a callback to get a DB handle given an index (DB_REPLICA/DB_PRIMARY)
* @return Closure
@ -567,7 +577,7 @@ class LocalRepo extends FileRepo {
public function invalidateImageRedirect( $title ) {
$key = $this->getSharedCacheKey( 'file-redirect', md5( $title->getDBkey() ) );
if ( $key ) {
$this->getMasterDB()->onTransactionPreCommitOrIdle(
$this->getPrimaryDB()->onTransactionPreCommitOrIdle(
function () use ( $key ) {
$this->wanCache->delete( $key );
},

View file

@ -442,7 +442,7 @@ class LocalFile extends File {
return;
}
$this->repo->getMasterDB()->onTransactionPreCommitOrIdle(
$this->repo->getPrimaryDB()->onTransactionPreCommitOrIdle(
static function () use ( $key ) {
MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
},
@ -515,7 +515,7 @@ class LocalFile extends File {
$this->extraDataLoaded = true;
$dbr = ( $flags & self::READ_LATEST )
? $this->repo->getMasterDB()
? $this->repo->getPrimaryDB()
: $this->repo->getReplicaDB();
$fileQuery = static::getQueryInfo();
@ -553,7 +553,7 @@ class LocalFile extends File {
$db = $this->repo->getReplicaDB();
$fieldMap = $this->loadExtraFieldsWithTimestamp( $db, $fname );
if ( !$fieldMap ) {
$db = $this->repo->getMasterDB();
$db = $this->repo->getPrimaryDB();
$fieldMap = $this->loadExtraFieldsWithTimestamp( $db, $fname );
}
@ -818,7 +818,7 @@ class LocalFile extends File {
return;
}
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
list( $major, $minor ) = self::splitMime( $this->mime );
if ( wfReadOnly() ) {
@ -1869,7 +1869,7 @@ class LocalFile extends File {
bool $createNullRevision = true,
bool $revert = false
) : Status {
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
# Imports or such might force a certain timestamp; otherwise we generate
# it and can fudge it slightly to keep (name,timestamp) unique on re-upload.
@ -2137,13 +2137,13 @@ class LocalFile extends File {
# Also log page, in case where we just created it above
$update['log_page'] = $updateLogPage;
}
$this->getRepo()->getMasterDB()->update(
$this->getRepo()->getPrimaryDB()->update(
'logging',
$update,
[ 'log_id' => $logId ],
$fname
);
$this->getRepo()->getMasterDB()->insert(
$this->getRepo()->getPrimaryDB()->insert(
'log_search',
[
'ls_field' => 'associated_rev_id',
@ -2339,7 +2339,7 @@ class LocalFile extends File {
$newTitleFile = $localRepo->newFile( $target );
DeferredUpdates::addUpdate(
new AutoCommitUpdate(
$this->getRepo()->getMasterDB(),
$this->getRepo()->getPrimaryDB(),
__METHOD__,
static function () use ( $oldTitleFile, $newTitleFile, $archiveNames ) {
$oldTitleFile->purgeEverything();
@ -2402,7 +2402,7 @@ class LocalFile extends File {
// To avoid slow purges in the transaction, move them outside...
DeferredUpdates::addUpdate(
new AutoCommitUpdate(
$this->getRepo()->getMasterDB(),
$this->getRepo()->getPrimaryDB(),
__METHOD__,
function () use ( $archiveNames ) {
$this->purgeEverything();
@ -2642,7 +2642,7 @@ class LocalFile extends File {
$this->sha1 = $this->repo->getFileSha1( $this->getPath() );
if ( !wfReadOnly() && strval( $this->sha1 ) != '' ) {
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$dbw->update( 'image',
[ 'img_sha1' => $this->sha1 ],
[ 'img_name' => $this->getName() ],
@ -2700,7 +2700,7 @@ class LocalFile extends File {
if ( !$this->locked ) {
$logger = LoggerFactory::getInstance( 'LocalFile' );
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$makesTransaction = !$dbw->trxLevel();
$dbw->startAtomic( self::ATOMIC_SECTION_LOCK );
// T56736: use simple lock to handle when the file does not exist.
@ -2745,7 +2745,7 @@ class LocalFile extends File {
if ( $this->locked ) {
--$this->locked;
if ( !$this->locked ) {
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$dbw->endAtomic( self::ATOMIC_SECTION_LOCK );
$this->lockedOwnTrx = false;
}

View file

@ -92,7 +92,7 @@ class LocalFileDeleteBatch {
public function addOlds() {
$archiveNames = [];
$dbw = $this->file->repo->getMasterDB();
$dbw = $this->file->repo->getPrimaryDB();
$result = $dbw->select( 'oldimage',
[ 'oi_archive_name' ],
[ 'oi_name' => $this->file->getName() ],
@ -135,7 +135,7 @@ class LocalFileDeleteBatch {
}
if ( count( $oldRels ) ) {
$dbw = $this->file->repo->getMasterDB();
$dbw = $this->file->repo->getPrimaryDB();
$res = $dbw->select(
'oldimage',
[ 'oi_archive_name', 'oi_sha1' ],
@ -184,7 +184,7 @@ class LocalFileDeleteBatch {
protected function doDBInserts() {
$now = time();
$dbw = $this->file->repo->getMasterDB();
$dbw = $this->file->repo->getPrimaryDB();
$commentStore = MediaWikiServices::getInstance()->getCommentStore();
@ -293,7 +293,7 @@ class LocalFileDeleteBatch {
}
private function doDBDeletes() {
$dbw = $this->file->repo->getMasterDB();
$dbw = $this->file->repo->getPrimaryDB();
list( $oldRels, $deleteCurrent ) = $this->getOldRels();
if ( count( $oldRels ) ) {

View file

@ -82,7 +82,7 @@ class LocalFileMoveBatch {
$this->newName = $this->file->repo->getNameFromTitle( $this->target );
$this->oldRel = $this->oldHash . $this->oldName;
$this->newRel = $this->newHash . $this->newName;
$this->db = $file->getRepo()->getMasterDB();
$this->db = $file->getRepo()->getPrimaryDB();
$this->logger = LoggerFactory::getInstance( 'imagemove' );
}

View file

@ -97,7 +97,7 @@ class LocalFileRestoreBatch {
$lockOwnsTrx = $this->file->lock();
$dbw = $this->file->repo->getMasterDB();
$dbw = $this->file->repo->getPrimaryDB();
$commentStore = MediaWikiServices::getInstance()->getCommentStore();

View file

@ -244,7 +244,7 @@ class OldLocalFile extends LocalFile {
$this->dataLoaded = true;
$dbr = ( $flags & self::READ_LATEST )
? $this->repo->getMasterDB()
? $this->repo->getPrimaryDB()
: $this->repo->getReplicaDB();
$conds = [ 'oi_name' => $this->getName() ];
@ -294,7 +294,7 @@ class OldLocalFile extends LocalFile {
);
if ( !$row ) { // fallback to master
$dbr = $this->repo->getMasterDB();
$dbr = $this->repo->getPrimaryDB();
$row = $dbr->selectRow(
$fileQuery['tables'],
$fileQuery['fields'],
@ -355,7 +355,7 @@ class OldLocalFile extends LocalFile {
return;
}
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
list( $major, $minor ) = self::splitMime( $this->mime );
wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema" );
@ -461,7 +461,7 @@ class OldLocalFile extends LocalFile {
* @return bool
*/
protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$services = MediaWikiServices::getInstance();
$mwProps = new MWFileProps( $services->getMimeAnalyzer() );

View file

@ -199,7 +199,7 @@ class JobQueueDB extends JobQueue {
* @return void
*/
protected function doBatchPush( array $jobs, $flags ) {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
// In general, there will be two cases here:
@ -289,7 +289,7 @@ class JobQueueDB extends JobQueue {
* @return RunnableJob|bool
*/
protected function doPop() {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
@ -337,7 +337,7 @@ class JobQueueDB extends JobQueue {
* @return stdClass|bool Row|false
*/
protected function claimRandom( $uuid, $rand, $gte ) {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
// Check cache to see if the queue has <= OFFSET items
@ -415,7 +415,7 @@ class JobQueueDB extends JobQueue {
* @return stdClass|bool Row|false
*/
protected function claimOldest( $uuid ) {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
@ -483,7 +483,7 @@ class JobQueueDB extends JobQueue {
throw new MWException( "Job of type '{$job->getType()}' has no ID." );
}
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
try {
@ -512,7 +512,7 @@ class JobQueueDB extends JobQueue {
// is deferred till "transaction idle", do the same here, so that the ordering is
// maintained. Having only the de-duplication registration succeed would cause
// jobs to become no-ops without any actual jobs that made them redundant.
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
$dbw->onTransactionCommitOrIdle(
@ -530,7 +530,7 @@ class JobQueueDB extends JobQueue {
* @return bool
*/
protected function doDelete() {
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
try {
@ -668,7 +668,7 @@ class JobQueueDB extends JobQueue {
public function recycleAndDeleteStaleJobs() {
$now = time();
$count = 0; // affected rows
$dbw = $this->getMasterDB();
$dbw = $this->getPrimaryDB();
/** @noinspection PhpUnusedLocalVariableInspection */
$scope = $this->getScopedNoTrxFlag( $dbw );
@ -785,7 +785,7 @@ class JobQueueDB extends JobQueue {
* @throws JobQueueConnectionError
* @return IMaintainableDatabase
*/
protected function getMasterDB() {
protected function getPrimaryDB() {
try {
return $this->getDB( DB_PRIMARY );
} catch ( DBConnectionError $e ) {
@ -793,6 +793,16 @@ class JobQueueDB extends JobQueue {
}
}
/**
* @deprecated since 1.37
* @throws JobQueueConnectionError
* @return IMaintainableDatabase
*/
public function getMasterDB() {
// wfDeprecated( __METHOD__, '1.37' );
return $this->getPrimaryDB();
}
/**
* @param int $index (DB_REPLICA/DB_PRIMARY)
* @return IMaintainableDatabase

View file

@ -76,7 +76,7 @@ class SqlModuleDependencyStore extends DependencyStore {
return;
}
try {
$dbw = $this->getMasterDb();
$dbw = $this->getPrimaryDB();
$depsBlobByEntity = $this->fetchDependencyBlobs( array_keys( $dataByEntity ), $dbw );
@ -128,7 +128,7 @@ class SqlModuleDependencyStore extends DependencyStore {
return;
}
try {
$dbw = $this->getMasterDb();
$dbw = $this->getPrimaryDB();
$disjunctionConds = [];
foreach ( (array)$entities as $entity ) {
@ -205,7 +205,7 @@ class SqlModuleDependencyStore extends DependencyStore {
/**
* @return DBConnRef
*/
private function getMasterDb() {
private function getPrimaryDb() {
return $this->lb
->getConnectionRef( DB_PRIMARY, [], false, ( $this->lb )::CONN_TRX_AUTOCOMMIT );
}

View file

@ -265,7 +265,7 @@ class UploadFromChunks extends UploadFromFile {
wfDebug( __METHOD__ . " update chunk status for {$this->mFileKey} offset:" .
$this->getOffset() . ' inx:' . $this->getChunkIndex() );
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$dbw->update(
'uploadstash',
[
@ -284,7 +284,7 @@ class UploadFromChunks extends UploadFromFile {
private function getChunkStatus() {
// get Master db to avoid race conditions.
// Otherwise, if chunk upload time < replag there will be spurious errors
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$row = $dbw->selectRow(
'uploadstash',
[

View file

@ -279,7 +279,7 @@ class UploadStash {
// insert the file metadata into the db.
wfDebug( __METHOD__ . " inserting $stashPath under $key" );
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$serializedFileProps = serialize( $fileProps );
if ( strlen( $serializedFileProps ) > self::MAX_US_PROPS_SIZE ) {
@ -339,7 +339,7 @@ class UploadStash {
}
wfDebug( __METHOD__ . ' clearing all rows for user ' . $this->user->getId() );
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$dbw->delete(
'uploadstash',
[ 'us_user' => $this->user->getId() ],
@ -368,7 +368,7 @@ class UploadStash {
);
}
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
// this is a cheap query. it runs on the master so that this function
// still works when there's lag. It won't be called all that often.
@ -406,7 +406,7 @@ class UploadStash {
// Ensure we have the UploadStashFile loaded for this key
$this->getFile( $key, true );
$dbw = $this->repo->getMasterDB();
$dbw = $this->repo->getPrimaryDB();
$dbw->delete(
'uploadstash',
@ -507,7 +507,7 @@ class UploadStash {
$dbr = null;
if ( $readFromDB === DB_PRIMARY ) {
// sometimes reading from the master is necessary, if there's replication lag.
$dbr = $this->repo->getMasterDB();
$dbr = $this->repo->getPrimaryDB();
} else {
$dbr = $this->repo->getReplicaDB();
}

View file

@ -61,7 +61,7 @@ class MigrateFileRepoLayout extends Maintenance {
$be = $be->getInternalBackend();
}
$dbw = $repo->getMasterDB();
$dbw = $repo->getPrimaryDB();
$origBase = $be->getContainerStoragePath( "{$repo->getName()}-original" );
$startTime = wfTimestampNow();

View file

@ -6,7 +6,7 @@ use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* @coversDefaultClass DBFileJournal
* @covers ::__construct
* @covers ::getMasterDB
* @covers ::getPrimaryDB
* @group Database
*/
class DBFileJournalIntegrationTest extends MediaWikiIntegrationTestCase {

View file

@ -45,7 +45,7 @@ class MigrateFileRepoLayoutTest extends MediaWikiIntegrationTestCase {
) );
$repoMock = $this->getMockBuilder( LocalRepo::class )
->onlyMethods( [ 'getMasterDB' ] )
->onlyMethods( [ 'getPrimaryDB' ] )
->setConstructorArgs( [ [
'name' => 'migratefilerepolayouttest',
'backend' => $backend
@ -53,7 +53,7 @@ class MigrateFileRepoLayoutTest extends MediaWikiIntegrationTestCase {
->getMock();
$repoMock
->method( 'getMasterDB' )
->method( 'getPrimaryDB' )
->willReturn( $dbMock );
$this->migratorMock = $this->getMockBuilder( MigrateFileRepoLayout::class )