WikiPage: Avoid locking comment and actor tables in doDeleteArticleReal
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 10 Apr 2018 14:39:22 +0000 (10:39 -0400)
committerAnomie <bjorsch@wikimedia.org>
Wed, 11 Apr 2018 15:26:54 +0000 (15:26 +0000)
The existing code was using FOR UPDATE when selecting the revision table
rows to be added to the archive table, which meant it was locking not
only `revision`, `revision_comment_temp`, and `revision_actor_temp` that
are being updated but also `comment` and `actor` that we have no
intention of touching. And since those both are intended to be widely
shared, that's likely to lead to unnecessary lock contention.

To avoid that, we make two queries: one against only the rows we want to
lock with FOR UPDATE, and the second to actually fetch the data without
FOR UPDATE.

Bug: T191892
Change-Id: I40ecf1786249c886a5ff25f29ec01edee3ff205d

includes/page/WikiPage.php

index f45036c..f3860c6 100644 (file)
@@ -2871,13 +2871,32 @@ class WikiPage implements Page, IDBAccessObject {
                // In the future, we may keep revisions and mark them with
                // the rev_deleted field, which is reserved for this purpose.
 
+               // Lock rows in `revision` and its temp tables, but not any others.
+               // Note array_intersect() preserves keys from the first arg, and we're
+               // assuming $revQuery has `revision` primary and isn't using subtables
+               // for anything we care about.
+               $res = $dbw->select(
+                       array_intersect(
+                               $revQuery['tables'],
+                               [ 'revision', 'revision_comment_temp', 'revision_actor_temp' ]
+                       ),
+                       '1',
+                       [ 'rev_page' => $id ],
+                       __METHOD__,
+                       'FOR UPDATE',
+                       $revQuery['joins']
+               );
+               foreach ( $res as $row ) {
+                       // Fetch all rows in case the DB needs that to properly lock them.
+               }
+
                // Get all of the page revisions
                $res = $dbw->select(
                        $revQuery['tables'],
                        $revQuery['fields'],
                        [ 'rev_page' => $id ],
                        __METHOD__,
-                       'FOR UPDATE',
+                       [],
                        $revQuery['joins']
                );