Skip to content

Commit f2f7a11

Browse files
committed
respect AWS region connecting with cluster reader endpoint for GDB (#1575)
1 parent bb7ae52 commit f2f7a11

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/AuroraInitialConnectionStrategyPlugin.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import java.util.Collections;
2222
import java.util.HashMap;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Map;
2526
import java.util.Properties;
2627
import java.util.Set;
2728
import java.util.concurrent.TimeUnit;
2829
import java.util.logging.Logger;
30+
import java.util.stream.Collectors;
31+
import org.jetbrains.annotations.Nullable;
2932
import software.amazon.jdbc.AwsWrapperProperty;
3033
import software.amazon.jdbc.HostListProviderService;
3134
import software.amazon.jdbc.HostRole;
@@ -37,6 +40,7 @@
3740
import software.amazon.jdbc.util.Messages;
3841
import software.amazon.jdbc.util.RdsUrlType;
3942
import software.amazon.jdbc.util.RdsUtils;
43+
import software.amazon.jdbc.util.StringUtils;
4044
import software.amazon.jdbc.util.Utils;
4145
import software.amazon.jdbc.util.WrapperUtils;
4246

@@ -161,7 +165,8 @@ public Connection connect(
161165

162166
if (type == RdsUrlType.RDS_READER_CLUSTER
163167
|| isInitialConnection && this.verifyOpenedConnectionType == VerifyOpenedConnectionType.READER) {
164-
Connection readerCandidateConn = this.getVerifiedReaderConnection(props, isInitialConnection, connectFunc);
168+
Connection readerCandidateConn =
169+
this.getVerifiedReaderConnection(type, hostSpec, props, isInitialConnection, connectFunc);
165170
if (readerCandidateConn == null) {
166171
// Can't get a reader connection. Continue with a normal workflow.
167172
LOGGER.finest("Continue with normal workflow.");
@@ -255,6 +260,8 @@ private Connection getVerifiedWriterConnection(
255260
}
256261

257262
private Connection getVerifiedReaderConnection(
263+
final RdsUrlType rdsUrlType,
264+
final HostSpec hostSpec,
258265
final Properties props,
259266
final boolean isInitialConnection,
260267
final JdbcCallable<Connection, SQLException> connectFunc)
@@ -267,14 +274,17 @@ private Connection getVerifiedReaderConnection(
267274

268275
Connection readerCandidateConn;
269276
HostSpec readerCandidate;
277+
final String awsRegion = rdsUrlType == RdsUrlType.RDS_READER_CLUSTER
278+
? this.rdsUtils.getRdsRegion(hostSpec.getHost())
279+
: null;
270280

271281
while (this.getTime() < endTimeNano) {
272282

273283
readerCandidateConn = null;
274284
readerCandidate = null;
275285

276286
try {
277-
readerCandidate = this.getReader(props);
287+
readerCandidate = this.getReader(props, awsRegion);
278288

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

@@ -372,12 +382,20 @@ private void delay(final long delayMs) {
372382
}
373383
}
374384

375-
private HostSpec getReader(final Properties props) throws SQLException {
385+
private HostSpec getReader(final Properties props, final @Nullable String awsRegion) throws SQLException {
376386

377387
final String strategy = READER_HOST_SELECTOR_STRATEGY.getString(props);
378388
if (this.pluginService.acceptsStrategy(HostRole.READER, strategy)) {
379389
try {
380-
return this.pluginService.getHostSpecByStrategy(HostRole.READER, strategy);
390+
if (!StringUtils.isNullOrEmpty(awsRegion)) {
391+
final List<HostSpec> hostsInRegion = this.pluginService.getHosts()
392+
.stream()
393+
.filter(x -> awsRegion.equalsIgnoreCase(this.rdsUtils.getRdsRegion(x.getHost())))
394+
.collect(Collectors.toList());
395+
return this.pluginService.getHostSpecByStrategy(hostsInRegion, HostRole.READER, strategy);
396+
} else {
397+
return this.pluginService.getHostSpecByStrategy(HostRole.READER, strategy);
398+
}
381399
} catch (UnsupportedOperationException ex) {
382400
throw ex;
383401
} catch (SQLException ex) {

0 commit comments

Comments
 (0)