Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
import software.amazon.jdbc.AwsWrapperProperty;
import software.amazon.jdbc.HostListProviderService;
import software.amazon.jdbc.HostRole;
Expand All @@ -37,6 +40,7 @@
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.RdsUrlType;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.Utils;
import software.amazon.jdbc.util.WrapperUtils;

Expand Down Expand Up @@ -161,7 +165,8 @@ public Connection connect(

if (type == RdsUrlType.RDS_READER_CLUSTER
|| isInitialConnection && this.verifyOpenedConnectionType == VerifyOpenedConnectionType.READER) {
Connection readerCandidateConn = this.getVerifiedReaderConnection(props, isInitialConnection, connectFunc);
Connection readerCandidateConn =
this.getVerifiedReaderConnection(type, hostSpec, props, isInitialConnection, connectFunc);
if (readerCandidateConn == null) {
// Can't get a reader connection. Continue with a normal workflow.
LOGGER.finest("Continue with normal workflow.");
Expand Down Expand Up @@ -254,6 +259,8 @@ private Connection getVerifiedWriterConnection(
}

private Connection getVerifiedReaderConnection(
final RdsUrlType rdsUrlType,
final HostSpec hostSpec,
final Properties props,
final boolean isInitialConnection,
final JdbcCallable<Connection, SQLException> connectFunc)
Expand All @@ -266,14 +273,17 @@ private Connection getVerifiedReaderConnection(

Connection readerCandidateConn;
HostSpec readerCandidate;
final String awsRegion = rdsUrlType == RdsUrlType.RDS_READER_CLUSTER
? this.rdsUtils.getRdsRegion(hostSpec.getHost())
: null;

while (this.getTime() < endTimeNano) {

readerCandidateConn = null;
readerCandidate = null;

try {
readerCandidate = this.getReader(props);
readerCandidate = this.getReader(props, awsRegion);

if (readerCandidate == null || this.rdsUtils.isRdsClusterDns(readerCandidate.getHost())) {

Expand Down Expand Up @@ -371,12 +381,20 @@ private void delay(final long delayMs) {
}
}

private HostSpec getReader(final Properties props) throws SQLException {
private HostSpec getReader(final Properties props, final @Nullable String awsRegion) throws SQLException {

final String strategy = READER_HOST_SELECTOR_STRATEGY.getString(props);
if (this.pluginService.acceptsStrategy(HostRole.READER, strategy)) {
try {
return this.pluginService.getHostSpecByStrategy(HostRole.READER, strategy);
if (!StringUtils.isNullOrEmpty(awsRegion)) {
final List<HostSpec> hostsInRegion = this.pluginService.getHosts()
.stream()
.filter(x -> awsRegion.equalsIgnoreCase(this.rdsUtils.getRdsRegion(x.getHost())))
.collect(Collectors.toList());
return this.pluginService.getHostSpecByStrategy(hostsInRegion, HostRole.READER, strategy);
} else {
return this.pluginService.getHostSpecByStrategy(HostRole.READER, strategy);
}
} catch (UnsupportedOperationException ex) {
throw ex;
} catch (SQLException ex) {
Expand Down
Loading