Inject Profiler into Database objects
authorAaron Schulz <aschulz@wikimedia.org>
Sat, 17 Sep 2016 22:30:17 +0000 (15:30 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Sat, 17 Sep 2016 22:32:57 +0000 (22:32 +0000)
* This removes the Profiler::instance() dependency from
  a class in /libs.
* Also removed unused setTransactionProfiler() method

Change-Id: Iec735a130914090dc51aed30a283565a66aabee6

includes/db/loadbalancer/LBFactoryMW.php
includes/libs/rdbms/database/Database.php
includes/libs/rdbms/lbfactory/LBFactory.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php

index 49d0624..b36197a 100644 (file)
@@ -42,6 +42,7 @@ abstract class LBFactoryMW extends LBFactory implements DestructibleService {
                $defaults = [
                        'localDomain' => wfWikiID(),
                        'hostname' => wfHostname(),
+                       'profiler' => Profiler::instance(),
                        'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
                        'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
                        'queryLogger' => LoggerFactory::getInstance( 'wfLogDBError' ),
index 9a63b7f..34b4b53 100644 (file)
@@ -223,7 +223,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
        /** @var int[] Prior mFlags values */
        private $priorFlags = [];
 
-       /** @var Profiler */
+       /** @var object|string Class name or object With profileIn/profileOut methods */
        protected $profiler;
        /** @var TransactionProfiler */
        protected $trxProfiler;
@@ -272,9 +272,7 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                        ? $params['srvCache']
                        : new HashBagOStuff();
 
-               $this->profiler = isset( $params['profiler'] )
-                       ? $params['profiler']
-                       : Profiler::instance(); // @TODO: remove global state
+               $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
                $this->trxProfiler = isset( $params['trxProfiler'] )
                        ? $params['trxProfiler']
                        : new TransactionProfiler();
@@ -498,14 +496,6 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                return $this->lazyMasterHandle;
        }
 
-       /**
-        * @param TransactionProfiler $profiler
-        * @since 1.27
-        */
-       public function setTransactionProfiler( TransactionProfiler $profiler ) {
-               $this->trxProfiler = $profiler;
-       }
-
        public function implicitGroupby() {
                return true;
        }
@@ -892,9 +882,13 @@ abstract class Database implements IDatabase, LoggerAwareInterface {
                $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : "";
 
                $startTime = microtime( true );
-               $this->profiler->profileIn( $queryProf );
+               if ( $this->profiler ) {
+                       call_user_func( [ $this->profiler, 'profileIn' ], $queryProf );
+               }
                $ret = $this->doQuery( $commentedSql );
-               $this->profiler->profileOut( $queryProf );
+               if ( $this->profiler ) {
+                       call_user_func( [ $this->profiler, 'profileOut' ], $queryProf );
+               }
                $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
 
                unset( $queryProfSection ); // profile out (if set)
index 49fac6a..72f2b5d 100644 (file)
@@ -30,6 +30,8 @@ use Psr\Log\LoggerInterface;
 abstract class LBFactory {
        /** @var ChronologyProtector */
        protected $chronProt;
+       /** @var object|string Class name or object With profileIn/profileOut methods */
+       protected $profiler;
        /** @var TransactionProfiler */
        protected $trxProfiler;
        /** @var LoggerInterface */
@@ -106,6 +108,8 @@ abstract class LBFactory {
                $this->chronProt = isset( $conf['chronProt'] )
                        ? $conf['chronProt']
                        : $this->newChronologyProtector();
+
+               $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
                $this->trxProfiler = isset( $conf['trxProfiler'] )
                        ? $conf['trxProfiler']
                        : new TransactionProfiler();
@@ -611,6 +615,7 @@ abstract class LBFactory {
                        'readOnlyReason' => $this->readOnlyReason,
                        'srvCache' => $this->srvCache,
                        'wanCache' => $this->wanCache,
+                       'profiler' => $this->profiler,
                        'trxProfiler' => $this->trxProfiler,
                        'queryLogger' => $this->queryLogger,
                        'connLogger' => $this->connLogger,
index 57c905f..8cd79a3 100644 (file)
@@ -53,6 +53,8 @@ class LoadBalancer implements ILoadBalancer {
        private $memCache;
        /** @var WANObjectCache */
        private $wanCache;
+       /** @var object|string Class name or object With profileIn/profileOut methods */
+       protected $profiler;
        /** @var TransactionProfiler */
        protected $trxProfiler;
        /** @var LoggerInterface */
@@ -170,6 +172,7 @@ class LoadBalancer implements ILoadBalancer {
                } else {
                        $this->wanCache = WANObjectCache::newEmpty();
                }
+               $this->profiler = isset( $params['profiler'] ) ? $params['profiler'] : null;
                if ( isset( $params['trxProfiler'] ) ) {
                        $this->trxProfiler = $params['trxProfiler'];
                } else {
@@ -821,6 +824,7 @@ class LoadBalancer implements ILoadBalancer {
                // Set loggers
                $server['connLogger'] = $this->connLogger;
                $server['queryLogger'] = $this->queryLogger;
+               $server['profiler'] = $this->profiler;
                $server['trxProfiler'] = $this->trxProfiler;
                $server['cliMode'] = $this->cliMode;
                $server['errorLogger'] = $this->errorLogger;