Clean up wiki ID and DB domain ID handling
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 25 Sep 2017 10:37:13 +0000 (12:37 +0200)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 30 Sep 2017 10:25:03 +0000 (10:25 +0000)
Bug: T174017
Change-Id: I42299a393c6691d39817db1d83a176a17df2474b

includes/Revision.php
includes/WatchedItemStore.php
includes/WikiMap.php
includes/deferred/LinksDeletionUpdate.php
includes/deferred/LinksUpdate.php
includes/deferred/WANCacheReapUpdate.php
includes/libs/rdbms/database/IDatabase.php
includes/resourceloader/ResourceLoaderWikiModule.php
includes/user/UserRightsProxy.php
maintenance/populateContentModel.php
tests/phpunit/includes/WikiMapTest.php

index 006e700..bcfbe63 100644 (file)
@@ -362,7 +362,7 @@ class Revision implements IDBAccessObject {
                $row = self::fetchFromConds( $db, $conditions, $flags );
                if ( $row ) {
                        $rev = new Revision( $row );
-                       $rev->mWiki = $db->getWikiID();
+                       $rev->mWiki = $db->getDomainID();
 
                        return $rev;
                }
@@ -1941,7 +1941,7 @@ class Revision implements IDBAccessObject {
                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
                return $cache->getWithSetCallback(
                        // Page/rev IDs passed in from DB to reflect history merges
-                       $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ),
+                       $cache->makeGlobalKey( 'revision', $db->getDomainID(), $pageId, $revId ),
                        $cache::TTL_WEEK,
                        function ( $curValue, &$ttl, array &$setOpts ) use ( $db, $pageId, $revId ) {
                                $setOpts += Database::getCacheSetOptions( $db );
index 69a9df2..60d8b76 100644 (file)
@@ -766,7 +766,7 @@ class WatchedItemStore implements StatsdAwareInterface {
                                                );
                                                if ( count( $watchersChunks ) > 1 ) {
                                                        $factory->commitAndWaitForReplication(
-                                                               __METHOD__, $ticket, [ 'wiki' => $dbw->getWikiID() ]
+                                                               __METHOD__, $ticket, [ 'domain' => $dbw->getDomainID() ]
                                                        );
                                                }
                                        }
index 4f3c461..8bb37b5 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\DatabaseDomain;
 
 /**
  * Helper tools for dealing with other locally-hosted wikis.
@@ -239,4 +240,22 @@ class WikiMap {
 
                return false;
        }
+
+       /**
+        * Get the wiki ID of a database domain
+        *
+        * This is like DatabaseDomain::getId() without encoding (for legacy reasons)
+        *
+        * @param string|DatabaseDomain $domain
+        * @return string
+        */
+       public static function getWikiIdFromDomain( $domain ) {
+               if ( !( $domain instanceof DatabaseDomain ) ) {
+                       $domain = DatabaseDomain::newFromId( $domain );
+               }
+
+               return strlen( $domain->getTablePrefix() )
+                       ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
+                       : $domain->getDatabase();
+       }
 }
index f579a1f..52e996a 100644 (file)
@@ -91,7 +91,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
                        $this->page->updateCategoryCounts( [], $catBatch, $id );
                        if ( count( $catBatches ) > 1 ) {
                                $lbFactory->commitAndWaitForReplication(
-                                       __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
+                                       __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
                                );
                        }
                }
@@ -187,7 +187,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
                        $dbw->delete( 'recentchanges', [ 'rc_id' => $rcIdBatch ], __METHOD__ );
                        if ( count( $rcIdBatches ) > 1 ) {
                                $lbFactory->commitAndWaitForReplication(
-                                       __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
+                                       __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
                                );
                        }
                }
