/** @var int Default 'maxLag' when unspecified */
const MAX_LAG_DEFAULT = 10;
+ /** @var int Default 'waitTimeout' when unspecified */
+ const MAX_WAIT_DEFAULT = 10;
/** @var int Seconds to cache master server read-only status */
const TTL_CACHE_READONLY = 5;
: DatabaseDomain::newUnspecified();
$this->setLocalDomain( $localDomain );
- $this->mWaitTimeout = isset( $params['waitTimeout'] ) ? $params['waitTimeout'] : 10;
+ $this->mWaitTimeout = isset( $params['waitTimeout'] )
+ ? $params['waitTimeout']
+ : self::MAX_WAIT_DEFAULT;
$this->mReadIndex = -1;
$this->mConns = [
for ( $i = 1; $i < $serverCount; $i++ ) {
if ( $this->mLoads[$i] > 0 ) {
$start = microtime( true );
- $ok = $this->doWait( $i, true, max( 1, (int)$timeout ) ) && $ok;
+ $ok = $this->doWait( $i, true, $timeout ) && $ok;
$timeout -= ( microtime( true ) - $start );
if ( $timeout <= 0 ) {
break; // timeout reached
* @return bool
*/
protected function doWait( $index, $open = false, $timeout = null ) {
- $close = false; // close the connection afterwards
+ $timeout = max( 1, $timeout ?: $this->mWaitTimeout );
// Check if we already know that the DB has reached this point
$server = $this->getServerName( $index );
$knownReachedPos instanceof DBMasterPos &&
$knownReachedPos->hasReached( $this->mWaitForPos )
) {
- $this->replLogger->debug( __METHOD__ .
+ $this->replLogger->debug(
+ __METHOD__ .
': replica DB {dbserver} known to be caught up (pos >= $knownReachedPos).',
- [ 'dbserver' => $server ] );
+ [ 'dbserver' => $server ]
+ );
return true;
}
// Find a connection to wait on, creating one if needed and allowed
+ $close = false; // close the connection afterwards
$conn = $this->getAnyOpenConnection( $index );
if ( !$conn ) {
if ( !$open ) {
- $this->replLogger->debug( __METHOD__ . ': no connection open for {dbserver}',
- [ 'dbserver' => $server ] );
+ $this->replLogger->debug(
+ __METHOD__ . ': no connection open for {dbserver}',
+ [ 'dbserver' => $server ]
+ );
return false;
} else {
$conn = $this->openConnection( $index, self::DOMAIN_ANY );
if ( !$conn ) {
- $this->replLogger->warning( __METHOD__ . ': failed to connect to {dbserver}',
- [ 'dbserver' => $server ] );
+ $this->replLogger->warning(
+ __METHOD__ . ': failed to connect to {dbserver}',
+ [ 'dbserver' => $server ]
+ );
return false;
}
}
}
- $this->replLogger->info( __METHOD__ . ': Waiting for replica DB {dbserver} to catch up...',
- [ 'dbserver' => $server ] );
- $timeout = $timeout ?: $this->mWaitTimeout;
+ $this->replLogger->info(
+ __METHOD__ .
+ ': Waiting for replica DB {dbserver} to catch up...',
+ [ 'dbserver' => $server ]
+ );
+
$result = $conn->masterPosWait( $this->mWaitForPos, $timeout );
- if ( $result == -1 || is_null( $result ) ) {
- // Timed out waiting for replica DB, use master instead
+ if ( $result === null ) {
+ $this->replLogger->warning(
+ __METHOD__ . ': Errored out waiting on {host} pos {pos}',
+ [
+ 'host' => $server,
+ 'pos' => $this->mWaitForPos,
+ 'trace' => ( new RuntimeException() )->getTraceAsString()
+ ]
+ );
+ $ok = false;
+ } elseif ( $result == -1 ) {
$this->replLogger->warning(
__METHOD__ . ': Timed out waiting on {host} pos {pos}',
- [ 'host' => $server, 'pos' => $this->mWaitForPos ]
+ [
+ 'host' => $server,
+ 'pos' => $this->mWaitForPos,
+ 'trace' => ( new RuntimeException() )->getTraceAsString()
+ ]
);
$ok = false;
} else {
// Use the local domain table prefix if the local domain is specified
$server['tablePrefix'] = $this->localDomain->getTablePrefix();
}
- $conn = $this->reallyOpenConnection( $server, $this->localDomain->getDatabase() );
+ $conn = $this->reallyOpenConnection( $server, $this->localDomain );
$host = $this->getServerName( $i );
if ( $conn->isOpen() ) {
$this->connLogger->debug( "Connected to database $i at '$host'." );
$server['foreignPoolRefCount'] = 0;
$server['foreign'] = true;
$server['autoCommitOnly'] = $autoCommit;
- $conn = $this->reallyOpenConnection( $server, $dbName );
+ $conn = $this->reallyOpenConnection( $server, $domainInstance );
if ( !$conn->isOpen() ) {
$this->connLogger->warning( __METHOD__ . ": connection error for $i/$domain" );
$this->errorConnection = $conn;
* Returns a Database object whether or not the connection was successful.
*
* @param array $server
- * @param string|null $dbNameOverride Use "" to not select any database
+ * @param DatabaseDomain $domainOverride Use an unspecified domain to not select any database
* @return Database
* @throws DBAccessError
* @throws InvalidArgumentException
*/
- protected function reallyOpenConnection( array $server, $dbNameOverride ) {
+ protected function reallyOpenConnection( array $server, DatabaseDomain $domainOverride ) {
if ( $this->disabled ) {
throw new DBAccessError();
}
- if ( $dbNameOverride !== null ) {
- $server['dbname'] = $dbNameOverride;
+ if ( $domainOverride->getDatabase() !== null ) {
+ $server['dbname'] = $domainOverride->getDatabase();
+ $server['schema'] = $domainOverride->getSchema();
}
// Let the handle know what the cluster master is (e.g. "db1052")
$result = $conn->masterPosWait( $pos, $timeout );
if ( $result == -1 || is_null( $result ) ) {
$msg = __METHOD__ . ': Timed out waiting on {host} pos {pos}';
- $this->replLogger->warning( $msg,
- [ 'host' => $conn->getServer(), 'pos' => $pos ] );
+ $this->replLogger->warning( $msg, [
+ 'host' => $conn->getServer(),
+ 'pos' => $pos,
+ 'trace' => ( new RuntimeException() )->getTraceAsString()
+ ] );
$ok = false;
} else {
$this->replLogger->info( __METHOD__ . ': Done' );
}
} else {
$ok = false; // something is misconfigured
- $this->replLogger->error( 'Could not get master pos for {host}',
- [ 'host' => $conn->getServer() ] );
+ $this->replLogger->error(
+ __METHOD__ . ': could not get master pos for {host}',
+ [
+ 'host' => $conn->getServer(),
+ 'trace' => ( new RuntimeException() )->getTraceAsString()
+ ]
+ );
}
return $ok;
$oldDomain = $this->localDomain->getId();
$this->setLocalDomain( new DatabaseDomain(
$this->localDomain->getDatabase(),
- null,
+ $this->localDomain->getSchema(),
$prefix
) );