Add TransactionProfiler::silenceForScope()

Add a helper method for the common use case of temporarily silencing
transaction profiler warnings.

Change-Id: I40de4daf8756da693de969e5526b471b624b2cee
This commit is contained in:
Gergő Tisza 2021-05-15 10:09:28 +02:00
parent 213a6329a6
commit 8925ba9c50
8 changed files with 45 additions and 43 deletions

View file

@ -28,6 +28,7 @@ use MediaWiki\Permissions\PermissionStatus;
use Psr\Log\LoggerInterface;
use Wikimedia\Rdbms\ChronologyProtector;
use Wikimedia\Rdbms\DBConnectionError;
use Wikimedia\ScopedCallback;
/**
* The MediaWiki class is the helper class for the index.php entry point.
@ -1168,14 +1169,10 @@ class MediaWiki {
* @param int $n Number of jobs to try to run
*/
private function triggerSyncJobs( $n ) {
$trxProfiler = Profiler::instance()->getTransactionProfiler();
$old = $trxProfiler->setSilenced( true );
try {
$runner = MediaWikiServices::getInstance()->getJobRunner();
$runner->run( [ 'maxJobs' => $n ] );
} finally {
$trxProfiler->setSilenced( $old );
}
$scope = Profiler::instance()->getTransactionProfiler()->silenceForScope();
$runner = MediaWikiServices::getInstance()->getJobRunner();
$runner->run( [ 'maxJobs' => $n ] );
ScopedCallback::consume( $scope );
}
/**

View file

@ -44,6 +44,7 @@ use StatusValue;
use User;
use WebRequest;
use Wikimedia\ObjectFactory;
use Wikimedia\ScopedCallback;
/**
* This serves as the entry point to the authentication system.
@ -1740,8 +1741,7 @@ class AuthManager implements LoggerAwareInterface {
] );
// Ignore warnings about master connections/writes...hard to avoid here
$trxProfiler = \Profiler::instance()->getTransactionProfiler();
$old = $trxProfiler->setSilenced( true );
$trxProfilerSilencedScope = \Profiler::instance()->getTransactionProfiler()->silenceForScope();
try {
$status = $user->addToDatabase();
if ( !$status->isOK() ) {
@ -1767,7 +1767,6 @@ class AuthManager implements LoggerAwareInterface {
return $status;
}
} catch ( \Exception $ex ) {
$trxProfiler->setSilenced( $old );
$this->logger->error( __METHOD__ . ': {username} failed with exception {exception}', [
'username' => $username,
'exception' => $ex,
@ -1805,7 +1804,7 @@ class AuthManager implements LoggerAwareInterface {
$logEntry->insert();
}
$trxProfiler->setSilenced( $old );
ScopedCallback::consume( $trxProfilerSilencedScope );
if ( $login ) {
$this->setSessionDataForUser( $user );

View file

@ -21,6 +21,7 @@
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DBQueryError;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\ScopedCallback;
/**
* LCStore implementation which uses the standard DB functions to store data.
@ -87,28 +88,24 @@ class LCStoreDB implements LCStore {
throw new MWException( __CLASS__ . ': must call startWrite() before finishWrite()' );
}
$trxProfiler = Profiler::instance()->getTransactionProfiler();
$oldSilenced = $trxProfiler->setSilenced( true );
$scope = Profiler::instance()->getTransactionProfiler()->silenceForScope();
$dbw = $this->getWriteConnection();
$dbw->startAtomic( __METHOD__ );
try {
$dbw = $this->getWriteConnection();
$dbw->startAtomic( __METHOD__ );
try {
$dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->code ], __METHOD__ );
foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
$dbw->insert( 'l10n_cache', $rows, __METHOD__ );
}
$this->writesDone = true;
} catch ( DBQueryError $e ) {
if ( $dbw->wasReadOnlyError() ) {
$this->readOnly = true; // just avoid site down time
} else {
throw $e;
}
$dbw->delete( 'l10n_cache', [ 'lc_lang' => $this->code ], __METHOD__ );
foreach ( array_chunk( $this->batch, 500 ) as $rows ) {
$dbw->insert( 'l10n_cache', $rows, __METHOD__ );
}
$this->writesDone = true;
} catch ( DBQueryError $e ) {
if ( $dbw->wasReadOnlyError() ) {
$this->readOnly = true; // just avoid site down time
} else {
throw $e;
}
$dbw->endAtomic( __METHOD__ );
} finally {
$trxProfiler->setSilenced( $oldSilenced );
}
$dbw->endAtomic( __METHOD__ );
ScopedCallback::consume( $scope );
$this->code = null;
$this->batch = [];

View file

@ -25,6 +25,7 @@ use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use RuntimeException;
use Wikimedia\ScopedCallback;
/**
* Detect high-contention DB queries via profiling calls.
@ -116,6 +117,17 @@ class TransactionProfiler implements LoggerAwareInterface {
return $old;
}
/**
* Disable the logging of warnings until the returned object goes out of scope or is consumed.
* @return ScopedCallback
*/
public function silenceForScope() {
$oldSilenced = $this->setSilenced( true );
return new ScopedCallback( function () use ( $oldSilenced ) {
$this->setSilenced( $oldSilenced );
} );
}
/**
* Set performance expectations
*

View file

@ -2197,7 +2197,7 @@ class LoadBalancer implements ILoadBalancer {
),
self::TTL_CACHE_READONLY,
function () use ( $domain ) {
$old = $this->trxProfiler->setSilenced( true );
$scope = $this->trxProfiler->silenceForScope();
try {
$index = $this->getWriterIndex();
// Reset the cache for isMasterConnectionReadOnly()
@ -2209,7 +2209,7 @@ class LoadBalancer implements ILoadBalancer {
} catch ( DBError $e ) {
$readOnly = 0;
}
$this->trxProfiler->setSilenced( $old );
ScopedCallback::consume( $scope );
return $readOnly;
},

View file

@ -1219,11 +1219,6 @@ class SqlBagOStuff extends MediumSpecificBagOStuff {
if ( $this->serverInfos ) {
return null; // no TransactionProfiler injected anyway
}
$trxProfiler = Profiler::instance()->getTransactionProfiler();
$oldSilenced = $trxProfiler->setSilenced( true );
return new ScopedCallback( static function () use ( $trxProfiler, $oldSilenced ) {
$trxProfiler->setSilenced( $oldSilenced );
} );
return Profiler::instance()->getTransactionProfiler()->silenceForScope();
}
}

View file

@ -22,6 +22,7 @@
*/
use MediaWiki\User\UserFactory;
use Wikimedia\ScopedCallback;
/**
* Special page allows users to request email confirmation message, and handles
@ -80,9 +81,9 @@ class SpecialConfirmEmail extends UnlistedSpecialPage {
$this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
}
} else {
$old = $trxProfiler->setSilenced( true );
$scope = $trxProfiler->silenceForScope();
$this->attemptConfirm( $code );
$trxProfiler->setSilenced( $old );
ScopedCallback::consume( $scope );
}
}

View file

@ -22,6 +22,7 @@
*/
use MediaWiki\User\UserFactory;
use Wikimedia\ScopedCallback;
/**
* Special page allows users to cancel an email confirmation using the e-mail
@ -56,9 +57,9 @@ class SpecialEmailInvalidate extends UnlistedSpecialPage {
$this->checkReadOnly();
$this->checkPermissions();
$old = $trxProfiler->setSilenced( true );
$scope = $trxProfiler->silenceForScope();
$this->attemptInvalidate( $code );
$trxProfiler->setSilenced( $old );
ScopedCallback::consume( $scope );
}
/**