Merge "objectcache: make BagOStuff::add() abstract to discourage non-atomic versions"
authorKrinkle <krinklemail@gmail.com>
Fri, 15 Mar 2019 01:00:26 +0000 (01:00 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 15 Mar 2019 01:00:26 +0000 (01:00 +0000)
includes/db/MWLBFactory.php
includes/filebackend/FileBackendGroup.php
includes/libs/filebackend/FileBackend.php
includes/libs/rdbms/database/Database.php
includes/profiler/Profiler.php
includes/user/User.php
tests/phpunit/includes/db/DatabaseTestHelper.php

index e50f855..cb1a69d 100644 (file)
@@ -54,7 +54,9 @@ abstract class MWLBFactory {
                                $mainConfig->get( 'DBmwschema' ),
                                $mainConfig->get( 'DBprefix' )
                        ),
-                       'profiler' => Profiler::instance(),
+                       'profiler' => function ( $section ) {
+                               return Profiler::instance()->scopedProfileIn( $section );
+                       },
                        'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
                        'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
                        'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
index cbf9bff..a091608 100644 (file)
@@ -189,7 +189,9 @@ class FileBackendGroup {
                        'wanCache' => MediaWikiServices::getInstance()->getMainWANObjectCache(),
                        'srvCache' => ObjectCache::getLocalServerInstance( 'hash' ),
                        'logger' => LoggerFactory::getInstance( 'FileOperation' ),
-                       'profiler' => Profiler::instance()
+                       'profiler' => function ( $section ) {
+                               return Profiler::instance()->scopedProfileIn( $section );
+                       }
                ];
                $config['lockManager'] =
                        LockManagerGroup::singleton( $config['wikiId'] )->get( $config['lockManager'] );
index a80b6d0..19373ea 100644 (file)
@@ -114,7 +114,7 @@ abstract class FileBackend implements LoggerAwareInterface {
        protected $fileJournal;
        /** @var LoggerInterface */
        protected $logger;
-       /** @var object|string Class name or object With profileIn/profileOut methods */
+       /** @var callable|null */
        protected $profiler;
 
        /** @var callable */
@@ -156,7 +156,8 @@ abstract class FileBackend implements LoggerAwareInterface {
         *   - obResetFunc : alternative callback to clear the output buffer
         *   - streamMimeFunc : alternative method to determine the content type from the path
         *   - logger : Optional PSR logger object.
-        *   - profiler : Optional class name or object With profileIn/profileOut methods.
+        *   - profiler : Optional callback that takes a section name argument and returns
+        *      a ScopedCallback instance that ends the profile section in its destructor.
         * @throws InvalidArgumentException
         */
        public function __construct( array $config ) {
@@ -187,6 +188,9 @@ abstract class FileBackend implements LoggerAwareInterface {
                $this->statusWrapper = $config['statusWrapper'] ?? null;
 
                $this->profiler = $config['profiler'] ?? null;
+               if ( !is_callable( $this->profiler ) ) {
+                       $this->profiler = null;
+               }
                $this->logger = $config['logger'] ?? new \Psr\Log\NullLogger();
                $this->statusWrapper = $config['statusWrapper'] ?? null;
                $this->tmpDirectory = $config['tmpDirectory'] ?? null;
@@ -1599,12 +1603,7 @@ abstract class FileBackend implements LoggerAwareInterface {
         * @return ScopedCallback|null
         */
        protected function scopedProfileSection( $section ) {
-               if ( $this->profiler ) {
-                       call_user_func( [ $this->profiler, 'profileIn' ], $section );
-                       return new ScopedCallback( [ $this->profiler, 'profileOut' ], [ $section ] );
-               }
-
-               return null;
+               return $this->profiler ? ( $this->profiler )( $section ) : null;
        }
 
        protected function resetOutputBuffer() {
index 4d2da17..13bf8f0 100644 (file)
@@ -260,7 +260,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        /** @var int[] Prior flags member variable values */
        private $priorFlags = [];
 
-       /** @var mixed Class name or object With profileIn/profileOut methods */
+       /** @var callable|null */
        protected $profiler;
        /** @var TransactionProfiler */
        protected $trxProfiler;
@@ -308,7 +308,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
                $this->srvCache = $params['srvCache'] ?? new HashBagOStuff();
 
-               $this->profiler = $params['profiler'];
+               $this->profiler = is_callable( $params['profiler'] ) ? $params['profiler'] : null;
                $this->trxProfiler = $params['trxProfiler'];
                $this->connLogger = $params['connLogger'];
                $this->queryLogger = $params['queryLogger'];
@@ -408,9 +408,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         *      used to adjust lock timeouts or encoding modes and the like.
         *   - connLogger: Optional PSR-3 logger interface instance.
         *   - queryLogger: Optional PSR-3 logger interface instance.
-        *   - profiler: Optional class name or object with profileIn()/profileOut() methods.
-        *      These will be called in query(), using a simplified version of the SQL that also
-        *      includes the agent as a SQL comment.
+        *   - profiler : Optional callback that takes a section name argument and returns
+        *      a ScopedCallback instance that ends the profile section in its destructor.
+        *      These will be called in query(), using a simplified version of the SQL that
+        *      also includes the agent as a SQL comment.
         *   - trxProfiler: Optional TransactionProfiler instance.
         *   - errorLogger: Optional callback that takes an Exception and logs it.
         *   - deprecationLogger: Optional callback that takes a string and logs it.
@@ -1272,19 +1273,13 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                $queryProf .= $this->trxShortId ? " [TRX#{$this->trxShortId}]" : "";
 
                $startTime = microtime( true );
-               if ( $this->profiler ) {
-                       $this->profiler->profileIn( $queryProf );
-               }
+               $ps = $this->profiler ? ( $this->profiler )( $queryProf ) : null;
                $this->affectedRowCount = null;
                $ret = $this->doQuery( $commentedSql );
                $this->affectedRowCount = $this->affectedRows();
-               if ( $this->profiler ) {
-                       $this->profiler->profileOut( $queryProf );
-               }
+               unset( $ps ); // profile out (if set)
                $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
 
-               unset( $queryProfSection ); // profile out (if set)
-
                if ( $ret !== false ) {
                        $this->lastPing = $startTime;
                        if ( $isWrite && $this->trxLevel ) {
index 1f490f9..554ca08 100644 (file)
@@ -147,11 +147,12 @@ abstract class Profiler {
                }
        }
 
-       // Kept BC for now, remove when possible
        public function profileIn( $functionname ) {
+               wfDeprecated( __METHOD__, '1.33' );
        }
 
        public function profileOut( $functionname ) {
+               wfDeprecated( __METHOD__, '1.33' );
        }
 
        /**
index d6de0aa..277731a 100644 (file)
@@ -4268,7 +4268,7 @@ class User implements IDBAccessObject, UserIdentity {
 
                Hooks::run( 'UserSaveSettings', [ $this ] );
                $this->clearSharedCache();
-               $this->getUserPage()->invalidateCache();
+               $this->getUserPage()->purgeSquid();
        }
 
        /**
index 65b82ab..ff10fef 100644 (file)
@@ -45,7 +45,7 @@ class DatabaseTestHelper extends Database {
        public function __construct( $testName, array $opts = [] ) {
                $this->testName = $testName;
 
-               $this->profiler = new ProfilerStub( [] );
+               $this->profiler = null;
                $this->trxProfiler = new TransactionProfiler();
                $this->cliMode = $opts['cliMode'] ?? true;
                $this->connLogger = new \Psr\Log\NullLogger();