MediaWiki master
MWLBFactory.php
Go to the documentation of this file.
1<?php
24use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
36use Wikimedia\RequestTimeout\CriticalSectionProvider;
37
45
47 private static $loggedDeprecations = [];
48
49 public const CORE_VIRTUAL_DOMAINS = [ 'virtual-botpasswords' ];
50
55 MainConfigNames::DBcompress,
56 MainConfigNames::DBDefaultGroup,
57 MainConfigNames::DBmwschema,
58 MainConfigNames::DBname,
59 MainConfigNames::DBpassword,
60 MainConfigNames::DBport,
61 MainConfigNames::DBprefix,
62 MainConfigNames::DBserver,
63 MainConfigNames::DBservers,
64 MainConfigNames::DBssl,
65 MainConfigNames::DBStrictWarnings,
66 MainConfigNames::DBtype,
67 MainConfigNames::DBuser,
68 MainConfigNames::DebugDumpSql,
69 MainConfigNames::DebugLogFile,
70 MainConfigNames::DebugToolbar,
71 MainConfigNames::ExternalServers,
72 MainConfigNames::SQLiteDataDir,
73 MainConfigNames::SQLMode,
74 MainConfigNames::VirtualDomainsMapping,
75 ];
79 private $options;
83 private $readOnlyMode;
87 private $chronologyProtector;
91 private $srvCache;
95 private $wanCache;
99 private $csProvider;
103 private $statsdDataFactory;
105 private array $virtualDomains;
106
117 public function __construct(
118 ServiceOptions $options,
119 ConfiguredReadOnlyMode $readOnlyMode,
120 ChronologyProtector $chronologyProtector,
121 BagOStuff $srvCache,
122 WANObjectCache $wanCache,
123 CriticalSectionProvider $csProvider,
124 StatsdDataFactoryInterface $statsdDataFactory,
125 array $virtualDomains
126 ) {
127 $this->options = $options;
128 $this->readOnlyMode = $readOnlyMode;
129 $this->chronologyProtector = $chronologyProtector;
130 $this->srvCache = $srvCache;
131 $this->wanCache = $wanCache;
132 $this->csProvider = $csProvider;
133 $this->statsdDataFactory = $statsdDataFactory;
134 $this->virtualDomains = $virtualDomains;
135 }
136
142 public function applyDefaultConfig( array $lbConf ) {
143 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
144
145 $typesWithSchema = self::getDbTypesWithSchemas();
146 if ( Profiler::instance() instanceof ProfilerStub ) {
147 $profilerCallback = null;
148 } else {
149 $profilerCallback = static function ( $section ) {
150 return Profiler::instance()->scopedProfileIn( $section );
151 };
152 }
153
154 $lbConf += [
155 'localDomain' => new DatabaseDomain(
156 $this->options->get( MainConfigNames::DBname ),
157 $this->options->get( MainConfigNames::DBmwschema ),
158 $this->options->get( MainConfigNames::DBprefix )
159 ),
160 'profiler' => $profilerCallback,
161 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
162 'logger' => LoggerFactory::getInstance( 'rdbms' ),
163 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
164 'deprecationLogger' => [ static::class, 'logDeprecation' ],
165 'statsdDataFactory' => $this->statsdDataFactory,
166 'cliMode' => MW_ENTRY_POINT === 'cli',
167 'readOnlyReason' => $this->readOnlyMode->getReason(),
168 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
169 'criticalSectionProvider' => $this->csProvider
170 ];
171
172 $serversCheck = [];
173 // When making changes here, remember to also specify MediaWiki-specific options
174 // for Database classes in the relevant Installer subclass.
175 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
176 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
177 if ( isset( $lbConf['servers'] ) ) {
178 // Server array is already explicitly configured
179 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
180 $lbConf['servers'] = [];
181 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
182 $lbConf['servers'][$i] = self::initServerInfo( $server, $this->options );
183 }
184 } else {
185 $server = self::initServerInfo(
186 [
187 'host' => $this->options->get( MainConfigNames::DBserver ),
188 'user' => $this->options->get( MainConfigNames::DBuser ),
189 'password' => $this->options->get( MainConfigNames::DBpassword ),
190 'dbname' => $this->options->get( MainConfigNames::DBname ),
191 'type' => $this->options->get( MainConfigNames::DBtype ),
192 'load' => 1
193 ],
194 $this->options
195 );
196
197 if ( $this->options->get( MainConfigNames::DBssl ) ) {
198 $server['ssl'] = true;
199 }
200 $server['flags'] |= $this->options->get( MainConfigNames::DBcompress ) ? DBO_COMPRESS : 0;
201 if ( $this->options->get( MainConfigNames::DBStrictWarnings ) ) {
202 $server['strictWarnings'] = true;
203 }
204
205 $lbConf['servers'] = [ $server ];
206 }
207 if ( !isset( $lbConf['externalClusters'] ) ) {
208 $lbConf['externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
209 }
210
211 $serversCheck = $lbConf['servers'];
212 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
213 if ( isset( $lbConf['serverTemplate'] ) ) {
214 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
215 $lbConf['serverTemplate']['schema'] = $this->options->get( MainConfigNames::DBmwschema );
216 }
217 $lbConf['serverTemplate']['sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
218 $serversCheck = [ $lbConf['serverTemplate'] ];
219 }
220 }
221
222 self::assertValidServerConfigs(
223 $serversCheck,
224 $this->options->get( MainConfigNames::DBname ),
225 $this->options->get( MainConfigNames::DBprefix )
226 );
227
228 $lbConf['chronologyProtector'] = $this->chronologyProtector;
229 $lbConf['srvCache'] = $this->srvCache;
230 $lbConf['wanCache'] = $this->wanCache;
231 $lbConf['virtualDomains'] = array_merge( $this->virtualDomains, self::CORE_VIRTUAL_DOMAINS );
232 $lbConf['virtualDomainsMapping'] = $this->options->get( MainConfigNames::VirtualDomainsMapping );
233
234 return $lbConf;
235 }
236
240 private function getDbTypesWithSchemas() {
241 return [ 'postgres' ];
242 }
243
249 private function initServerInfo( array $server, ServiceOptions $options ) {
250 if ( $server['type'] === 'sqlite' ) {
251 $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
252 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
253 // See https://www.sqlite.org/lang_transaction.html
254 // See https://www.sqlite.org/lockingv3.html#shared_lock
255 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
256 if ( MW_ENTRY_POINT === 'rest' && !$isHttpRead ) {
257 // Hack to support some re-entrant invocations using sqlite
258 // See: T259685, T91820
259 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
260 if ( $request->hasHeader( 'Promise-Non-Write-API-Action' ) ) {
261 $isHttpRead = true;
262 }
263 }
264 $server += [
265 'dbDirectory' => $options->get( MainConfigNames::SQLiteDataDir ),
266 'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
267 ];
268 } elseif ( $server['type'] === 'postgres' ) {
269 $server += [ 'port' => $options->get( MainConfigNames::DBport ) ];
270 }
271
272 if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
273 $server += [ 'schema' => $options->get( MainConfigNames::DBmwschema ) ];
274 }
275
276 $flags = $server['flags'] ?? DBO_DEFAULT;
277 if ( $options->get( MainConfigNames::DebugDumpSql )
278 || $options->get( MainConfigNames::DebugLogFile )
279 || $options->get( MainConfigNames::DebugToolbar )
280 ) {
281 $flags |= DBO_DEBUG;
282 }
283 $server['flags'] = $flags;
284
285 $server += [
286 'tablePrefix' => $options->get( MainConfigNames::DBprefix ),
287 'sqlMode' => $options->get( MainConfigNames::SQLMode ),
288 ];
289
290 return $server;
291 }
292
298 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
299 foreach ( $servers as $server ) {
300 $type = $server['type'] ?? null;
301 $srvDB = $server['dbname'] ?? null; // server DB
302 $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
303
304 if ( $type === 'mysql' ) {
305 // A DB name is not needed to connect to mysql; 'dbname' is useless.
306 // This field only defines the DB to use for unspecified DB domains.
307 if ( $srvDB !== null && $srvDB !== $ldDB ) {
308 self::reportMismatchedDBs( $srvDB, $ldDB );
309 }
310 } elseif ( $type === 'postgres' ) {
311 if ( $srvTP !== '' ) {
312 self::reportIfPrefixSet( $srvTP, $type );
313 }
314 }
315
316 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
317 self::reportMismatchedPrefixes( $srvTP, $ldTP );
318 }
319 }
320 }
321
327 private function reportIfPrefixSet( $prefix, $dbType ) {
328 $e = new UnexpectedValueException(
329 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
330 "MediaWiki does not support using a table prefix with this RDBMS type."
331 );
332 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
333 exit;
334 }
335
341 private function reportMismatchedDBs( $srvDB, $ldDB ) {
342 $e = new UnexpectedValueException(
343 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
344 "Set \$wgDBname to the database used by this wiki project. " .
345 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
346 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
347 "use of Database::getDomainId(), and other features are not reliable when " .
348 "\$wgDBservers does not match the local wiki database/prefix."
349 );
350 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
351 exit;
352 }
353
359 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
360 $e = new UnexpectedValueException(
361 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
362 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
363 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
364 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
365 "use of Database::getDomainId(), and other features are not reliable when " .
366 "\$wgDBservers does not match the local wiki database/prefix."
367 );
368 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_RAW );
369 exit;
370 }
371
379 public function getLBFactoryClass( array $config ) {
380 $compat = [
381 // For LocalSettings.php compat after removing underscores (since 1.23).
382 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
383 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
384 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
385 // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
386 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
387 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
388 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
389 ];
390
391 $class = $config['class'];
392 return $compat[$class] ?? $class;
393 }
394
398 public function setDomainAliases( ILBFactory $lbFactory ) {
399 $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
400 // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
401 // that assume corresponding database domain IDs and wiki IDs have identical values
402 $rawLocalDomain = strlen( $domain->getTablePrefix() )
403 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
404 : (string)$domain->getDatabase();
405
406 $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
407 }
408
432 public function applyGlobalState(
433 ILBFactory $lbFactory,
434 Config $config,
436 ): void {
437 if ( MW_ENTRY_POINT === 'cli' ) {
438 $lbFactory->getMainLB()->setTransactionListener(
439 __METHOD__,
440 static function ( $trigger ) use ( $stats, $config ) {
441 // During maintenance scripts and PHPUnit integration tests, we let
442 // DeferredUpdates run immediately from addUpdate(), unless a transaction
443 // is active. Notify DeferredUpdates after any commit to try now.
444 // See DeferredUpdates::tryOpportunisticExecute for why.
445 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
446 DeferredUpdates::tryOpportunisticExecute();
447 }
448 // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
449 MediaWiki::emitBufferedStatsdData( $stats, $config );
450 }
451 );
453 __METHOD__,
454 static function () use ( $stats, $config ) {
455 // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
456 MediaWiki::emitBufferedStatsdData( $stats, $config );
457 }
458 );
459
460 }
461 }
462
468 public static function logDeprecation( $msg ) {
469 if ( isset( self::$loggedDeprecations[$msg] ) ) {
470 return;
471 }
472 self::$loggedDeprecations[$msg] = true;
473 MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
474 }
475}
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
const MW_ENTRY_POINT
Definition api.php:35
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:85
static output(Throwable $e, $mode, Throwable $eNew=null)
MediaWiki-specific class for generating database load balancers.
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, array $virtualDomains)
static logDeprecation( $msg)
Log a database deprecation warning.
const APPLY_DEFAULT_CONFIG_OPTIONS
setDomainAliases(ILBFactory $lbFactory)
applyDefaultConfig(array $lbConf)
const CORE_VIRTUAL_DOMAINS
applyGlobalState(ILBFactory $lbFactory, Config $config, IBufferingStatsdDataFactory $stats)
Apply global state from the current web request or other PHP process.
getLBFactoryClass(array $config)
Decide which LBFactory class to use.
A class for passing options to services.
Debug toolbar.
Definition MWDebug.php:48
Defer callable updates to run later in the PHP process.
Create PSR-3 logger objects.
A class containing constants representing the names of configuration variables.
Stub profiler that does nothing.
static instance()
Definition Profiler.php:105
Multi-datacenter aware caching interface.
Provide a given client with protection against visible database lag.
Determine whether a site is statically configured as read-only.
Class to handle database/schema/prefix specifications for IDatabase.
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Interface for configuration instances.
Definition Config.php:32
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
Manager of ILoadBalancer objects and, indirectly, IDatabase connections.
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
getMainLB( $domain=false)
Get the tracked load balancer instance for the main cluster that handles the given domain.
setWaitForReplicationListener( $name, callable $callback=null)
Add a callback to be run in every call to waitForReplication() before waiting.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
const DBO_COMPRESS
Definition defines.php:19
const DBO_DEFAULT
Definition defines.php:13
const DBO_DEBUG
Definition defines.php:9