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\Logger\LoggerFactory
;
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\LBFactory
;
27 use Wikimedia\Rdbms\DatabaseDomain
;
30 * MediaWiki-specific class for generating database load balancers
33 abstract class MWLBFactory
{
35 * @param array $lbConf Config for LBFactory::__construct()
36 * @param Config $mainConfig Main config object from MediaWikiServices
37 * @param ConfiguredReadOnlyMode $readOnlyMode
40 public static function applyDefaultConfig( array $lbConf, Config
$mainConfig,
41 ConfiguredReadOnlyMode
$readOnlyMode
43 global $wgCommandLineMode;
45 static $typesWithSchema = [ 'postgres', 'msssql' ];
48 'localDomain' => new DatabaseDomain(
49 $mainConfig->get( 'DBname' ),
50 $mainConfig->get( 'DBmwschema' ),
51 $mainConfig->get( 'DBprefix' )
53 'profiler' => Profiler
::instance(),
54 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
55 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
56 'queryLogger' => LoggerFactory
::getInstance( 'DBQuery' ),
57 'connLogger' => LoggerFactory
::getInstance( 'DBConnection' ),
58 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
59 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ],
60 'cliMode' => $wgCommandLineMode,
61 'hostname' => wfHostname(),
62 'readOnlyReason' => $readOnlyMode->getReason(),
65 // When making changes here, remember to also specify MediaWiki-specific options
66 // for Database classes in the relevant Installer subclass.
67 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
68 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple
::class ) {
69 if ( isset( $lbConf['servers'] ) ) {
70 // Server array is already explicitly configured; leave alone
71 } elseif ( is_array( $mainConfig->get( 'DBservers' ) ) ) {
72 foreach ( $mainConfig->get( 'DBservers' ) as $i => $server ) {
73 if ( $server['type'] === 'sqlite' ) {
74 $server +
= [ 'dbDirectory' => $mainConfig->get( 'SQLiteDataDir' ) ];
75 } elseif ( $server['type'] === 'postgres' ) {
77 'port' => $mainConfig->get( 'DBport' ),
78 // Work around the reserved word usage in MediaWiki schema
79 'keywordTableMap' => [ 'user' => 'mwuser', 'text' => 'pagecontent' ]
81 } elseif ( $server['type'] === 'mssql' ) {
83 'port' => $mainConfig->get( 'DBport' ),
84 'useWindowsAuth' => $mainConfig->get( 'DBWindowsAuthentication' )
88 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
89 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
93 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
94 'flags' => DBO_DEFAULT
,
95 'sqlMode' => $mainConfig->get( 'SQLMode' ),
96 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
99 $lbConf['servers'][$i] = $server;
102 $flags = DBO_DEFAULT
;
103 $flags |
= $mainConfig->get( 'DebugDumpSql' ) ? DBO_DEBUG
: 0;
104 $flags |
= $mainConfig->get( 'DBssl' ) ? DBO_SSL
: 0;
105 $flags |
= $mainConfig->get( 'DBcompress' ) ? DBO_COMPRESS
: 0;
107 'host' => $mainConfig->get( 'DBserver' ),
108 'user' => $mainConfig->get( 'DBuser' ),
109 'password' => $mainConfig->get( 'DBpassword' ),
110 'dbname' => $mainConfig->get( 'DBname' ),
111 'tablePrefix' => $mainConfig->get( 'DBprefix' ),
112 'type' => $mainConfig->get( 'DBtype' ),
115 'sqlMode' => $mainConfig->get( 'SQLMode' ),
116 'utf8Mode' => $mainConfig->get( 'DBmysql5' )
118 if ( in_array( $server['type'], $typesWithSchema, true ) ) {
119 $server +
= [ 'schema' => $mainConfig->get( 'DBmwschema' ) ];
121 if ( $server['type'] === 'sqlite' ) {
122 $server[ 'dbDirectory'] = $mainConfig->get( 'SQLiteDataDir' );
123 } elseif ( $server['type'] === 'postgres' ) {
124 $server['port'] = $mainConfig->get( 'DBport' );
125 // Work around the reserved word usage in MediaWiki schema
126 $server['keywordTableMap'] = [ 'user' => 'mwuser', 'text' => 'pagecontent' ];
127 } elseif ( $server['type'] === 'mssql' ) {
128 $server['port'] = $mainConfig->get( 'DBport' );
129 $server['useWindowsAuth'] = $mainConfig->get( 'DBWindowsAuthentication' );
131 $lbConf['servers'] = [ $server ];
133 if ( !isset( $lbConf['externalClusters'] ) ) {
134 $lbConf['externalClusters'] = $mainConfig->get( 'ExternalServers' );
136 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti
::class ) {
137 if ( isset( $lbConf['serverTemplate'] ) ) {
138 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
139 $lbConf['serverTemplate']['schema'] = $mainConfig->get( 'DBmwschema' );
141 $lbConf['serverTemplate']['sqlMode'] = $mainConfig->get( 'SQLMode' );
142 $lbConf['serverTemplate']['utf8Mode'] = $mainConfig->get( 'DBmysql5' );
146 $services = MediaWikiServices
::getInstance();
148 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
149 $sCache = $services->getLocalServerObjectCache();
150 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
151 $lbConf['srvCache'] = $sCache;
153 $mStash = $services->getMainObjectStash();
154 if ( $mStash->getQoS( $mStash::ATTR_EMULATION
) > $mStash::QOS_EMULATION_SQL
) {
155 $lbConf['memStash'] = $mStash;
157 $wCache = $services->getMainWANObjectCache();
158 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
159 $lbConf['wanCache'] = $wCache;
166 * Returns the LBFactory class to use and the load balancer configuration.
168 * @todo instead of this, use a ServiceContainer for managing the different implementations.
170 * @param array $config (e.g. $wgLBFactoryConf)
171 * @return string Class name
173 public static function getLBFactoryClass( array $config ) {
174 // For configuration backward compatibility after removing
175 // underscores from class names in MediaWiki 1.23.
177 'LBFactory_Simple' => 'LBFactorySimple',
178 'LBFactory_Single' => 'LBFactorySingle',
179 'LBFactory_Multi' => 'LBFactoryMulti'
182 $class = $config['class'];
184 if ( isset( $bcClasses[$class] ) ) {
185 $class = $bcClasses[$class];
187 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
192 // For configuration backward compatibility after moving classes to namespaces (1.29)
194 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle
::class,
195 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple
::class,
196 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti
::class
199 if ( isset( $compat[$class] ) ) {
200 $class = $compat[$class];
206 public static function setSchemaAliases( LBFactory
$lbFactory ) {
207 $mainLB = $lbFactory->getMainLB();
208 $masterType = $mainLB->getServerType( $mainLB->getWriterIndex() );
209 if ( $masterType === 'mysql' ) {
211 * When SQLite indexes were introduced in r45764, it was noted that
212 * SQLite requires index names to be unique within the whole database,
213 * not just within a schema. As discussed in CR r45819, to avoid the
214 * need for a schema change on existing installations, the indexes
215 * were implicitly mapped from the new names to the old names.
217 * This mapping can be removed if DB patches are introduced to alter
218 * the relevant tables in existing installations. Note that because
219 * this index mapping applies to table creation, even new installations
220 * of MySQL have the old names (except for installations created during
221 * a period where this mapping was inappropriately removed, see
224 $lbFactory->setIndexAliases( [
225 'ar_usertext_timestamp' => 'usertext_timestamp',
226 'un_user_id' => 'user_id',
227 'un_user_ip' => 'user_ip',