From 5c681e438fd162ac6b140e07e15f2dbc1393a775 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 17 Sep 2016 15:30:17 -0700 Subject: [PATCH] Inject Profiler into Database objects * This removes the Profiler::instance() dependency from a class in /libs. * Also removed unused setTransactionProfiler() method Change-Id: Iec735a130914090dc51aed30a283565a66aabee6 --- includes/db/loadbalancer/LBFactoryMW.php | 1 + includes/libs/rdbms/database/Database.php | 22 +++++++------------ includes/libs/rdbms/lbfactory/LBFactory.php | 5 +++++ .../libs/rdbms/loadbalancer/LoadBalancer.php | 4 ++++ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/includes/db/loadbalancer/LBFactoryMW.php b/includes/db/loadbalancer/LBFactoryMW.php index 49d0624710..b36197a808 100644 --- a/includes/db/loadbalancer/LBFactoryMW.php +++ b/includes/db/loadbalancer/LBFactoryMW.php @@ -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' ), diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 9a63b7fced..34b4b53b9a 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -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) diff --git a/includes/libs/rdbms/lbfactory/LBFactory.php b/includes/libs/rdbms/lbfactory/LBFactory.php index 49fac6afc2..72f2b5d6b0 100644 --- a/includes/libs/rdbms/lbfactory/LBFactory.php +++ b/includes/libs/rdbms/lbfactory/LBFactory.php @@ -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, diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index 57c905facc..8cd79a3e9d 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -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; -- 2.20.1