MediaWiki  master
MWLBFactory.php
Go to the documentation of this file.
1 <?php
31 use Wikimedia\RequestTimeout\CriticalSectionProvider;
32 
39 abstract class MWLBFactory {
40 
42  private static $loggedDeprecations = [];
43 
48  'DBcompress',
49  'DBDefaultGroup',
50  'DBmwschema',
51  'DBname',
52  'DBpassword',
53  'DBport',
54  'DBprefix',
55  'DBserver',
56  'DBservers',
57  'DBssl',
58  'DBtype',
59  'DBuser',
60  'DebugDumpSql',
61  'DebugLogFile',
62  'DebugToolbar',
63  'ExternalServers',
64  'SQLiteDataDir',
65  'SQLMode',
66  ];
67 
79  public static function applyDefaultConfig(
80  array $lbConf,
81  ServiceOptions $options,
82  ConfiguredReadOnlyMode $readOnlyMode,
83  BagOStuff $cpStash,
84  BagOStuff $srvCache,
85  WANObjectCache $wanCache,
86  CriticalSectionProvider $csProvider
87  ) {
88  $options->assertRequiredOptions( self::APPLY_DEFAULT_CONFIG_OPTIONS );
89 
90  global $wgCommandLineMode;
91 
92  $typesWithSchema = self::getDbTypesWithSchemas();
93 
94  $lbConf += [
95  'localDomain' => new DatabaseDomain(
96  $options->get( 'DBname' ),
97  $options->get( 'DBmwschema' ),
98  $options->get( 'DBprefix' )
99  ),
100  'profiler' => static function ( $section ) {
101  return Profiler::instance()->scopedProfileIn( $section );
102  },
103  'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
104  'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
105  'queryLogger' => LoggerFactory::getInstance( 'DBQuery' ),
106  'connLogger' => LoggerFactory::getInstance( 'DBConnection' ),
107  'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
108  'errorLogger' => [ MWExceptionHandler::class, 'logException' ],
109  'deprecationLogger' => [ static::class, 'logDeprecation' ],
110  'cliMode' => $wgCommandLineMode,
111  'readOnlyReason' => $readOnlyMode->getReason(),
112  'defaultGroup' => $options->get( 'DBDefaultGroup' ),
113  'criticalSectionProvider' => $csProvider
114  ];
115 
116  $serversCheck = [];
117  // When making changes here, remember to also specify MediaWiki-specific options
118  // for Database classes in the relevant Installer subclass.
119  // Such as MysqlInstaller::openConnection and PostgresInstaller::openConnectionWithParams.
120  if ( $lbConf['class'] === Wikimedia\Rdbms\LBFactorySimple::class ) {
121  if ( isset( $lbConf['servers'] ) ) {
122  // Server array is already explicitly configured
123  } elseif ( is_array( $options->get( 'DBservers' ) ) ) {
124  $lbConf['servers'] = [];
125  foreach ( $options->get( 'DBservers' ) as $i => $server ) {
126  $lbConf['servers'][$i] = self::initServerInfo( $server, $options );
127  }
128  } else {
129  $server = self::initServerInfo(
130  [
131  'host' => $options->get( 'DBserver' ),
132  'user' => $options->get( 'DBuser' ),
133  'password' => $options->get( 'DBpassword' ),
134  'dbname' => $options->get( 'DBname' ),
135  'type' => $options->get( 'DBtype' ),
136  'load' => 1
137  ],
138  $options
139  );
140 
141  $server['flags'] |= $options->get( 'DBssl' ) ? DBO_SSL : 0;
142  $server['flags'] |= $options->get( 'DBcompress' ) ? DBO_COMPRESS : 0;
143 
144  $lbConf['servers'] = [ $server ];
145  }
146  if ( !isset( $lbConf['externalClusters'] ) ) {
147  $lbConf['externalClusters'] = $options->get( 'ExternalServers' );
148  }
149 
150  $serversCheck = $lbConf['servers'];
151  } elseif ( $lbConf['class'] === Wikimedia\Rdbms\LBFactoryMulti::class ) {
152  if ( isset( $lbConf['serverTemplate'] ) ) {
153  if ( in_array( $lbConf['serverTemplate']['type'], $typesWithSchema, true ) ) {
154  $lbConf['serverTemplate']['schema'] = $options->get( 'DBmwschema' );
155  }
156  $lbConf['serverTemplate']['sqlMode'] = $options->get( 'SQLMode' );
157  $serversCheck = [ $lbConf['serverTemplate'] ];
158  }
159  }
160 
162  $serversCheck,
163  $options->get( 'DBname' ),
164  $options->get( 'DBprefix' )
165  );
166 
167  $lbConf['cpStash'] = $cpStash;
168  $lbConf['srvCache'] = $srvCache;
169  $lbConf['wanCache'] = $wanCache;
170 
171  return $lbConf;
172  }
173 
177  private static function getDbTypesWithSchemas() {
178  return [ 'postgres' ];
179  }
180 
186  private static function initServerInfo( array $server, ServiceOptions $options ) {
187  if ( $server['type'] === 'sqlite' ) {
188  $httpMethod = $_SERVER['REQUEST_METHOD'] ?? null;
189  // T93097: hint for how file-based databases (e.g. sqlite) should go about locking.
190  // See https://www.sqlite.org/lang_transaction.html
191  // See https://www.sqlite.org/lockingv3.html#shared_lock
192  $isHttpRead = in_array( $httpMethod, [ 'GET', 'HEAD', 'OPTIONS', 'TRACE' ] );
193  if ( MW_ENTRY_POINT === 'rest' && !$isHttpRead ) {
194  // Hack to support some re-entrant invocations using sqlite
195  // See: T259685, T91820
197  if ( $request->hasHeader( 'Promise-Non-Write-API-Action' ) ) {
198  $isHttpRead = true;
199  }
200  }
201  $server += [
202  'dbDirectory' => $options->get( 'SQLiteDataDir' ),
203  'trxMode' => $isHttpRead ? 'DEFERRED' : 'IMMEDIATE'
204  ];
205  } elseif ( $server['type'] === 'postgres' ) {
206  $server += [ 'port' => $options->get( 'DBport' ) ];
207  }
208 
209  if ( in_array( $server['type'], self::getDbTypesWithSchemas(), true ) ) {
210  $server += [ 'schema' => $options->get( 'DBmwschema' ) ];
211  }
212 
213  $flags = $server['flags'] ?? DBO_DEFAULT;
214  if ( $options->get( 'DebugDumpSql' )
215  || $options->get( 'DebugLogFile' )
216  || $options->get( 'DebugToolbar' )
217  ) {
218  $flags |= DBO_DEBUG;
219  }
220  $server['flags'] = $flags;
221 
222  $server += [
223  'tablePrefix' => $options->get( 'DBprefix' ),
224  'sqlMode' => $options->get( 'SQLMode' ),
225  ];
226 
227  return $server;
228  }
229 
235  private static function assertValidServerConfigs( array $servers, $ldDB, $ldTP ) {
236  foreach ( $servers as $server ) {
237  $type = $server['type'] ?? null;
238  $srvDB = $server['dbname'] ?? null; // server DB
239  $srvTP = $server['tablePrefix'] ?? ''; // server table prefix
240 
241  if ( $type === 'mysql' ) {
242  // A DB name is not needed to connect to mysql; 'dbname' is useless.
243  // This field only defines the DB to use for unspecified DB domains.
244  if ( $srvDB !== null && $srvDB !== $ldDB ) {
245  self::reportMismatchedDBs( $srvDB, $ldDB );
246  }
247  } elseif ( $type === 'postgres' ) {
248  if ( $srvTP !== '' ) {
249  self::reportIfPrefixSet( $srvTP, $type );
250  }
251  }
252 
253  if ( $srvTP !== '' && $srvTP !== $ldTP ) {
254  self::reportMismatchedPrefixes( $srvTP, $ldTP );
255  }
256  }
257  }
258 
264  private static function reportIfPrefixSet( $prefix, $dbType ) {
265  $e = new UnexpectedValueException(
266  "\$wgDBprefix is set to '$prefix' but the database type is '$dbType'. " .
267  "MediaWiki does not support using a table prefix with this RDBMS type."
268  );
270  exit;
271  }
272 
278  private static function reportMismatchedDBs( $srvDB, $ldDB ) {
279  $e = new UnexpectedValueException(
280  "\$wgDBservers has dbname='$srvDB' but \$wgDBname='$ldDB'. " .
281  "Set \$wgDBname to the database used by this wiki project. " .
282  "There is rarely a need to set 'dbname' in \$wgDBservers. " .
283  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
284  "use of Database::getDomainId(), and other features are not reliable when " .
285  "\$wgDBservers does not match the local wiki database/prefix."
286  );
288  exit;
289  }
290 
296  private static function reportMismatchedPrefixes( $srvTP, $ldTP ) {
297  $e = new UnexpectedValueException(
298  "\$wgDBservers has tablePrefix='$srvTP' but \$wgDBprefix='$ldTP'. " .
299  "Set \$wgDBprefix to the table prefix used by this wiki project. " .
300  "There is rarely a need to set 'tablePrefix' in \$wgDBservers. " .
301  "Cross-wiki database access, use of WikiMap::getCurrentWikiDbDomain(), " .
302  "use of Database::getDomainId(), and other features are not reliable when " .
303  "\$wgDBservers does not match the local wiki database/prefix."
304  );
306  exit;
307  }
308 
316  public static function getLBFactoryClass( array $config ) {
317  $compat = [
318  // For LocalSettings.php compat after removing underscores (since 1.23).
319  'LBFactory_Single' => Wikimedia\Rdbms\LBFactorySingle::class,
320  'LBFactory_Simple' => Wikimedia\Rdbms\LBFactorySimple::class,
321  'LBFactory_Multi' => Wikimedia\Rdbms\LBFactoryMulti::class,
322  // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
323  'LBFactorySingle' => Wikimedia\Rdbms\LBFactorySingle::class,
324  'LBFactorySimple' => Wikimedia\Rdbms\LBFactorySimple::class,
325  'LBFactoryMulti' => Wikimedia\Rdbms\LBFactoryMulti::class
326  ];
327 
328  $class = $config['class'];
329  return $compat[$class] ?? $class;
330  }
331 
335  public static function setDomainAliases( ILBFactory $lbFactory ) {
336  $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
337  // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
338  // that assume corresponding database domain IDs and wiki IDs have identical values
339  $rawLocalDomain = strlen( $domain->getTablePrefix() )
340  ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
341  : (string)$domain->getDatabase();
342 
343  $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
344  }
345 
369  public static function applyGlobalState(
370  ILBFactory $lbFactory,
371  Config $config,
373  ): void {
374  // Use the global WebRequest singleton. The main reason for using this
375  // is to call WebRequest::getIP() which is non-trivial to reproduce statically
376  // because it needs $wgUsePrivateIPs, as well as ProxyLookup and HookRunner services.
377  // TODO: Create a static version of WebRequest::getIP that accepts these three
378  // as dependencies, and then call that here. The other uses of $req below can
379  // trivially use $_COOKIES, $_GET and $_SERVER instead.
380  $req = RequestContext::getMain()->getRequest();
381 
382  // Set user IP/agent information for agent session consistency purposes
383  $reqStart = (int)( $_SERVER['REQUEST_TIME_FLOAT'] ?? time() );
384  $cpPosInfo = LBFactory::getCPInfoFromCookieValue(
385  // The cookie has no prefix and is set by MediaWiki::preOutputCommit()
386  $req->getCookie( 'cpPosIndex', '' ),
387  // Mitigate broken client-side cookie expiration handling (T190082)
388  $reqStart - ChronologyProtector::POSITION_COOKIE_TTL
389  );
390  $lbFactory->setRequestInfo( [
391  'IPAddress' => $req->getIP(),
392  'UserAgent' => $req->getHeader( 'User-Agent' ),
393  'ChronologyProtection' => $req->getHeader( 'MediaWiki-Chronology-Protection' ),
394  'ChronologyPositionIndex' => $req->getInt( 'cpPosIndex', $cpPosInfo['index'] ),
395  'ChronologyClientId' => $cpPosInfo['clientId']
396  ?? $req->getHeader( 'MediaWiki-Chronology-Client-Id' )
397  ] );
398 
399  if ( $config->get( 'CommandLineMode' ) ) {
400  // Disable buffering and delaying of DeferredUpdates and stats
401  // for maintenance scripts and PHPUnit tests.
402  // Hook into period lag checks which often happen in long-running scripts
403  $lbFactory->setWaitForReplicationListener(
404  __METHOD__,
405  static function () use ( $stats, $config ) {
407  // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
408  MediaWiki::emitBufferedStatsdData( $stats, $config );
409  }
410  );
411  // Check for other windows to run them. A script may read or do a few writes
412  // to the primary DB but mostly be writing to something else, like a file store.
413  $lbFactory->getMainLB()->setTransactionListener(
414  __METHOD__,
415  static function ( $trigger ) use ( $stats, $config ) {
416  if ( $trigger === IDatabase::TRIGGER_COMMIT ) {
418  }
419  // Flush stats periodically in long-running CLI scripts to avoid OOM (T181385)
420  MediaWiki::emitBufferedStatsdData( $stats, $config );
421  }
422  );
423 
424  }
425  }
426 
432  public static function logDeprecation( $msg ) {
433  if ( isset( self::$loggedDeprecations[$msg] ) ) {
434  return;
435  }
436  self::$loggedDeprecations[$msg] = true;
437  MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
438  }
439 }
MWLBFactory
MediaWiki-specific class for generating database load balancers.
Definition: MWLBFactory.php:39
Wikimedia\Rdbms\ILBFactory\getLocalDomainID
getLocalDomainID()
Get the local (and default) database domain ID of connection handles.
Wikimedia\Rdbms\ILBFactory\setRequestInfo
setRequestInfo(array $info)
Inject HTTP request header/cookie information during setup of this instance.
MWLBFactory\reportMismatchedDBs
static reportMismatchedDBs( $srvDB, $ldDB)
Definition: MWLBFactory.php:278
MediaWiki\emitBufferedStatsdData
static emitBufferedStatsdData(IBufferingStatsdDataFactory $stats, Config $config)
Send out any buffered statsd data according to sampling rules.
Definition: MediaWiki.php:1155
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:69
MWLBFactory\initServerInfo
static initServerInfo(array $server, ServiceOptions $options)
Definition: MWLBFactory.php:186
MWLBFactory\assertValidServerConfigs
static assertValidServerConfigs(array $servers, $ldDB, $ldTP)
Definition: MWLBFactory.php:235
DBO_DEBUG
const DBO_DEBUG
Definition: defines.php:9
ConfiguredReadOnlyMode
A read-only mode service which does not depend on LoadBalancer.
Definition: ConfiguredReadOnlyMode.php:9
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:86
DBO_SSL
const DBO_SSL
Definition: defines.php:17
Wikimedia\Rdbms\ILBFactory\getMainLB
getMainLB( $domain=false)
Get the tracked load balancer instance for a main cluster.
MWLBFactory\logDeprecation
static logDeprecation( $msg)
Log a database deprecation warning.
Definition: MWLBFactory.php:432
MWLBFactory\setDomainAliases
static setDomainAliases(ILBFactory $lbFactory)
Definition: MWLBFactory.php:335
ConfiguredReadOnlyMode\getReason
getReason()
Get the value of $wgReadOnly or the contents of $wgReadOnlyFile.
Definition: ConfiguredReadOnlyMode.php:42
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
MWLBFactory\reportMismatchedPrefixes
static reportMismatchedPrefixes( $srvTP, $ldTP)
Definition: MWLBFactory.php:296
MWLBFactory\$loggedDeprecations
static array $loggedDeprecations
Cache of already-logged deprecation messages.
Definition: MWLBFactory.php:42
Config
Interface for configuration instances.
Definition: Config.php:30
MWLBFactory\applyDefaultConfig
static applyDefaultConfig(array $lbConf, ServiceOptions $options, ConfiguredReadOnlyMode $readOnlyMode, BagOStuff $cpStash, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider)
Definition: MWLBFactory.php:79
MWLBFactory\APPLY_DEFAULT_CONFIG_OPTIONS
const APPLY_DEFAULT_CONFIG_OPTIONS
Definition: MWLBFactory.php:47
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:29
Wikimedia\Rdbms\ILBFactory\setWaitForReplicationListener
setWaitForReplicationListener( $name, callable $callback=null)
Add a callback to be run in every call to waitForReplication() before waiting.
MWExceptionRenderer\output
static output(Throwable $e, $mode, Throwable $eNew=null)
Definition: MWExceptionRenderer.php:41
MWLBFactory\getLBFactoryClass
static getLBFactoryClass(array $config)
Decide which LBFactory class to use.
Definition: MWLBFactory.php:316
MWExceptionRenderer\AS_PRETTY
const AS_PRETTY
Definition: MWExceptionRenderer.php:34
DBO_COMPRESS
const DBO_COMPRESS
Definition: defines.php:18
DeferredUpdates\tryOpportunisticExecute
static tryOpportunisticExecute( $mode='run')
Consume and execute all pending updates unless an update is already in progress or the LBFactory serv...
Definition: DeferredUpdates.php:282
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:131
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:484
MWLBFactory\applyGlobalState
static applyGlobalState(ILBFactory $lbFactory, Config $config, IBufferingStatsdDataFactory $stats)
Apply global state from the current web request or other PHP process.
Definition: MWLBFactory.php:369
IBufferingStatsdDataFactory
MediaWiki adaptation of StatsdDataFactory that provides buffering functionality.
Definition: IBufferingStatsdDataFactory.php:13
Wikimedia\Rdbms\ILBFactory\setDomainAliases
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
Wikimedia\Rdbms\ChronologyProtector
Provide a given client with protection against visible database lag.
Definition: ChronologyProtector.php:136
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:42
Wikimedia
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
MediaWiki\Config\ServiceOptions\get
get( $key)
Definition: ServiceOptions.php:93
Wikimedia\Rdbms\DatabaseDomain
Class to handle database/schema/prefix specifications for IDatabase.
Definition: DatabaseDomain.php:40
DBO_DEFAULT
const DBO_DEFAULT
Definition: defines.php:13
MWLBFactory\getDbTypesWithSchemas
static getDbTypesWithSchemas()
Definition: MWLBFactory.php:177
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1360
MWDebug\sendRawDeprecated
static sendRawDeprecated( $msg, $sendToLog=true, $callerFunc='')
Send a raw deprecation message to the log and the debug toolbar, without filtering of duplicate messa...
Definition: MWDebug.php:369
MW_ENTRY_POINT
const MW_ENTRY_POINT
Definition: api.php:41
MediaWiki\Rest\EntryPoint\getMainRequest
static getMainRequest()
Definition: EntryPoint.php:87
MWLBFactory\reportIfPrefixSet
static reportIfPrefixSet( $prefix, $dbType)
Definition: MWLBFactory.php:264
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71
Wikimedia\Rdbms\ILBFactory
An interface for generating database load balancers.
Definition: ILBFactory.php:33
$type
$type
Definition: testCompression.php:52