+ $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 );
+ }