MediaWiki master
MWLBFactory.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\DB;
11
28use Wikimedia\RequestTimeout\CriticalSectionProvider;
31
39
41 private static array $loggedDeprecations = [];
42
43 private ServiceOptions $options;
44 private ConfiguredReadOnlyMode $readOnlyMode;
45 private ChronologyProtector $chronologyProtector;
46 private BagOStuff $srvCache;
47 private WANObjectCache $wanCache;
48 private CriticalSectionProvider $csProvider;
49 private StatsFactory $statsFactory;
50 private TracerInterface $tracer;
51 private ?string $uniqueIdentifier;
52
63 public function __construct(
64 ConfiguredReadOnlyMode $readOnlyMode,
65 ChronologyProtector $chronologyProtector,
66 BagOStuff $srvCache,
67 WANObjectCache $wanCache,
68 CriticalSectionProvider $csProvider,
69 StatsFactory $statsFactory,
70 TracerInterface $tracer,
71 ?string $uniqueIdentifier = null
72 ) {
73 $this->readOnlyMode = $readOnlyMode;
74 $this->chronologyProtector = $chronologyProtector;
75 $this->srvCache = $srvCache;
76 $this->wanCache = $wanCache;
77 $this->csProvider = $csProvider;
78 $this->statsFactory = $statsFactory;
79 $this->tracer = $tracer;
80 $this->uniqueIdentifier = $uniqueIdentifier;
81 }
82
88 public function applyServices( array $lbConf ): array {
89 if ( Profiler::instance() instanceof ProfilerStub ) {
90 $profilerCallback = null;
91 } else {
92 $profilerCallback = static function ( $section ) {
93 return Profiler::instance()->scopedProfileIn( $section );
94 };
95 }
96
97 $lbConf += [
98 'profiler' => $profilerCallback,
99 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
100 'logger' => LoggerFactory::getInstance( 'rdbms' ),
101 'errorLogger' => MWExceptionHandler::logException( ... ),
102 'deprecationLogger' => static::logDeprecation( ... ),
103 'statsFactory' => $this->statsFactory,
104 'cliMode' => MW_ENTRY_POINT === 'cli',
105 'readOnlyReason' => $this->readOnlyMode->getReason(),
106 'criticalSectionProvider' => $this->csProvider,
107 'uniqueIdentifier' => $this->uniqueIdentifier,
108 ];
109 $lbConf['chronologyProtector'] = $this->chronologyProtector;
110 $lbConf['srvCache'] = $this->srvCache;
111 $lbConf['wanCache'] = $this->wanCache;
112 $lbConf['tracer'] = $this->tracer;
113
114 return $lbConf;
115 }
116
124 public function getLBFactoryClass( array $config ): string {
125 $compat = [
126 // For LocalSettings.php compat after removing underscores (since 1.23).
127 'LBFactory_Single' => LBFactorySingle::class,
128 'LBFactory_Simple' => LBFactorySimple::class,
129 'LBFactory_Multi' => LBFactoryMulti::class,
130 // For LocalSettings.php compat after moving classes to namespaces (since 1.29).
131 'LBFactorySingle' => LBFactorySingle::class,
132 'LBFactorySimple' => LBFactorySimple::class,
133 'LBFactoryMulti' => LBFactoryMulti::class
134 ];
135
136 $class = $config['class'];
137 return $compat[$class] ?? $class;
138 }
139
140 public function setDomainAliases( ILBFactory $lbFactory ): void {
141 $domain = DatabaseDomain::newFromId( $lbFactory->getLocalDomainID() );
142 // For compatibility with hyphenated $wgDBname values on older wikis, handle callers
143 // that assume corresponding database domain IDs and wiki IDs have identical values
144 $rawLocalDomain = strlen( $domain->getTablePrefix() )
145 ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
146 : (string)$domain->getDatabase();
147
148 $lbFactory->setDomainAliases( [ $rawLocalDomain => $domain ] );
149 }
150
156 public static function logDeprecation( string $msg ): void {
157 if ( isset( self::$loggedDeprecations[$msg] ) ) {
158 return;
159 }
160 self::$loggedDeprecations[$msg] = true;
161 MWDebug::sendRawDeprecated( $msg, true, wfGetCaller() );
162 }
163}
164
166class_alias( MWLBFactory::class, 'MWLBFactory' );
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:71
const MW_ENTRY_POINT
Definition api.php:21
A class for passing options to services.
MediaWiki-specific class for generating database load balancers.
__construct(ConfiguredReadOnlyMode $readOnlyMode, ChronologyProtector $chronologyProtector, BagOStuff $srvCache, WANObjectCache $wanCache, CriticalSectionProvider $csProvider, StatsFactory $statsFactory, TracerInterface $tracer, ?string $uniqueIdentifier=null)
applyServices(array $lbConf)
getLBFactoryClass(array $config)
Decide which LBFactory class to use.
setDomainAliases(ILBFactory $lbFactory)
static logDeprecation(string $msg)
Log a database deprecation warning.
Debug toolbar.
Definition MWDebug.php:35
Handler class for MWExceptions.
Create PSR-3 logger objects.
Stub profiler that does nothing.
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:26
Abstract class for any ephemeral data store.
Definition BagOStuff.php:73
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.
LoadBalancer manager for sites with several "main" database clusters.
LoadBalancer manager for sites with one "main" cluster and any number of "external" clusters.
LoadBalancer manager for sites with one "main" cluster using only injected database connections.
This is the primary interface for validating metrics definitions, caching defined metrics,...
Manager of ILoadBalancer objects and, indirectly, IDatabase connections.
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
Base interface for an OpenTelemetry tracer responsible for creating spans.