Cleanups to DatabaseMysqlBase
[lhc/web/wiklou.git] / includes / db / loadbalancer / LBFactoryMW.php
1 <?php
2 /**
3 * Generator of database load balancing objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Services\DestructibleService;
26 use MediaWiki\Logger\LoggerFactory;
27
28 /**
29 * Legacy MediaWiki-specific class for generating database load balancers
30 * @ingroup Database
31 */
32 abstract class LBFactoryMW extends LBFactory implements DestructibleService {
33 /** @noinspection PhpMissingParentConstructorInspection */
34 /**
35 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
36 * @param array $conf
37 * @TODO: inject objects via dependency framework
38 */
39 public function __construct( array $conf ) {
40 global $wgCommandLineMode, $wgSQLMode, $wgDBmysql5;
41
42 $defaults = [
43 'localDomain' => wfWikiID(),
44 'hostname' => wfHostname(),
45 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
46 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
47 'queryLogger' => LoggerFactory::getInstance( 'wfLogDBError' ),
48 'connLogger' => LoggerFactory::getInstance( 'wfLogDBError' ),
49 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
50 'errorLogger' => [ MWExceptionHandler::class, 'logException' ]
51 ];
52 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
53 $sCache = ObjectCache::getLocalServerInstance();
54 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
55 $defaults['srvCache'] = $sCache;
56 }
57 $cCache = ObjectCache::getLocalClusterInstance();
58 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
59 $defaults['memCache'] = $cCache;
60 }
61 $wCache = ObjectCache::getMainWANInstance();
62 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
63 $defaults['wanCache'] = $wCache;
64 }
65
66 $this->agent = isset( $params['agent'] ) ? $params['agent'] : '';
67 $this->cliMode = isset( $params['cliMode'] ) ? $params['cliMode'] : $wgCommandLineMode;
68
69 if ( isset( $conf['serverTemplate'] ) ) { // LBFactoryMulti
70 $conf['serverTemplate']['sqlMode'] = $wgSQLMode;
71 $conf['serverTemplate']['utf8Mode'] = $wgDBmysql5;
72 }
73
74 parent::__construct( $conf + $defaults );
75 }
76
77 /**
78 * Returns the LBFactory class to use and the load balancer configuration.
79 *
80 * @todo instead of this, use a ServiceContainer for managing the different implementations.
81 *
82 * @param array $config (e.g. $wgLBFactoryConf)
83 * @return string Class name
84 */
85 public static function getLBFactoryClass( array $config ) {
86 // For configuration backward compatibility after removing
87 // underscores from class names in MediaWiki 1.23.
88 $bcClasses = [
89 'LBFactory_Simple' => 'LBFactorySimple',
90 'LBFactory_Single' => 'LBFactorySingle',
91 'LBFactory_Multi' => 'LBFactoryMulti'
92 ];
93
94 $class = $config['class'];
95
96 if ( isset( $bcClasses[$class] ) ) {
97 $class = $bcClasses[$class];
98 wfDeprecated(
99 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
100 '1.23'
101 );
102 }
103
104 return $class;
105 }
106
107 /**
108 * @return bool
109 * @since 1.27
110 * @deprecated Since 1.28; use laggedReplicaUsed()
111 */
112 public function laggedSlaveUsed() {
113 return $this->laggedReplicaUsed();
114 }
115
116 protected function newChronologyProtector() {
117 $request = RequestContext::getMain()->getRequest();
118 $chronProt = new ChronologyProtector(
119 ObjectCache::getMainStashInstance(),
120 [
121 'ip' => $request->getIP(),
122 'agent' => $request->getHeader( 'User-Agent' ),
123 ],
124 $request->getFloat( 'cpPosTime', $request->getCookie( 'cpPosTime', '' ) )
125 );
126 if ( PHP_SAPI === 'cli' ) {
127 $chronProt->setEnabled( false );
128 } elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) {
129 // Request opted out of using position wait logic. This is useful for requests
130 // done by the job queue or background ETL that do not have a meaningful session.
131 $chronProt->setWaitEnabled( false );
132 }
133
134 return $chronProt;
135 }
136
137 /**
138 * Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed
139 *
140 * Note that unlike cookies, this works accross domains
141 *
142 * @param string $url
143 * @param float $time UNIX timestamp just before shutdown() was called
144 * @return string
145 * @since 1.28
146 */
147 public function appendPreShutdownTimeAsQuery( $url, $time ) {
148 $usedCluster = 0;
149 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$usedCluster ) {
150 $usedCluster |= ( $lb->getServerCount() > 1 );
151 } );
152
153 if ( !$usedCluster ) {
154 return $url; // no master/replica clusters touched
155 }
156
157 return wfAppendQuery( $url, [ 'cpPosTime' => $time ] );
158 }
159 }