Add mergeable update support DeferredUpdates
authorAaron Schulz <aschulz@wikimedia.org>
Mon, 30 Nov 2015 23:26:45 +0000 (15:26 -0800)
committerKrinkle <krinklemail@gmail.com>
Fri, 4 Dec 2015 19:08:51 +0000 (19:08 +0000)
* DeferrableUpdate classes can implement MergeableUpdate.
  Duplicate updates will be merged via the merge() method.
* Make SquidUpdate support merge() so that duplicate URL
  purges are now caught accross the entire pre-send request
  execution.

Change-Id: Idffdd3e71d89e4a0f28281e65a881113caae497c

autoload.php
includes/deferred/DeferredUpdates.php
includes/deferred/MergeableUpdate.php [new file with mode: 0644]
includes/deferred/SquidUpdate.php
tests/phpunit/includes/deferred/DeferredUpdatesTest.php
tests/phpunit/includes/deferred/SquidUpdateTest.php [new file with mode: 0644]

index f79e58b..685849b 100644 (file)
@@ -793,6 +793,7 @@ $wgAutoloadLocalClasses = array(
        'MergeHistoryPager' => __DIR__ . '/includes/specials/SpecialMergeHistory.php',
        'MergeLogFormatter' => __DIR__ . '/includes/logging/MergeLogFormatter.php',
        'MergeMessageFileList' => __DIR__ . '/maintenance/mergeMessageFileList.php',
+       'MergeableUpdate' => __DIR__ . '/includes/deferred/MergeableUpdate.php',
        'Message' => __DIR__ . '/includes/Message.php',
        'MessageBlobStore' => __DIR__ . '/includes/cache/MessageBlobStore.php',
        'MessageCache' => __DIR__ . '/includes/cache/MessageCache.php',
index 3323c04..adad908 100644 (file)
@@ -95,7 +95,19 @@ class DeferredUpdates {
        private static function push( array &$queue, DeferrableUpdate $update ) {
                global $wgCommandLineMode;
 
-               array_push( $queue, $update );
+               if ( $update instanceof MergeableUpdate ) {
+                       $class = get_class( $update ); // fully-qualified class
+                       if ( isset( $queue[$class] ) ) {
+                               /** @var $existingUpdate MergeableUpdate */
+                               $existingUpdate = $queue[$class];
+                               $existingUpdate->merge( $update );
+                       } else {
+                               $queue[$class] = $update;
+                       }
+               } else {
+                       $queue[] = $update;
+               }
+
                if ( self::$forceDeferral ) {
                        return; // do not run
                }
diff --git a/includes/deferred/MergeableUpdate.php b/includes/deferred/MergeableUpdate.php
new file mode 100644 (file)
index 0000000..70760ce
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+/**
+ * Interface that deferrable updates can implement. DeferredUpdates uses this to merge
+ * all pending updates of PHP class into a single update by calling merge().
+ *
+ * @since 1.27
+ */
+interface MergeableUpdate {
+       /**
+        * Merge this update with $update
+        *
+        * @param MergeableUpdate $update Update of the same class type
+        */
+       function merge( MergeableUpdate $update );
+}
index d33e4a5..c223de8 100644 (file)
  * @ingroup Cache
  */
 
+use Wikimedia\Assert\Assert;
+
 /**
  * Handles purging appropriate Squid URLs given a title (or titles)
  * @ingroup Cache
  */
-class SquidUpdate implements DeferrableUpdate {
+class SquidUpdate implements DeferrableUpdate, MergeableUpdate {
        /** @var string[] Collection of URLs to purge */
        protected $urls = array();
 
@@ -33,8 +35,7 @@ class SquidUpdate implements DeferrableUpdate {
         * @param string[] $urlArr Collection of URLs to purge
         */
        public function __construct( array $urlArr ) {
-               // Remove duplicate URLs from list
-               $this->urls = array_unique( $urlArr );
+               $this->urls = $urlArr;
        }
 
        /**
@@ -59,9 +60,7 @@ class SquidUpdate implements DeferrableUpdate {
         * @deprecated 1.27
         */
        public static function newSimplePurge( Title $title ) {
-               $urlArr = $title->getSquidURLs();
-
-               return new SquidUpdate( $urlArr );
+               return new SquidUpdate( $title->getSquidURLs() );
        }
 
        /**
@@ -71,6 +70,13 @@ class SquidUpdate implements DeferrableUpdate {
                self::purge( $this->urls );
        }
 
+       public function merge( MergeableUpdate $update ) {
+               /** @var SquidUpdate $update */
+               Assert::parameterType( __CLASS__, $update, '$update' );
+
+               $this->urls = array_merge( $this->urls, $update->urls );
+       }
+
        /**
         * Purges a list of Squids defined in $wgSquidServers.
         * $urlArr should contain the full URLs to purge as values
@@ -86,6 +92,9 @@ class SquidUpdate implements DeferrableUpdate {
                        return;
                }
 
+               // Remove duplicate URLs from list
+               $urlArr = array_unique( $urlArr );
+
                wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) );
 
                if ( $wgHTCPRouting ) {
@@ -93,8 +102,6 @@ class SquidUpdate implements DeferrableUpdate {
                }
 
                if ( $wgSquidServers ) {
-                       // Remove duplicate URLs
-                       $urlArr = array_unique( $urlArr );
                        // Maximum number of parallel connections per squid
                        $maxSocketsPerSquid = 8;
                        // Number of requests to send per socket
@@ -127,7 +134,7 @@ class SquidUpdate implements DeferrableUpdate {
         * @throws MWException
         * @param string[] $urlArr Collection of URLs to purge
         */
-       protected static function HTCPPurge( array $urlArr ) {
+       private static function HTCPPurge( array $urlArr ) {
                global $wgHTCPRouting, $wgHTCPMulticastTTL;
 
                // HTCP CLR operation
@@ -158,8 +165,6 @@ class SquidUpdate implements DeferrableUpdate {
                                $wgHTCPMulticastTTL );
                }
 
-               // Remove duplicate URLs from collection
-               $urlArr = array_unique( $urlArr );
                // Get sequential trx IDs for packet loss counting
                $ids = UIDGenerator::newSequentialPerNodeIDs(
                        'squidhtcppurge', 32, count( $urlArr ), UIDGenerator::QUICK_VOLATILE
index 42e48ff..736f6e8 100644 (file)
@@ -1,8 +1,5 @@
 <?php
 
-/**
- * @group DeferredUpdates
- */
 class DeferredUpdatesTest extends MediaWikiTestCase {
        public function testDoUpdatesWeb() {
                $this->setMwGlobals( 'wgCommandLineMode', false );
diff --git a/tests/phpunit/includes/deferred/SquidUpdateTest.php b/tests/phpunit/includes/deferred/SquidUpdateTest.php
new file mode 100644 (file)
index 0000000..6ceb42c
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+class SquidUpdatesTest extends MediaWikiTestCase {
+       public function testPurgeMergeWeb() {
+               $this->setMwGlobals( 'wgCommandLineMode', false );
+
+               $urls1 = array();
+               $title = Title::newMainPage();
+               $urls1[] = $title->getCanonicalURL( '?x=1' );
+               $urls1[] = $title->getCanonicalURL( '?x=2' );
+               $urls1[] = $title->getCanonicalURL( '?x=3' );
+               $update1 = new SquidUpdate( $urls1 );
+               DeferredUpdates::addUpdate( $update1 );
+
+               $urls2 = array();
+               $urls2[] = $title->getCanonicalURL( '?x=2' );
+               $urls2[] = $title->getCanonicalURL( '?x=3' );
+               $urls2[] = $title->getCanonicalURL( '?x=4' );
+               $update2 = new SquidUpdate( $urls2 );
+               DeferredUpdates::addUpdate( $update2 );
+
+               $wrapper = TestingAccessWrapper::newFromObject( $update1 );
+               $this->assertEquals( array_merge( $urls1, $urls2 ), $wrapper->urls );
+       }
+}