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 Psr\Log\LoggerInterface
;
27 use MediaWiki\Logger\LoggerFactory
;
30 * An interface for generating database load balancers
33 abstract class LBFactory
implements DestructibleService
{
35 /** @var ChronologyProtector */
38 /** @var TransactionProfiler */
39 protected $trxProfiler;
41 /** @var LoggerInterface */
44 /** @var string|bool Reason all LBs are read-only or false if not */
45 protected $readOnlyReason = false;
47 const SHUTDOWN_NO_CHRONPROT
= 1; // don't save ChronologyProtector positions (for async code)
50 * Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
53 public function __construct( array $conf ) {
54 if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
55 $this->readOnlyReason
= $conf['readOnlyReason'];
58 $this->chronProt
= $this->newChronologyProtector();
59 $this->trxProfiler
= Profiler
::instance()->getTransactionProfiler();
60 $this->logger
= LoggerFactory
::getInstance( 'DBTransaction' );
64 * Disables all load balancers. All connections are closed, and any attempt to
65 * open a new connection will result in a DBAccessError.
66 * @see LoadBalancer::disable()
68 public function destroy() {
70 $this->forEachLBCallMethod( 'disable' );
74 * Disables all access to the load balancer, will cause all database access
75 * to throw a DBAccessError
77 public static function disableBackend() {
78 MediaWikiServices
::disableStorageBackend();
82 * Get an LBFactory instance
84 * @deprecated since 1.27, use MediaWikiServices::getDBLoadBalancerFactory() instead.
88 public static function singleton() {
89 return MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
93 * Returns the LBFactory class to use and the load balancer configuration.
95 * @todo instead of this, use a ServiceContainer for managing the different implementations.
97 * @param array $config (e.g. $wgLBFactoryConf)
98 * @return string Class name
100 public static function getLBFactoryClass( array $config ) {
101 // For configuration backward compatibility after removing
102 // underscores from class names in MediaWiki 1.23.
104 'LBFactory_Simple' => 'LBFactorySimple',
105 'LBFactory_Single' => 'LBFactorySingle',
106 'LBFactory_Multi' => 'LBFactoryMulti',
107 'LBFactory_Fake' => 'LBFactoryFake',
110 $class = $config['class'];
112 if ( isset( $bcClasses[$class] ) ) {
113 $class = $bcClasses[$class];
115 '$wgLBFactoryConf must be updated. See RELEASE-NOTES for details',
124 * Shut down, close connections and destroy the cached instance.
126 * @deprecated since 1.27, use LBFactory::destroy()
128 public static function destroyInstance() {
129 self
::singleton()->destroy();
133 * Create a new load balancer object. The resulting object will be untracked,
134 * not chronology-protected, and the caller is responsible for cleaning it up.
136 * @param bool|string $wiki Wiki ID, or false for the current wiki
137 * @return LoadBalancer
139 abstract public function newMainLB( $wiki = false );
142 * Get a cached (tracked) load balancer object.
144 * @param bool|string $wiki Wiki ID, or false for the current wiki
145 * @return LoadBalancer
147 abstract public function getMainLB( $wiki = false );
150 * Create a new load balancer for external storage. The resulting object will be
151 * untracked, not chronology-protected, and the caller is responsible for
154 * @param string $cluster External storage cluster, or false for core
155 * @param bool|string $wiki Wiki ID, or false for the current wiki
156 * @return LoadBalancer
158 abstract protected function newExternalLB( $cluster, $wiki = false );
161 * Get a cached (tracked) load balancer for external storage
163 * @param string $cluster External storage cluster, or false for core
164 * @param bool|string $wiki Wiki ID, or false for the current wiki
165 * @return LoadBalancer
167 abstract public function &getExternalLB( $cluster, $wiki = false );
170 * Execute a function for each tracked load balancer
171 * The callback is called with the load balancer as the first parameter,
172 * and $params passed as the subsequent parameters.
174 * @param callable $callback
175 * @param array $params
177 abstract public function forEachLB( $callback, array $params = [] );
180 * Prepare all tracked load balancers for shutdown
181 * @param integer $flags Supports SHUTDOWN_* flags
184 public function shutdown( $flags = 0 ) {
188 * Call a method of each tracked load balancer
190 * @param string $methodName
193 private function forEachLBCallMethod( $methodName, array $args = [] ) {
195 function ( LoadBalancer
$loadBalancer, $methodName, array $args ) {
196 call_user_func_array( [ $loadBalancer, $methodName ], $args );
198 [ $methodName, $args ]
203 * Commit on all connections. Done for two reasons:
204 * 1. To commit changes to the masters.
205 * 2. To release the snapshot on all connections, master and slave.
206 * @param string $fname Caller name
208 public function commitAll( $fname = __METHOD__
) {
209 $this->logMultiDbTransaction();
211 $start = microtime( true );
212 $this->forEachLBCallMethod( 'commitAll', [ $fname ] );
213 $timeMs = 1000 * ( microtime( true ) - $start );
215 RequestContext
::getMain()->getStats()->timing( "db.commit-all", $timeMs );
219 * Commit changes on all master connections
220 * @param string $fname Caller name
221 * @param array $options Options map:
222 * - maxWriteDuration: abort if more than this much time was spent in write queries
224 public function commitMasterChanges( $fname = __METHOD__
, array $options = [] ) {
225 $limit = isset( $options['maxWriteDuration'] ) ?
$options['maxWriteDuration'] : 0;
227 $this->logMultiDbTransaction();
228 $this->forEachLB( function ( LoadBalancer
$lb ) use ( $limit ) {
229 $lb->forEachOpenConnection( function ( IDatabase
$db ) use ( $limit ) {
230 $time = $db->pendingWriteQueryDuration();
231 if ( $limit > 0 && $time > $limit ) {
232 throw new DBTransactionError(
234 wfMessage( 'transaction-duration-limit-exceeded', $time, $limit )->text()
240 $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
244 * Rollback changes on all master connections
245 * @param string $fname Caller name
248 public function rollbackMasterChanges( $fname = __METHOD__
) {
249 $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname ] );
253 * Log query info if multi DB transactions are going to be committed now
255 private function logMultiDbTransaction() {
257 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$callersByDB ) {
258 $masterName = $lb->getServerName( $lb->getWriterIndex() );
259 $callers = $lb->pendingMasterChangeCallers();
261 $callersByDB[$masterName] = $callers;
265 if ( count( $callersByDB ) >= 2 ) {
266 $dbs = implode( ', ', array_keys( $callersByDB ) );
267 $msg = "Multi-DB transaction [{$dbs}]:\n";
268 foreach ( $callersByDB as $db => $callers ) {
269 $msg .= "$db: " . implode( '; ', $callers ) . "\n";
271 $this->logger
->info( $msg );
276 * Determine if any master connection has pending changes
280 public function hasMasterChanges() {
282 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
283 $ret = $ret ||
$lb->hasMasterChanges();
290 * Detemine if any lagged slave connection was used
294 public function laggedSlaveUsed() {
296 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
297 $ret = $ret ||
$lb->laggedSlaveUsed();
304 * Determine if any master connection has pending/written changes from this request
308 public function hasOrMadeRecentMasterChanges() {
310 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$ret ) {
311 $ret = $ret ||
$lb->hasOrMadeRecentMasterChanges();
317 * Waits for the slave DBs to catch up to the current master position
319 * Use this when updating very large numbers of rows, as in maintenance scripts,
320 * to avoid causing too much lag. Of course, this is a no-op if there are no slaves.
322 * By default this waits on all DB clusters actually used in this request.
323 * This makes sense when lag being waiting on is caused by the code that does this check.
324 * In that case, setting "ifWritesSince" can avoid the overhead of waiting for clusters
325 * that were not changed since the last wait check. To forcefully wait on a specific cluster
326 * for a given wiki, use the 'wiki' parameter. To forcefully wait on an "external" cluster,
327 * use the "cluster" parameter.
329 * Never call this function after a large DB write that is *still* in a transaction.
330 * It only makes sense to call this after the possible lag inducing changes were committed.
332 * @param array $opts Optional fields that include:
333 * - wiki : wait on the load balancer DBs that handles the given wiki
334 * - cluster : wait on the given external load balancer DBs
335 * - timeout : Max wait time. Default: ~60 seconds
336 * - ifWritesSince: Only wait if writes were done since this UNIX timestamp
337 * @throws DBReplicationWaitError If a timeout or error occured waiting on a DB cluster
340 public function waitForReplication( array $opts = [] ) {
345 'ifWritesSince' => null
348 // Figure out which clusters need to be checked
349 /** @var LoadBalancer[] $lbs */
351 if ( $opts['cluster'] !== false ) {
352 $lbs[] = $this->getExternalLB( $opts['cluster'] );
353 } elseif ( $opts['wiki'] !== false ) {
354 $lbs[] = $this->getMainLB( $opts['wiki'] );
356 $this->forEachLB( function ( LoadBalancer
$lb ) use ( &$lbs ) {
360 return; // nothing actually used
364 // Get all the master positions of applicable DBs right now.
365 // This can be faster since waiting on one cluster reduces the
366 // time needed to wait on the next clusters.
367 $masterPositions = array_fill( 0, count( $lbs ), false );
368 foreach ( $lbs as $i => $lb ) {
369 if ( $lb->getServerCount() <= 1 ) {
370 // Bug 27975 - Don't try to wait for slaves if there are none
371 // Prevents permission error when getting master position
373 } elseif ( $opts['ifWritesSince']
374 && $lb->lastMasterChangeTimestamp() < $opts['ifWritesSince']
376 continue; // no writes since the last wait
378 $masterPositions[$i] = $lb->getMasterPos();
382 foreach ( $lbs as $i => $lb ) {
383 if ( $masterPositions[$i] ) {
384 // The DBMS may not support getMasterPos() or the whole
385 // load balancer might be fake (e.g. $wgAllDBsAreLocalhost).
386 if ( !$lb->waitForAll( $masterPositions[$i], $opts['timeout'] ) ) {
387 $failed[] = $lb->getServerName( $lb->getWriterIndex() );
393 throw new DBReplicationWaitError(
394 "Could not wait for slaves to catch up to " .
395 implode( ', ', $failed )
401 * Disable the ChronologyProtector for all load balancers
403 * This can be called at the start of special API entry points
407 public function disableChronologyProtection() {
408 $this->chronProt
->setEnabled( false );
412 * @return ChronologyProtector
414 protected function newChronologyProtector() {
415 $request = RequestContext
::getMain()->getRequest();
416 $chronProt = new ChronologyProtector(
417 ObjectCache
::getMainStashInstance(),
419 'ip' => $request->getIP(),
420 'agent' => $request->getHeader( 'User-Agent' )
423 if ( PHP_SAPI
=== 'cli' ) {
424 $chronProt->setEnabled( false );
425 } elseif ( $request->getHeader( 'ChronologyProtection' ) === 'false' ) {
426 // Request opted out of using position wait logic. This is useful for requests
427 // done by the job queue or background ETL that do not have a meaningful session.
428 $chronProt->setWaitEnabled( false );
435 * @param ChronologyProtector $cp
437 protected function shutdownChronologyProtector( ChronologyProtector
$cp ) {
438 // Get all the master positions needed
439 $this->forEachLB( function ( LoadBalancer
$lb ) use ( $cp ) {
440 $cp->shutdownLB( $lb );
442 // Write them to the stash
443 $unsavedPositions = $cp->shutdown();
444 // If the positions failed to write to the stash, at least wait on local datacenter
445 // slaves to catch up before responding. Even if there are several DCs, this increases
446 // the chance that the user will see their own changes immediately afterwards. As long
447 // as the sticky DC cookie applies (same domain), this is not even an issue.
448 $this->forEachLB( function ( LoadBalancer
$lb ) use ( $unsavedPositions ) {
449 $masterName = $lb->getServerName( $lb->getWriterIndex() );
450 if ( isset( $unsavedPositions[$masterName] ) ) {
451 $lb->waitForAll( $unsavedPositions[$masterName] );
457 * Close all open database connections on all open load balancers.
460 public function closeAll() {
461 $this->forEachLBCallMethod( 'closeAll', [] );
467 * Exception class for attempted DB access
469 class DBAccessError
extends MWException
{
470 public function __construct() {
471 parent
::__construct( "Mediawiki tried to access the database via wfGetDB(). " .
472 "This is not allowed, because database access has been disabled." );
477 * Exception class for replica DB wait timeouts
479 class DBReplicationWaitError
extends Exception
{