2121import java .util .Collections ;
2222import java .util .HashMap ;
2323import java .util .HashSet ;
24+ import java .util .List ;
2425import java .util .Map ;
2526import java .util .Properties ;
2627import java .util .Set ;
2728import java .util .concurrent .TimeUnit ;
2829import java .util .logging .Logger ;
30+ import java .util .stream .Collectors ;
31+ import org .jetbrains .annotations .Nullable ;
2932import software .amazon .jdbc .AwsWrapperProperty ;
3033import software .amazon .jdbc .HostListProviderService ;
3134import software .amazon .jdbc .HostRole ;
3740import software .amazon .jdbc .util .Messages ;
3841import software .amazon .jdbc .util .RdsUrlType ;
3942import software .amazon .jdbc .util .RdsUtils ;
43+ import software .amazon .jdbc .util .StringUtils ;
4044import software .amazon .jdbc .util .Utils ;
4145import 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