$host = $this->getServerName( $i );
if ( $lag === false && !is_infinite( $maxServerLag ) ) {
- $this->replLogger->error(
+ $this->replLogger->debug(
__METHOD__ .
": server {host} is not replicating?", [ 'host' => $host ] );
unset( $loads[$i] );
}
public function getAnyOpenConnection( $i, $flags = 0 ) {
+ $i = ( $i === self::DB_MASTER ) ? $this->getWriterIndex() : $i;
$autocommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
+
foreach ( $this->conns as $connsByServer ) {
- if ( !isset( $connsByServer[$i] ) ) {
- continue;
+ if ( $i === self::DB_REPLICA ) {
+ $indexes = array_keys( $connsByServer );
+ } else {
+ $indexes = isset( $connsByServer[$i] ) ? [ $i ] : [];
}
- foreach ( $connsByServer[$i] as $conn ) {
- if ( !$autocommit || $conn->getLBInfo( 'autoCommitOnly' ) ) {
- return $conn;
+ foreach ( $indexes as $index ) {
+ foreach ( $connsByServer[$index] as $conn ) {
+ if ( !$conn->isOpen() ) {
+ continue; // some sort of error occured?
+ }
+ if ( !$autocommit || $conn->getLBInfo( 'autoCommitOnly' ) ) {
+ return $conn;
+ }
}
}
}
}
unset( $this->conns[$connFreeKey][$i][$oldDomain] );
// Note that if $domain is an empty string, getDomainID() might not match it
- $this->conns[$connInUseKey][$i][$conn->getDomainId()] = $conn;
+ $this->conns[$connInUseKey][$i][$conn->getDomainID()] = $conn;
$this->connLogger->debug( __METHOD__ .
": reusing free connection from $oldDomain for $domain" );
break;
}
}
- /**
- * @param IDatabase $conn
- * @param DBMasterPos|bool $pos
- * @param int|null $timeout
- * @return bool
- */
public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = null ) {
$timeout = max( 1, $timeout ?: $this->waitTimeout );
$pos = $masterConn->getMasterPos();
} else {
$masterConn = $this->openConnection( $this->getWriterIndex(), self::DOMAIN_ANY );
+ if ( !$masterConn ) {
+ throw new DBReplicationWaitError(
+ null,
+ "Could not obtain a master database connection to get the position"
+ );
+ }
$pos = $masterConn->getMasterPos();
$this->closeConnection( $masterConn );
}