}
public function getMasterPos() {
- # If this entire request was served from a replica DB without opening a connection to the
- # master (however unlikely that may be), then we can fetch the position from the replica DB.
+ $index = $this->getWriterIndex();
+
+ $conn = $this->getAnyOpenConnection( $index );
+ if ( $conn ) {
+ return $conn->getMasterPos();
+ }
+
+ $conn = $this->getConnection( $index, self::CONN_SILENCE_ERRORS );
+ if ( !$conn ) {
+ $this->reportConnectionError();
+ return null; // unreachable due to exception
+ }
+
+ try {
+ $pos = $conn->getMasterPos();
+ } finally {
+ $this->closeConnection( $conn );
+ }
+
+ return $pos;
+ }
+
+ public function getReplicaResumePos() {
+ // Get the position of any existing master server connection
$masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
- if ( !$masterConn ) {
- $serverCount = $this->getServerCount();
- for ( $i = 1; $i < $serverCount; $i++ ) {
- $conn = $this->getAnyOpenConnection( $i );
- if ( $conn ) {
- return $conn->getReplicaPos();
- }
- }
- } else {
+ if ( $masterConn ) {
return $masterConn->getMasterPos();
}
- return false;
+ // Get the highest position of any existing replica server connection
+ $highestPos = false;
+ $serverCount = $this->getServerCount();
+ for ( $i = 1; $i < $serverCount; $i++ ) {
+ if ( !empty( $this->servers[$i]['is static'] ) ) {
+ continue; // server does not use replication
+ }
+
+ $conn = $this->getAnyOpenConnection( $i );
+ $pos = $conn ? $conn->getReplicaPos() : false;
+ if ( !$pos ) {
+ continue; // no open connection or could not get position
+ }
+
+ $highestPos = $highestPos ?: $pos;
+ if ( $pos->hasReached( $highestPos ) ) {
+ $highestPos = $pos;
+ }
+ }
+
+ return $highestPos;
}
public function disable() {
}
/**
- * @covers LoadBalancer::getLocalDomainID()
- * @covers LoadBalancer::resolveDomainID()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getLocalDomainID()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::resolveDomainID()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::haveIndex()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::isNonZeroLoad()
*/
public function testWithoutReplica() {
global $wgDBname;
}
] );
+ $this->assertEquals( 1, $lb->getServerCount() );
+ $this->assertFalse( $lb->hasReplicaServers() );
+ $this->assertFalse( $lb->hasStreamingReplicaServers() );
+
+ $this->assertTrue( $lb->haveIndex( 0 ) );
+ $this->assertFalse( $lb->haveIndex( 1 ) );
+ $this->assertFalse( $lb->isNonZeroLoad( 0 ) );
+ $this->assertFalse( $lb->isNonZeroLoad( 1 ) );
+
$ld = DatabaseDomain::newFromId( $lb->getLocalDomainID() );
$this->assertEquals( $wgDBname, $ld->getDatabase(), 'local domain DB set' );
$this->assertEquals( $this->dbPrefix(), $ld->getTablePrefix(), 'local domain prefix set' );
$lb->closeAll();
}
+ /**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getReaderIndex()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getWriterIndex()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::haveIndex()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::isNonZeroLoad()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getServerName()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getServerInfo()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getServerType()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getServerAttributes()
+ */
public function testWithReplica() {
global $wgDBserver;
$this->assertTrue( $lb->hasReplicaServers() );
$this->assertTrue( $lb->hasStreamingReplicaServers() );
+ $this->assertTrue( $lb->haveIndex( 0 ) );
+ $this->assertTrue( $lb->haveIndex( 1 ) );
+ $this->assertFalse( $lb->isNonZeroLoad( 0 ) );
+ $this->assertTrue( $lb->isNonZeroLoad( 1 ) );
+
+ for ( $i = 0; $i < $lb->getServerCount(); ++$i ) {
+ $this->assertType( 'string', $lb->getServerName( $i ) );
+ $this->assertType( 'array', $lb->getServerInfo( $i ) );
+ $this->assertType( 'string', $lb->getServerType( $i ) );
+ $this->assertType( 'array', $lb->getServerAttributes( $i ) );
+ }
+
$dbw = $lb->getConnection( DB_MASTER );
$this->assertTrue( $dbw->getLBInfo( 'master' ), 'master shows as master' );
$this->assertEquals(
'cluster master set' );
$this->assertTrue( $dbr->getFlag( $dbw::DBO_TRX ), "DBO_TRX set on replica" );
$this->assertWriteForbidden( $dbr );
+ $this->assertEquals( $dbr->getLBInfo( 'serverIndex' ), $lb->getReaderIndex() );
if ( !$lb->getServerAttributes( $lb->getWriterIndex() )[$dbw::ATTR_DB_LEVEL_LOCKING] ) {
$dbwAuto = $lb->getConnection( DB_MASTER, [], false, $lb::CONN_TRX_AUTOCOMMIT );
}
/**
- * @covers LoadBalancer::openConnection()
- * @covers LoadBalancer::getAnyOpenConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::openConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getAnyOpenConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getWriterIndex()
*/
function testOpenConnection() {
$lb = $this->newSingleServerLocalLoadBalancer();
$lb->closeAll();
}
+ /**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::openConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getWriterIndex()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::forEachOpenMasterConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::setTransactionListener()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::beginMasterChanges()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::finalizeMasterChanges()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::approveMasterChanges()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::commitMasterChanges()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::runMasterTransactionIdleCallbacks()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::runMasterTransactionListenerCallbacks()
+ */
public function testTransactionCallbackChains() {
global $wgDBserver, $wgDBname, $wgDBuser, $wgDBpassword, $wgDBtype, $wgSQLiteDataDir;
$conn2->close();
}
+ /**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnectionRef
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnection()
+ */
public function testDBConnRefReadsMasterAndReplicaRoles() {
$lb = $this->newSingleServerLocalLoadBalancer();
}
/**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnectionRef
* @expectedException \Wikimedia\Rdbms\DBReadOnlyRoleError
*/
public function testDBConnRefWritesReplicaRole() {
}
/**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnectionRef
* @expectedException \Wikimedia\Rdbms\DBReadOnlyRoleError
*/
public function testDBConnRefWritesReplicaRoleIndex() {
}
/**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnectionRef
* @expectedException \Wikimedia\Rdbms\DBReadOnlyRoleError
*/
public function testDBConnRefWritesReplicaRoleInsert() {
$rConn->insert( 'test', [ 't' => 1 ], __METHOD__ );
}
+ /**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getConnection()
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getMaintenanceConnectionRef()
+ */
public function testQueryGroupIndex() {
$lb = $this->newMultiServerLocalLoadBalancer( [ 'defaultGroup' => false ] );
/** @var LoadBalancer $lbWrapper */
$rRC = $lb->getConnectionRef( DB_REPLICA, [ 'recentchanges' ] );
$rWL = $lb->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
+ $rRCMaint = $lb->getMaintenanceConnectionRef( DB_REPLICA, [ 'recentchanges' ] );
+ $rWLMaint = $lb->getMaintenanceConnectionRef( DB_REPLICA, [ 'watchlist' ] );
$this->assertEquals( 3, $rRC->getLBInfo( 'serverIndex' ) );
$this->assertEquals( 3, $rWL->getLBInfo( 'serverIndex' ) );
+ $this->assertEquals( 3, $rRCMaint->getLBInfo( 'serverIndex' ) );
+ $this->assertEquals( 3, $rWLMaint->getLBInfo( 'serverIndex' ) );
$rLog = $lb->getConnectionRef( DB_REPLICA, [ 'logging', 'watchlist' ] );
$logIndexPicked = $rLog->getLBInfo( 'serverIndex' );
$rGeneric = $lb->getConnectionRef( DB_REPLICA );
$this->assertEquals( $lb->getWriterIndex(), $rGeneric->getLBInfo( 'serverIndex' ) );
}
+
+ /**
+ * @covers \Wikimedia\Rdbms\LoadBalancer::getLazyConnectionRef
+ */
+ public function testGetLazyConnectionRef() {
+ $lb = $this->newMultiServerLocalLoadBalancer();
+
+ $rMaster = $lb->getLazyConnectionRef( DB_MASTER );
+ $rReplica = $lb->getLazyConnectionRef( 1 );
+ $this->assertFalse( $lb->getAnyOpenConnection( 0 ) );
+ $this->assertFalse( $lb->getAnyOpenConnection( 1 ) );
+
+ $rMaster->getType();
+ $rReplica->getType();
+ $rMaster->getDomainID();
+ $rReplica->getDomainID();
+ $this->assertFalse( $lb->getAnyOpenConnection( 0 ) );
+ $this->assertFalse( $lb->getAnyOpenConnection( 1 ) );
+
+ $rMaster->query( "SELECT 1", __METHOD__ );
+ $this->assertNotFalse( $lb->getAnyOpenConnection( 0 ) );
+
+ $rReplica->query( "SELECT 1", __METHOD__ );
+ $this->assertNotFalse( $lb->getAnyOpenConnection( 0 ) );
+ $this->assertNotFalse( $lb->getAnyOpenConnection( 1 ) );
+ }
}