Aaron Schulz has uploaded a new change for review.

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

Change subject: Switch to using atomic sections and commitMasterChanges()
......................................................................

Switch to using atomic sections and commitMasterChanges()

It is safer to commit all DBs at once rather than just one.

Change-Id: Ia52f93b36924ead3fe4a2831f437a9985cdf7634
---
M MergeUser.php
1 file changed, 16 insertions(+), 31 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/UserMerge 
refs/changes/95/304695/1

diff --git a/MergeUser.php b/MergeUser.php
index b4b7a62..89608fc 100755
--- a/MergeUser.php
+++ b/MergeUser.php
@@ -18,7 +18,7 @@
        /** @var integer */
        private $flags;
 
-       const USE_MULTI_COMMIT = 1; // allow begin/commit; useful for jobs
+       const USE_MULTI_COMMIT = 1; // allow begin/commit; useful for jobs or 
CLI mode
 
        /**
         * @param User $oldUser
@@ -66,7 +66,7 @@
         */
        private function mergeEditcount() {
                $dbw = wfGetDB( DB_MASTER );
-               $this->begin( $dbw );
+               $dbw->startAtomic( __METHOD__ );
 
                $totalEdits = $dbw->selectField(
                        'user',
@@ -94,11 +94,11 @@
                        );
                }
 
-               $this->commit( $dbw );
+               $dbw->endAtomic( __METHOD__ );
        }
 
        private function mergeBlocks( DatabaseBase $dbw ) {
-               $this->begin( $dbw );
+               $dbw->startAtomic( __METHOD__ );
 
                // Pull blocks directly from master
                $rows = $dbw->select(
@@ -121,9 +121,11 @@
 
                if ( !$newBlock && !$oldBlock ) {
                        // No one is blocked, yaaay
+                       $dbw->endAtomic( __METHOD__ );
                        return;
                } elseif ( $newBlock && !$oldBlock ) {
                        // Only the new user is blocked, so nothing to do.
+                       $dbw->endAtomic( __METHOD__ );
                        return;
                } elseif ( $oldBlock && !$newBlock ) {
                        // Just move the old block to the new username
@@ -133,6 +135,7 @@
                                [ 'ipb_id' => $oldBlock->ipb_id ],
                                __METHOD__
                        );
+                       $dbw->endAtomic( __METHOD__ );
                        return;
                }
 
@@ -154,7 +157,7 @@
                        );
                }
 
-               $this->commit( $dbw );
+               $dbw->endAtomic( __METHOD__ );
        }
 
        /**
@@ -220,13 +223,14 @@
                Hooks::run( 'UserMergeAccountFields', [ &$updateFields ] );
 
                $dbw = wfGetDB( DB_MASTER );
+               $lbFactory = wfGetLBFactory();
 
                $this->deduplicateWatchlistEntries( $dbw );
                $this->mergeBlocks( $dbw );
 
-               // For readability, flush any trx (though mergeBlocks will 
manage this)
                if ( $this->flags & self::USE_MULTI_COMMIT ) {
-                       $dbw->commit( __METHOD__, 'flush' );
+                       // Flush prior writes; this actives the non-transaction 
path in the loop below.
+                       $lbFactory->commitMasterChanges( __METHOD__ );
                }
 
                foreach ( $updateFields as $fieldInfo ) {
@@ -253,9 +257,8 @@
                                $limit = 200;
                                do {
                                        $checkSince = microtime( true );
-                                       // Batch and wait for slaves (ORDER BY 
+ LIMIT is not well supported)
-                                       $db->begin();
-                                       // Grab a batch of values on a mostly 
unique column for this user ID
+                                       // Note that UPDATE with ORDER BY + 
LIMIT is not well supported.
+                                       // Grab a batch of values on a mostly 
unique column for this user ID.
                                        $res = $db->select(
                                                $tableName,
                                                [ $keyField ],
@@ -278,7 +281,7 @@
                                                        $options
                                                );
                                        }
-                                       $db->commit();
+                                       $lbFactory->commitMasterChanges( 
__METHOD__ );
                                        wfWaitForSlaves( $checkSince, false, 
'*' );
                                } while ( count( $keyValues ) >= $limit );
                        }
@@ -296,7 +299,7 @@
         * @param DatabaseBase $dbw
         */
        private function deduplicateWatchlistEntries( $dbw ) {
-               $this->begin( $dbw );
+               $dbw->startAtomic( __METHOD__ );
 
                $res = $dbw->select(
                        [
@@ -348,7 +351,7 @@
                        );
                }
 
-               $this->commit( $dbw );
+               $dbw->endAtomic( __METHOD__ );
        }
 
        /**
@@ -500,23 +503,5 @@
                Hooks::run( 'DeleteAccount', [ &$this->oldUser ] );
 
                DeferredUpdates::addUpdate( SiteStatsUpdate::factory( [ 'users' 
=> -1 ] ) );
-       }
-
-       /**
-        * @param DatabaseBase $dbw
-        */
-       private function begin( $dbw ) {
-               if ( $this->flags & self::USE_MULTI_COMMIT ) {
-                       $dbw->begin( __METHOD__ );
-               }
-       }
-
-       /**
-        * @param DatabaseBase $dbw
-        */
-       private function commit( $dbw ) {
-               if ( $this->flags & self::USE_MULTI_COMMIT ) {
-                       $dbw->commit( __METHOD__ );
-               }
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia52f93b36924ead3fe4a2831f437a9985cdf7634
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/UserMerge
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