@@ -209,7 +209,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
                        if ( count( $pkDeleteConds ) >= $bSize ) {
                                $dbw->delete( $table, $dbw->makeList( $pkDeleteConds, LIST_OR ), __METHOD__ );
                                $lbFactory->commitAndWaitForReplication(
-                                       __METHOD__, $this->ticket, [ 'wiki' => $dbw->getWikiID() ]
+                                       __METHOD__, $this->ticket, [ 'domain' => $dbw->getDomainID() ]
                                );
                                $pkDeleteConds = [];
                        }
@@ -230,7 +230,7 @@ class LinksDeletionUpdate extends DataUpdate implements EnqueueableDataUpdate {
 
        public function getAsJobSpecification() {
                return [
-                       'wiki' => $this->getDB()->getWikiID(),
+                       'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
                        'job'  => new JobSpecification(
                                'deleteLinks',
                                [ 'pageId' => $this->pageId, 'timestamp' => $this->timestamp ],
index 33d4e70..dfe89ba 100644 (file)
@@ -366,20 +366,22 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
                        return;
                }
 
-               $wikiId = $this->getDB()->getWikiID();
+               $domainId = $this->getDB()->getDomainID();
                $wp = WikiPage::factory( $this->mTitle );
                $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                // T163801: try to release any row locks to reduce contention
-               $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] );
+               $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
 
                foreach ( array_chunk( array_keys( $added ), $wgUpdateRowsPerQuery ) as $addBatch ) {
                        $wp->updateCategoryCounts( $addBatch, [], $this->mId );
-                       $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] );
+                       $lbf->commitAndWaitForReplication(
+                               __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
                }
 
                foreach ( array_chunk( array_keys( $deleted ), $wgUpdateRowsPerQuery ) as $deleteBatch ) {
                        $wp->updateCategoryCounts( [], $deleteBatch, $this->mId );
-                       $lbf->commitAndWaitForReplication( __METHOD__, $this->ticket, [ 'wiki' => $wikiId ] );
+                       $lbf->commitAndWaitForReplication(
+                               __METHOD__, $this->ticket, [ 'domain' => $domainId ] );
                }
        }
 
@@ -450,10 +452,12 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
                        }
                }
 
+               $domainId = $this->getDB()->getDomainID();
+
                foreach ( $deleteWheres as $deleteWhere ) {
                        $this->getDB()->delete( $table, $deleteWhere, __METHOD__ );
                        $lbf->commitAndWaitForReplication(
-                               __METHOD__, $this->ticket, [ 'wiki' => $this->getDB()->getWikiID() ]
+                               __METHOD__, $this->ticket, [ 'domain' => $domainId ]
                        );
                }
 
@@ -461,7 +465,7 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
                foreach ( $insertBatches as $insertBatch ) {
                        $this->getDB()->insert( $table, $insertBatch, __METHOD__, 'IGNORE' );
                        $lbf->commitAndWaitForReplication(
-                               __METHOD__, $this->ticket, [ 'wiki' => $this->getDB()->getWikiID() ]
+                               __METHOD__, $this->ticket, [ 'domain' => $domainId ]
                        );
                }
 
