use EmptyBagOStuff;
use WANObjectCache;
use ArrayUtils;
+use LogicException;
use UnexpectedValueException;
use InvalidArgumentException;
use RuntimeException;
private $loadMonitorConfig;
/** @var string Alternate local DB domain instead of DatabaseDomain::getId() */
private $localDomainIdAlias;
- /** @var int */
+ /** @var int Amount of replication lag, in seconds, that is considered "high" */
private $maxLag;
+ /** @var string|bool The query group list to be used by default */
+ private $defaultGroup;
/** @var string Current server name */
private $hostname;
/** @var array[] Map of (name => callable) */
private $trxRecurringCallbacks = [];
- /** @var Database DB connection object that caused a problem */
+ /** @var Database Connection handle that caused a problem */
private $errorConnection;
- /** @var int The generic (not query grouped) replica DB index */
+ /** @var int The generic (not query grouped) replica server index */
private $genericReadIndex = -1;
- /** @var int[] The group replica DB indexes keyed by group */
+ /** @var int[] The group replica server indexes keyed by group */
private $readIndexByGroup = [];
/** @var bool|DBMasterPos Replication sync position or false if not set */
private $waitForPos;
/** @var bool Whether the generic reader fell back to a lagged replica DB */
private $laggedReplicaMode = false;
- /** @var bool Whether the generic reader fell back to a lagged replica DB */
- private $allReplicasDownMode = false;
/** @var string The last DB selection or connection error */
private $lastError = 'Unknown error';
- /** @var string|bool Reason the LB is read-only or false if not */
+ /** @var string|bool Reason this instance is read-only or false if not */
private $readOnlyReason = false;
/** @var int Total number of new connections ever made with this instance */
private $connectionCounter = 0;
/** @var bool Whether any connection has been attempted yet */
private $connectionAttempted = false;
- /** @var int|null An integer ID of the managing LBFactory instance or null */
+ /** @var int|null Integer ID of the managing LBFactory instance or null if none */
private $ownerId;
- /** @var string|bool String if a requested DBO_TRX transaction round is active */
+ /** @var string|bool Explicit DBO_TRX transaction round active or false if none */
private $trxRoundId = false;
/** @var string Stage of the current transaction round in the transaction round life-cycle */
private $trxRoundStage = self::ROUND_CURSORY;
- /** @var string|null */
- private $defaultGroup = null;
-
/** @var int Warn when this many connection are held */
const CONN_HELD_WARN_THRESHOLD = 10;
const MAX_LAG_DEFAULT = 6;
/** @var int Default 'waitTimeout' when unspecified */
const MAX_WAIT_DEFAULT = 10;
- /** @var int Seconds to cache master server read-only status */
+ /** @var int Seconds to cache master DB server read-only status */
const TTL_CACHE_READONLY = 5;
const KEY_LOCAL = 'local';
}
}
- $this->defaultGroup = $params['defaultGroup'] ?? null;
+ $this->defaultGroup = $params['defaultGroup'] ?? self::GROUP_GENERIC;
$this->ownerId = $params['ownerId'] ?? null;
}
}
/**
- * @param int $flags
- * @return bool
+ * @param string[]|string|bool $groups Query group list or false for the default
+ * @param int $i Specific server index or DB_MASTER/DB_REPLICA
+ * @return string[]|bool[] Query group list
+ */
+ private function resolveGroups( $groups, $i ) {
+ if ( $groups === false ) {
+ $resolvedGroups = [ $this->defaultGroup ];
+ } elseif ( is_string( $groups ) ) {
+ $resolvedGroups = [ $groups ];
+ } elseif ( is_array( $groups ) ) {
+ $resolvedGroups = $groups ?: [ $this->defaultGroup ];
+ } else {
+ throw new InvalidArgumentException( "Invalid query groups provided" );
+ }
+
+ if ( $groups !== [] && $groups !== false && $i > 0 ) {
+ $groupList = implode( ', ', $groups );
+ throw new LogicException( "Got query groups ($groupList) with a server index (#$i)" );
+ }
+
+ return $resolvedGroups;
+ }
+
+ /**
+ * @param int $flags Bitfield of class CONN_* constants
+ * @param int $i Specific server index or DB_MASTER/DB_REPLICA
+ * @return int Sanitized bitfield
*/
- private function sanitizeConnectionFlags( $flags ) {
- if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) === self::CONN_TRX_AUTOCOMMIT ) {
- // Assuming all servers are of the same type (or similar), which is overwhelmingly
- // the case, use the master server information to get the attributes. The information
- // for $i cannot be used since it might be DB_REPLICA, which might require connection
- // attempts in order to be resolved into a real server index.
+ private function sanitizeConnectionFlags( $flags, $i ) {
+ // Whether an outside caller is explicitly requesting the master database server
+ if ( $i === self::DB_MASTER || $i === $this->getWriterIndex() ) {
+ $flags |= self::CONN_INTENT_WRITABLE;
+ }
+
+ if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT ) {
+ // Callers use CONN_TRX_AUTOCOMMIT to bypass REPEATABLE-READ staleness without
+ // resorting to row locks (e.g. FOR UPDATE) or to make small out-of-band commits
+ // during larger transactions. This is useful for avoiding lock contention.
+
+ // Master DB server attributes (should match those of the replica DB servers)
$attributes = $this->getServerAttributes( $this->getWriterIndex() );
if ( $attributes[Database::ATTR_DB_LEVEL_LOCKING] ) {
- // Callers sometimes want to (a) escape REPEATABLE-READ stateness without locking
- // rows (e.g. FOR UPDATE) or (b) make small commits during a larger transactions
- // to reduce lock contention. None of these apply for sqlite and using separate
- // connections just causes self-deadlocks.
+ // The RDBMS does not support concurrent writes (e.g. SQLite), so attempts
+ // to use separate connections would just cause self-deadlocks. Note that
+ // REPEATABLE-READ staleness is not an issue since DB-level locking means
+ // that transactions are Strict Serializable anyway.
$flags &= ~self::CONN_TRX_AUTOCOMMIT;
- $this->connLogger->info( __METHOD__ .
- ': ignoring CONN_TRX_AUTOCOMMIT to avoid deadlocks.' );
+ $type = $this->getServerType( $this->getWriterIndex() );
+ $this->connLogger->info( __METHOD__ . ": CONN_TRX_AUTOCOMMIT disallowed ($type)" );
}
}
}
}
- /**
+ /**
* Get a LoadMonitor instance
*
* @return ILoadMonitor
}
/**
- * @param int $i
- * @param array $groups
+ * Get the server index to use for a specified server index and query group list
+ *
+ * @param int $i Specific server index or DB_MASTER/DB_REPLICA
+ * @param string[]|bool[] $groups Resolved query group list (non-empty)
* @param string|bool $domain
- * @return int The index of a specific server (replica DBs are checked for connectivity)
+ * @return int A specific server index (replica DBs are checked for connectivity)
*/
- private function getConnectionIndex( $i, $groups, $domain ) {
- // Check one "group" per default: the generic pool
- $defaultGroups = $this->defaultGroup ? [ $this->defaultGroup ] : [ false ];
-
- $groups = ( $groups === false || $groups === [] )
- ? $defaultGroups
- : (array)$groups;
-
+ private function getConnectionIndex( $i, array $groups, $domain ) {
if ( $i === self::DB_MASTER ) {
$i = $this->getWriterIndex();
} elseif ( $i === self::DB_REPLICA ) {
- # Try to find an available server in any the query groups (in order)
+ // Find an available server in any of the query groups (in order)
foreach ( $groups as $group ) {
$groupIndex = $this->getReaderIndex( $group, $domain );
if ( $groupIndex !== false ) {
- $i = $groupIndex;
+ $i = $groupIndex; // group connection succeeded
break;
}
}
+ } elseif ( !isset( $this->servers[$i] ) ) {
+ throw new UnexpectedValueException( "Invalid server index index #$i" );
}
- # Operation-based index
if ( $i === self::DB_REPLICA ) {
- $this->lastError = 'Unknown error'; // reset error string
- # Try the general server pool if $groups are unavailable.
- $i = ( $groups === [ false ] )
- ? false // don't bother with this if that is what was tried above
- : $this->getReaderIndex( false, $domain );
- # Couldn't find a working server in getReaderIndex()?
+ // No specific server was yet found
+ $this->lastError = 'Unknown error'; // set here in case of worse failure
+ // Either make one last connection attempt or give up
+ $i = in_array( $this->defaultGroup, $groups, true )
+ // Connection attempt already included the default query group; give up
+ ? false
+ // Connection attempt was for other query groups; try the default one
+ : $this->getReaderIndex( $this->defaultGroup, $domain );
+
if ( $i === false ) {
+ // Still coundn't find a working non-zero read load server
$this->lastError = 'No working replica DB server: ' . $this->lastError;
- // Throw an exception
$this->reportConnectionError();
return null; // unreachable due to exception
}
return $index;
}
- if ( $group !== false ) {
+ if ( $group !== self::GROUP_GENERIC ) {
// Use the server weight array for this load group
if ( isset( $this->groupLoads[$group] ) ) {
$loads = $this->groupLoads[$group];
// Cache the reader index for future DB_REPLICA handles
$this->setExistingReaderIndex( $group, $i );
// Record whether the generic reader index is in "lagged replica DB" mode
- if ( $group === false && $laggedReplicaMode ) {
+ if ( $group === self::GROUP_GENERIC && $laggedReplicaMode ) {
$this->laggedReplicaMode = true;
}
* @return int Server index or -1 if none was chosen
*/
protected function getExistingReaderIndex( $group ) {
- if ( $group === false ) {
+ if ( $group === self::GROUP_GENERIC ) {
$index = $this->genericReadIndex;
} else {
$index = $this->readIndexByGroup[$group] ?? -1;
throw new UnexpectedValueException( "Cannot set a negative read server index" );
}
- if ( $group === false ) {
+ if ( $group === self::GROUP_GENERIC ) {
$this->genericReadIndex = $index;
} else {
$this->readIndexByGroup[$group] = $index;
/**
* @param array $loads List of server weights
* @param string|bool $domain
- * @return array (reader index, lagged replica mode) or false on failure
+ * @return array (reader index, lagged replica mode) or (false, false) on failure
*/
private function pickReaderIndex( array $loads, $domain = false ) {
if ( $loads === [] ) {
$serverName = $this->getServerName( $i );
$this->connLogger->debug( __METHOD__ . ": Using reader #$i: $serverName..." );
- $conn = $this->getConnection( $i, [], $domain, self::CONN_SILENCE_ERRORS );
+ // Get a connection to this server without triggering other server connections
+ $flags = self::CONN_SILENCE_ERRORS;
+ $conn = $this->getServerConnection( $i, $domain, $flags );
if ( !$conn ) {
$this->connLogger->warning( __METHOD__ . ": Failed connecting to $i/$domain" );
unset( $currentLoads[$i] ); // avoid this server next iteration
public function getAnyOpenConnection( $i, $flags = 0 ) {
$i = ( $i === self::DB_MASTER ) ? $this->getWriterIndex() : $i;
+ // Connection handles required to be in auto-commit mode use a separate connection
+ // pool since the main pool is effected by implicit and explicit transaction rounds
$autocommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
+ $conn = false;
foreach ( $this->conns as $connsByServer ) {
+ // Get the connection array server indexes to inspect
if ( $i === self::DB_REPLICA ) {
$indexes = array_keys( $connsByServer );
} else {
}
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;
- }
+ $conn = $this->pickAnyOpenConnection( $connsByServer[$index], $autocommit );
+ if ( $conn ) {
+ break;
}
}
}
- return false;
+ if ( $conn ) {
+ $this->enforceConnectionFlags( $conn, $flags );
+ }
+
+ return $conn;
+ }
+
+ /**
+ * @param IDatabase[] $candidateConns
+ * @param bool $autocommit Whether to only look for auto-commit connections
+ * @return IDatabase|false An appropriate open connection or false if none found
+ */
+ private function pickAnyOpenConnection( $candidateConns, $autocommit ) {
+ $conn = false;
+
+ foreach ( $candidateConns as $candidateConn ) {
+ if ( !$candidateConn->isOpen() ) {
+ continue; // some sort of error occured?
+ } elseif (
+ $autocommit &&
+ (
+ // Connection is transaction round aware
+ !$candidateConn->getLBInfo( 'autoCommitOnly' ) ||
+ // Some sort of error left a transaction open?
+ $candidateConn->trxLevel()
+ )
+ ) {
+ continue; // some sort of error left a transaction open?
+ }
+
+ $conn = $candidateConn;
+ }
+
+ return $conn;
}
/**
* Wait for a given replica DB to catch up to the master pos stored in "waitForPos"
- * @param int $index Server index
+ * @param int $index Specific server index
* @param bool $open Check the server even if a new connection has to be made
* @param int|null $timeout Max seconds to wait; default is "waitTimeout"
* @return bool
// Find a connection to wait on, creating one if needed and allowed
$close = false; // close the connection afterwards
- $conn = $this->getAnyOpenConnection( $index );
+ $flags = self::CONN_SILENCE_ERRORS;
+ $conn = $this->getAnyOpenConnection( $index, $flags );
if ( !$conn ) {
if ( !$open ) {
$this->replLogger->debug(
return false;
}
- // Open a temporary new connection in order to wait for replication
- $conn = $this->getConnection( $index, [], self::DOMAIN_ANY, self::CONN_SILENCE_ERRORS );
+ // Get a connection to this server without triggering other server connections
+ $conn = $this->getServerConnection( $index, self::DOMAIN_ANY, $flags );
if ( !$conn ) {
$this->replLogger->warning(
__METHOD__ . ': failed to connect to {dbserver}',
}
public function getConnection( $i, $groups = [], $domain = false, $flags = 0 ) {
- if ( !is_int( $i ) ) {
- throw new InvalidArgumentException( "Cannot connect without a server index" );
- } elseif ( $groups && $i > 0 ) {
- throw new InvalidArgumentException( "Got query groups with server index #$i" );
+ $domain = $this->resolveDomainID( $domain );
+ $groups = $this->resolveGroups( $groups, $i );
+ $flags = $this->sanitizeConnectionFlags( $flags, $i );
+ // If given DB_MASTER/DB_REPLICA, resolve it to a specific server index. Resolving
+ // DB_REPLICA might trigger getServerConnection() calls due to the getReaderIndex()
+ // connectivity checks or LoadMonitor::scaleLoads() server state cache regeneration.
+ // The use of getServerConnection() instead of getConnection() avoids infinite loops.
+ $serverIndex = $this->getConnectionIndex( $i, $groups, $domain );
+ // Get an open connection to that server (might trigger a new connection)
+ $conn = $this->getServerConnection( $serverIndex, $domain, $flags );
+ // Set master DB handles as read-only if there is high replication lag
+ if ( $serverIndex === $this->getWriterIndex() && $this->getLaggedReplicaMode( $domain ) ) {
+ $reason = ( $this->getExistingReaderIndex( self::GROUP_GENERIC ) >= 0 )
+ ? 'The database is read-only until replication lag decreases.'
+ : 'The database is read-only until replica database servers becomes reachable.';
+ $conn->setLBInfo( 'readOnlyReason', $reason );
}
- $domain = $this->resolveDomainID( $domain );
- $flags = $this->sanitizeConnectionFlags( $flags );
- $masterOnly = ( $i === self::DB_MASTER || $i === $this->getWriterIndex() );
+ return $conn;
+ }
+ /**
+ * @param int $i Specific server index
+ * @param string $domain Resolved DB domain
+ * @param int $flags Bitfield of class CONN_* constants
+ * @return IDatabase|bool
+ * @throws InvalidArgumentException When the server index is invalid
+ */
+ public function getServerConnection( $i, $domain, $flags = 0 ) {
// Number of connections made before getting the server index and handle
$priorConnectionsMade = $this->connectionCounter;
- // Choose a server if $i is DB_MASTER/DB_REPLICA (might trigger new connections)
- $serverIndex = $this->getConnectionIndex( $i, $groups, $domain );
- // Get an open connection to that server (might trigger a new connection)
+ // Get an open connection to this server (might trigger a new connection)
$conn = $this->localDomain->equals( $domain )
- ? $this->getLocalConnection( $serverIndex, $flags )
- : $this->getForeignConnection( $serverIndex, $domain, $flags );
- // Throw an error or bail out if the connection attempt failed
+ ? $this->getLocalConnection( $i, $flags )
+ : $this->getForeignConnection( $i, $domain, $flags );
+ // Throw an error or otherwise bail out if the connection attempt failed
if ( !( $conn instanceof IDatabase ) ) {
if ( ( $flags & self::CONN_SILENCE_ERRORS ) != self::CONN_SILENCE_ERRORS ) {
$this->reportConnectionError();
// Profile any new connections caused by this method
if ( $this->connectionCounter > $priorConnectionsMade ) {
- $host = $conn->getServer();
- $dbname = $conn->getDBname();
- $this->trxProfiler->recordConnection( $host, $dbname, $masterOnly );
+ $this->trxProfiler->recordConnection(
+ $conn->getServer(),
+ $conn->getDBname(),
+ ( ( $flags & self::CONN_INTENT_WRITABLE ) == self::CONN_INTENT_WRITABLE )
+ );
}
if ( !$conn->isOpen() ) {
- // Connection was made but later unrecoverably lost for some reason.
- // Do not return a handle that will just throw exceptions on use,
- // but let the calling code (e.g. getReaderIndex) try another server.
$this->errorConnection = $conn;
+ // Connection was made but later unrecoverably lost for some reason.
+ // Do not return a handle that will just throw exceptions on use, but
+ // let the calling code, e.g. getReaderIndex(), try another server.
return false;
}
+ // Make sure that flags like CONN_TRX_AUTOCOMMIT are respected by this handle
$this->enforceConnectionFlags( $conn, $flags );
- if ( $serverIndex === $this->getWriterIndex() ) {
- // If the load balancer is read-only, perhaps due to replication lag, then master
- // DB handles will reflect that. Note that Database::assertIsWritableMaster() takes
- // care of replica DB handles whereas getReadOnlyReason() would cause infinite loops.
- $conn->setLBInfo( 'readOnlyReason', $this->getReadOnlyReason( $domain, $conn ) );
+ // Set master DB handles as read-only if the load balancer is configured as read-only
+ // or the master database server is running in server-side read-only mode. Note that
+ // replica DB handles are always read-only via Database::assertIsWritableMaster().
+ // Read-only mode due to replication lag is *avoided* here to avoid recursion.
+ if ( $conn->getLBInfo( 'serverIndex' ) === $this->getWriterIndex() ) {
+ if ( $this->readOnlyReason !== false ) {
+ $conn->setLBInfo( 'readOnlyReason', $this->readOnlyReason );
+ } elseif ( $this->masterRunningReadOnly( $domain, $conn ) ) {
+ $conn->setLBInfo(
+ 'readOnlyReason',
+ 'The master database server is running in read-only mode.'
+ );
+ }
}
return $conn;
// Database instance to this method. Any caller passing in a DBConnRef is broken.
$this->connLogger->error(
__METHOD__ . ": got DBConnRef instance.\n" .
- ( new RuntimeException() )->getTraceAsString() );
+ ( new LogicException() )->getTraceAsString() );
return;
}
/**
* @param int $i
- * @param bool $domain
+ * @param string|bool $domain
* @param int $flags
* @return Database|bool Live database handle or false on failure
* @deprecated Since 1.34 Use getConnection() instead
* @param int $i Server index
* @param int $flags Class CONN_* constant bitfield
* @return Database
+ * @throws InvalidArgumentException When the server index is invalid
+ * @throws UnexpectedValueException When the DB domain of the connection is corrupted
*/
private function getLocalConnection( $i, $flags = 0 ) {
// Connection handles required to be in auto-commit mode use a separate connection
* @param int $flags Class CONN_* constant bitfield
* @return Database|bool Returns false on connection error
* @throws DBError When database selection fails
+ * @throws InvalidArgumentException When the server index is invalid
+ * @throws UnexpectedValueException When the DB domain of the connection is corrupted
*/
private function getForeignConnection( $i, $domain, $flags = 0 ) {
$domainInstance = DatabaseDomain::newFromId( $domain );
* Test if the specified index represents an open connection
*
* @param int $index Server index
- * @private
* @return bool
*/
private function isOpen( $index ) {
- if ( !is_int( $index ) ) {
- return false;
- }
-
return (bool)$this->getAnyOpenConnection( $index );
}
}
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() {
}
if ( $conn->getFlag( $conn::DBO_TRX ) ) {
- $conn->setLBInfo( 'trxRoundId', false );
+ $conn->setLBInfo( 'trxRoundId', null ); // remove the round ID
}
if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) {
}
public function getLaggedReplicaMode( $domain = false ) {
- if (
- // Avoid recursion if there is only one DB
- $this->hasStreamingReplicaServers() &&
- // Avoid recursion if the (non-zero load) master DB was picked for generic reads
- $this->genericReadIndex !== $this->getWriterIndex() &&
- // Stay in lagged replica mode during the load balancer instance lifetime
- !$this->laggedReplicaMode
- ) {
+ if ( $this->laggedReplicaMode ) {
+ return true; // stay in lagged replica mode
+ }
+
+ if ( $this->hasStreamingReplicaServers() ) {
try {
- // Calling this method will set "laggedReplicaMode" as needed
- $this->getReaderIndex( false, $domain );
+ // Set "laggedReplicaMode"
+ $this->getReaderIndex( self::GROUP_GENERIC, $domain );
} catch ( DBConnectionError $e ) {
- // Avoid expensive re-connect attempts and failures
- $this->allReplicasDownMode = true;
+ // Sanity: avoid expensive re-connect attempts and failures
$this->laggedReplicaMode = true;
}
}
public function getReadOnlyReason( $domain = false, IDatabase $conn = null ) {
if ( $this->readOnlyReason !== false ) {
return $this->readOnlyReason;
- } elseif ( $this->getLaggedReplicaMode( $domain ) ) {
- if ( $this->allReplicasDownMode ) {
- return 'The database has been automatically locked ' .
- 'until the replica database servers become available';
- } else {
- return 'The database has been automatically locked ' .
- 'while the replica database servers catch up to the master.';
- }
} elseif ( $this->masterRunningReadOnly( $domain, $conn ) ) {
- return 'The database master is running in read-only mode.';
+ return 'The master database server is running in read-only mode.';
+ } elseif ( $this->getLaggedReplicaMode( $domain ) ) {
+ return ( $this->getExistingReaderIndex( self::GROUP_GENERIC ) >= 0 )
+ ? 'The database is read-only until replication lag decreases.'
+ : 'The database is read-only until a replica database server becomes reachable.';
}
return false;
function () use ( $domain, $conn ) {
$old = $this->trxProfiler->setSilenced( true );
try {
- $dbw = $conn ?: $this->getConnection( self::DB_MASTER, [], $domain );
+ $index = $this->getWriterIndex();
+ $dbw = $conn ?: $this->getServerConnection( $index, $domain );
$readOnly = (int)$dbw->serverIsReadOnly();
if ( !$conn ) {
$this->reuseConnection( $dbw );
$readOnly = 0;
}
$this->trxProfiler->setSilenced( $old );
+
return $readOnly;
},
[ 'pcTTL' => $cache::TTL_PROC_LONG, 'busyValue' => 0 ]
if ( !$pos ) {
// Get the current master position, opening a connection if needed
$index = $this->getWriterIndex();
- $masterConn = $this->getAnyOpenConnection( $index );
+ $flags = self::CONN_SILENCE_ERRORS;
+ $masterConn = $this->getAnyOpenConnection( $index, $flags );
if ( $masterConn ) {
$pos = $masterConn->getMasterPos();
} else {
- $flags = self::CONN_SILENCE_ERRORS;
- $masterConn = $this->getConnection( $index, [], self::DOMAIN_ANY, $flags );
+ $masterConn = $this->getServerConnection( $index, self::DOMAIN_ANY, $flags );
if ( !$masterConn ) {
throw new DBReplicationWaitError(
null,