Skip to content

Commit bffdcce

Browse files
refactor: change getHostSpecByStrategy and getHost methods to pass properties
1 parent 6bfd40c commit bffdcce

21 files changed

+177
-256
lines changed

wrapper/src/main/java/software/amazon/jdbc/ConnectionPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Connection forceConnect(
128128
* @throws UnsupportedOperationException if this {@link ConnectionPlugin} does not support the
129129
* requested strategy
130130
*/
131-
HostSpec getHostSpecByStrategy(final HostRole role, final String strategy)
131+
HostSpec getHostSpecByStrategy(final HostRole role, final String strategy, final Properties props)
132132
throws SQLException, UnsupportedOperationException;
133133

134134
void initHostProvider(

wrapper/src/main/java/software/amazon/jdbc/ConnectionPluginManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public boolean acceptsStrategy(HostRole role, String strategy) throws SQLExcepti
381381
* {@link ConnectionPlugin} instances do not support the
382382
* requested strategy
383383
*/
384-
public HostSpec getHostSpecByStrategy(HostRole role, String strategy)
384+
public HostSpec getHostSpecByStrategy(HostRole role, String strategy, Properties props)
385385
throws SQLException, UnsupportedOperationException {
386386
try {
387387
for (ConnectionPlugin plugin : this.plugins) {
@@ -392,7 +392,7 @@ public HostSpec getHostSpecByStrategy(HostRole role, String strategy)
392392

393393
if (isSubscribed) {
394394
try {
395-
final HostSpec host = plugin.getHostSpecByStrategy(role, strategy);
395+
final HostSpec host = plugin.getHostSpecByStrategy(role, strategy, props);
396396
if (host != null) {
397397
return host;
398398
}

wrapper/src/main/java/software/amazon/jdbc/ConnectionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ boolean acceptsUrl(
6464
* @throws UnsupportedOperationException if the strategy is unsupported by the provider
6565
*/
6666
HostSpec getHostSpecByStrategy(
67-
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy)
67+
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy, @NonNull Properties props)
6868
throws SQLException, UnsupportedOperationException;
6969

7070
/**

wrapper/src/main/java/software/amazon/jdbc/ConnectionProviderManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ public boolean acceptsStrategy(HostRole role, String strategy) {
141141
* @throws UnsupportedOperationException if the available {@link ConnectionProvider} instances do
142142
* not support the requested strategy
143143
*/
144-
public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, String strategy)
144+
public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, String strategy, Properties props)
145145
throws SQLException, UnsupportedOperationException {
146146
HostSpec host = null;
147147
if (connProvider != null) {
148148
connProviderLock.readLock().lock();
149149
try {
150150
if (connProvider != null && connProvider.acceptsStrategy(role, strategy)) {
151-
host = connProvider.getHostSpecByStrategy(hosts, role, strategy);
151+
host = connProvider.getHostSpecByStrategy(hosts, role, strategy, props);
152152
}
153153
} catch (UnsupportedOperationException e) {
154154
// The custom provider does not support the provided strategy, ignore it and try with the default provider.
@@ -158,7 +158,7 @@ public HostSpec getHostSpecByStrategy(List<HostSpec> hosts, HostRole role, Strin
158158
}
159159

160160
if (host == null) {
161-
host = defaultProvider.getHostSpecByStrategy(hosts, role, strategy);
161+
host = defaultProvider.getHostSpecByStrategy(hosts, role, strategy, props);
162162
}
163163

164164
return host;

wrapper/src/main/java/software/amazon/jdbc/DataSourceConnectionProvider.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.checkerframework.checker.nullness.qual.NonNull;
3030
import software.amazon.jdbc.dialect.Dialect;
3131
import software.amazon.jdbc.exceptions.SQLLoginException;
32-
import software.amazon.jdbc.plugin.readwritesplitting.ReadWriteSplittingPlugin;
3332
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
3433
import software.amazon.jdbc.util.Messages;
3534
import software.amazon.jdbc.util.PropertyUtils;
@@ -88,7 +87,7 @@ public boolean acceptsStrategy(@NonNull HostRole role, @NonNull String strategy)
8887

8988
@Override
9089
public HostSpec getHostSpecByStrategy(
91-
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy)
90+
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy, @NonNull Properties props)
9291
throws SQLException {
9392
if (!acceptedStrategies.containsKey(strategy)) {
9493
throw new UnsupportedOperationException(
@@ -97,7 +96,7 @@ public HostSpec getHostSpecByStrategy(
9796
new Object[] {strategy, DataSourceConnectionProvider.class}));
9897
}
9998

100-
return acceptedStrategies.get(strategy).getHost(hosts, role);
99+
return acceptedStrategies.get(strategy).getHost(hosts, role, props);
101100
}
102101

103102
/**
@@ -116,14 +115,6 @@ public Connection connect(
116115
final @NonNull HostSpec hostSpec,
117116
final @NonNull Properties props)
118117
throws SQLException {
119-
final String strategy = ReadWriteSplittingPlugin.READER_HOST_SELECTOR_STRATEGY.getString(props);
120-
if (RoundRobinHostSelector.STRATEGY_ROUND_ROBIN.equals(strategy)) {
121-
final RoundRobinHostSelector roundRobinHostSelector =
122-
(RoundRobinHostSelector) acceptedStrategies.get(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
123-
if (roundRobinHostSelector.getHostCacheEntry(hostSpec) != null) {
124-
roundRobinHostSelector.updateCachePropertiesForHost(hostSpec, props);
125-
}
126-
}
127118

128119
final Properties copy = PropertyUtils.copyProperties(props);
129120

wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.checkerframework.checker.nullness.qual.NonNull;
2828
import software.amazon.jdbc.dialect.Dialect;
2929
import software.amazon.jdbc.exceptions.SQLLoginException;
30-
import software.amazon.jdbc.plugin.readwritesplitting.ReadWriteSplittingPlugin;
3130
import software.amazon.jdbc.targetdriverdialect.ConnectInfo;
3231
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
3332
import software.amazon.jdbc.util.Messages;
@@ -83,7 +82,7 @@ public boolean acceptsStrategy(@NonNull HostRole role, @NonNull String strategy)
8382

8483
@Override
8584
public HostSpec getHostSpecByStrategy(
86-
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy)
85+
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy, @NonNull Properties props)
8786
throws SQLException {
8887
if (!acceptedStrategies.containsKey(strategy)) {
8988
throw new UnsupportedOperationException(
@@ -92,7 +91,7 @@ public HostSpec getHostSpecByStrategy(
9291
new Object[] {strategy, DriverConnectionProvider.class}));
9392
}
9493

95-
return acceptedStrategies.get(strategy).getHost(hosts, role);
94+
return acceptedStrategies.get(strategy).getHost(hosts, role, props);
9695
}
9796

9897
/**
@@ -111,14 +110,6 @@ public Connection connect(
111110
final @NonNull HostSpec hostSpec,
112111
final @NonNull Properties props)
113112
throws SQLException {
114-
final String strategy = ReadWriteSplittingPlugin.READER_HOST_SELECTOR_STRATEGY.getString(props);
115-
if (RoundRobinHostSelector.STRATEGY_ROUND_ROBIN.equals(strategy)) {
116-
final RoundRobinHostSelector roundRobinHostSelector =
117-
(RoundRobinHostSelector) acceptedStrategies.get(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
118-
if (roundRobinHostSelector.getHostCacheEntry(hostSpec) != null) {
119-
roundRobinHostSelector.updateCachePropertiesForHost(hostSpec, props);
120-
}
121-
}
122113

123114
final Properties copy = PropertyUtils.copyProperties(props);
124115
final ConnectInfo connectInfo =

wrapper/src/main/java/software/amazon/jdbc/HikariPooledConnectionProvider.java

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.checkerframework.checker.nullness.qual.NonNull;
3434
import software.amazon.jdbc.cleanup.CanReleaseResources;
3535
import software.amazon.jdbc.dialect.Dialect;
36-
import software.amazon.jdbc.plugin.readwritesplitting.ReadWriteSplittingPlugin;
3736
import software.amazon.jdbc.util.HikariCPSQLException;
3837
import software.amazon.jdbc.util.Messages;
3938
import software.amazon.jdbc.util.RdsUrlType;
@@ -47,12 +46,11 @@ public class HikariPooledConnectionProvider implements PooledConnectionProvider,
4746
private static final Logger LOGGER =
4847
Logger.getLogger(HikariPooledConnectionProvider.class.getName());
4948
private static final Map<String, HostSelector> acceptedStrategies =
50-
Collections.unmodifiableMap(new HashMap<String, HostSelector>() {
49+
new HashMap<String, HostSelector>() {
5150
{
52-
put(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS, new LeastConnectionsHostSelector());
5351
put(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN, new RoundRobinHostSelector());
5452
}
55-
});
53+
};
5654
private static final RdsUtils rdsUtils = new RdsUtils();
5755
private static SlidingExpirationCache<PoolKey, HikariDataSource> databasePools =
5856
new SlidingExpirationCache<>(
@@ -104,10 +102,8 @@ public HikariPooledConnectionProvider(
104102
HikariPoolConfigurator hikariPoolConfigurator, HikariPoolMapping mapping) {
105103
this.poolConfigurator = hikariPoolConfigurator;
106104
this.poolMapping = mapping;
107-
final LeastConnectionsHostSelector hostSelector =
108-
(LeastConnectionsHostSelector) acceptedStrategies
109-
.get(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS);
110-
hostSelector.setDatabasePools(databasePools);
105+
final LeastConnectionsHostSelector hostSelector = new LeastConnectionsHostSelector(databasePools);
106+
acceptedStrategies.put(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS, hostSelector);
111107
}
112108

113109
/**
@@ -144,10 +140,8 @@ public HikariPooledConnectionProvider(
144140
this.poolMapping = mapping;
145141
poolExpirationCheckNanos = poolExpirationNanos;
146142
databasePools.setCleanupIntervalNanos(poolCleanupNanos);
147-
final LeastConnectionsHostSelector hostSelector =
148-
(LeastConnectionsHostSelector) acceptedStrategies
149-
.get(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS);
150-
hostSelector.setDatabasePools(databasePools);
143+
final LeastConnectionsHostSelector hostSelector = new LeastConnectionsHostSelector(databasePools);
144+
acceptedStrategies.put(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS, hostSelector);
151145
}
152146

153147
@Override
@@ -164,17 +158,17 @@ public boolean acceptsStrategy(@NonNull HostRole role, @NonNull String strategy)
164158

165159
@Override
166160
public HostSpec getHostSpecByStrategy(
167-
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy)
161+
@NonNull List<HostSpec> hosts, @NonNull HostRole role, @NonNull String strategy, Properties props)
168162
throws SQLException {
169163
final HostSpec selectedHost;
170164
switch (strategy) {
171165
case LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS:
172166
selectedHost =
173-
acceptedStrategies.get(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS).getHost(hosts, role);
167+
acceptedStrategies.get(LeastConnectionsHostSelector.STRATEGY_LEAST_CONNECTIONS).getHost(hosts, role, props);
174168
break;
175169

176170
case RoundRobinHostSelector.STRATEGY_ROUND_ROBIN:
177-
selectedHost = acceptedStrategies.get(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN).getHost(hosts, role);
171+
selectedHost = acceptedStrategies.get(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN).getHost(hosts, role, props);
178172
break;
179173

180174
default:
@@ -203,15 +197,6 @@ public Connection connect(
203197

204198
ds.setPassword(props.getProperty(PropertyDefinition.PASSWORD.name));
205199

206-
final String strategy = ReadWriteSplittingPlugin.READER_HOST_SELECTOR_STRATEGY.getString(props);
207-
if (RoundRobinHostSelector.STRATEGY_ROUND_ROBIN.equals(strategy)) {
208-
final RoundRobinHostSelector roundRobinHostSelector =
209-
(RoundRobinHostSelector) acceptedStrategies.get(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
210-
if (roundRobinHostSelector.getHostCacheEntry(hostSpec) != null) {
211-
roundRobinHostSelector.updateCachePropertiesForHost(hostSpec, props);
212-
}
213-
}
214-
215200
return ds.getConnection();
216201
}
217202

wrapper/src/main/java/software/amazon/jdbc/HostSelector.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.sql.SQLException;
2020
import java.util.List;
21+
import java.util.Properties;
2122

2223
public interface HostSelector {
2324

@@ -26,9 +27,10 @@ public interface HostSelector {
2627
*
2728
* @param hosts a list of available hosts to pick from
2829
* @param role the desired host role - either a writer or a reader
30+
* @param props a properties object containing any necessary parameters
2931
* @return a host matching the requested role
3032
* @throws SQLException if the host list does not contain any hosts matching the requested role or
3133
* an error occurs while selecting a host
3234
*/
33-
HostSpec getHost(List<HostSpec> hosts, HostRole role) throws SQLException;
35+
HostSpec getHost(List<HostSpec> hosts, HostRole role, Properties props) throws SQLException;
3436
}

wrapper/src/main/java/software/amazon/jdbc/LeastConnectionsHostSelector.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@
2020
import java.sql.SQLException;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Properties;
2324
import java.util.stream.Collectors;
2425
import software.amazon.jdbc.util.Messages;
2526
import software.amazon.jdbc.util.SlidingExpirationCache;
2627

2728
public class LeastConnectionsHostSelector implements HostSelector {
2829
public static final String STRATEGY_LEAST_CONNECTIONS = "leastConnections";
29-
private SlidingExpirationCache<HikariPooledConnectionProvider.PoolKey, HikariDataSource> databasePools;
30+
private final SlidingExpirationCache<HikariPooledConnectionProvider.PoolKey, HikariDataSource> databasePools;
3031

31-
public void setDatabasePools(
32+
public LeastConnectionsHostSelector(
3233
SlidingExpirationCache<HikariPooledConnectionProvider.PoolKey, HikariDataSource> databasePools) {
3334
this.databasePools = databasePools;
3435
}
3536

36-
public HostSpec getHost(List<HostSpec> hosts, HostRole role) throws SQLException {
37+
@Override
38+
public HostSpec getHost(List<HostSpec> hosts, HostRole role, Properties props) throws SQLException {
3739
List<HostSpec> eligibleHosts = hosts.stream()
3840
.filter(hostSpec -> role.equals(hostSpec.getRole()))
3941
.sorted((hostSpec1, hostSpec2) ->

wrapper/src/main/java/software/amazon/jdbc/PluginService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ EnumSet<NodeChangeOptions> setCurrentConnection(
8181
* {@link ConnectionPlugin} instances do not support the
8282
* requested strategy
8383
*/
84-
HostSpec getHostSpecByStrategy(HostRole role, String strategy)
84+
HostSpec getHostSpecByStrategy(HostRole role, String strategy, Properties props)
8585
throws SQLException, UnsupportedOperationException;
8686

8787
/**

0 commit comments

Comments
 (0)