@@ -1143,7 +1147,7 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
                }
 
                return [
-                       'wiki' => $this->getDB()->getWikiID(),
+                       'wiki' => WikiMap::getWikiIdFromDomain( $this->getDB()->getDomainID() ),
                        'job'  => new JobSpecification(
                                'refreshLinksPrioritized',
                                [
index 2e5298c..5ffc938 100644 (file)
@@ -43,7 +43,7 @@ class WANCacheReapUpdate implements DeferrableUpdate {
                        [ $this, 'getTitleChangeEvents' ],
                        [ $this, 'getEventAffectedKeys' ],
                        [
-                               'channel' => 'table:recentchanges:' . $this->db->getWikiID(),
+                               'channel' => 'table:recentchanges:' . $this->db->getDomainID(),
                                'logger' => $this->logger
                        ]
                );
index 78889e5..5d0e03f 100644 (file)
@@ -346,6 +346,7 @@ interface IDatabase {
         * Alias for getDomainID()
         *
         * @return string
+        * @deprecated 1.30
         */
        public function getWikiID();
 
index fd31eac..bebc188 100644 (file)
@@ -373,7 +373,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                        if ( $module instanceof self ) {
                                $mDB = $module->getDB();
                                // Subclasses may disable getDB and implement getTitleInfo differently
-                               if ( $mDB && $mDB->getWikiID() === $db->getWikiID() ) {
+                               if ( $mDB && $mDB->getDomainID() === $db->getDomainID() ) {
                                        $wikiModules[] = $module;
                                        $allPages += $module->getPages( $context );
                                }
@@ -395,14 +395,17 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
 
                $cache = ObjectCache::getMainWANInstance();
                $allInfo = $cache->getWithSetCallback(
-                       $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getWikiID(), $hash ),
+                       $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID(), $hash ),
                        $cache::TTL_HOUR,
                        function ( $curVal, &$ttl, array &$setOpts ) use ( $func, $pageNames, $db, $fname ) {
                                $setOpts += Database::getCacheSetOptions( $db );
 
                                return call_user_func( $func, $db, $pageNames, $fname );
                        },
-                       [ 'checkKeys' => [ $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getWikiID() ) ] ]
+                       [
+                               'checkKeys' => [
+                                       $cache->makeGlobalKey( 'resourceloader', 'titleinfo', $db->getDomainID() ) ]
+                       ]
                );
 
                foreach ( $wikiModules as $wikiModule ) {
index 98586e7..3c2731a 100644 (file)
@@ -277,11 +277,11 @@ class UserRightsProxy {
                        __METHOD__
                );
 
-               $wikiId = $this->db->getWikiID();
+               $domainId = $this->db->getDomainID();
                $userId = $this->id;
                $this->db->onTransactionPreCommitOrIdle(
-                       function () use ( $wikiId, $userId ) {
-                               User::purge( $wikiId, $userId );
+                       function () use ( $domainId, $userId ) {
+                               User::purge( $domainId, $userId );
                        },
                        __METHOD__
                );
index 7d83180..74a918a 100644 (file)
@@ -24,6 +24,7 @@
 require_once __DIR__ . '/Maintenance.php';
 
 use Wikimedia\Rdbms\IDatabase;
+use MediaWiki\MediaWikiServices;
 
 /**
  * Usage:
@@ -45,9 +46,8 @@ class PopulateContentModel extends Maintenance {
        public function execute() {
                $dbw = $this->getDB( DB_MASTER );
 
-               $this->wikiId = $dbw->getWikiID();
-
-               $this->wanCache = ObjectCache::getMainWANInstance();
+               $this->wikiId = $dbw->getDomainID();
+               $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
 
                $ns = $this->getOption( 'ns' );
                if ( !ctype_digit( $ns ) && $ns !== 'all' ) {
index 186ffdb..53e0b10 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+use Wikimedia\Rdbms\DatabaseDomain;
 
 /**
  * @covers WikiMap
@@ -233,4 +234,20 @@ class WikiMapTest extends MediaWikiLangTestCase {
        public function testGetWikiFromUrl( $url, $wiki ) {
                $this->assertEquals( $wiki, WikiMap::getWikiFromUrl( $url ) );
        }
+
+       /**
+        * @dataProvider provideGetWikiIdFromDomain
+        * @covers WikiMap::getWikiIdFromDomain()
+        */
+       public function testGetWikiIdFromDomain( $domain, $wikiId ) {
+               $this->assertEquals( $wikiId, WikiMap::getWikiIdFromDomain( $domain ) );
+       }
+
+       public function provideGetWikiIdFromDomain() {
+               return [
+                       [ 'db-prefix', 'db-prefix' ],
+                       [ wfWikiID(), wfWikiID() ],
+                       [ new DatabaseDomain( 'db-dash', null, 'prefix' ), 'db-dash-prefix' ]
+               ];
+       }
 }