3 * Generator of database load balancing objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Config\ServiceOptions
;
25 use MediaWiki\Logger\LoggerFactory
;
26 use Wikimedia\Rdbms\LBFactory
;
27 use Wikimedia\Rdbms\DatabaseDomain
;
30 * MediaWiki-specific class for generating database load balancers
33 abstract class MWLBFactory
{
35 /** @var array Cache of already-logged deprecation messages */
36 private static $loggedDeprecations = [];
39 * TODO Make this a const when HHVM support is dropped (T192166)
44 public static $applyDefaultConfigOptions = [
57 'DBWindowsAuthentication',
65 * @param array $lbConf Config for LBFactory::__construct()
66 * @param ServiceOptions $options
67 * @param ConfiguredReadOnlyMode $readOnlyMode
68 * @param BagOStuff $srvCace
69 * @param BagOStuff $mainStash
70 * @param WANObjectCache $wanCache
73 public static function applyDefaultConfig(
75 ServiceOptions
$options,
76 ConfiguredReadOnlyMode
$readOnlyMode,
79 WANObjectCache
$wanCache
81 $options->assertRequiredOptions( self
::$applyDefaultConfigOptions );
83 global $wgCommandLineMode;
85 $typesWithSchema = self
::getDbTypesWithSchemas();
88 'localDomain' => new DatabaseDomain(
89 $options->get( 'DBname' ),
90 $options->get( 'DBmwschema' ),
91 $options->get( 'DBprefix' )
93 'profiler' => function ( $section ) {
94 return Profiler
::instance()->scopedProfileIn( $section );
96 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
97 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
98 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
99 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
100 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
101 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
102 'deprecationLogger' => [ static::class, 'logDeprecation' ],
103 'cliMode' => $wgCommandLineMode,
104 'hostname' => wfHostname(),
105 'readOnlyReason' => $readOnlyMode->getReason(),
106 'defaultGroup' => $options->get( 'DBDefaultGroup' ),
110 // When making changes here, remember to also specify MediaWiki-specific options
111 // for Database classes in the relevant Installer subclass.
112 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
113 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple
::class ) {
114 if ( isset( $lbConf['servers'] ) ) {
115 // Server array is already explicitly configured
116 } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
117 $lbConf['servers'] = [];
118 foreach ( $options->get( 'DBservers' ) as $i => $server ) {
119 $lbConf['servers'][$i] = self
::initServerInfo( $server, $options );
122 $server = self
::initServerInfo(
124 'host' => $options->get( 'DBserver' ),
125 'user' => $options->get( 'DBuser' ),
126 'password' => $options->get( 'DBpassword' ),
127 'dbname' => $options->get( 'DBname' ),
128 'type' => $options->get( 'DBtype' ),
134 $server['flags'] |
= $options->get( 'DBssl' ) ? DBO_SSL
: 0;
135 $server['flags'] |
= $options->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
137 $lbConf['servers'] = [ $server ];
139 if ( !isset( $lbConf['externalClusters'] ) ) {
140 $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
143 $serversCheck = $lbConf['servers'];
144 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti
::class ) {
145 if ( isset( $lbConf['serverTemplate'] ) ) {
146 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
147 $lbConf['serverTemplate']['schema'] = $options->get( 'DBmwschema' );
149 $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
151 $serversCheck = [ $lbConf['serverTemplate'] ] ??
[];
154 self
::assertValidServerConfigs( $serversCheck, $options->get( 'DBname' ),
155 $options->get( 'DBprefix' ) );
157 $lbConf = self
::injectObjectCaches( $lbConf, $srvCace, $mainStash, $wanCache );
165 private static function getDbTypesWithSchemas() {
166 return [ 'postgres', 'msssql' ];
170 * @param array $server
171 * @param ServiceOptions $options
174 private static function initServerInfo( array $server, ServiceOptions
$options ) {
175 if ( $server['type'] === 'sqlite' ) {
176 $httpMethod = $_SERVER['REQUEST_METHOD'] ??
null;
177 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
178 // See https://www.sqlite.org/lang_transaction.html
179 // See https://www.sqlite.org/lockingv3.html#shared_lock
180 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
182 'dbDirectory' => $options->get( 'SQLiteDataDir' ),
183 'trxMode' => $isHttpRead ?
'DEFERRED' : 'IMMEDIATE'
185 } elseif ( $server['type'] === 'postgres' ) {
187 'port' => $options->get( 'DBport' ),
188 // Work around the reserved word usage in MediaWiki schema
189 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
191 } elseif ( $server['type'] === 'oracle' ) {
193 // Work around the reserved word usage in MediaWiki schema
194 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
196 } elseif ( $server['type'] === 'mssql' ) {
198 'port' => $options->get( 'DBport' ),
199 'useWindowsAuth' => $options->get( 'DBWindowsAuthentication' )
203 if ( in_array( $server['type'], self
::getDbTypesWithSchemas(), true ) ) {
204 $server +
= [ 'schema' => $options->get( 'DBmwschema' ) ];
207 $flags = DBO_DEFAULT
;
208 $flags |
= $options->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
209 if ( $server['type'] === 'oracle' ) {
210 $flags |
= $options->get( 'DBOracleDRCP' ) ? DBO_PERSISTENT
: 0;
214 'tablePrefix' => $options->get( 'DBprefix' ),
216 'sqlMode' => $options->get( 'SQLMode' ),
223 * @param array $lbConf
224 * @param BagOStuff $sCache
225 * @param BagOStuff $mStash
226 * @param WANObjectCache $wCache
229 private static function injectObjectCaches(
230 array $lbConf, BagOStuff
$sCache, BagOStuff
$mStash, WANObjectCache
$wCache
232 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
233 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
234 $lbConf['srvCache'] = $sCache;
236 if ( $mStash->getQoS( $mStash::ATTR_EMULATION
) > $mStash::QOS_EMULATION_SQL
) {
237 $lbConf['memStash'] = $mStash;
239 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
240 $lbConf['wanCache'] = $wCache;
247 * @param array $servers
248 * @param string $lbDB Local domain database name
249 * @param string $lbTP Local domain prefix
251 private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
252 foreach ( $servers as $server ) {
253 $type = $server['type'] ??
null;
254 $srvDB = $server['dbname'] ??
null; // server DB
255 $srvTP = $server['tablePrefix'] ??
''; // server table prefix
257 if ( $type === 'mysql' ) {
258 // A DB name is not needed to connect to mysql; 'dbname' is useless.
259 // This field only defines the DB to use for unspecified DB domains.
260 if ( $srvDB !== null && $srvDB !== $ldDB ) {
261 self
::reportMismatchedDBs( $srvDB, $ldDB );
263 } elseif ( $type === 'postgres' ) {
264 if ( $srvTP !== '' ) {
265 self
::reportIfPrefixSet( $srvTP, $type );
269 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
270 self
::reportMismatchedPrefixes( $srvTP, $ldTP );
276 * @param string $prefix Table prefix
277 * @param string $dbType Database type
279 private static function reportIfPrefixSet( $prefix, $dbType ) {
280 $e = new UnexpectedValueException(
281 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
282 "MediaWiki does not support using a table prefix with this RDBMS type."
284 MWExceptionRenderer
::output( $e, MWExceptionRenderer
::AS_PRETTY
);
289 * @param string $srvDB Server config database
290 * @param string $ldDB Local DB domain database
292 private static function reportMismatchedDBs( $srvDB, $ldDB ) {
293 $e = new UnexpectedValueException(
294 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
295 "Set \$wgDBname to the database used by this wiki project. " .
296 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
297 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
298 "use of Database::getDomainId(), and other features are not reliable when " .
299 "\$wgDBservers does not match the local wiki database/prefix."
301 MWExceptionRenderer
::output( $e, MWExceptionRenderer
::AS_PRETTY
);
306 * @param string $srvTP Server config table prefix
307 * @param string $ldTP Local DB domain database
309 private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
310 $e = new UnexpectedValueException(
311 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
312 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
313 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
314 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
315 "use of Database::getDomainId(), and other features are not reliable when " .
316 "\$wgDBservers does not match the local wiki database/prefix."
318 MWExceptionRenderer
::output( $e, MWExceptionRenderer
::AS_PRETTY
);
323 * Returns the LBFactory class to use and the load balancer configuration.
325 * @todo instead of this, use a ServiceContainer for managing the different implementations.
327 * @param array $config (e.g. $wgLBFactoryConf)
328 * @return string Class name
330 public static function getLBFactoryClass( array $config ) {
331 // For configuration backward compatibility after removing
332 // underscores from class names in MediaWiki 1.23.
334 'LBFactory_Simple' => 'LBFactorySimple',
335 'LBFactory_Single' => 'LBFactorySingle',
336 'LBFactory_Multi' => 'LBFactoryMulti'
339 $class = $config['class'];
341 if ( isset( $bcClasses[$class] ) ) {
342 $class = $bcClasses[$class];
344 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
349 // For configuration backward compatibility after moving classes to namespaces (1.29)
351 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle
::class,
352 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple
::class,
353 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti
::class
356 if ( isset( $compat[$class] ) ) {
357 $class = $compat[$class];
364 * @param LBFactory $lbFactory
365 * @param string $dbType 'mysql', 'sqlite', etc.
367 public static function setSchemaAliases( LBFactory
$lbFactory, $dbType ) {
368 if ( $dbType instanceof Config
) {
369 // Before 1.34 this took a whole Config just to get $dbType
370 wfDeprecated( __METHOD__
. ' with Config argument', '1.34' );
371 $dbType = $dbType->get( 'DBtype' );
373 if ( $dbType === 'mysql' ) {
375 * When SQLite indexes were introduced in r45764, it was noted that
376 * SQLite requires index names to be unique within the whole database,
377 * not just within a schema. As discussed in CR r45819, to avoid the
378 * need for a schema change on existing installations, the indexes
379 * were implicitly mapped from the new names to the old names.
381 * This mapping can be removed if DB patches are introduced to alter
382 * the relevant tables in existing installations. Note that because
383 * this index mapping applies to table creation, even new installations
384 * of MySQL have the old names (except for installations created during
385 * a period where this mapping was inappropriately removed, see
388 $lbFactory->setIndexAliases( [
389 'ar_usertext_timestamp' => 'usertext_timestamp',
390 'un_user_id' => 'user_id',
391 'un_user_ip' => 'user_ip',
397 * Log a database deprecation warning
398 * @param string $msg Deprecation message
400 public static function logDeprecation( $msg ) {
401 global $wgDevelopmentWarnings;
403 if ( isset( self
::$loggedDeprecations[$msg] ) ) {
406 self
::$loggedDeprecations[$msg] = true;
408 if ( $wgDevelopmentWarnings ) {
409 trigger_error( $msg, E_USER_DEPRECATED
);
411 wfDebugLog( 'deprecated', $msg, 'private' );