r52085 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52084‎ | r52085 | r52086 >
Date:04:58, 18 June 2009
Author:tstarling
Status:deferred
Tags:
Comment:
Fix a DB error which occurs on Wikimedia every time there is replication lag: check the master for local user existence before attempting an INSERT INTO user, don't trust the slave for existence data.
Modified paths:
  • /trunk/extensions/CentralAuth/CentralAuthHooks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/CentralAuth/CentralAuthHooks.php
@@ -183,30 +183,51 @@
184184 return true;
185185 }
186186
 187+ // Clean up username
 188+ $title = Title::makeTitleSafe( NS_USER, $userName );
 189+ if ( !$title ) {
 190+ wfDebug( __METHOD__.": invalid username\n" );
 191+ }
 192+ $userName = $title->getText();
 193+
 194+ // Try the central user
187195 $centralUser = new CentralAuthUser( $userName );
188 - $localId = User::idFromName( $userName );
189 -
190196 if ( $centralUser->authenticateWithToken( $token ) != 'ok' ) {
191197 wfDebug( __METHOD__.": token mismatch\n" );
192 - } elseif ( !$centralUser->isAttached() && $localId ) {
 198+ return true;
 199+ }
 200+
 201+ // Try the local user from the slave DB
 202+ $localId = User::idFromName( $userName );
 203+
 204+ // Fetch the user ID from the master, so that we don't try to create the user
 205+ // when they already exist, due to replication lag
 206+ if ( !$localId && wfGetLB()->getReaderIndex() != 0 ) {
 207+ $dbw = wfGetDB( DB_MASTER );
 208+ $localId = $dbw->selectField( 'user', 'user_id',
 209+ array( 'user_name' => $userName ), __METHOD__ );
 210+ }
 211+
 212+ if ( !$centralUser->isAttached() && $localId ) {
193213 wfDebug( __METHOD__.": exists, and not attached\n" );
 214+ return true;
 215+ }
 216+
 217+ if ( !$localId ) {
 218+ // User does not exist locally, attempt to create it
 219+ if ( !self::attemptAddUser( $user, $userName ) ) {
 220+ // Can't create user, give up now
 221+ return true;
 222+ }
194223 } else {
195 - if ( !$localId ) {
196 - // User does not exist locally, attempt to create it
197 - if ( !self::attemptAddUser( $user, $userName ) ) {
198 - // Can't create user, give up now
199 - return true;
200 - }
201 - } else {
202 - $user->setID( $localId );
203 - $user->loadFromId();
204 - }
205 - // Auth OK.
206 - wfDebug( __METHOD__.": logged in from session\n" );
207 - self::initSession( $user, $token );
208 - $user->centralAuthObj = $centralUser;
209 - $result = true;
 224+ $user->setID( $localId );
 225+ $user->loadFromId();
210226 }
 227+ // Auth OK.
 228+ wfDebug( __METHOD__.": logged in from session\n" );
 229+ self::initSession( $user, $token );
 230+ $user->centralAuthObj = $centralUser;
 231+ $result = true;
211232
212233 return true;
213234 }

Status & tagging log