From 0f9fa35896b3a2dd3ce61306f3c9ed90ec2ad4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Wed, 2 Aug 2023 14:18:55 +0200 Subject: [PATCH] ThreadItemStore: Ignore duplicates caused by duplicate executions For some unknown reason our updates are being processed more than once at the same time. Ignore or work around the "duplicate entry" errors caused by that. Bug: T323080 Bug: T341811 Change-Id: Iaae1dc2d5ed5bf4ac6760fb1d39dc21f2af89e9a (cherry picked from commit a783b9371fb11aea5a61d8f595061b1fe80ae9f1) --- includes/ThreadItemStore.php | 40 +++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/includes/ThreadItemStore.php b/includes/ThreadItemStore.php index d3a6f0dfe..7a5c502d6 100644 --- a/includes/ThreadItemStore.php +++ b/includes/ThreadItemStore.php @@ -499,7 +499,8 @@ class ThreadItemStore { 'itp_oldest_revision_id' => $rev->getId(), 'itp_newest_revision_id' => $rev->getId(), ], - $method + $method, + [ 'IGNORE' ] ); } else { $oldestTime = ( new MWTimestamp( $itemPagesRow->oldest_rev_timestamp ) )->getTimestamp( TS_MW ); @@ -554,14 +555,15 @@ class ThreadItemStore { 'itr_headinglevel' => $item->isPlaceholderHeading() ? null : $item->getHeadingLevel(), ] : [] ); + $itemRevisionsConds = [ + 'itr_itemid_id' => $itemIdsIds[ $item->getId() ], + 'itr_items_id' => $itemsIds[ $item->getId() ], + 'itr_revision_id' => $rev->getId(), + ]; $itemRevisionsId = $dbw->newSelectQueryBuilder() ->from( 'discussiontools_item_revisions' ) ->field( 'itr_id' ) - ->where( [ - 'itr_itemid_id' => $itemIdsIds[ $item->getId() ], - 'itr_items_id' => $itemsIds[ $item->getId() ], - 'itr_revision_id' => $rev->getId(), - ] ) + ->where( $itemRevisionsConds ) ->caller( $method ) ->fetchField(); if ( $itemRevisionsId === false ) { @@ -591,15 +593,29 @@ class ThreadItemStore { $method ); $itemRevisionsId = $itemRevisionsUpdateId; + $didInsert = true; } else { - $dbw->insert( - 'discussiontools_item_revisions', - $newOrUpdateRevRow, - $method + $itemRevisionsId = $this->findOrInsertIdButTryHarder( + static function ( $dbw ) use ( $itemRevisionsConds, $method ) { + return $dbw->newSelectQueryBuilder() + ->from( 'discussiontools_item_revisions' ) + ->field( 'itr_id' ) + ->where( $itemRevisionsConds ) + ->caller( $method ) + ->fetchField(); + }, + static function ( $dbw ) use ( $newOrUpdateRevRow, $method ) { + $dbw->insert( + 'discussiontools_item_revisions', + $newOrUpdateRevRow, + $method, + [ 'IGNORE' ] + ); + return $dbw->insertId(); + }, + $didInsert ); - $itemRevisionsId = $dbw->insertId(); } - $didInsert = true; } $itemRevisionsIds[ $item->getId() ] = $itemRevisionsId;