mediawiki-extensions-Linter/includes/RecordLintJob.php
Kunal Mehta eebd04aa00 Add caching to looking up totals
The query itself is too expensive to be run on large Wikimedia wikis. So
put it behind WAN cache and touch the check keys for each category
whenever those have errors added or deleted from them.

If this happens to get out of sync, it will get fully refreshed
regularly when the totals are sent to statsd.

WANObjectCache's 'lockTSE' feature will help avoid cache stampedes that
made this query expensive in the past.

Change-Id: I3774103a29fa0f29d36283950f136259fa71bffe
2017-05-29 07:33:41 -07:00

124 lines
3.4 KiB
PHP

<?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
*/
namespace MediaWiki\Linter;
use Job;
use MediaWiki\MediaWikiServices;
use Title;
class RecordLintJob extends Job {
/**
* RecordLintJob constructor.
* @param Title $title
* @param array $params
*/
public function __construct( Title $title, array $params ) {
parent::__construct( 'RecordLintJob', $title, $params );
}
public function run() {
if ( $this->title->getLatestRevID() != $this->params['revision'] ) {
// Outdated now, let a later job handle it
return true;
}
// [ 'category' => [ 'id' => LintError ] ]
$errors = [];
foreach ( $this->params['errors'] as $errorInfo ) {
$error = new LintError(
$errorInfo['type'],
$errorInfo['location'],
$errorInfo['params']
);
// Use unique id as key to get rid of exact dupes
// (e.g. same category of error in same template)
$errors[$error->category][$error->id()] = $error;
}
$lintDb = new Database( $this->title->getArticleID() );
$toSet = [];
foreach ( $errors as $category => $catErrors ) {
// If there are too many errors for a category, trim some of them.
if ( count( $catErrors ) > $lintDb::MAX_PER_CAT ) {
$catErrors = array_slice( $catErrors, 0, $lintDb::MAX_PER_CAT );
}
$toSet = array_merge( $toSet, $catErrors );
}
$changes = $lintDb->setForPage( $toSet );
$this->updateStats( $lintDb, $changes );
return true;
}
/**
* Send stats to statsd and update totals cache
*
* @param Database $lintDb
* @param array $changes
*/
protected function updateStats( Database $lintDb, array $changes ) {
global $wgLinterStatsdSampleFactor;
$mwServices = MediaWikiServices::getInstance();
$totalsLookup = new TotalsLookup(
new CategoryManager(),
$mwServices->getMainWANObjectCache()
);
if ( $wgLinterStatsdSampleFactor === false ) {
// Don't send to statsd, but update cache with $changes
$raw = $changes['added'];
foreach ( $changes['deleted'] as $cat => $count ) {
if ( isset( $raw[$cat] ) ) {
$raw[$cat] -= $count;
} else {
// Negative value
$raw[$cat] = 0 - $count;
}
}
foreach ( $raw as $cat => $count ) {
if ( $count != 0 ) {
// There was a change in counts, invalidate the cache
$totalsLookup->touchCategoryCache( $cat );
}
}
return;
} elseif ( mt_rand( 1, $wgLinterStatsdSampleFactor ) != 1 ) {
return;
}
$totals = $lintDb->getTotals();
$wiki = wfWikiID();
$stats = $mwServices->getStatsdDataFactory();
foreach ( $totals as $name => $count ) {
$stats->gauge( "linter.category.$name.$wiki", $count );
}
$stats->gauge( "linter.totals.$wiki", array_sum( $totals ) );
$totalsLookup->touchAllCategoriesCache();
}
}