Merge "Move updater/installer specific methods out of DatabaseBase"
[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 $defaults = [
41 'domain' => wfWikiID(),
42 'hostname' => wfHostname(),
43 'trxProfiler' => Profiler::instance()->getTransactionProfiler(),
44 'replLogger' => LoggerFactory::getInstance( 'DBReplication' ),
45 'queryLogger' => LoggerFactory::getInstance( 'wfLogDBError' ),
46 'connLogger' => LoggerFactory::getInstance( 'wfLogDBError' ),
47 'perfLogger' => LoggerFactory::getInstance( 'DBPerformance' ),
48 'errorLogger' => [ MWExceptionHandler::class, 'logException' ]
49 ];
50 // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
51 $sCache = ObjectCache::getLocalServerInstance();
52 if ( $sCache->getQoS( $sCache::ATTR_EMULATION ) > $sCache::QOS_EMULATION_SQL ) {
53 $defaults['srvCache'] = $sCache;
54 }
55 $cCache = ObjectCache::getLocalClusterInstance();
56 if ( $cCache->getQoS( $cCache::ATTR_EMULATION ) > $cCache::QOS_EMULATION_SQL ) {
57 $defaults['memCache'] = $cCache;
58 }
59 $wCache = ObjectCache::getMainWANInstance();
60 if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
61 $defaults['wanCache'] = $wCache;
62 }
63
64 parent::__construct( $conf + $defaults );
65 }
66
67 /**
68 * Returns the LBFactory class to use and the load balancer configuration.
69 *
70 * @todo instead of this, use a ServiceContainer for managing the different implementations.
71 *
72 * @param array $config (e.g. $wgLBFactoryConf)
73 * @return string Class name
74 */
75 public static function getLBFactoryClass( array $config ) {
76 // For configuration backward compatibility after removing
77 // underscores from class names in MediaWiki 1.23.
78 $bcClasses = [
79 'LBFactory_Simple' => 'LBFactorySimple',
80 'LBFactory_Single' => 'LBFactorySingle',
81 'LBFactory_Multi' => 'LBFactoryMulti'
82 ];
83
84 $class = $config['class'];
85
86 if ( isset( $bcClasses[$class] ) ) {
87 $class = $bcClasses[$class];
88 wfDeprecated(
89 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
90 '1.23'
91 );
92 }
93
94 return $class;
95 }
96
97 /**
98 * @return bool
99 * @since 1.27
100 * @deprecated Since 1.28; use laggedReplicaUsed()
101 */
102 public function laggedSlaveUsed() {
103 return $this->laggedReplicaUsed();
104 }
105
106 protected function newChronologyProtector() {
107 $request = RequestContext::getMain()->getRequest();
108 $chronProt = new ChronologyProtector(
109 ObjectCache::getMainStashInstance(),
110 [
111 'ip' => $request->getIP(),
112 'agent' => $request->getHeader( 'User-Agent' ),
113 ],
114 $request->getFloat( 'cpPosTime', $request->getCookie( 'cpPosTime', '' ) )
115 );
116 if ( PHP_SAPI === 'cli' ) {
117 $chronProt->setEnabled( false );
118 } elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) {
119 // Request opted out of using position wait logic. This is useful for requests
120 // done by the job queue or background ETL that do not have a meaningful session.
121 $chronProt->setWaitEnabled( false );
122 }
123
124 return $chronProt;
125 }
126
127 /**
128 * Append ?cpPosTime parameter to a URL for ChronologyProtector purposes if needed
129 *
130 * Note that unlike cookies, this works accross domains
131 *
132 * @param string $url
133 * @param float $time UNIX timestamp just before shutdown() was called
134 * @return string
135 * @since 1.28
136 */
137 public function appendPreShutdownTimeAsQuery( $url, $time ) {
138 $usedCluster = 0;
139 $this->forEachLB( function ( LoadBalancer $lb ) use ( &$usedCluster ) {
140 $usedCluster |= ( $lb->getServerCount() > 1 );
141 } );
142
143 if ( !$usedCluster ) {
144 return $url; // no master/replica clusters touched
145 }
146
147 return wfAppendQuery( $url, [ 'cpPosTime' => $time ] );
148 }
149 }