Moved RecentChange::purgeExpiredChanges to a job
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 17 Jan 2015 01:02:10 +0000 (17:02 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 17 Jan 2015 01:39:31 +0000 (17:39 -0800)
* Also added a selectFieldValues helper method to the DB classes
  since this use case keeps coming up.

Change-Id: I62cdbb497dc2c8fe4758e756d13688b85165e869

autoload.php
includes/DefaultSettings.php
includes/changes/RecentChange.php
includes/db/Database.php
includes/jobqueue/jobs/RecentChangesUpdateJob.php [new file with mode: 0644]
includes/page/WikiPage.php

index 46c8b01..ab0102a 100644 (file)
@@ -938,6 +938,7 @@ $wgAutoloadLocalClasses = array(
        'RebuildSitesCache' => __DIR__ . '/maintenance/rebuildSitesCache.php',
        'RebuildTextIndex' => __DIR__ . '/maintenance/rebuildtextindex.php',
        'RecentChange' => __DIR__ . '/includes/changes/RecentChange.php',
+       'RecentChangesUpdateJob' => __DIR__ . '/includes/jobqueue/jobs/RecentChangesUpdateJob.php',
        'RecompressTracked' => __DIR__ . '/maintenance/storage/recompressTracked.php',
        'RedirectSpecialArticle' => __DIR__ . '/includes/specialpage/RedirectSpecialPage.php',
        'RedirectSpecialPage' => __DIR__ . '/includes/specialpage/RedirectSpecialPage.php',
index ee462d8..be7d8c4 100644 (file)
@@ -6411,6 +6411,7 @@ $wgJobClasses = array(
        'AssembleUploadChunks' => 'AssembleUploadChunksJob',
        'PublishStashedFile' => 'PublishStashedFileJob',
        'ThumbnailRender' => 'ThumbnailRenderJob',
+       'recentChangesUpdate' => 'RecentChangesUpdateJob',
        'null' => 'NullJob'
 );
 
index 86cd1d7..b430bab 100644 (file)
@@ -796,29 +796,6 @@ class RecentChange {
                return ChangesList::showCharacterDifference( $old, $new );
        }
 
-       /**
-        * Purge expired changes from the recentchanges table
-        * @since 1.22
-        */
-       public static function purgeExpiredChanges() {
-               if ( wfReadOnly() ) {
-                       return;
-               }
-
-               $method = __METHOD__;
-               $dbw = wfGetDB( DB_MASTER );
-               $dbw->onTransactionIdle( function () use ( $dbw, $method ) {
-                       global $wgRCMaxAge;
-
-                       $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
-                       $dbw->delete(
-                               'recentchanges',
-                               array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
-                               $method
-                       );
-               } );
-       }
-
        private static function checkIPAddress( $ip ) {
                global $wgRequest;
                if ( $ip ) {
index 054f27a..cedd624 100644 (file)
@@ -1391,9 +1391,13 @@ abstract class DatabaseBase implements IDatabase {
         *
         * @return bool|mixed The value from the field, or false on failure.
         */
-       public function selectField( $table, $var, $cond = '', $fname = __METHOD__,
-               $options = array()
+       public function selectField(
+               $table, $var, $cond = '', $fname = __METHOD__, $options = array()
        ) {
+               if ( $var === '*' ) { // sanity
+                       throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
+               }
+
                if ( !is_array( $options ) ) {
                        $options = array( $options );
                }
@@ -1401,7 +1405,6 @@ abstract class DatabaseBase implements IDatabase {
                $options['LIMIT'] = 1;
 
                $res = $this->select( $table, $var, $cond, $fname, $options );
-
                if ( $res === false || !$this->numRows( $res ) ) {
                        return false;
                }
@@ -1415,6 +1418,48 @@ abstract class DatabaseBase implements IDatabase {
                }
        }
 
+       /**
+        * A SELECT wrapper which returns a list of single field values from result rows.
+        *
+        * Usually throws a DBQueryError on failure. If errors are explicitly
+        * ignored, returns false on failure.
+        *
+        * If no result rows are returned from the query, false is returned.
+        *
+        * @param string|array $table Table name. See DatabaseBase::select() for details.
+        * @param string $var The field name to select. This must be a valid SQL
+        *   fragment: do not use unvalidated user input.
+        * @param string|array $cond The condition array. See DatabaseBase::select() for details.
+        * @param string $fname The function name of the caller.
+        * @param string|array $options The query options. See DatabaseBase::select() for details.
+        *
+        * @return bool|array The values from the field, or false on failure
+        * @since 1.25
+        */
+       public function selectFieldValues(
+               $table, $var, $cond = '', $fname = __METHOD__, $options = array()
+       ) {
+               if ( $var === '*' ) { // sanity
+                       throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
+               }
+
+               if ( !is_array( $options ) ) {
+                       $options = array( $options );
+               }
+
+               $res = $this->select( $table, $var, $cond, $fname, $options );
+               if ( $res === false ) {
+                       return false;
+               }
+
+               $values = array();
+               foreach ( $res as $row ) {
+                       $values[] = $row->$var;
+               }
+
+               return $values;
+       }
+
        /**
         * Returns an optional USE INDEX clause to go after the table, and a
         * string to go at the end of the query.
diff --git a/includes/jobqueue/jobs/RecentChangesUpdateJob.php b/includes/jobqueue/jobs/RecentChangesUpdateJob.php
new file mode 100644 (file)
index 0000000..9f22ba4
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+/**
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Aaron Schulz
+ */
+
+/**
+ * Job for pruning recent changes
+ *
+ * @ingroup JobQueue
+ * @since 1.25
+ */
+class RecentChangesUpdateJob extends Job {
+       function __construct( $title, $params ) {
+               parent::__construct( 'recentChangesUpdate', $title, $params );
+
+               if ( !isset( $params['type'] ) ) {
+                       throw new Exception( "Missing 'type' parameter." );
+               }
+
+               $this->removeDuplicates = true;
+       }
+
+       /**
+        * @return RecentChangesUpdateJob
+        */
+       final public static function newPurgeJob() {
+               return new self(
+                       SpecialPage::getTitleFor( 'Recentchanges' ), array( 'type' => 'purge' )
+               );
+       }
+
+       public function run() {
+               if ( $this->params['type'] === 'purge' ) {
+                       $this->purgeExpiredRows();
+               } else {
+                       throw new Exception( "Invalid 'type' parameter '{$this->params['type']}'." );
+               }
+
+               return true;
+       }
+
+       protected function purgeExpiredRows() {
+               global $wgRCMaxAge;
+
+               $dbw = wfGetDB( DB_MASTER );
+               if ( !$dbw->lock( 'recentchanges-prune', __METHOD__, 1 ) ) {
+                       return true; // already in progress
+               }
+
+               $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
+               do {
+                       $rcIds = $dbw->selectFieldValues( 'recentchanges',
+                               'rc_id',
+                               array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
+                               __METHOD__,
+                               array( 'LIMIT' => 100 ) // avoid slave lag
+                       );
+                       if ( $rcIds ) {
+                               $dbw->delete( 'recentchanges', array( 'rc_id' => $rcIds ), __METHOD__ );
+                       }
+               } while ( $rcIds );
+
+               $dbw->unlock( 'recentchanges-prune', __METHOD__ );
+       }
+}
index 8373dc0..dc84ea0 100644 (file)
@@ -2167,11 +2167,8 @@ class WikiPage implements Page, IDBAccessObject {
                Hooks::run( 'ArticleEditUpdates', array( &$this, &$editInfo, $options['changed'] ) );
 
                if ( Hooks::run( 'ArticleEditUpdatesDeleteFromRecentchanges', array( &$this ) ) ) {
-                       if ( 0 == mt_rand( 0, 99 ) ) {
-                               // Flush old entries from the `recentchanges` table; we do this on
-                               // random requests so as to avoid an increase in writes for no good reason
-                               RecentChange::purgeExpiredChanges();
-                       }
+                       // Flush old entries from the `recentchanges` table
+                       JobQueueGroup::singleton()->push( RecentChangesUpdateJob::newPurgeJob() );
                }
 
                if ( !$this->exists() ) {