Allow passing the default DB group to use in Maintenance scripts
authorMarius Hoch <hoo@online.de>
Tue, 12 Jun 2018 21:49:52 +0000 (23:49 +0200)
committerMarius Hoch <hoo@online.de>
Tue, 12 Jun 2018 21:52:40 +0000 (23:52 +0200)
Also allows setting it via a global, although that is probably
less useful.

Bug: T147169
Change-Id: Ic51204a6f6ce9db4cc96108e823e388512724eff

includes/DefaultSettings.php
includes/db/MWLBFactory.php
includes/libs/rdbms/lbfactory/LBFactory.php
includes/libs/rdbms/loadbalancer/LoadBalancer.php
maintenance/Maintenance.php

index 56fb534..b032f2b 100644 (file)
@@ -1894,6 +1894,11 @@ $wgSQLMode = '';
  */
 $wgDBmwschema = null;
 
+/**
+ * Default group to use when getting database connections.
+ */
+$wgDBDefaultGroup = null;
+
 /**
  * To override default SQLite data directory ($docroot/../data)
  */
index 79f787d..e50f855 100644 (file)
@@ -65,6 +65,7 @@ abstract class MWLBFactory {
                        'cliMode' => $wgCommandLineMode,
                        'hostname' => wfHostname(),
                        'readOnlyReason' => $readOnlyMode->getReason(),
+                       'defaultGroup' => $mainConfig->get( 'DBDefaultGroup' ),
                ];
 
                // When making changes here, remember to also specify MediaWiki-specific options
index 34436fb..b226f0e 100644 (file)
@@ -91,6 +91,9 @@ abstract class LBFactory implements ILBFactory {
        /** @var string One of the ROUND_* class constants */
        private $trxRoundStage = self::ROUND_CURSORY;
 
+       /** @var string|null */
+       private $defaultGroup = null;
+
        const ROUND_CURSORY = 'cursory';
        const ROUND_BEGINNING = 'within-begin';
        const ROUND_COMMITTING = 'within-commit';
@@ -138,6 +141,7 @@ abstract class LBFactory implements ILBFactory {
                $this->cliMode = $conf['cliMode'] ?? ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' );
                $this->hostname = $conf['hostname'] ?? gethostname();
                $this->agent = $conf['agent'] ?? '';
+               $this->defaultGroup = $conf['defaultGroup'] ?? null;
 
                $this->ticket = mt_rand();
        }
@@ -575,6 +579,7 @@ abstract class LBFactory implements ILBFactory {
                        'hostname' => $this->hostname,
                        'cliMode' => $this->cliMode,
                        'agent' => $this->agent,
+                       'defaultGroup' => $this->defaultGroup,
                        'chronologyCallback' => function ( ILoadBalancer $lb ) {
                                // Defer ChronologyProtector construction in case setRequestInfo() ends up
                                // being called later (but before the first connection attempt) (T192611)
index 6b271a7..e01b24e 100644 (file)
@@ -123,6 +123,9 @@ class LoadBalancer implements ILoadBalancer {
        /** @var string Stage of the current transaction round in the transaction round life-cycle */
        private $trxRoundStage = self::ROUND_CURSORY;
 
+       /** @var string|null */
+       private $defaultGroup = null;
+
        /** @var int Warn when this many connection are held */
        const CONN_HELD_WARN_THRESHOLD = 10;
 
@@ -259,6 +262,8 @@ class LoadBalancer implements ILoadBalancer {
                                $this->trxRoundStage = self::ROUND_ROLLBACK_CALLBACKS;
                        }
                }
+
+               $this->defaultGroup = $params['defaultGroup'] ?? null;
        }
 
        /**
@@ -717,8 +722,11 @@ class LoadBalancer implements ILoadBalancer {
                        }
                }
 
+               // Check one "group" per default: the generic pool
+               $defaultGroups = $this->defaultGroup ? [ $this->defaultGroup ] : [ false ];
+
                $groups = ( $groups === false || $groups === [] )
-                       ? [ false ] // check one "group": the generic pool
+                       ? $defaultGroups
                        : (array)$groups;
 
                $masterOnly = ( $i == self::DB_MASTER || $i == $this->getWriterIndex() );
index 80fd7b9..39f8c2a 100644 (file)
@@ -538,6 +538,7 @@ abstract class Maintenance {
                if ( $this->getDbType() > 0 ) {
                        $this->addOption( 'dbuser', 'The DB user to use for this script', false, true );
                        $this->addOption( 'dbpass', 'The password to use for this script', false, true );
+                       $this->addOption( 'dbgroupdefault', 'The default DB group to use.', false, true );
                }
 
                # Save additional script dependant options to display
@@ -1118,7 +1119,7 @@ abstract class Maintenance {
         */
        public function finalSetup() {
                global $wgCommandLineMode, $wgShowSQLErrors, $wgServer;
-               global $wgDBadminuser, $wgDBadminpassword;
+               global $wgDBadminuser, $wgDBadminpassword, $wgDBDefaultGroup;
                global $wgDBuser, $wgDBpassword, $wgDBservers, $wgLBFactoryConf;
 
                # Turn off output buffering again, it might have been turned on in the settings files
@@ -1140,6 +1141,9 @@ abstract class Maintenance {
                if ( $this->mDbPass ) {
                        $wgDBadminpassword = $this->mDbPass;
                }
+               if ( $this->hasOption( 'dbgroupdefault' ) ) {
+                       $wgDBDefaultGroup = $this->getOption( 'dbgroupdefault', null );
+               }
 
                if ( $this->getDbType() == self::DB_ADMIN && isset( $wgDBadminuser ) ) {
                        $wgDBuser = $wgDBadminuser;