Moved "large write query" code to TransactionProfiler
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 15 Jan 2015 20:53:27 +0000 (12:53 -0800)
committerBryanDavis <bdavis@wikimedia.org>
Thu, 5 Feb 2015 01:42:25 +0000 (01:42 +0000)
Change-Id: Ic05e832eb21545a4e639b52aca7b3a5811a890ce

includes/db/Database.php
includes/profiler/TransactionProfiler.php

index 054f27a..92d998c 100644 (file)
@@ -46,9 +46,6 @@ abstract class DatabaseBase implements IDatabase {
        /** Maximum time to wait before retry */
        const DEADLOCK_DELAY_MAX = 1500000;
 
-       /** How many row changes in a write query trigger a log entry */
-       const LOG_WRITE_THRESHOLD = 300;
-
        protected $mLastQuery = '';
        protected $mDoneWrites = false;
        protected $mPHPError = false;
@@ -1160,11 +1157,13 @@ abstract class DatabaseBase implements IDatabase {
 
                # Log the query time and feed it into the DB trx profiler
                if ( $queryProf != '' ) {
+                       $that = $this;
                        $queryStartTime = microtime( true );
                        $queryProfile = new ScopedCallback(
-                               function () use ( $queryStartTime, $queryProf, $isMaster ) {
-                                       $trxProfiler = Profiler::instance()->getTransactionProfiler();
-                                       $trxProfiler->recordQueryCompletion( $queryProf, $queryStartTime, $isMaster );
+                               function () use ( $that, $queryStartTime, $queryProf, $isMaster ) {
+                                       $n = $that->affectedRows();
+                                       $trxProf = Profiler::instance()->getTransactionProfiler();
+                                       $trxProf->recordQueryCompletion( $queryProf, $queryStartTime, $isMaster, $n );
                                }
                        );
                }
@@ -1206,13 +1205,6 @@ abstract class DatabaseBase implements IDatabase {
 
                if ( false === $ret ) {
                        $this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
-               } else {
-                       $n = $this->affectedRows();
-                       if ( $isWriteQuery && $n > self::LOG_WRITE_THRESHOLD && PHP_SAPI !== 'cli' ) {
-                               wfDebugLog( 'DBPerformance',
-                                       "Query affected $n rows:\n" .
-                                       DatabaseBase::generalizeSQL( $sql ) . "\n" . wfBacktrace( true ) );
-                       }
                }
 
                $res = $this->resultObject( $ret );
index 886bc5a..3637781 100644 (file)
@@ -34,6 +34,8 @@ class TransactionProfiler {
        protected $dbLockThreshold = 3.0;
        /** @var float Seconds */
        protected $eventThreshold = .25;
+       /** @var integer */
+       protected $affectedThreshold = 500;
 
        /** @var array transaction ID => (write start time, list of DBs involved) */
        protected $dbTrxHoldingLocks = array();
@@ -71,14 +73,20 @@ class TransactionProfiler {
         *
         * This assumes that all queries are synchronous (non-overlapping)
         *
-        * @param string $query Function name
+        * @param string $query Function name or generalized SQL
         * @param float $sTime Starting UNIX wall time
         * @param bool $isWrite Whether this is a write query
+        * @param integer $n Number of affected rows
         */
-       public function recordQueryCompletion( $query, $sTime, $isWrite = false ) {
+       public function recordQueryCompletion( $query, $sTime, $isWrite = false, $n = 0 ) {
                $eTime = microtime( true );
                $elapsed = ( $eTime - $sTime );
 
+               if ( $isWrite && $n > $this->affectedThreshold && PHP_SAPI !== 'cli' ) {
+                       wfDebugLog( 'DBPerformance',
+                               "Query affected $n rows:\n" . $query . "\n" . wfBacktrace( true ) );
+               }
+
                if ( !$this->dbTrxHoldingLocks ) {
                        // Short-circuit
                        return;