From: Marius Hoch Date: Tue, 12 Jun 2018 21:49:52 +0000 (+0200) Subject: Allow passing the default DB group to use in Maintenance scripts X-Git-Tag: 1.34.0-rc.0~5019^2 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=d74413bbe724ce6d49441b2e3028209cf4382637;p=lhc%2Fweb%2Fwiklou.git Allow passing the default DB group to use in Maintenance scripts Also allows setting it via a global, although that is probably less useful. Bug: T147169 Change-Id: Ic51204a6f6ce9db4cc96108e823e388512724eff --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 56fb534754..b032f2b3f4 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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) */ diff --git a/includes/db/MWLBFactory.php b/includes/db/MWLBFactory.php index 79f787dba4..e50f855edb 100644 --- a/includes/db/MWLBFactory.php +++ b/includes/db/MWLBFactory.php @@ -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 diff --git a/includes/libs/rdbms/lbfactory/LBFactory.php b/includes/libs/rdbms/lbfactory/LBFactory.php index 34436fb4a4..b226f0eeca 100644 --- a/includes/libs/rdbms/lbfactory/LBFactory.php +++ b/includes/libs/rdbms/lbfactory/LBFactory.php @@ -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) diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index 6b271a7708..e01b24e08a 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -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() ); diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 80fd7b9f70..39f8c2afee 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -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;