jenkins-bot has submitted this change and it was merged.

Change subject: Avoid making DB replication lag in onLinksUpdateComplete()
......................................................................


Avoid making DB replication lag in onLinksUpdateComplete()

Also removed old legacy code branch.

Change-Id: Ib69dd3958b74d09ec137448af132712c6aa61083
---
M GlobalUsageHooks.php
M GlobalUsage_body.php
2 files changed, 33 insertions(+), 24 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/GlobalUsageHooks.php b/GlobalUsageHooks.php
index b766ade..778a0ff 100644
--- a/GlobalUsageHooks.php
+++ b/GlobalUsageHooks.php
@@ -14,7 +14,7 @@
         * @param $linksUpdater LinksUpdate
         * @return bool
         */
-       public static function onLinksUpdateComplete( $linksUpdater ) {
+       public static function onLinksUpdateComplete( LinksUpdate $linksUpdater 
) {
                $title = $linksUpdater->getTitle();
 
                // Create a list of locally existing images (DB keys)
@@ -22,24 +22,11 @@
 
                $localFiles = array();
                $repo = RepoGroup::singleton()->getLocalRepo();
-               if ( defined( 'FileRepo::NAME_AND_TIME_ONLY' ) ) { // MW 1.23
-                       $imagesInfo = $repo->findFiles( $images, 
FileRepo::NAME_AND_TIME_ONLY );
-                       foreach ( $imagesInfo as $dbKey => $info ) {
-                               $localFiles[] = $dbKey;
-                               if ( $dbKey !== $info['title'] ) { // redirect
-                                       $localFiles[] = $info['title'];
-                               }
-                       }
-               } else {
-                       // Unrolling findFiles() here because pages with 
thousands of images trigger an OOM
-                       foreach ( $images as $dbKey ) {
-                               $file = $repo->findFile( $dbKey );
-                               if ( $file ) {
-                                       $localFiles[] = $dbKey;
-                                       if ( $file->getTitle()->getDBkey() !== 
$dbKey ) { // redirect
-                                               $localFiles[] = 
$file->getTitle()->getDBkey();
-                                       }
-                               }
+               $imagesInfo = $repo->findFiles( $images, 
FileRepo::NAME_AND_TIME_ONLY );
+               foreach ( $imagesInfo as $dbKey => $info ) {
+                       $localFiles[] = $dbKey;
+                       if ( $dbKey !== $info['title'] ) { // redirect
+                               $localFiles[] = $info['title'];
                        }
                }
                $localFiles = array_values( array_unique( $localFiles ) );
@@ -106,6 +93,7 @@
         */
        public static function onArticleDeleteComplete( $article, $user, 
$reason, $id ) {
                $gu = self::getGlobalUsage();
+               // @FIXME: avoid making DB replication lag
                $gu->deleteLinksFromPage( $id );
 
                return true;
diff --git a/GlobalUsage_body.php b/GlobalUsage_body.php
index ca3fe1c..f9cd85e 100644
--- a/GlobalUsage_body.php
+++ b/GlobalUsage_body.php
@@ -1,6 +1,8 @@
 <?php
+use MediaWiki\MediaWikiServices;
 
 class GlobalUsage {
+       /** @var string */
        private $interwiki;
 
        /**
@@ -27,6 +29,8 @@
         * @param $pageIdFlags int
         */
        public function insertLinks( $title, $images, $pageIdFlags = 
Title::GAID_FOR_UPDATE ) {
+               global $wgUpdateRowsPerQuery;
+
                $insert = array();
                foreach ( $images as $name ) {
                        $insert[] = array(
@@ -38,7 +42,13 @@
                                'gil_to' => $name
                        );
                }
-               $this->db->insert( 'globalimagelinks', $insert, __METHOD__, 
array( 'IGNORE' ) );
+
+               $lbFactory = 
MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+               $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
+               foreach ( array_chunk( $insert, $wgUpdateRowsPerQuery ) as 
$insertBatch ) {
+                       $this->db->insert( 'globalimagelinks', $insertBatch, 
__METHOD__, array( 'IGNORE' ) );
+                       $lbFactory->commitAndWaitForReplication( __METHOD__, 
$ticket );
+               }
        }
 
        /**
@@ -58,8 +68,10 @@
                );
 
                $images = array();
-               foreach ( $res as $row )
+               foreach ( $res as $row ) {
                        $images[] = $row->gil_to;
+               }
+
                return $images;
        }
 
@@ -69,15 +81,24 @@
         * @param $id int Page id of the page
         * @param $to mixed File name(s)
         */
-       public function deleteLinksFromPage( $id, $to = null ) {
+       public function deleteLinksFromPage( $id, array $to = null ) {
+               global $wgUpdateRowsPerQuery;
+
                $where = array(
                        'gil_wiki' => $this->interwiki,
                        'gil_page' => $id
                );
                if ( $to ) {
-                       $where['gil_to'] = $to;
+                       $lbFactory = 
MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+                       $ticket = $lbFactory->getEmptyTransactionTicket( 
__METHOD__ );
+                       foreach ( array_chunk( $to, $wgUpdateRowsPerQuery ) as 
$toBatch ) {
+                               $where['gil_to'] = $toBatch;
+                               $this->db->delete( 'globalimagelinks', $where, 
__METHOD__ );
+                               $lbFactory->commitAndWaitForReplication( 
__METHOD__, $ticket );
+                       }
+               } else {
+                       $this->db->delete( 'globalimagelinks', $where, 
__METHOD__ );
                }
-               $this->db->delete( 'globalimagelinks', $where, __METHOD__ );
        }
 
        /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib69dd3958b74d09ec137448af132712c6aa61083
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/GlobalUsage
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to