Update hit counts in a DeferredUpdate

Bug: T274455
Change-Id: Iadd383f150c5f9b736e37bfd8bdad56298f3d8d5
This commit is contained in:
Daimona Eaytoy 2021-02-12 14:15:00 +01:00 committed by Krinkle
parent 431226ac39
commit 2470bea0d9
2 changed files with 19 additions and 24 deletions

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Extension\AbuseFilter\Watcher;
use DeferredUpdates;
use MediaWiki\Extension\AbuseFilter\CentralDBManager;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\ILoadBalancer;
@ -34,14 +35,17 @@ class UpdateHitCountWatcher implements Watcher {
* @inheritDoc
*/
public function run( array $localFilters, array $globalFilters, string $group ) : void {
if ( count( $localFilters ) ) {
$this->updateHitCounts( $this->loadBalancer->getConnectionRef( DB_MASTER ), $localFilters );
}
// Run in a DeferredUpdate to avoid master queries on raw/view requests (T274455)
DeferredUpdates::addCallableUpdate( function () use ( $localFilters, $globalFilters ) {
if ( $localFilters ) {
$this->updateHitCounts( $this->loadBalancer->getConnectionRef( DB_MASTER ), $localFilters );
}
if ( count( $globalFilters ) ) {
$fdb = $this->centralDBManager->getConnection( DB_MASTER );
$this->updateHitCounts( $fdb, $globalFilters );
}
if ( $globalFilters ) {
$fdb = $this->centralDBManager->getConnection( DB_MASTER );
$this->updateHitCounts( $fdb, $globalFilters );
}
} );
}
/**
@ -49,16 +53,11 @@ class UpdateHitCountWatcher implements Watcher {
* @param array $loggedFilters
*/
private function updateHitCounts( IDatabase $dbw, array $loggedFilters ) : void {
$method = __METHOD__;
$dbw->onTransactionPreCommitOrIdle(
function () use ( $dbw, $loggedFilters, $method ) {
$dbw->update( 'abuse_filter',
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $loggedFilters ],
$method
);
},
$method
$dbw->update(
'abuse_filter',
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $loggedFilters ],
__METHOD__
);
}
}

View file

@ -5,14 +5,15 @@ namespace MediaWiki\Extension\AbuseFilter\Tests\Unit\Watcher;
use IDatabase;
use MediaWiki\Extension\AbuseFilter\CentralDBManager;
use MediaWiki\Extension\AbuseFilter\Watcher\UpdateHitCountWatcher;
use MediaWikiUnitTestCase;
use MediaWikiIntegrationTestCase;
use Wikimedia\Rdbms\ILoadBalancer;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\Watcher\UpdateHitCountWatcher
* @covers ::__construct
* @todo Make this a unit test once DeferredUpdates uses DI (T265749)
*/
class UpdateHitCountWatcherTest extends MediaWikiUnitTestCase {
class UpdateHitCountWatcherTest extends MediaWikiIntegrationTestCase {
/**
* @covers ::run
@ -21,9 +22,6 @@ class UpdateHitCountWatcherTest extends MediaWikiUnitTestCase {
public function testRun() {
$localFilters = [ 1, 2, 3 ];
$globalFilters = [ 4, 5, 6 ];
$onTransactionCB = function ( $cb ) {
$cb();
};
$localDB = $this->createMock( IDatabase::class );
$localDB->expects( $this->once() )->method( 'update' )->with(
@ -31,7 +29,6 @@ class UpdateHitCountWatcherTest extends MediaWikiUnitTestCase {
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $localFilters ]
);
$localDB->method( 'onTransactionPreCommitOrIdle' )->willReturnCallback( $onTransactionCB );
$lb = $this->createMock( ILoadBalancer::class );
$lb->method( 'getConnectionRef' )->willReturn( $localDB );
@ -41,7 +38,6 @@ class UpdateHitCountWatcherTest extends MediaWikiUnitTestCase {
[ 'af_hit_count=af_hit_count+1' ],
[ 'af_id' => $globalFilters ]
);
$globalDB->method( 'onTransactionPreCommitOrIdle' )->willReturnCallback( $onTransactionCB );
$centralDBManager = $this->createMock( CentralDBManager::class );
$centralDBManager->method( 'getConnection' )->willReturn( $globalDB );