Aaron Schulz has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/232322

Change subject: [WIP] Added CentralAuthUser::setLoadFromMasterFlag()
......................................................................

[WIP] Added CentralAuthUser::setLoadFromMasterFlag()

* Callers have been added to places that can easily use the master DB
  without much chance of cross-cluster queries

Change-Id: Iec8936ae0c8cb22923834470b23408e4d413f1f4
---
M includes/CentralAuthHooks.php
M includes/CentralAuthUser.php
M includes/GlobalRename/GlobalRenameRequest.php
M includes/LocalRenameJob/LocalRenameJob.php
M includes/LocalRenameJob/LocalRenameUserJob.php
M includes/SuppressUserJob.php
M includes/api/ApiDeleteGlobalAccount.php
M includes/specials/SpecialCentralAuth.php
M includes/specials/SpecialGlobalRenameProgress.php
M includes/specials/SpecialGlobalRenameUser.php
M includes/specials/SpecialGlobalUserMerge.php
M includes/specials/SpecialMergeAccount.php
M maintenance/deleteEmptyAccounts.php
M maintenance/forceRenameUsers.php
M maintenance/migrateAccount.php
M maintenance/migratePass1.php
M maintenance/populateHomeDB.php
M maintenance/populateListOfUsersToRename.php
M maintenance/resetGlobalUserTokens.php
19 files changed, 68 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralAuth 
refs/changes/22/232322/1

diff --git a/includes/CentralAuthHooks.php b/includes/CentralAuthHooks.php
index 0544cc8..e9f5524 100644
--- a/includes/CentralAuthHooks.php
+++ b/includes/CentralAuthHooks.php
@@ -463,6 +463,7 @@
                global $wgCentralAuthEnableGlobalRenameRequest;
 
                $centralUser = CentralAuthUser::getInstance( $user );
