saper has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/248340

Change subject: Revert removal of $wgDebugDBTransactions
......................................................................

Revert removal of $wgDebugDBTransactions

This reverts partially commit a88df43d3f068b75f4d50c3c599ad2ccaf589f94.

Change-Id: I639f65767f102d71d6a5a9a6134f51e1aad326b3
---
M RELEASE-NOTES-1.27
M autoload.php
M includes/DefaultSettings.php
M includes/db/Database.php
M includes/db/DatabasePostgres.php
M includes/objectcache/SqlBagOStuff.php
6 files changed, 140 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/40/248340/1

diff --git a/RELEASE-NOTES-1.27 b/RELEASE-NOTES-1.27
index f8293b9..f12bb0b 100644
--- a/RELEASE-NOTES-1.27
+++ b/RELEASE-NOTES-1.27
@@ -15,7 +15,6 @@
   making the behavior configurable. The default values (`false` for the former,
   1000 for the latter) are now hard-coded.
 * $wgDebugDumpSqlLength was removed (deprecated in 1.24).
-* $wgDebugDBTransactions was removed (deprecated in 1.20).
 * $wgUseXVO has been removed, as it provides functionality only used by
   custom Wikimedia patches against Squid 2.x that probably noone uses in
   production anymore. There is now $wgUseKeyHeader that provides similar
diff --git a/autoload.php b/autoload.php
index 8720f33..187e791 100644
--- a/autoload.php
+++ b/autoload.php
@@ -937,6 +937,7 @@
        'PostgresBlob' => __DIR__ . '/includes/db/DatabasePostgres.php',
        'PostgresField' => __DIR__ . '/includes/db/DatabasePostgres.php',
        'PostgresInstaller' => __DIR__ . 
'/includes/installer/PostgresInstaller.php',
+       'PostgresTransactionState' => __DIR__ . 
'/includes/db/DatabasePostgres.php',
        'PostgresUpdater' => __DIR__ . 
