/** @var int */
protected $syncTimeout = 3;
+ /** @var LoadBalancer|null */
+ protected $separateMainLB;
/** @var array */
protected $conns;
/** @var array UNIX timestamps */
$this->serverInfos = [ $params['server'] ];
$this->numServers = count( $this->serverInfos );
} else {
+ // Default to using the main wiki's database servers
$this->serverInfos = false;
$this->numServers = 1;
}
$this->slaveOnly = !empty( $params['slaveOnly'] );
}
+ protected function getSeparateMainLB() {
+ global $wgDBtype;
+
+ if ( $wgDBtype === 'mysql' && $this->usesMainDB() ) {
+ if ( !$this->separateMainLB ) {
+ // We must keep a separate connection to MySQL in order to avoid deadlocks
+ $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
+ $this->separateMainLB = $lbFactory->newMainLB();
+ }
+ return $this->separateMainLB;
+ } else {
+ // However, SQLite has an opposite behavior. And PostgreSQL needs to know
+ // if we are in transaction or not (@TODO: find some PostgreSQL work-around).
+ return null;
+ }
+ }
+
/**
* Get a connection to the specified database
*
$db = DatabaseBase::factory( $type, $info );
$db->clearFlag( DBO_TRX );
} else {
- // We must keep a separate connection to MySQL in order to avoid deadlocks
- // However, SQLite has an opposite behavior. And PostgreSQL needs to know
- // if we are in transaction or not (@TODO: find some work-around).
$index = $this->slaveOnly ? DB_SLAVE : DB_MASTER;
- if ( wfGetDB( $index )->getType() == 'mysql' ) {
- $lb = wfGetLBFactory()->newMainLB();
- $db = $lb->getConnection( $index );
+ if ( $this->getSeparateMainLB() ) {
+ $db = $this->getSeparateMainLB()->getConnection( $index );
$db->clearFlag( DBO_TRX ); // auto-commit mode
} else {
$db = wfGetDB( $index );
+ // Can't mess with transaction rounds (DBO_TRX) :(
}
}
$this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
protected function waitForSlaves() {
if ( $this->usesMainDB() ) {
+ $lb = $this->getSeparateMainLB()
+ ?: MediaWikiServices::getInstance()->getDBLoadBalancer();
// Main LB is used; wait for any slaves to catch up
try {
- $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
- $lbFactory->waitForReplication( [ 'wiki' => wfWikiID() ] );
- return true;
+ $pos = $lb->getMasterPos();
+ if ( $pos ) {
+ return $lb->waitForAll( $pos, 3 );
+ }
} catch ( DBReplicationWaitError $e ) {
return false;
}
- } else {
- // Custom DB server list; probably doesn't use replication
- return true;
}
+
+ // Custom DB server list; probably doesn't use replication
+ return true;
}
}