Aaron Schulz has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/345778 )

Change subject: Database: clean up lockTables() and add postgres support
......................................................................

Database: clean up lockTables() and add postgres support

Remove LOW_PRIORITY feature, which is ignored by mysql.

Change-Id: I499061bcc2763afb1ff4a43319064eed4ba3a8fe
---
M includes/libs/rdbms/database/Database.php
M includes/libs/rdbms/database/DatabaseMysqlBase.php
M includes/libs/rdbms/database/DatabasePostgres.php
M includes/libs/rdbms/database/IMaintainableDatabase.php
M includes/libs/rdbms/database/MaintainableDBConnRef.php
5 files changed, 111 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/78/345778/1

diff --git a/includes/libs/rdbms/database/Database.php 
b/includes/libs/rdbms/database/Database.php
index fbfd899..7422aac 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -3306,26 +3306,53 @@
                return false;
        }
 
+       public function supportsSessionTableLocks() {
+               return false;
+       }
+
        /**
-        * Lock specific tables
+        * Lock specific tables for the currently active transaction
+        *
+        * Only use this method within a transaction
         *
         * @param array $read Array of tables to lock for read access
         * @param array $write Array of tables to lock for write access
         * @param string $method Name of caller
-        * @param bool $lowPriority Whether to indicate writes to be LOW 
PRIORITY
         * @return bool
         */
