private $errorConnection;
/** @var int The generic (not query grouped) replica DB index */
private $genericReadIndex = -1;
- /** @var bool|DBMasterPos False if not set */
+ /** @var int[] The group replica DB 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 any connection has been attempted yet */
private $connectionAttempted = false;
+ /** @var int|null An integer ID of the managing LBFactory instance or null */
+ private $ownerId;
/** @var string|bool String if a requested DBO_TRX transaction round is active */
private $trxRoundId = false;
/** @var string Stage of the current transaction round in the transaction round life-cycle */
}
$this->defaultGroup = $params['defaultGroup'] ?? null;
+ $this->ownerId = $params['ownerId'] ?? null;
}
public function getLocalDomainID() {
}
/**
+ * @param int $flags
+ * @return bool
+ */
+ 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.
+ $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.
+ $flags &= ~self::CONN_TRX_AUTOCOMMIT;
+ $this->connLogger->info( __METHOD__ .
+ ': ignoring CONN_TRX_AUTOCOMMIT to avoid deadlocks.' );
+ }
+ }
+
+ return $flags;
+ }
+
+ /**
+ * @param IDatabase $conn
+ * @param int $flags
+ * @throws DBUnexpectedError
+ */
+ private function enforceConnectionFlags( IDatabase $conn, $flags ) {
+ if ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT ) {
+ if ( $conn->trxLevel() ) { // sanity
+ throw new DBUnexpectedError(
+ $conn,
+ 'Handle requested with CONN_TRX_AUTOCOMMIT yet it has a transaction'
+ );
+ }
+
+ $conn->clearFlag( $conn::DBO_TRX ); // auto-commit mode
+ }
+ }
+
+ /**
* Get a LoadMonitor instance
*
* @return ILoadMonitor
* @param int $i
* @param array $groups
* @param string|bool $domain
- * @return int
+ * @return int The index of a specific server (replica DBs are checked for connectivity)
*/
private function getConnectionIndex( $i, $groups, $domain ) {
// Check one "group" per default: the generic pool
? $defaultGroups
: (array)$groups;
- if ( $i == self::DB_MASTER ) {
+ if ( $i === self::DB_MASTER ) {
$i = $this->getWriterIndex();
- } elseif ( $i == self::DB_REPLICA ) {
+ } elseif ( $i === self::DB_REPLICA ) {
# Try to find an available server in any the query groups (in order)
foreach ( $groups as $group ) {
$groupIndex = $this->getReaderIndex( $group, $domain );
}
# Operation-based index
- if ( $i == self::DB_REPLICA ) {
+ if ( $i === self::DB_REPLICA ) {
$this->lastError = 'Unknown error'; // reset error string
# Try the general server pool if $groups are unavailable.
$i = ( $groups === [ false ] )
$this->lastError = 'No working replica DB server: ' . $this->lastError;
// Throw an exception
$this->reportConnectionError();
- return null; // not reached
+ return null; // unreachable due to exception
}
}
if ( count( $this->servers ) == 1 ) {
// Skip the load balancing if there's only one server
return $this->getWriterIndex();
- } elseif ( $group === false && $this->genericReadIndex >= 0 ) {
- // A generic reader index was already selected and "waitForPos" was handled
- return $this->genericReadIndex;
+ }
+
+ $index = $this->getExistingReaderIndex( $group );
+ if ( $index >= 0 ) {
+ // A reader index was already selected and "waitForPos" was handled
+ return $index;
}
if ( $group !== false ) {
$this->getLoadMonitor()->scaleLoads( $loads, $domain );
// Pick a server to use, accounting for weights, load, lag, and "waitForPos"
+ $this->lazyLoadReplicationPositions(); // optimizes server candidate selection
list( $i, $laggedReplicaMode ) = $this->pickReaderIndex( $loads, $domain );
if ( $i === false ) {
// DB connection unsuccessful
$laggedReplicaMode = true;
}
- if ( $this->genericReadIndex < 0 && $this->genericLoads[$i] > 0 && $group === false ) {
- // Cache the generic (ungrouped) reader index for future DB_REPLICA handles
- $this->genericReadIndex = $i;
- // Record if the generic reader index is in "lagged replica DB" mode
- $this->laggedReplicaMode = ( $laggedReplicaMode || $this->laggedReplicaMode );
+ // 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 ) {
+ $this->laggedReplicaMode = true;
}
$serverName = $this->getServerName( $i );
return $i;
}
+ /**
+ * Get the server index chosen by the load balancer for use with the given query group
+ *
+ * @param string|bool $group Query group; use false for the generic group
+ * @return int Server index or -1 if none was chosen
+ */
+ protected function getExistingReaderIndex( $group ) {
+ if ( $group === false ) {
+ $index = $this->genericReadIndex;
+ } else {
+ $index = $this->readIndexByGroup[$group] ?? -1;
+ }
+
+ return $index;
+ }
+
+ /**
+ * Set the server index chosen by the load balancer for use with the given query group
+ *
+ * @param string|bool $group Query group; use false for the generic group
+ * @param int $index The index of a specific server
+ */
+ private function setExistingReaderIndex( $group, $index ) {
+ if ( $index < 0 ) {
+ throw new UnexpectedValueException( "Cannot set a negative read server index" );
+ }
+
+ if ( $group === false ) {
+ $this->genericReadIndex = $index;
+ } else {
+ $this->readIndexByGroup[$group] = $index;
+ }
+ }
+
/**
* @param array $loads List of server weights
* @param string|bool $domain
} else {
$i = false;
if ( $this->waitForPos && $this->waitForPos->asOfTime() ) {
+ $this->replLogger->debug( __METHOD__ . ": replication positions detected" );
// "chronologyCallback" sets "waitForPos" for session consistency.
// This triggers doWait() after connect, so it's especially good to
// avoid lagged servers so as to avoid excessive delay in that method.
$serverName = $this->getServerName( $i );
$this->connLogger->debug( __METHOD__ . ": Using reader #$i: $serverName..." );
- $conn = $this->openConnection( $i, $domain );
+ $conn = $this->getConnection( $i, [], $domain, self::CONN_SILENCE_ERRORS );
if ( !$conn ) {
$this->connLogger->warning( __METHOD__ . ": Failed connecting to $i/$domain" );
unset( $currentLoads[$i] ); // avoid this server next iteration
);
return false;
- } else {
- $conn = $this->openConnection( $index, self::DOMAIN_ANY );
- if ( !$conn ) {
- $this->replLogger->warning(
- __METHOD__ . ': failed to connect to {dbserver}',
- [ 'dbserver' => $server ]
- );
+ }
+ // Open a temporary new connection in order to wait for replication
+ $conn = $this->getConnection( $index, [], self::DOMAIN_ANY, self::CONN_SILENCE_ERRORS );
+ if ( !$conn ) {
+ $this->replLogger->warning(
+ __METHOD__ . ': failed to connect to {dbserver}',
+ [ 'dbserver' => $server ]
+ );
- return false;
- }
- // Avoid connection spam in waitForAll() when connections
- // are made just for the sake of doing this lag check.
- $close = true;
+ return false;
}
+ // Avoid connection spam in waitForAll() when connections
+ // are made just for the sake of doing this lag check.
+ $close = true;
}
$this->replLogger->info(
}
public function getConnection( $i, $groups = [], $domain = false, $flags = 0 ) {
- if ( $i === null || $i === false ) {
+ 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 );
+ $flags = $this->sanitizeConnectionFlags( $flags );
$masterOnly = ( $i == self::DB_MASTER || $i == $this->getWriterIndex() );
- 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.
- $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.
- $flags &= ~self::CONN_TRX_AUTOCOMMIT;
- $this->connLogger->info( __METHOD__ .
- ': ignoring CONN_TRX_AUTOCOMMIT to avoid deadlocks.' );
- }
- }
-
// Number of connections made before getting the server index and handle
$priorConnectionsMade = $this->connsOpened;
- // Decide which server to use (might trigger new connections)
+
+ // 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)
- $conn = $this->openConnection( $serverIndex, $domain, $flags );
- if ( !$conn ) {
- $this->reportConnectionError();
- return null; // unreachable due to exception
+ $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
+ if ( !( $conn instanceof IDatabase ) ) {
+ if ( ( $flags & self::CONN_SILENCE_ERRORS ) != self::CONN_SILENCE_ERRORS ) {
+ $this->reportConnectionError();
+ }
+
+ return false;
}
// Profile any new connections caused by this method
$this->trxProfiler->recordConnection( $host, $dbname, $masterOnly );
}
+ 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;
+ return false;
+ }
+
+ $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
: self::DB_REPLICA;
}
+ /**
+ * @param int $i
+ * @param bool $domain
+ * @param int $flags
+ * @return Database|bool Live database handle or false on failure
+ * @deprecated Since 1.34 Use getConnection() instead
+ */
public function openConnection( $i, $domain = false, $flags = 0 ) {
- $domain = $this->resolveDomainID( $domain );
-
- if ( !$this->connectionAttempted && $this->chronologyCallback ) {
- // Load any "waitFor" positions before connecting so that doWait() is triggered
- $this->connLogger->debug( __METHOD__ . ': calling initLB() before first connection.' );
- $this->connectionAttempted = true;
- ( $this->chronologyCallback )( $this );
- }
-
- $conn = $this->localDomain->equals( $domain )
- ? $this->openLocalConnection( $i, $flags )
- : $this->openForeignConnection( $i, $domain, $flags );
-
- if ( $conn instanceof IDatabase && !$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;
- $conn = false;
- }
-
- if (
- $conn instanceof IDatabase &&
- ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT )
- ) {
- if ( $conn->trxLevel() ) { // sanity
- throw new DBUnexpectedError(
- $conn,
- 'Handle requested with CONN_TRX_AUTOCOMMIT yet it has a transaction'
- );
- }
-
- $conn->clearFlag( $conn::DBO_TRX ); // auto-commit mode
- }
-
- return $conn;
+ return $this->getConnection( $i, [], $domain, $flags | self::CONN_SILENCE_ERRORS );
}
/**
* @param int $flags Class CONN_* constant bitfield
* @return Database
*/
- private function openLocalConnection( $i, $flags = 0 ) {
+ private function getLocalConnection( $i, $flags = 0 ) {
// 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 );
* @return Database|bool Returns false on connection error
* @throws DBError When database selection fails
*/
- private function openForeignConnection( $i, $domain, $flags = 0 ) {
+ private function getForeignConnection( $i, $domain, $flags = 0 ) {
$domainInstance = DatabaseDomain::newFromId( $domain );
// 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
}
}
+ $this->lazyLoadReplicationPositions(); // session consistency
+
return $db;
}
+ /**
+ * Make sure that any "waitForPos" positions are loaded and available to doWait()
+ */
+ private function lazyLoadReplicationPositions() {
+ if ( !$this->connectionAttempted && $this->chronologyCallback ) {
+ $this->connectionAttempted = true;
+ ( $this->chronologyCallback )( $this ); // generally calls waitFor()
+ $this->connLogger->debug( __METHOD__ . ': executed chronology callback.' );
+ }
+ }
+
/**
* @throws DBConnectionError
*/
$conn->close();
}
- public function commitAll( $fname = __METHOD__ ) {
- $this->commitMasterChanges( $fname );
+ public function commitAll( $fname = __METHOD__, $owner = null ) {
+ $this->commitMasterChanges( $fname, $owner );
$this->flushMasterSnapshots( $fname );
$this->flushReplicaSnapshots( $fname );
}
- public function finalizeMasterChanges() {
+ public function finalizeMasterChanges( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
$this->assertTransactionRoundStage( [ self::ROUND_CURSORY, self::ROUND_FINALIZED ] );
$this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
return $total;
}
- public function approveMasterChanges( array $options ) {
+ public function approveMasterChanges( array $options, $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
$this->assertTransactionRoundStage( self::ROUND_FINALIZED );
$limit = $options['maxWriteDuration'] ?? 0;
$this->trxRoundStage = self::ROUND_APPROVED;
}
- public function beginMasterChanges( $fname = __METHOD__ ) {
+ public function beginMasterChanges( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
if ( $this->trxRoundId !== false ) {
throw new DBTransactionError(
null,
$this->trxRoundStage = self::ROUND_CURSORY;
}
- public function commitMasterChanges( $fname = __METHOD__ ) {
+ public function commitMasterChanges( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
$this->assertTransactionRoundStage( self::ROUND_APPROVED );
$failures = [];
$this->trxRoundStage = self::ROUND_COMMIT_CALLBACKS;
}
- public function runMasterTransactionIdleCallbacks() {
+ public function runMasterTransactionIdleCallbacks( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
$type = IDatabase::TRIGGER_COMMIT;
} elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
return $e;
}
- public function runMasterTransactionListenerCallbacks() {
+ public function runMasterTransactionListenerCallbacks( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
$type = IDatabase::TRIGGER_COMMIT;
} elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
return $e;
}
- public function rollbackMasterChanges( $fname = __METHOD__ ) {
+ public function rollbackMasterChanges( $fname = __METHOD__, $owner = null ) {
+ $this->assertOwnership( $fname, $owner );
+
$restore = ( $this->trxRoundId !== false );
$this->trxRoundId = false;
$this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
/**
* @param string|string[] $stage
+ * @throws DBTransactionError
*/
private function assertTransactionRoundStage( $stage ) {
$stages = (array)$stage;
}
}
+ /**
+ * @param string $fname
+ * @param int|null $owner Owner ID of the caller
+ * @throws DBTransactionError
+ */
+ private function assertOwnership( $fname, $owner ) {
+ if ( $this->ownerId !== null && $owner !== $this->ownerId ) {
+ throw new DBTransactionError(
+ null,
+ "$fname: LoadBalancer is owned by LBFactory #{$this->ownerId} (got '$owner')."
+ );
+ }
+ }
+
/**
* Make all DB servers with DBO_DEFAULT/DBO_TRX set join the transaction round
*
if ( !$pos ) {
// Get the current master position, opening a connection if needed
- $masterConn = $this->getAnyOpenConnection( $this->getWriterIndex() );
+ $index = $this->getWriterIndex();
+ $masterConn = $this->getAnyOpenConnection( $index );
if ( $masterConn ) {
$pos = $masterConn->getMasterPos();
} else {
- $masterConn = $this->openConnection( $this->getWriterIndex(), self::DOMAIN_ANY );
+ $masterConn = $this->getConnection( $index, [], self::DOMAIN_ANY, self::CONN_SILENCE_ERRORS );
if ( !$masterConn ) {
throw new DBReplicationWaitError(
null,
}
if ( $pos instanceof DBMasterPos ) {
+ $start = microtime( true );
$result = $conn->masterPosWait( $pos, $timeout );
+ $seconds = max( microtime( true ) - $start, 0 );
if ( $result == -1 || is_null( $result ) ) {
- $msg = __METHOD__ . ': timed out waiting on {host} pos {pos}';
+ $msg = __METHOD__ . ': timed out waiting on {host} pos {pos} [{seconds}s]';
$this->replLogger->warning( $msg, [
'host' => $conn->getServer(),
'pos' => $pos,
+ 'seconds' => round( $seconds, 6 ),
'trace' => ( new RuntimeException() )->getTraceAsString()
] );
$ok = false;