jenkins-bot has submitted this change and it was merged.

Change subject: Avoid TransactionProfiler notices for TEMPORARY table writes
......................................................................


Avoid TransactionProfiler notices for TEMPORARY table writes

* Make Database detect and track temporary tables for the session.
  Creating, dropping, and updating them do not count as "write" queries.
  Even a read-only mysql slave can have these operations performed on it
  and it can be useful for complex read queries that need temporary results.
* Rename handleTransactionLoss() to handleSessionLoss() and cover named locks 
too.

Bug: T145947
Change-Id: I826439e9e9f550f32a9c46b3dd60e8e8015aa274
---
M includes/libs/rdbms/database/Database.php
1 file changed, 37 insertions(+), 4 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/libs/rdbms/database/Database.php 
b/includes/libs/rdbms/database/Database.php
index f9e9296..538d0b8 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -203,6 +203,8 @@
 
        /** @var array Map of (name => 1) for locks obtained via lock() */
        private $mNamedLocksHeld = [];
+       /** @var array Map of (table name => 1) for TEMPORARY tables */
+       private $mSessionTempTables = [];
 
        /** @var IDatabase|null Lazy handle to the master DB this server 
replicates from */
        private $lazyMasterHandle;
@@ -772,11 +774,40 @@
                return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 
'SHOW', 'SET' ], true );
        }
 
+       /**
+        * @param string $sql A SQL query
+        * @return bool Whether $sql is SQL for creating/dropping a new 
TEMPORARY table
+        */
+       protected function registerTempTableOperation( $sql ) {
+               if ( preg_match(
+                       
'/^(CREATE|DROP)\s+TEMPORARY\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
+                       $sql,
+                       $matches
+               ) ) {
+                       list( , $verb, $table ) = $matches;
+                       if ( $verb === 'CREATE' ) {
+                               $this->mSessionTempTables[$table] = 1;
+                       } else {
+                               unset( $this->mSessionTempTables[$table] );
+                       }
+
+                       return true;
+               } elseif ( preg_match(
+                       
'/^(?:INSERT\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+[`"\']?(\w+)[`"\']?/i',
+                       $sql,
+                       $matches
+               ) ) {
+                       return isset( $this->mSessionTempTables[$matches[1]] );
+               }
+
+               return false;
+       }
+
        public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) 
{
                $priorWritesPending = $this->writesOrCallbacksPending();
                $this->mLastQuery = $sql;
 
-               $isWrite = $this->isWriteQuery( $sql );
+               $isWrite = $this->isWriteQuery( $sql ) && 
!$this->registerTempTableOperation( $sql );
                if ( $isWrite ) {
                        $reason = $this->getReadOnlyReason();
                        if ( $reason !== false ) {
@@ -822,7 +853,7 @@
                        $lastError = $this->lastError();
                        $lastErrno = $this->lastErrno();
                        # Update state tracking to reflect transaction loss due 
to disconnection
-                       $this->handleTransactionLoss();
+                       $this->handleSessionLoss();
                        if ( $this->reconnect() ) {
                                $msg = __METHOD__ . ": lost connection to 
{$this->getServer()}; reconnected";
                                $this->connLogger->warning( $msg );
@@ -851,7 +882,7 @@
                                        $tempIgnore = false; // not recoverable
                                }
                                # Update state tracking to reflect transaction 
loss
-                               $this->handleTransactionLoss();
+                               $this->handleSessionLoss();
                        }
 
                        $this->reportQueryError(
@@ -964,10 +995,12 @@
                return true;
        }
 
-       private function handleTransactionLoss() {
+       private function handleSessionLoss() {
                $this->mTrxLevel = 0;
                $this->mTrxIdleCallbacks = []; // bug 65263
                $this->mTrxPreCommitCallbacks = []; // bug 65263
+               $this->mSessionTempTables = [];
+               $this->mNamedLocksHeld = [];
                try {
                        // Handle callbacks in mTrxEndCallbacks
                        $this->runOnTransactionIdleCallbacks( 
self::TRIGGER_ROLLBACK );

-- 
To view, visit https://gerrit.wikimedia.org/r/311591
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I826439e9e9f550f32a9c46b3dd60e8e8015aa274
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <asch...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Parent5446 <tylerro...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to