Convert maintenance scripts to LoadBalancer::getMaintenanceConnectionRef()
authorAaron Schulz <aschulz@wikimedia.org>
Sun, 21 Jul 2019 08:08:11 +0000 (01:08 -0700)
committerKrinkle <krinklemail@gmail.com>
Fri, 2 Aug 2019 22:29:43 +0000 (22:29 +0000)
Change-Id: I8944a052f51a1941ad3b76a40fc9c46d1363c426

maintenance/Maintenance.php
maintenance/includes/BackupDumper.php
maintenance/includes/TextPassDumper.php
maintenance/shell.php
maintenance/sql.php
maintenance/storage/orphanStats.php
maintenance/storage/recompressTracked.php
maintenance/storage/trackBlobs.php

index 0263425..8d3b2af 100644 (file)
@@ -1366,28 +1366,34 @@ abstract class Maintenance {
        }
 
        /**
-        * Returns a database to be used by current maintenance script. It can be set by setDB().
-        * If not set, wfGetDB() will be used.
-        * This function has the same parameters as wfGetDB()
+        * Returns a database to be used by current maintenance script.
+        *
+        * This uses the main LBFactory instance by default unless overriden via setDB().
+        *
+        * This function has the same parameters as LoadBalancer::getConnection().
         *
         * @param int $db DB index (DB_REPLICA/DB_MASTER)
         * @param string|string[] $groups default: empty array
-        * @param string|bool $wiki default: current wiki
+        * @param string|bool $dbDomain default: current wiki
         * @return IMaintainableDatabase
         */