'/includes/installer/PostgresUpdater.php',
        'Preferences' => __DIR__ . '/includes/Preferences.php',
        'PreferencesForm' => __DIR__ . '/includes/Preferences.php',
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 189ce42..a31f39d 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -5477,6 +5477,13 @@
 $wgDebugComments = false;
 
 /**
+ * Extensive database transaction state debugging
+ *
+ * @since 1.20
+ */
+$wgDebugDBTransactions = false;
+
+/**
  * Write SQL queries to the debug log.
  *
  * This setting is only used $wgLBFactoryConf['class'] is set to
@@ -5487,6 +5494,13 @@
 $wgDebugDumpSql = false;
 
 /**
+ * Trim logged SQL queries to this many bytes. Set 0/false/null to do no
+ * trimming.
+ * @since 1.24
+ */
+$wgDebugDumpSqlLength = 500;
+
+/**
  * Performance expectations for DB usage
  *
  * @since 1.26
diff --git a/includes/db/Database.php b/includes/db/Database.php
index 2f135a4..48f4f3a 100644
--- a/includes/db/Database.php
+++ b/includes/db/Database.php
@@ -485,7 +485,11 @@
         *   - DBO_PERSISTENT: use persistant database connection
         */
        public function setFlag( $flag ) {
+               global $wgDebugDBTransactions;
                $this->mFlags |= $flag;
+               if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) {
+                       wfDebug( "Implicit transactions are now enabled.\n" );
+               }
        }
 
        /**
@@ -500,7 +504,11 @@
         *   - DBO_PERSISTENT: use persistant database connection
         */
        public function clearFlag( $flag ) {
+               global $wgDebugDBTransactions;
                $this->mFlags &= ~$flag;
+               if ( ( $flag & DBO_TRX ) && $wgDebugDBTransactions ) {
+                       wfDebug( "Implicit transactions are now disabled.\n" );
+               }
        }
 
        /**
@@ -605,7 +613,7 @@
         * @param array $params Parameters passed from DatabaseBase::factory()
         */
        function __construct( array $params ) {
-               global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode;
+               global $wgDBprefix, $wgDBmwschema, $wgCommandLineMode, 
$wgDebugDBTransactions;
 
                $this->mTrxAtomicLevels = new SplStack;
                $this->srvCache = ObjectCache::newAccelerator( 'hash' );
@@ -623,8 +631,14 @@
                if ( $this->mFlags & DBO_DEFAULT ) {
                        if ( $wgCommandLineMode ) {
                                $this->mFlags &= ~DBO_TRX;
+                               if ( $wgDebugDBTransactions ) {
+                                       wfDebug( "Implicit transaction open 
disabled.\n" );
+                               }
                        } else {
                                $this->mFlags |= DBO_TRX;
+                               if ( $wgDebugDBTransactions ) {
+                                       wfDebug( "Implicit transaction open 
enabled.\n" );
+                               }
                        }
                }
 
@@ -921,7 +935,7 @@
         *     for a successful read query, or false on failure if $tempIgnore 
set
         */
        public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) 
{
-               global $wgUser;
+               global $wgUser, $wgDebugDBTransactions;
 
                $this->mLastQuery = $sql;
 
@@ -951,6 +965,9 @@
                $commentedSql = preg_replace( '/\s|$/', " /* $fname $userName 
*/ ", $sql, 1 );
 
                if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX ) && 
$this->isTransactableQuery( $sql ) ) {
+                       if ( $wgDebugDBTransactions ) {
+                               wfDebug( "Implicit transaction start.\n" );
+                       }
                        $this->begin( __METHOD__ . " ($fname)" );
                        $this->mTrxAutomatic = true;
                }
@@ -3424,6 +3441,8 @@
         * @throws DBError
         */
        final public function begin( $fname = __METHOD__ ) {
+               global $wgDebugDBTransactions;
+
                if ( $this->mTrxLevel ) { // implicit commit
                        if ( $this->mTrxAtomicLevels ) {
                                // If the current transaction was an automatic 
atomic one, then we definitely have
@@ -3446,8 +3465,9 @@
                                        ) )
                                );
                        } else {
-                               // if the transaction was automatic and has 
done write operations
-                               if ( $this->mTrxDoneWrites ) {
+                               // if the transaction was automatic and has 
done write operations,
+                               // log it if $wgDebugDBTransactions is enabled.
+                               if ( $this->mTrxDoneWrites && 
$wgDebugDBTransactions ) {
                                        wfDebug( "$fname: Automatic transaction 
with writes in progress" .
                                                " (from {$this->mTrxFname}), 
performing implicit commit!\n"
                                        );
diff --git a/includes/db/DatabasePostgres.php b/includes/db/DatabasePostgres.php
index 9c5127f..e5f01e2 100644
--- a/includes/db/DatabasePostgres.php
+++ b/includes/db/DatabasePostgres.php
@@ -129,6 +129,89 @@
 }
 
 /**
+ * Used to debug transaction processing
+ * Only used if $wgDebugDBTransactions is true
+ *
+ * @since 1.19
+ * @ingroup Database
+ */
+class PostgresTransactionState {
+       private static $WATCHED = array(
+               array(
+                       "desc" => "%s: Connection state changed from %s -> 
%s\n",
+                       "states" => array(
+                               PGSQL_CONNECTION_OK => "OK",
+                               PGSQL_CONNECTION_BAD => "BAD"
+                       )
+               ),
+               array(
+                       "desc" => "%s: Transaction state changed from %s -> 
%s\n",
+                       "states" => array(
+                               PGSQL_TRANSACTION_IDLE => "IDLE",
+                               PGSQL_TRANSACTION_ACTIVE => "ACTIVE",
+                               PGSQL_TRANSACTION_INTRANS => "TRANS",
+                               PGSQL_TRANSACTION_INERROR => "ERROR",
+                               PGSQL_TRANSACTION_UNKNOWN => "UNKNOWN"
+                       )
+               )
+       );
+
+       /** @var array */
+       private $mNewState;
+
+       /** @var array */
+       private $mCurrentState;
+
+       public function __construct( $conn ) {
+               $this->mConn = $conn;
+               $this->update();
+               $this->mCurrentState = $this->mNewState;
+       }
+
+       public function update() {
+               $this->mNewState = array(
+                       pg_connection_status( $this->mConn ),
+                       pg_transaction_status( $this->mConn )
+               );
+       }
+
+       public function check() {
+               global $wgDebugDBTransactions;
+               $this->update();
+               if ( $wgDebugDBTransactions ) {
+                       if ( $this->mCurrentState !== $this->mNewState ) {
+                               $old = reset( $this->mCurrentState );
+                               $new = reset( $this->mNewState );
+                               foreach ( self::$WATCHED as $watched ) {
+                                       if ( $old !== $new ) {
+                                               $this->log_changed( $old, $new, 
$watched );
+                                       }
+                                       $old = next( $this->mCurrentState );
+                                       $new = next( $this->mNewState );
+                               }
+                       }
+               }
+               $this->mCurrentState = $this->mNewState;
+       }
+
+       protected function describe_changed( $status, $desc_table ) {
+               if ( isset( $desc_table[$status] ) ) {
+                       return $desc_table[$status];
+               } else {
+                       return "STATUS " . $status;
+               }
+       }
+
+       protected function log_changed( $old, $new, $watched ) {
+               wfDebug( sprintf( $watched["desc"],
+                       $this->mConn,
+                       $this->describe_changed( $old, $watched["states"] ),
+                       $this->describe_changed( $new, $watched["states"] )
+               ) );
+       }
+}
+
+/**
  * Manage savepoints within a transaction
  * @ingroup Database
  * @since 1.19
@@ -169,7 +252,11 @@
        }
 
        protected function query( $keyword, $msg_ok, $msg_failed ) {
+               global $wgDebugDBTransactions;
                if ( $this->dbw->doQuery( $keyword . " " . $this->id ) !== 
false ) {
+                       if ( $wgDebugDBTransactions ) {
+                               wfDebug( sprintf( $msg_ok, $this->id ) );
+                       }
                } else {
                        wfDebug( sprintf( $msg_failed, $this->id ) );
                }
@@ -219,6 +306,9 @@
 
        /** @var string Connect string to open a PostgreSQL connection */
        private $connectString;
+
+       /** @var PostgresTransactionState */
+       private $mTransactionState;
 
        /** @var string */
        private $mCoreSchema;
@@ -338,6 +428,7 @@
                }
 
                $this->mOpened = true;
+               $this->mTransactionState = new PostgresTransactionState( 
$this->mConn );
 
                global $wgCommandLineMode;
                # If called from the command-line (e.g. importDump), only show 
errors
@@ -399,10 +490,12 @@
                while ( $res = pg_get_result( $this->mConn ) ) {
                        pg_free_result( $res );
                }
+               $this->mTransactionState->check();
                if ( pg_send_query( $this->mConn, $sql ) === false ) {
                        throw new DBUnexpectedError( $this, "Unable to post new 
query to PostgreSQL\n" );
                }
                $this->mLastResult = pg_get_result( $this->mConn );
+               $this->mTransactionState->check();
                $this->mAffectedRows = null;
                if ( pg_result_error( $this->mLastResult ) ) {
                        return false;
diff --git a/includes/objectcache/SqlBagOStuff.php 
b/includes/objectcache/SqlBagOStuff.php
index 1f384c7..88c9f33 100644
--- a/includes/objectcache/SqlBagOStuff.php
+++ b/includes/objectcache/SqlBagOStuff.php
@@ -131,6 +131,8 @@
         * @throws MWException
         */
        protected function getDB( $serverIndex ) {
+               global $wgDebugDBTransactions;
+
                if ( !isset( $this->conns[$serverIndex] ) ) {
                        if ( $serverIndex >= $this->numServers ) {
                                throw new MWException( __METHOD__ . ": Invalid 
server index \"$serverIndex\"" );
@@ -145,6 +147,9 @@
 
                        # If server connection info was given, use that
                        if ( $this->serverInfos ) {
+                               if ( $wgDebugDBTransactions ) {
+                                       $this->logger->debug( "Using provided 
serverInfo for SqlBagOStuff" );
+                               }
                                $info = $this->serverInfos[$serverIndex];
                                $type = isset( $info['type'] ) ? $info['type'] 
: 'mysql';
                                $host = isset( $info['host'] ) ? $info['host'] 
: '[unknown]';
@@ -168,7 +173,9 @@
                                        $db = wfGetDB( $index );
                                }
                        }
-                       $this->logger->debug( sprintf( "Connection %s will be 
used for SqlBagOStuff", $db ) );
+                       if ( $wgDebugDBTransactions ) {
+                               $this->logger->debug( sprintf( "Connection %s 
will be used for SqlBagOStuff", $db ) );
+                       }
                        $this->conns[$serverIndex] = $db;
                }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I639f65767f102d71d6a5a9a6134f51e1aad326b3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: saper <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to