Merge "Ignore noop DB transactions errors on connection loss"
[lhc/web/wiklou.git] / includes / deferred / CdnCacheUpdate.php
index eb54bc2..32f6adc 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Squid cache purging.
+ * CDN cache purging.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 use Wikimedia\Assert\Assert;
 
 /**
- * Handles purging appropriate Squid URLs given a title (or titles)
+ * Handles purging appropriate CDN URLs given a title (or titles)
  * @ingroup Cache
  */
 class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
        /** @var string[] Collection of URLs to purge */
-       protected $urls = array();
+       protected $urls = [];
 
        /**
         * @param string[] $urlArr Collection of URLs to purge
@@ -38,6 +38,13 @@ class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
                $this->urls = $urlArr;
        }
 
+       public function merge( MergeableUpdate $update ) {
+               /** @var CdnCacheUpdate $update */
+               Assert::parameterType( __CLASS__, $update, '$update' );
+
+               $this->urls = array_merge( $this->urls, $update->urls );
+       }
+
        /**
         * Create an update object from an array of Title objects, or a TitleArray object
         *
@@ -45,10 +52,10 @@ class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
         * @param string[] $urlArr
         * @return CdnCacheUpdate
         */
-       public static function newFromTitles( $titles, $urlArr = array() ) {
+       public static function newFromTitles( $titles, $urlArr = [] ) {
                /** @var Title $title */
                foreach ( $titles as $title ) {
-                       $urlArr = array_merge( $urlArr, $title->getSquidURLs() );
+                       $urlArr = array_merge( $urlArr, $title->getCdnUrls() );
                }
 
                return new CdnCacheUpdate( $urlArr );
@@ -60,28 +67,32 @@ class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
         * @deprecated 1.27
         */
        public static function newSimplePurge( Title $title ) {
-               return new CdnCacheUpdate( $title->getSquidURLs() );
+               return new CdnCacheUpdate( $title->getCdnUrls() );
        }
 
        /**
         * Purges the list of URLs passed to the constructor.
         */
        public function doUpdate() {
-               self::purge( $this->urls );
-       }
+               global $wgCdnReboundPurgeDelay;
 
-       public function merge( MergeableUpdate $update ) {
-               /** @var CdnCacheUpdate $update */
-               Assert::parameterType( __CLASS__, $update, '$update' );
+               self::purge( $this->urls );
 
-               $this->urls = array_merge( $this->urls, $update->urls );
+               if ( $wgCdnReboundPurgeDelay > 0 ) {
+                       JobQueueGroup::singleton()->lazyPush( new CdnPurgeJob(
+                               Title::makeTitle( NS_SPECIAL, 'Badtitle/' . __CLASS__ ),
+                               [
+                                       'urls' => $this->urls,
+                                       'jobReleaseTimestamp' => time() + $wgCdnReboundPurgeDelay
+                               ]
+                       ) );
+               }
        }
 
        /**
-        * Purges a list of Squids defined in $wgSquidServers.
+        * Purges a list of CDN nodes defined in $wgSquidServers.
         * $urlArr should contain the full URLs to purge as values
         * (example: $urlArr[] = 'http://my.host/something')
-        * XXX report broken Squids per mail or log
         *
         * @param string[] $urlArr List of full URLs to purge
         */
@@ -97,10 +108,22 @@ class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
 
                wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) );
 
+               // Reliably broadcast the purge to all edge nodes
+               $relayer = EventRelayerGroup::singleton()->getRelayer( 'cdn-url-purges' );
+               $relayer->notify(
+                       'cdn-url-purges',
+                       [
+                               'urls' => array_values( $urlArr ), // JSON array
+                               'timestamp' => microtime( true )
+                       ]
+               );
+
+               // Send lossy UDP broadcasting if enabled
                if ( $wgHTCPRouting ) {
                        self::HTCPPurge( $urlArr );
                }
 
+               // Do direct server purges if enabled (this does not scale very well)
                if ( $wgSquidServers ) {
                        // Maximum number of parallel connections per squid
                        $maxSocketsPerSquid = 8;
@@ -184,7 +207,7 @@ class CdnCacheUpdate implements DeferrableUpdate, MergeableUpdate {
 
                        if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
                                // Normalize single entries
-                               $conf = array( $conf );
+                               $conf = [ $conf ];
                        }
                        foreach ( $conf as $subconf ) {
                                if ( !isset( $subconf['host'] ) || !isset( $subconf['port'] ) ) {