-       protected function getDB( $db, $groups = [], $wiki = false ) {
+       protected function getDB( $db, $groups = [], $dbDomain = false ) {
                if ( $this->mDb === null ) {
-                       return wfGetDB( $db, $groups, $wiki );
+                       return MediaWikiServices::getInstance()
+                               ->getDBLoadBalancerFactory()
+                               ->getMainLB( $dbDomain )
+                               ->getMaintenanceConnectionRef( $db, $groups, $dbDomain );
                }
+
                return $this->mDb;
        }
 
        /**
         * Sets database object to be returned by getDB().
         *
-        * @param IDatabase $db
+        * @param IMaintainableDatabase $db
         */
-       public function setDB( IDatabase $db ) {
+       public function setDB( IMaintainableDatabase $db ) {
                $this->mDb = $db;
        }
 
index df3b4a1..08eade9 100644 (file)
@@ -30,7 +30,7 @@ require_once __DIR__ . '/../../includes/export/WikiExporter.php';
 
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\LoadBalancer;
-use Wikimedia\Rdbms\IDatabase;
+use Wikimedia\Rdbms\IMaintainableDatabase;
 
 /**
  * @ingroup Dump
@@ -68,7 +68,7 @@ abstract class BackupDumper extends Maintenance {
        /**
         * The dependency-injected database to use.
         *
-        * @var IDatabase|null
+        * @var IMaintainableDatabase|null
         *
         * @see self::setDB
         */
@@ -328,7 +328,7 @@ abstract class BackupDumper extends Maintenance {
         * @todo Fixme: the --server parameter is currently not respected, as it
         * doesn't seem terribly easy to ask the load balancer for a particular
         * connection by name.
-        * @return IDatabase
+        * @return IMaintainableDatabase
         */
        function backupDb() {
                if ( $this->forcedDb !== null ) {
@@ -337,7 +337,7 @@ abstract class BackupDumper extends Maintenance {
 
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $this->lb = $lbFactory->newMainLB();
-               $db = $this->lb->getConnection( DB_REPLICA, 'dump' );
+               $db = $this->lb->getMaintenanceConnectionRef( DB_REPLICA, 'dump' );
 
                // Discourage the server from disconnecting us if it takes a long time
                // to read out the big ol' batch query.
@@ -350,10 +350,9 @@ abstract class BackupDumper extends Maintenance {
         * Force the dump to use the provided database connection for database
         * operations, wherever possible.
         *
-        * @param IDatabase|null $db (Optional) the database connection to use. If null, resort to
-        *   use the globally provided ways to get database connections.
+        * @param IMaintainableDatabase $db The database connection to use
         */
-       function setDB( IDatabase $db = null ) {
+       function setDB( IMaintainableDatabase $db ) {
                parent::setDB( $db );
                $this->forcedDb = $db;
        }
index f9b3951..21b92c5 100644 (file)
@@ -240,7 +240,7 @@ TEXT
                }
 
                try {
-                       $this->db = $this->lb->getConnection( DB_REPLICA, 'dump' );
+                       $this->db = $this->lb->getMaintenanceConnectionRef( DB_REPLICA, 'dump' );
                } catch ( Exception $e ) {
                        throw new MWException( __METHOD__
                                . " rotating DB failed to obtain new database (" . $e->getMessage() . ")" );
index a67417f..f5ebc02 100644 (file)
@@ -93,8 +93,8 @@ class MediaWikiShell extends Maintenance {
                }
                if ( $d > 1 ) {
                        # Set DBO_DEBUG (equivalent of $wgDebugDumpSql)
-                       wfGetDB( DB_MASTER )->setFlag( DBO_DEBUG );
-                       wfGetDB( DB_REPLICA )->setFlag( DBO_DEBUG );
+                       $this->getDB( DB_MASTER )->setFlag( DBO_DEBUG );
+                       $this->getDB( DB_REPLICA )->setFlag( DBO_DEBUG );
                }
        }
 
index 3b0607f..21d8b2d 100644 (file)
@@ -83,8 +83,7 @@ class MwSql extends Maintenance {
                        $index = DB_MASTER;
                }
 
-               /** @var IDatabase $db DB handle for the appropriate cluster/wiki */
-               $db = $lb->getConnection( $index, [], $wiki );
+               $db = $lb->getMaintenanceConnectionRef( $index, [], $wiki );
                if ( $replicaDB != '' && $db->getLBInfo( 'master' ) !== null ) {
                        $this->fatalError( "The server selected ({$db->getServer()}) is not a replica DB." );
                }
index 3866be7..60f88ba 100644 (file)
@@ -42,7 +42,7 @@ class OrphanStats extends Maintenance {
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $lb = $lbFactory->getExternalLB( $cluster );
 
-               return $lb->getConnection( DB_REPLICA );
+               return $lb->getMaintenanceConnectionRef( DB_REPLICA );
        }
 
        public function execute() {
index 6fd53cc..92b6679 100644 (file)
  * @ingroup Maintenance ExternalStorage
  */
 
+use Wikimedia\Rdbms\IMaintainableDatabase;
 use MediaWiki\Logger\LegacyLogger;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Shell\Shell;
-use Wikimedia\Rdbms\IDatabase;
 
 $optionsWithArgs = RecompressTracked::getOptionsWithArgs();
 require __DIR__ . '/../commandLine.inc';
@@ -275,6 +275,7 @@ class RecompressTracked {
        /**
         * Dispatch a command to the next available replica DB.
         * This may block until a replica DB finishes its work and becomes available.
+        * @param array ...$args
         */
        function dispatch( ...$args ) {
                $pipes = $this->replicaPipes;
@@ -647,13 +648,13 @@ class RecompressTracked {
        /**
         * Gets a DB master connection for the given external cluster name
         * @param string $cluster
-        * @return IDatabase
+        * @return IMaintainableDatabase
         */
        function getExtDB( $cluster ) {
                $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                $lb = $lbFactory->getExternalLB( $cluster );
 
-               return $lb->getConnection( DB_MASTER );
+               return $lb->getMaintenanceConnectionRef( DB_MASTER );
        }
 
        /**
index 385ae6a..d9793b4 100644 (file)
@@ -232,7 +232,7 @@ class TrackBlobs {
                $pos = $dbw->getMasterPos();
                $dbr->masterPosWait( $pos, 100000 );
 
-               $textClause = $this->getTextClause( $this->clusters );
+               $textClause = $this->getTextClause();
                $startId = 0;
                $endId = $dbr->selectField( 'text', 'MAX(old_id)', '', __METHOD__ );
                $rowsInserted = 0;
@@ -325,9 +325,9 @@ class TrackBlobs {
                        $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                        $lb = $lbFactory->getExternalLB( $cluster );
                        try {
-                               $extDB = $lb->getConnection( DB_REPLICA );
+                               $extDB = $lb->getMaintenanceConnectionRef( DB_REPLICA );
                        } catch ( DBConnectionError $e ) {
-                               if ( strpos( $e->error, 'Unknown database' ) !== false ) {
+                               if ( strpos( $e->getMessage(), 'Unknown database' ) !== false ) {
                                        echo "No database on $cluster\n";
                                } else {
                                        echo "Error on $cluster: " . $e->getMessage() . "\n";
@@ -362,8 +362,8 @@ class TrackBlobs {
 
                                foreach ( $res as $row ) {
                                        gmp_setbit( $actualBlobs, $row->blob_id );
+                                       $startId = $row->blob_id;
                                }
-                               $startId = $row->blob_id;
 
                                ++$batchesDone;
                                if ( $batchesDone >= $this->reportingInterval ) {