+               $centralUser->setLoadFromMasterFlag();
                if ( $centralUser->exists() || 
$centralUser->renameInProgressOn( wfWikiID() ) ) {
                        $abortError = wfMessage( 'centralauth-account-exists' 
)->text();
                        return false;
@@ -584,6 +585,7 @@
                                // otherwise wgAuth will handle those.
                                $testName = $user->getName() . '~' . wfWikiID();
                                $test = new CentralAuthUser( $testName );
+                               $test->setLoadFromMasterFlag();
                                if ( $test->exists() && $test->isAttached() ) {
                                        $msg = array( 
'centralauth-abortlogin-renamed', $testName );
                                        return false;
@@ -1051,9 +1053,11 @@
         * @param $newName
         * @param $warnings
         * @return bool
+        * @throws ErrorPageError
         */
        static function onRenameUserWarning( $oldName, $newName, &$warnings ) {
                $oldCentral = new CentralAuthUser( $oldName );
+               $oldCentral->setLoadFromMasterFlag();
                if ( $oldCentral->exists() && $oldCentral->isAttached() ) {
                        $warnings[] = array( 'centralauth-renameuser-merged', 
$oldName, $newName );
                }
@@ -1062,6 +1066,7 @@
                }
 
                $newCentral = new CentralAuthUser( $newName );
+               $newCentral->setLoadFromMasterFlag();
                if ( $newCentral->exists() && !$newCentral->isAttached() ) {
                        $warnings[] = array( 'centralauth-renameuser-reserved', 
$oldName, $newName );
                }
@@ -1083,6 +1088,7 @@
         */
        static function onRenameUserPreRename( $uid, $oldName, $newName ) {
                $oldCentral = new CentralAuthUser( $oldName );
+               $oldCentral->setLoadFromMasterFlag();
                // If we're doing a global rename, the account will not get 
unattached
                // because the old account no longer exists
                if ( $oldCentral->exists() && $oldCentral->isAttached() ) {
@@ -1100,7 +1106,9 @@
         */
        static function onRenameUserComplete( $userId, $oldName, $newName ) {
                $oldCentral = new CentralAuthUser( $oldName );
+               $oldCentral->setLoadFromMasterFlag();
                $newCentral = new CentralAuthUser( $newName );
+               $newCentral->setLoadFromMasterFlag();
 
                if ( $newCentral->exists() && $oldCentral->renameInProgressOn( 
wfWikiID() ) ) {
                        // This is a global rename, just update the row.
diff --git a/includes/CentralAuthUser.php b/includes/CentralAuthUser.php
index 6f2dc26..0f721c5 100644
--- a/includes/CentralAuthUser.php
+++ b/includes/CentralAuthUser.php
@@ -179,6 +179,24 @@
        }
 
        /**
+        * If loadState() was not called already, make sure the master DB is 
used
+        * when it *is* called and needs to query the DB due to a cache miss
+        *
+        * The cache should normally be highly up-to-date in the master 
datacenter, where
+        * data changing POST requests should be routed anyway, so it is not 
usually necessary
+        * to bypass the cache.
+        *
+        * This can be called right after __construct() or getInstance() as 
needed
+        *
+        * @since 1.26
+        */
+       public function setLoadFromMasterFlag() {
+               if ( !isset( $this->mGlobalId ) ) {
+                       $this->mFromMaster = true;
+               }
+       }
+
+       /**
         * Clear state information cache
         * Does not clear $this->mName, so the state information can be 
reloaded with loadState()
         */
@@ -216,7 +234,7 @@
 
                wfDebugLog( 'CentralAuthVerbose', "Loading state for global 
user {$this->mName} from DB" );
 
-               $this->mFromMaster = self::centralLBHasRecentMasterChanges();
+               $this->mFromMaster = $this->mFromMaster ?: 
self::centralLBHasRecentMasterChanges();
                if ( $this->mFromMaster ) {
                        $db = self::getCentralDB();
                } else {
@@ -234,7 +252,8 @@
                        __METHOD__,
                        array(),
                        array(
-                               'localuser' => array( 'LEFT OUTER JOIN', array( 
'gu_name=lu_name', 'lu_wiki' => wfWikiID() ) )
+                               'localuser' => array( 'LEFT OUTER JOIN',
+                                       array( 'gu_name=lu_name', 'lu_wiki' => 
wfWikiID() ) )
                        )
                );
 
@@ -408,11 +427,9 @@
                        $this->resetState();
                }
 
-               $ttl = $this->mFromMaster ? 86400 : 30;
-
                $obj = $this->getCacheObject();
                wfDebugLog( 'CentralAuthVerbose', "Saving user {$this->mName} 
to cache." );
-               ObjectCache::getMainWANInstance()->set( $this->getCacheKey(), 
$obj, $ttl );
+               ObjectCache::getMainWANInstance()->set( $this->getCacheKey(), 
$obj, 86400 );
        }
 
        /**
@@ -2056,9 +2073,9 @@
                        return $this->mAttachedInfo;
                }
 
-               $dbw = self::getCentralDB();
+               $db = $this->mFromMaster ? self::getCentralDB() : 
self::getCentralSlaveDB();
 
-               $result = $dbw->select(
+               $result = $db->select(
                        'localuser',
                        array(
                                'lu_wiki',
diff --git a/includes/GlobalRename/GlobalRenameRequest.php 
b/includes/GlobalRename/GlobalRenameRequest.php
index 4ae7f40..9b0b146 100644
--- a/includes/GlobalRename/GlobalRenameRequest.php
+++ b/includes/GlobalRename/GlobalRenameRequest.php
@@ -429,6 +429,7 @@
                // New user creation checks against local wiki only using an API
                // request, but we need to check against te central user table 
instead
                $centralUser = new CentralAuthUser( $safe );
+               $centralUser->setLoadFromMasterFlag();
                if ( $centralUser->exists() || $centralUser->listUnattached() ) 
{
                        $status->fatal( 'globalrenamerequest-newname-err-taken' 
);
                        return $status;
diff --git a/includes/LocalRenameJob/LocalRenameJob.php 
b/includes/LocalRenameJob/LocalRenameJob.php
index 6bc5847..94783f0 100644
--- a/includes/LocalRenameJob/LocalRenameJob.php
+++ b/includes/LocalRenameJob/LocalRenameJob.php
@@ -54,6 +54,7 @@
                        return $user;
                }
                $caUser = CentralAuthUser::getInstance( $user );
+               $caUser->setLoadFromMasterFlag();
                // Race condition where the renamer isn't attached here, but
                // someone creates an account in the meantime and then bad
                // stuff could happen...
diff --git a/includes/LocalRenameJob/LocalRenameUserJob.php 
b/includes/LocalRenameJob/LocalRenameUserJob.php
index ec41e51..107f8e4 100644
--- a/includes/LocalRenameJob/LocalRenameUserJob.php
+++ b/includes/LocalRenameJob/LocalRenameUserJob.php
@@ -77,6 +77,7 @@
        private function promoteToGlobal() {
                $newName = $this->params['to'];
                $caUser = new CentralAuthUser( $newName );
+               $caUser->setLoadFromMasterFlag();
                $status = $caUser->promoteToGlobal( wfWikiID() );
                if ( !$status->isOK() ) {
                        if ( $status->hasMessage( 'promote-not-on-wiki' ) ) {
diff --git a/includes/SuppressUserJob.php b/includes/SuppressUserJob.php
index 3559dea..f3c914b 100644
--- a/includes/SuppressUserJob.php
+++ b/includes/SuppressUserJob.php
@@ -28,6 +28,7 @@
                $suppress = $this->params['suppress'];
                $reason = $this->params['reason'];
                $user = new CentralAuthUser( $username );
+               $user->setLoadFromMasterFlag();
                if ( !$user->exists() ) {
                        wfDebugLog( 'suppressjob', "Requested to suppress 
non-existent user {$username} by {$by}." );
                }
diff --git a/includes/api/ApiDeleteGlobalAccount.php 
b/includes/api/ApiDeleteGlobalAccount.php
index 9986b26..efc146b 100644
--- a/includes/api/ApiDeleteGlobalAccount.php
+++ b/includes/api/ApiDeleteGlobalAccount.php
@@ -36,7 +36,9 @@
                }
 
                $params = $this->extractRequestParams();
+
                $globalUser = new CentralAuthUser( $params['user'] );
+               $globalUser->setLoadFromMasterFlag();
                if ( !$globalUser->exists() ) {
                        $this->dieUsageMsg( array( 'nosuchuser', 
$globalUser->getName() ) );
                } elseif ( $globalUser->isOversighted() && 
!$this->getUser()->isAllowed( 'centralauth-oversight' ) ) {
diff --git a/includes/specials/SpecialCentralAuth.php 
b/includes/specials/SpecialCentralAuth.php
index d96d620..4c74209 100644
--- a/includes/specials/SpecialCentralAuth.php
+++ b/includes/specials/SpecialCentralAuth.php
@@ -77,7 +77,11 @@
                        )->plain()
                ) );
 
-               $this->mGlobalUser = $globalUser = new CentralAuthUser( 
$this->mUserName );
+               $globalUser = new CentralAuthUser( $this->mUserName );
+               if ( $this->getRequest()->wasPosted() ) {
+                       $globalUser->setLoadFromMasterFlag();
+               }
+               $this->mGlobalUser = $globalUser;
 
                if ( ( $globalUser->isOversighted() || $globalUser->isHidden() 
) && !$this->mCanOversight ) {
                        // Claim that there's nothing if the global account is 
hidden and the user is not
diff --git a/includes/specials/SpecialGlobalRenameProgress.php 
b/includes/specials/SpecialGlobalRenameProgress.php
index dc5f841..701f607 100644
--- a/includes/specials/SpecialGlobalRenameProgress.php
+++ b/includes/specials/SpecialGlobalRenameProgress.php
@@ -121,6 +121,7 @@
                // $newname will always be defined since we check
                // for 0 result rows above
                $caUser = new CentralAuthUser( $newName );
+               $caUser->setLoadFromMasterFlag();
                $attached = $caUser->listAttached();
                foreach ( $attached as $wiki ) {
                        // If it's not in the db table, and there is
diff --git a/includes/specials/SpecialGlobalRenameUser.php 
b/includes/specials/SpecialGlobalRenameUser.php
index 49ac3d5..6bf0d3f 100644
--- a/includes/specials/SpecialGlobalRenameUser.php
+++ b/includes/specials/SpecialGlobalRenameUser.php
@@ -139,6 +139,7 @@
                                continue;
                        }
                        $ca = new CentralAuthUser( $name );
+                       $ca->setLoadFromMasterFlag();
                        if ( $ca->isHidden() ) {
                                $display[] = $this->msg( 
'centralauth-rename-conflict-hidden' )->text();
                        } else {
diff --git a/includes/specials/SpecialGlobalUserMerge.php 
b/includes/specials/SpecialGlobalUserMerge.php
index b032883..4630ac7 100644
--- a/includes/specials/SpecialGlobalUserMerge.php
+++ b/includes/specials/SpecialGlobalUserMerge.php
@@ -95,6 +95,7 @@
                }
 
                $caUser = new CentralAuthUser( $name );
+               $caUser->setLoadFromMasterFlag();
                if ( !$caUser->exists() ) {
                        return $this->msg( 'centralauth-usermerge-invalid', 
$name )->escaped();
                }
@@ -149,7 +150,9 @@
                                        // DWIM and ignore it
                                        continue;
                                }
-                               $this->oldCAUsers[] = new CentralAuthUser( 
$name );
+                               $caUser = new CentralAuthUser( $name );
+                               $caUser->setLoadFromMasterFlag();
+                               $this->oldCAUsers[] = $caUser;
                        }
                }
 
diff --git a/includes/specials/SpecialMergeAccount.php 
b/includes/specials/SpecialMergeAccount.php
index 215c962..7fa0573 100644
--- a/includes/specials/SpecialMergeAccount.php
+++ b/includes/specials/SpecialMergeAccount.php
@@ -191,7 +191,9 @@
 
        function doDryRunMerge() {
                global $wgCentralAuthDryRun;
+
                $globalUser = new CentralAuthUser( $this->getUser()->getName() 
);
+               $globalUser->setLoadFromMasterFlag();
 
                if ( $globalUser->exists() ) {
                        // Already exists - race condition
@@ -250,7 +252,9 @@
 
        function doInitialMerge() {
                global $wgCentralAuthDryRun;
+
                $globalUser = new CentralAuthUser( $this->getUser()->getName() 
);
+               $globalUser->setLoadFromMasterFlag();
 
                if ( $wgCentralAuthDryRun ) {
                        $this->dryRunError();
@@ -277,7 +281,9 @@
 
        function doCleanupMerge() {
                global $wgCentralAuthDryRun;
+
                $globalUser = new CentralAuthUser( $this->getUser()->getName() 
);
+               $globalUser->setLoadFromMasterFlag();
 
                if ( !$globalUser->exists() ) {
                        throw new Exception( "User doesn't exist -- race 
condition?" );
@@ -310,7 +316,9 @@
 
        function doAttachMerge() {
                global $wgCentralAuthDryRun;
+
                $globalUser = new CentralAuthUser( $this->getUser()->getName() 
);
+               $globalUser->setLoadFromMasterFlag();
 
                if ( !$globalUser->exists() ) {
                        throw new Exception( "User doesn't exist -- race 
condition?" );
diff --git a/maintenance/deleteEmptyAccounts.php 
b/maintenance/deleteEmptyAccounts.php
index a982364..c749a6f 100644
--- a/maintenance/deleteEmptyAccounts.php
+++ b/maintenance/deleteEmptyAccounts.php
@@ -75,6 +75,8 @@
 
        function process( $username ) {
                $central = new CentralAuthUser( $username );
+               $central->setLoadFromMasterFlag();
+
                if ( !$central->exists() ) {
                        $this->output( "ERROR: [$username] Central account does 
not exist. So how'd we find it?\n" );
                        return;
diff --git a/maintenance/forceRenameUsers.php b/maintenance/forceRenameUsers.php
index 42bd405..8806a19 100644
--- a/maintenance/forceRenameUsers.php
+++ b/maintenance/forceRenameUsers.php
@@ -76,6 +76,7 @@
                }
                $this->log( "Beginning rename of $newNamePrefix" );
                $newCAUser = new CentralAuthUser( $newNamePrefix );
+               $newCAUser->setLoadFromMasterFlag();
                $count = 0;
                // Edge case: Someone created User:Foo~wiki manually.
                // So just start appending numbers to the end of the name
@@ -83,6 +84,7 @@
                while ( $newCAUser->exists() ) {
                        $count++;
                        $newCAUser = new CentralAuthUser( $newNamePrefix . 
(string)$count );
+                       $newCAUser->setLoadFromMasterFlag();
                }
                if ( $newNamePrefix !== $newCAUser->getName() ) {
                        $this->log( "WARNING: New name is now 
{$newCAUser->getName()}" );
@@ -136,6 +138,8 @@
                foreach ( $rows as $row ) {
                        $user = User::newFromName( $row->utr_name );
                        $caUser = new CentralAuthUser( $row->utr_name );
+                       $caUser->setLoadFromMasterFlag();
+
                        if ( !$user->getId() ) {
                                $this->log( "'{$row->utr_name}' has been 
renamed since the last was list generated." );
                                $updates->remove( $row->utr_name, 
$row->utr_wiki );
diff --git a/maintenance/migrateAccount.php b/maintenance/migrateAccount.php
index a6ddf20..47bd3af 100644
--- a/maintenance/migrateAccount.php
+++ b/maintenance/migrateAccount.php
@@ -102,6 +102,7 @@
                $this->output( "CentralAuth account migration for: " . 
$username . "\n");
 
                $central = new CentralAuthUser( $username );
+               $central->setLoadFromMasterFlag();
                try {
                        $unattached = $central->queryUnattached();
                } catch ( Exception $e ) {
diff --git a/maintenance/migratePass1.php b/maintenance/migratePass1.php
index ed8fd4e..f5ad42a 100644
--- a/maintenance/migratePass1.php
+++ b/maintenance/migratePass1.php
@@ -33,6 +33,7 @@
                foreach( $result as $row ) {
                        $this->fromPrefix = $row->gn_name;
                        $central = new CentralAuthUser( $row->gn_name );
+                       $central->setLoadFromMasterFlag();
                        if ( $central->storeAndMigrate() ) {
                                $this->migrated++;
                        }
diff --git a/maintenance/populateHomeDB.php b/maintenance/populateHomeDB.php
index a7b568d..bd066f4 100644
--- a/maintenance/populateHomeDB.php
+++ b/maintenance/populateHomeDB.php
@@ -31,6 +31,7 @@
 
                        foreach( $result as $row ) {
                                $central = new CentralAuthUser( $row->gu_name );
+                               $central->setLoadFromMasterFlag();
                                $central->mStateDirty = true;
                                $central->saveSettings();
                                $count++;
diff --git a/maintenance/populateListOfUsersToRename.php 
b/maintenance/populateListOfUsersToRename.php
index c0384c6..4e0b029 100644
--- a/maintenance/populateListOfUsersToRename.php
+++ b/maintenance/populateListOfUsersToRename.php
@@ -74,6 +74,7 @@
                                        $attachableWikis = $cache->get( 
$row->name );
                                } else {
                                        $ca = new CentralAuthUser( $row->name );
+                                       $ca->setLoadFromMasterFlag();
                                        $attachableWikis = array();
                                        $unattached = $ca->queryUnattached();
                                        if ( $ca->exists() ) {
diff --git a/maintenance/resetGlobalUserTokens.php 
b/maintenance/resetGlobalUserTokens.php
index 9753cbe..043b42f 100644
--- a/maintenance/resetGlobalUserTokens.php
+++ b/maintenance/resetGlobalUserTokens.php
@@ -94,6 +94,7 @@
 
        private function updateUser( $username ) {
                $user = new CentralAuthUser( $username );
+               $user->setLoadFromMasterFlag();
                $this->output( 'Resetting user_token for "' . $username . '": ' 
);
                // Change value
                $user->resetAuthToken();

-- 
To view, visit https://gerrit.wikimedia.org/r/232322
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec8936ae0c8cb22923834470b23408e4d413f1f4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to