It's often the case that the best algorithm to use for a given task
depends on whether or not replication is in use. Due to our unabashed
Wikipedia-centrism, we often just use the replication-friendly version,
-but if you like, you can use wfGetLB()->getServerCount() > 1 to
+but if you like, you can use LoadBalancer::getServerCount() > 1 to
check to see if replication is in use.
=== Lag ===
slave to catch up to that position before doing any reads from it. If
this wait times out, reads are allowed anyway, but the request is
considered to be in "lagged slave mode". Lagged slave mode can be
-checked by calling wfGetLB()->getLaggedReplicaMode(). The only
+checked by calling LoadBalancer::getLaggedReplicaMode(). The only
practical consequence at present is a warning displayed in the page
footer.
* $wgSharedSchema is the table schema for the shared database. It defaults to
* $wgDBmwschema.
*
- * @deprecated since 1.21 In new code, use the $wiki parameter to wfGetLB() to
- * access remote databases. Using wfGetLB() allows the shared database to
+ * @deprecated since 1.21 In new code, use the $wiki parameter to LBFactory::getMainLB() to
+ * access remote databases. Using LBFactory::getMainLB() allows the shared database to
* reside on separate servers to the wiki's own database, with suitable
* configuration of $wgLBFactoryConf.
*/
<?php
+use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
* @return IDatabase
*/
protected function getConnection( $id, $groups = [] ) {
- $loadBalancer = wfGetLB( $this->wiki );
+ $loadBalancer = $this->getLoadBalancer();
return $loadBalancer->getConnection( $id, $groups, $this->wiki );
}
/**
* Get the database type used for read operations.
*
- * @see wfGetLB
+ * @see MediaWikiServices::getDBLoadBalancer
*
* @since 1.21
*
* @return LoadBalancer The database load balancer object
*/
public function getLoadBalancer() {
- return wfGetLB( $this->wiki );
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ return $lbFactory->getMainLB( $this->wiki );
}
}
* @ingroup FileRepo
*/
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\IDatabase;
+use Wikimedia\Rdbms\LoadBalancer;
+
/**
* A foreign repository with a MediaWiki database accessible via the configured LBFactory
*
* @return IDatabase
*/
function getMasterDB() {
- return wfGetLB( $this->wiki )->getConnectionRef( DB_MASTER, [], $this->wiki );
+ return $this->getDBLoadBalancer()->getConnectionRef( DB_MASTER, [], $this->wiki );
}
/**
* @return IDatabase
*/
function getReplicaDB() {
- return wfGetLB( $this->wiki )->getConnectionRef( DB_REPLICA, [], $this->wiki );
+ return $this->getDBLoadBalancer()->getConnectionRef( DB_REPLICA, [], $this->wiki );
}
/**
*/
protected function getDBFactory() {
return function ( $index ) {
- return wfGetLB( $this->wiki )->getConnectionRef( $index, [], $this->wiki );
+ return $this->getDBLoadBalancer()->getConnectionRef( $index, [], $this->wiki );
};
}
+ /**
+ * @return LoadBalancer
+ */
+ protected function getDBLoadBalancer() {
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ return $lbFactory->getMainLB( $this->wiki );
+ }
+
function hasSharedCache() {
return $this->hasSharedCache;
}
protected $mAPI;
/** @var string The name of the database (for a connection to be established
- * with wfGetLB( 'wikiid' ))
+ * with LBFactory::getMainLB( 'wikiid' ))
*/
protected $mWikiID;
break;
case 'actor':
// Make sure this thread sees its own changes
- if ( wfGetLB()->hasOrMadeRecentMasterChanges() ) {
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+ if ( $lb->hasOrMadeRecentMasterChanges() ) {
$flags |= self::READ_LATEST;
$this->queryFlagsUsed = $flags;
}
require_once __DIR__ . '/Maintenance.php';
+use MediaWiki\MediaWikiServices;
+
/**
* Maintenance script that reports the hostname of a replica DB server.
*
$db = $this->getDB( DB_REPLICA, $this->getOption( 'group' ) );
$host = $db->getServer();
} else {
- $lb = wfGetLB();
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
$i = $lb->getReaderIndex();
$host = $lb->getServerName( $i );
}
require_once __DIR__ . '/Maintenance.php';
+use MediaWiki\MediaWikiServices;
+
class InitEditCount extends Maintenance {
public function __construct() {
parent::__construct();
} elseif ( $this->hasOption( 'quick' ) ) {
$backgroundMode = false;
} else {
- $backgroundMode = wfGetLB()->getServerCount() > 1;
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+ $backgroundMode = $lb->getServerCount() > 1;
}
$actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
require_once __DIR__ . '/Maintenance.php';
+use MediaWiki\MediaWikiServices;
+
/**
* Maintenance script to show database lag.
*
}
public function execute() {
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
if ( $this->hasOption( 'r' ) ) {
- $lb = wfGetLB();
echo 'time ';
$serverCount = $lb->getServerCount();
sleep( 5 );
}
} else {
- $lb = wfGetLB();
$lags = $lb->getLagTimes();
foreach ( $lags as $i => $lag ) {
$name = $lb->getServerName( $i );
-- The URL of the file api.php
iw_api nvarchar(max) NOT NULL,
- -- The name of the database (for a connection to be established with wfGetLB( 'wikiid' ))
+ -- The name of the database (for a connection to be established with LBFactory::getMainLB( 'wikiid' ))
iw_wikiid nvarchar(64) NOT NULL,
-- A boolean value indicating whether the wiki is in this project
-- The URL of the file api.php
iw_api blob NOT NULL,
- -- The name of the database (for a connection to be established with wfGetLB( 'wikiid' ))
+ -- The name of the database (for a connection to be established with LBFactory::getMainLB( 'wikiid' ))
iw_wikiid varchar(64) NOT NULL,
-- A boolean value indicating whether the wiki is in this project
require_once __DIR__ . '/Maintenance.php';
+use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\DBReplicationWaitError;
+
/**
* Maintenance script to update cached special pages.
*
* mysql connection to "go away"
*/
private function reopenAndWaitForReplicas() {
- if ( !wfGetLB()->pingAll() ) {
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ $lb = $lbFactory->getMainLB();
+ if ( !$lb->pingAll() ) {
$this->output( "\n" );
do {
$this->error( "Connection failed, reconnecting in 10 seconds..." );
sleep( 10 );
- } while ( !wfGetLB()->pingAll() );
+ } while ( !$lb->pingAll() );
$this->output( "Reconnected\n\n" );
}
- # Wait for the replica DB to catch up
- wfWaitForSlaves();
+ // Wait for the replica DB to catch up
+ try {
+ $lbFactory->waitForReplication();
+ } catch ( DBReplicationWaitError $e ) {
+ // ignore
+ }
}
public function doSpecialPageCacheUpdates( $dbw ) {
$teardown[] = $this->markSetupDone( 'setupDatabase' );
# CREATE TEMPORARY TABLE breaks if there is more than one server
- if ( wfGetLB()->getServerCount() != 1 ) {
+ if ( MediaWikiServices::getInstance()->getDBLoadBalancer()->getServerCount() != 1 ) {
$this->useTemporaryTables = false;
}
*/
public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
- $blobStore = new SqlBlobStore( wfGetLB(), $cache );
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+ $blobStore = new SqlBlobStore( $lb, $cache );
$blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
$factory = $this->getMockBuilder( BlobStoreFactory::class )
*/
public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $blobStore = new SqlBlobStore( wfGetLB(), $cache );
+ $blobStore = new SqlBlobStore( $lb, $cache );
$blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) );
- $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
+ $store = $this->getRevisionStore( $lb, $blobStore, $cache );
$record = $store->newRevisionFromRow(
$this->makeRow( $row ),
];
$cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $blobStore = new SqlBlobStore( wfGetLB(), $cache );
+ $blobStore = new SqlBlobStore( $lb, $cache );
$blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
- $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
+ $store = $this->getRevisionStore( $lb, $blobStore, $cache );
$record = $store->newRevisionFromRow(
$this->makeRow( $row ),
<?php
+use MediaWiki\MediaWikiServices;
+
/**
* Tests for the DBSiteStore class.
*
private function newDBSiteStore() {
// NOTE: Use the real DB load balancer for now. Eventually, the test framework should
// provide a LoadBalancer that is safe to use in unit tests.
- return new DBSiteStore( wfGetLB() );
+ $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+ return new DBSiteStore( $lb );
}
/**