MediaWiki 1.40.4
MWLBFactory.php
Go to the documentation of this file.
1<?php
24use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
34use Wikimedia\RequestTimeout\CriticalSectionProvider;
35
43
45 private static $loggedDeprecations = [];
46
51 'CommandLineMode',
52 MainConfigNames::DBcompress,
53 MainConfigNames::DBDefaultGroup,
54 MainConfigNames::DBmwschema,
55 MainConfigNames::DBname,
56 MainConfigNames::DBpassword,
57 MainConfigNames::DBport,
58 MainConfigNames::DBprefix,
59 MainConfigNames::DBserver,
60 MainConfigNames::DBservers,
61 MainConfigNames::DBssl,
62 MainConfigNames::DBtype,
63 MainConfigNames::DBuser,
64 MainConfigNames::DebugDumpSql,
65 MainConfigNames::DebugLogFile,
66 MainConfigNames::DebugToolbar,
67 MainConfigNames::ExternalServers,
68 MainConfigNames::SQLiteDataDir,
69 MainConfigNames::SQLMode,
70 ];
74 private $options;
78 private $readOnlyMode;
82 private $cpStash;
86 private $srvCache;
90 private $wanCache;
94 private $csProvider;
98 private $statsdDataFactory;
102 private $databaseFactory;
103
114 public function __construct(
115 ServiceOptions $options,
116 ConfiguredReadOnlyMode $readOnlyMode,
117 BagOStuff $cpStash,
118 BagOStuff $srvCache,
119 WANObjectCache $wanCache,
120 CriticalSectionProvider $csProvider,
121 StatsdDataFactoryInterface $statsdDataFactory,
122 DatabaseFactory $databaseFactory
123 ) {
124 $this->options = $options;
125 $this->readOnlyMode = $readOnlyMode;
126 $this->cpStash = $cpStash;
127 $this->srvCache = $srvCache;
128 $this->wanCache = $wanCache;
129 $this->csProvider = $csProvider;
130 $this->statsdDataFactory = $statsdDataFactory;
131 $this->databaseFactory = $databaseFactory;
132 }
133
139 public function applyDefaultConfig( array $lbConf ) {
140 $this->options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
141
142 $typesWithSchema = self::getDbTypesWithSchemas();
143
144 $lbConf += [
145 'localDomain' => new DatabaseDomain(
146 $this->options->get( MainConfigNames::DBname ),
147 $this->options->get( MainConfigNames::DBmwschema ),
148 $this->options->get( MainConfigNames::DBprefix )
149 ),
150 'profiler' => static function ( $section ) {
151 return Profiler::instance()->scopedProfileIn( $section );
152 },
153 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
154 'logger' => LoggerFactory::getInstance( 'rdbms' ),
155 'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
156 'deprecationLogger' => [ static::class, 'logDeprecation' ],
157 'statsdDataFactory' => $this->statsdDataFactory,
158 'cliMode' => $this->options->get( 'CommandLineMode' ),
159 'readOnlyReason' => $this->readOnlyMode->getReason(),
160 'defaultGroup' => $this->options->get( MainConfigNames::DBDefaultGroup ),
161 'criticalSectionProvider' => $this->csProvider
162 ];
163
164 $serversCheck = [];
165 // When making changes here, remember to also specify MediaWiki-specific options
166 // for Database classes in the relevant Installer subclass.
167 // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
168 if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
169 if ( isset( $lbConf['servers'] ) ) {
170 // Server array is already explicitly configured
171 } elseif ( is_array( $this->options->get( MainConfigNames::DBservers ) ) ) {
172 $lbConf['servers'] = [];
173 foreach ( $this->options->get( MainConfigNames::DBservers ) as $i => $server ) {
174 $lbConf['servers'][$i] = self::initServerInfo( $server, $this->options );
175 }
176 } else {
177 $server = self::initServerInfo(
178 [
179 'host' => $this->options->get( MainConfigNames::DBserver ),
180 'user' => $this->options->get( MainConfigNames::DBuser ),
181 'password' => $this->options->get( MainConfigNames::DBpassword ),
182 'dbname' => $this->options->get( MainConfigNames::DBname ),
183 'type' => $this->options->get( MainConfigNames::DBtype ),
184 'load' => 1
185 ],
186 $this->options
187 );
188
189 if ( $this->options->get( MainConfigNames::DBssl ) ) {
190 $server['ssl'] = true;
191 }
192 $server['flags'] |= $this->options->get( MainConfigNames::DBcompress ) ? DBO_COMPRESS : 0;
193
194 $lbConf['servers'] = [ $server ];
195 }
196 if ( !isset( $lbConf['externalClusters'] ) ) {
197 $lbConf['externalClusters'] = $this->options->get( MainConfigNames::ExternalServers );
198 }
199
200 $serversCheck = $lbConf['servers'];
201 } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
202 if ( isset( $lbConf['serverTemplate'] ) ) {
203 if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
204 $lbConf['serverTemplate']['schema'] = $this->options->get( MainConfigNames::DBmwschema );
205 }
206 $lbConf['serverTemplate']['sqlMode'] = $this->options->get( MainConfigNames::SQLMode );
207 $serversCheck = [ $lbConf['serverTemplate'] ];
208 }
209 }
210
211 self::assertValidServerConfigs(
212 $serversCheck,
213 $this->options->get( MainConfigNames::DBname ),
214 $this->options->get( MainConfigNames::DBprefix )
215 );
216
217 $lbConf['cpStash'] = $this->cpStash;
218 $lbConf['srvCache'] = $this->srvCache;
219 $lbConf['wanCache'] = $this->wanCache;
220 $lbConf['databaseFactory'] = $this->databaseFactory;
221
222 return $lbConf;
223 }
224
228 private function getDbTypesWithSchemas() {
229 return [ 'postgres' ];
230 }
231
237 private function initServerInfo( array $server, ServiceOptions $options ) {
238 if ( $server['type'] === 'sqlite' ) {
239 $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
240 // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
241 // See https://www.sqlite.org/lang_transaction.html
242 // See https://www.sqlite.org/lockingv3.html#shared_lock
243 $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
244 if ( MW_ENTRY_POINT === 'rest' && !$isHttpRead ) {
245 // Hack to support some re-entrant invocations using sqlite
246 // See: T259685, T91820
247 $request = \MediaWiki\Rest\EntryPoint::getMainRequest();
248 if ( $request->hasHeader( 'Promise-Non-Write-API-Action' ) ) {
249 $isHttpRead = true;
250 }
251 }
252 $server += [
253 'dbDirectory' => $options->get( MainConfigNames::SQLiteDataDir ),
254 'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
255 ];
256 } elseif ( $server['type'] === 'postgres' ) {
257 $server += [ 'port' => $options->get( MainConfigNames::DBport ) ];
258 }
259
260 if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
261 $server += [ 'schema' => $options->get( MainConfigNames::DBmwschema ) ];
262 }
263
264 $flags = $server['flags'] ?? DBO_DEFAULT;
265 if ( $options->get( MainConfigNames::DebugDumpSql )
266 || $options->get( MainConfigNames::DebugLogFile )
267 || $options->get( MainConfigNames::DebugToolbar )
268 ) {
269 $flags |= DBO_DEBUG;
270 }
271 $server['flags'] = $flags;
272
273 $server += [
274 'tablePrefix' => $options->get( MainConfigNames::DBprefix ),
275 'sqlMode' => $options->get( MainConfigNames::SQLMode ),
276 ];
277
278 return $server;
279 }
280
286 private function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
287 foreach ( $servers as $server ) {
288 $type = $server['type'] ?? null;
289 $srvDB = $server['dbname'] ?? null; // server DB
290 $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
291
292 if ( $type === 'mysql' ) {
293 // A DB name is not needed to connect to mysql; 'dbname' is useless.
294 // This field only defines the DB to use for unspecified DB domains.
295 if ( $srvDB !== null && $srvDB !== $ldDB ) {
296 self::reportMismatchedDBs( $srvDB, $ldDB );
297 }
298 } elseif ( $type === 'postgres' ) {
299 if ( $srvTP !== '' ) {
300 self::reportIfPrefixSet( $srvTP, $type );
301 }
302 }
303
304 if ( $srvTP !== '' && $srvTP !== $ldTP ) {
305 self::reportMismatchedPrefixes( $srvTP, $ldTP );
306 }
307 }
308 }
309
315 private function reportIfPrefixSet( $prefix, $dbType ) {
316 $e = new UnexpectedValueException(
317 "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
318 "MediaWiki does not support using a table prefix with this RDBMS type."
319 );
320 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
321 exit;
322 }
323
329 private function reportMismatchedDBs( $srvDB, $ldDB ) {
330 $e = new UnexpectedValueException(
331 "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
332 "Set \$wgDBname to the database used by this wiki project. " .
333 "There is rarely a need to set 'dbname' in \$wgDBservers. " .
334 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
335 "use of Database::getDomainId(), and other features are not reliable when " .
336 "\$wgDBservers does not match the local wiki database/prefix."
337 );
338 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
339 exit;
340 }
341
347 private function reportMismatchedPrefixes( $srvTP, $ldTP ) {
348 $e = new UnexpectedValueException(
349 "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
350 "Set \$wgDBprefix to the table prefix used by this wiki project. " .
351 "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
352 "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
353 "use of Database::getDomainId(), and other features are not reliable when " .
354 "\$wgDBservers does not match the local wiki database/prefix."
355 );
356 MWExceptionRenderer::output( $e, MWExceptionRenderer::AS_PRETTY );
357 exit;
358 }
359
367 public function getLBFactoryClass( array $config ) {
368 $compat = [
369 // For LocalSettings.php compat after removing underscores (since 1.23).
370 'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
371 'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
372 'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
373 // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
374 'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
375 'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
376 'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
377 ];
378
379 $class = $config['class'];
380 return $compat[$class] ?? $class;
381 }
382
386 public function setDomainAliases( ILBFactory $lbFactory ) {
387 $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
388 // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
389 // that assume corresponding database domain IDs and wiki IDs have identical values
390 $rawLocalDomain = strlen( $domain->getTablePrefix() )
391 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
392 : (string)$domain->getDatabase();
393
394 $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
395 }
396
420 public function applyGlobalState(
421 ILBFactory $lbFactory,
422 Config $config,
424 ): void {
425 // Use the global WebRequest singleton. The main reason for using this
426 // is to call WebRequest::getIP() which is non-trivial to reproduce statically
427 // because it needs $wgUsePrivateIPs, as well as ProxyLookup and HookRunner services.
428 // TODO: Create a static version of WebRequest::getIP that accepts these three
429 // as dependencies, and then call that here. The other uses of $req below can
430 // trivially use $_COOKIES, $_GET and $_SERVER instead.
431 $req = RequestContext::getMain()->getRequest();
432
433 // Set user IP/agent information for agent session consistency purposes
434 $reqStart = (int)( $_SERVER['REQUEST_TIME_FLOAT'] ?? time() );
435 $cpPosInfo = LBFactory::getCPInfoFromCookieValue(
436 // The cookie has no prefix and is set by MediaWiki::preOutputCommit()
437 $req->getCookie( 'cpPosIndex', '' ),
438 // Mitigate broken client-side cookie expiration handling (T190082)
439 $reqStart - ChronologyProtector::POSITION_COOKIE_TTL
440 );
441 $lbFactory->setRequestInfo( [
442 'IPAddress' => $req->getIP(),
443 'UserAgent' => $req->getHeader( 'User-Agent' ),
444 'ChronologyProtection' => $req->getHeader( 'MediaWiki-Chronology-Protection' ),
445 'ChronologyPositionIndex' => $req->getInt( 'cpPosIndex', $cpPosInfo['index'] ),
446 'ChronologyClientId' => $cpPosInfo['clientId']
447 ?? $req->getHeader( 'MediaWiki-Chronology-Client-Id' )
448 ] );
449
450 if ( $config->get( 'CommandLineMode' ) ) {
451 // Disable buffering and delaying of DeferredUpdates and stats
452 // for maintenance scripts and PHPUnit tests.
453 // Hook into period lag checks which often happen in long-running scripts
455 __METHOD__,
456 static function () use ( $stats, $config ) {
457 DeferredUpdates::tryOpportunisticExecute();
458 // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
459 MediaWiki::emitBufferedStatsdData( $stats, $config );
460 }
461 );
462 // Check for other windows to run them. A script may read or do a few writes
463 // to the primary DB but mostly be writing to something else, like a file store.
464 $lbFactory->getMainLB()->setTransactionListener(
465 __METHOD__,
466 static function ( $trigger ) use ( $stats, $config ) {
467 if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
468 DeferredUpdates::tryOpportunisticExecute();
469 }
470 // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
471 MediaWiki::emitBufferedStatsdData( $stats, $config );
472 }
473 );
474
475 }
476 }
477
483 public static function logDeprecation( $msg ) {
484 if ( isset( self::$loggedDeprecations[$msg] ) ) {
485 return;
486 }
487 self::$loggedDeprecations[$msg] = true;
488 MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
489 }
490}
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
const MW_ENTRY_POINT
Definition api.php:42
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:85
A read-only mode service which does not depend on LoadBalancer.
static output(Throwable $e, $mode, Throwable $eNew=null)
MediaWiki-specific class for generating database load balancers.
static logDeprecation( $msg)
Log a database deprecation warning.
const APPLY_DEFAULT_CONFIG_OPTIONS
__construct(ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $cpStash, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsdDataFactoryInterface $statsdDataFactory, DatabaseFactory $databaseFactory)
setDomainAliases(ILBFactory $lbFactory)
applyDefaultConfig(array $lbConf)
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.
PSR-3 logger instance factory.
A class containing constants representing the names of configuration variables.
static emitBufferedStatsdData(IBufferingStatsdDataFactory $stats, Config $config)
Send out any buffered statsd data according to sampling rules.
Group all the pieces relevant to the context of a request into one instance.
Multi-datacenter aware caching interface.
Provide a given client with protection against visible database lag.
Class to handle database/schema/prefix specifications for IDatabase.
Constructs Database objects.
Interface for configuration instances.
Definition Config.php:30
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
Manager of ILoadBalancer objects and, indirectly, IDatabase connections.
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
setRequestInfo(array $info)
Inject HTTP request header/cookie information during setup of this instance.
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