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\MediaWikiServices
;
25 use MediaWiki\Services\DestructibleService
;
26 use MediaWiki\Logger\LoggerFactory
;
29 * Legacy MediaWiki-specific class for generating database load balancers
32 abstract class LBFactoryMW
extends LBFactory
implements DestructibleService
{
33 /** @noinspection PhpMissingParentConstructorInspection */
35 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
37 * @TODO: inject objects via dependency framework
39 public function __construct( array $conf ) {
40 global $wgCommandLineMode, $wgSQLMode, $wgDBmysql5;
43 'localDomain' => wfWikiID(),
44 'hostname' => wfHostname(),
45 'profiler' => Profiler
::instance(),
46 'trxProfiler' => Profiler
::instance()->getTransactionProfiler(),
47 'replLogger' => LoggerFactory
::getInstance( 'DBReplication' ),
48 'queryLogger' => LoggerFactory
::getInstance( 'wfLogDBError' ),
49 'connLogger' => LoggerFactory
::getInstance( 'wfLogDBError' ),
50 'perfLogger' => LoggerFactory
::getInstance( 'DBPerformance' ),
51 'errorLogger' => [ MWExceptionHandler
::class, 'logException' ]
53 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
54 $sCache = ObjectCache
::getLocalServerInstance();
55 if ( $sCache->getQoS( $sCache::ATTR_EMULATION
) > $sCache::QOS_EMULATION_SQL
) {
56 $defaults['srvCache'] = $sCache;
58 $cCache = ObjectCache
::getLocalClusterInstance();
59 if ( $cCache->getQoS( $cCache::ATTR_EMULATION
) > $cCache::QOS_EMULATION_SQL
) {
60 $defaults['memCache'] = $cCache;
62 $wCache = ObjectCache
::getMainWANInstance();
63 if ( $wCache->getQoS( $wCache::ATTR_EMULATION
) > $wCache::QOS_EMULATION_SQL
) {
64 $defaults['wanCache'] = $wCache;
67 $this->agent
= isset( $params['agent'] ) ?
$params['agent'] : '';
68 $this->cliMode
= isset( $params['cliMode'] ) ?
$params['cliMode'] : $wgCommandLineMode;
70 if ( isset( $conf['serverTemplate'] ) ) { // LBFactoryMulti
71 $conf['serverTemplate']['sqlMode'] = $wgSQLMode;
72 $conf['serverTemplate']['utf8Mode'] = $wgDBmysql5;
75 parent
::__construct( $conf +
$defaults );
79 * Returns the LBFactory class to use and the load balancer configuration.
81 * @todo instead of this, use a ServiceContainer for managing the different implementations.
83 * @param array $config (e.g. $wgLBFactoryConf)
84 * @return string Class name
86 public static function getLBFactoryClass( array $config ) {
87 // For configuration backward compatibility after removing
88 // underscores from class names in MediaWiki 1.23.
90 'LBFactory_Simple' => 'LBFactorySimple',
91 'LBFactory_Single' => 'LBFactorySingle',
92 'LBFactory_Multi' => 'LBFactoryMulti'
95 $class = $config['class'];
97 if ( isset( $bcClasses[$class] ) ) {
98 $class = $bcClasses[$class];
100 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
111 * @deprecated Since 1.28; use laggedReplicaUsed()
113 public function laggedSlaveUsed() {
114 return $this->laggedReplicaUsed();
117 protected function newChronologyProtector() {
118 $request = RequestContext
::getMain()->getRequest();
119 $chronProt = new ChronologyProtector(
120 ObjectCache
::getMainStashInstance(),
122 'ip' => $request->getIP(),
123 'agent' => $request->getHeader( 'User-Agent' ),
125 $request->getFloat( 'cpPosTime', $request->getCookie( 'cpPosTime', '' ) )
127 if ( PHP_SAPI
=== 'cli' ) {
128 $chronProt->setEnabled( false );
129 } elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) {
130 // Request opted out of using position wait logic. This is useful for requests
131 // done by the job queue or background ETL that do not have a meaningful session.
132 $chronProt->setWaitEnabled( false );
139 * Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed
141 * Note that unlike cookies, this works accross domains
144 * @param float $time UNIX timestamp just before shutdown() was called
148 public function appendPreShutdownTimeAsQuery( $url, $time ) {
150 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$usedCluster ) {
151 $usedCluster |
= ( $lb->getServerCount() > 1 );
154 if ( !$usedCluster ) {
155 return $url; // no master/replica clusters touched
158 return wfAppendQuery( $url, [ 'cpPosTime' => $time ] );