-       public function lockTables( $read, $write, $method, $lowPriority = true 
) {
+       final public function lockTables( array $read, array $write, $method ) {
+               if ( !$this->supportsSessionTableLocks() && !$this->mTrxLevel ) 
{
+                       $this->queryLogger->warning( __METHOD__ .
+                               ": session scope table locks are not supported 
by this RDBMS."
+                       );
+               }
+
+               return $this->doLockTables( $read, $write, $method );
+       }
+
+       protected function doLockTables( array $read, array $write, $method ) {
                return true;
        }
 
        /**
-        * Unlock specific tables
+        * Unlock specific tables for the currently active transaction
+        *
+        * Only use this method within a transaction
         *
         * @param string $method The caller
         * @return bool
         */
-       public function unlockTables( $method ) {
+       final public function unlockTables( $method ) {
+               if ( !$this->supportsSessionTableLocks() && !$this->mTrxLevel ) 
{
+                       $this->queryLogger->warning( __METHOD__ .
+                               ": session scope table locks are not supported 
by this RDBMS."
+                       );
+               }
+
+               return $this->doUnlockTables( $method );
+       }
+
+       protected function doUnlockTables( $method ) {
                return true;
        }
 
diff --git a/includes/libs/rdbms/database/DatabaseMysqlBase.php 
b/includes/libs/rdbms/database/DatabaseMysqlBase.php
index e900027..89271ba 100644
--- a/includes/libs/rdbms/database/DatabaseMysqlBase.php
+++ b/includes/libs/rdbms/database/DatabaseMysqlBase.php
@@ -1054,36 +1054,26 @@
                return true;
        }
 
-       /**
-        * @param array $read
-        * @param array $write
-        * @param string $method
-        * @param bool $lowPriority
-        * @return bool
-        */
-       public function lockTables( $read, $write, $method, $lowPriority = true 
) {
-               $items = [];
+       public function supportsSessionTableLocks() {
+               return true;
+       }
 
+       protected function doLockTables( array $read, array $write, $method ) {
+               $items = [];
                foreach ( $write as $table ) {
-                       $tbl = $this->tableName( $table ) .
-                               ( $lowPriority ? ' LOW_PRIORITY' : '' ) .
-                               ' WRITE';
-                       $items[] = $tbl;
+                       $items[] = $this->tableName( $table ) . ' WRITE';
                }
                foreach ( $read as $table ) {
                        $items[] = $this->tableName( $table ) . ' READ';
                }
+
                $sql = "LOCK TABLES " . implode( ',', $items );
                $this->query( $sql, $method );
 
                return true;
        }
 
-       /**
-        * @param string $method
-        * @return bool
-        */
-       public function unlockTables( $method ) {
+       protected function doUnlockTables( $method ) {
                $this->query( "UNLOCK TABLES", $method );
 
                return true;
diff --git a/includes/libs/rdbms/database/DatabasePostgres.php 
b/includes/libs/rdbms/database/DatabasePostgres.php
index af9716d..5bcd4a8 100644
--- a/includes/libs/rdbms/database/DatabasePostgres.php
+++ b/includes/libs/rdbms/database/DatabasePostgres.php
@@ -1305,6 +1305,33 @@
                return parent::streamStatementEnd( $sql, $newLine );
        }
 
+       public function doLockTables( array $read, array $write, $method ) {
+               $tablesWrite = [];
+               foreach ( $write as $table ) {
+                       $tablesWrite[] = $this->tableName( $table );
+               }
+               $tablesRead = [];
+               foreach ( $read as $table ) {
+                       $tablesRead[] = $this->tableName( $table );
+               }
+
+               // Acquire locks for the duration of the current transaction...
+               if ( $tablesWrite ) {
+                       $this->query(
+                               'LOCK TABLE ONLY ' . implode( ',', $tablesWrite 
) . ' IN EXCLUSIVE MODE',
+                               $method
+                       );
+               }
+               if ( $tablesRead ) {
+                       $this->query(
+                               'LOCK TABLE ONLY ' . implode( ',', $tablesRead 
) . ' IN SHARE MODE',
+                               $method
+                       );
+               }
+
+               return true;
+       }
+
        public function lockIsFree( $lockName, $method ) {
                // 
http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
                $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) 
);
diff --git a/includes/libs/rdbms/database/IMaintainableDatabase.php 
b/includes/libs/rdbms/database/IMaintainableDatabase.php
index 138cf2d..ab88cb9 100644
--- a/includes/libs/rdbms/database/IMaintainableDatabase.php
+++ b/includes/libs/rdbms/database/IMaintainableDatabase.php
@@ -210,6 +210,38 @@
        public function duplicateTableStructure(
                $oldName, $newName, $temporary = false, $fname = __METHOD__
        );
+
+       /**
+        * Checks if lockTables() supports tables locks outside the scope of 
transactions
+        *
+        * @return bool
+        * @since 1.29
+        */
+       public function supportsSessionTableLocks();
+
+       /**
+        * Lock specific tables for the currently active transaction
+        *
+        * Only use this method within a transaction
+        *
+        * @param array $read Array of tables to lock for read access
+        * @param array $write Array of tables to lock for write access
+        * @param string $method Name of caller
+        * @return bool
+        * @since 1.29
+        */
+       public function lockTables( array $read, array $write, $method );
+
+       /**
+        * Unlock specific tables for the currently active transaction
+        *
+        * Only use this method within a transaction
+        *
+        * @param string $method The caller
+        * @return bool
+        * @since 1.29
+        */
+       public function unlockTables( $method );
 }
 
 class_alias( 'Wikimedia\Rdbms\IMaintainableDatabase', 'IMaintainableDatabase' 
);
diff --git a/includes/libs/rdbms/database/MaintainableDBConnRef.php 
b/includes/libs/rdbms/database/MaintainableDBConnRef.php
index 30c62de..bd6ca8c 100644
--- a/includes/libs/rdbms/database/MaintainableDBConnRef.php
+++ b/includes/libs/rdbms/database/MaintainableDBConnRef.php
@@ -68,6 +68,18 @@
        ) {
                return $this->__call( __FUNCTION__, func_get_args() );
        }
+
+       public function supportsSessionTableLocks() {
+               return $this->__call( __FUNCTION__, func_get_args() );
+       }
+
+       public function lockTables( array $read, array $write, $method ) {
+               return $this->__call( __FUNCTION__, func_get_args() );
+       }
+
+       public function unlockTables( $method ) {
+               return $this->__call( __FUNCTION__, func_get_args() );
+       }
 }
 
 class_alias( 'Wikimedia\Rdbms\MaintainableDBConnRef', 'MaintainableDBConnRef' 
);

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

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

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

Reply via email to