Skip to content

Commit 656b38c

Browse files
enable WeightedRandomHostSelector, and Limitless Plugin to use it
1 parent c2d2f6b commit 656b38c

File tree

9 files changed

+45
-7
lines changed

9 files changed

+45
-7
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class C3P0PooledConnectionProvider implements PooledConnectionProvider, C
5050
put(HighestWeightHostSelector.STRATEGY_HIGHEST_WEIGHT, new HighestWeightHostSelector());
5151
put(RandomHostSelector.STRATEGY_RANDOM, new RandomHostSelector());
5252
put(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN, new RoundRobinHostSelector());
53+
put(WeightedRandomHostSelector.STRATEGY_WEIGHTED_RANDOM, new WeightedRandomHostSelector());
5354
}
5455
});
5556
protected static final long poolExpirationCheckNanos = TimeUnit.MINUTES.toNanos(30);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class DataSourceConnectionProvider implements ConnectionProvider {
5353
put(HighestWeightHostSelector.STRATEGY_HIGHEST_WEIGHT, new HighestWeightHostSelector());
5454
put(RandomHostSelector.STRATEGY_RANDOM, new RandomHostSelector());
5555
put(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN, new RoundRobinHostSelector());
56+
put(WeightedRandomHostSelector.STRATEGY_WEIGHTED_RANDOM, new WeightedRandomHostSelector());
5657
}
5758
});
5859
private final @NonNull DataSource dataSource;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class DriverConnectionProvider implements ConnectionProvider {
5050
put(HighestWeightHostSelector.STRATEGY_HIGHEST_WEIGHT, new HighestWeightHostSelector());
5151
put(RandomHostSelector.STRATEGY_RANDOM, new RandomHostSelector());
5252
put(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN, new RoundRobinHostSelector());
53+
put(WeightedRandomHostSelector.STRATEGY_WEIGHTED_RANDOM, new WeightedRandomHostSelector());
5354
}
5455
});
5556

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class HikariPooledConnectionProvider implements PooledConnectionProvider,
5757
put(HighestWeightHostSelector.STRATEGY_HIGHEST_WEIGHT, new HighestWeightHostSelector());
5858
put(RandomHostSelector.STRATEGY_RANDOM, new RandomHostSelector());
5959
put(RoundRobinHostSelector.STRATEGY_ROUND_ROBIN, new RoundRobinHostSelector());
60+
put(WeightedRandomHostSelector.STRATEGY_WEIGHTED_RANDOM, new WeightedRandomHostSelector());
6061
}
6162
});
6263

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public HostSpec getHost(
104104
return entry.getKey();
105105
}
106106
}
107-
// TODO: proper messaging
108-
throw new SQLException(Messages.get("HostSelector.TODO", new Object[] {role}));
107+
108+
throw new SQLException(Messages.get("HostSelector.weightedRandomUnableToGetHost", new Object[] {role}));
109109
}
110110

111111
private Map<String, Integer> getHostWeightPairMap(final String hostWeightMapString) throws SQLException {
@@ -124,7 +124,6 @@ private Map<String, Integer> getHostWeightPairMap(final String hostWeightMapStri
124124
for (final String hostWeightPair : hostWeightPairs) {
125125
final Matcher matcher = HOST_WEIGHT_PAIRS_PATTERN.matcher(hostWeightPair);
126126
if (!matcher.matches()) {
127-
// TODO: add this message
128127
throw new SQLException(Messages.get("HostSelector.weightedRandomInvalidHostWeightPairs"));
129128
}
130129

wrapper/src/main/java/software/amazon/jdbc/plugin/limitless/LimitlessRouterServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import software.amazon.jdbc.HostSpec;
3535
import software.amazon.jdbc.PluginService;
3636
import software.amazon.jdbc.PropertyDefinition;
37-
import software.amazon.jdbc.RoundRobinHostSelector;
37+
import software.amazon.jdbc.WeightedRandomHostSelector;
3838
import software.amazon.jdbc.hostavailability.HostAvailability;
3939
import software.amazon.jdbc.util.FullServicesContainer;
4040
import software.amazon.jdbc.util.HostSelectorUtils;
@@ -127,15 +127,15 @@ public void establishConnection(final LimitlessConnectionContext context) throws
127127
return;
128128
}
129129

130-
HostSelectorUtils.setHostWeightPairsProperty(RoundRobinHostSelector.ROUND_ROBIN_HOST_WEIGHT_PAIRS,
130+
HostSelectorUtils.setHostWeightPairsProperty(WeightedRandomHostSelector.WEIGHTED_RANDOM_HOST_WEIGHT_PAIRS,
131131
context.getProps(),
132132
context.getLimitlessRouters());
133133
HostSpec selectedHostSpec;
134134
try {
135135
selectedHostSpec = this.pluginService.getHostSpecByStrategy(
136136
context.getLimitlessRouters(),
137137
HostRole.WRITER,
138-
RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
138+
WeightedRandomHostSelector.STRATEGY_WEIGHTED_RANDOM);
139139
LOGGER.fine(Messages.get(
140140
"LimitlessRouterServiceImpl.selectedHost",
141141
new Object[] {selectedHostSpec != null ? selectedHostSpec.getHost() : "null"}));

wrapper/src/main/java/software/amazon/jdbc/util/HostSelectorUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package software.amazon.jdbc.util;
218

319
import java.util.List;

wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ HostSelector.noHostsMatchingRole=No hosts were found matching the requested ''{0
194194
HostSelector.roundRobinInvalidHostWeightPairs=The provided host weight pairs have not been configured correctly. Please ensure the provided host weight pairs is a comma separated list of pairs, each pair in the format of <host>:<weight>. Weight values must be an integer greater than or equal to the default weight value of 1.
195195
HostSelector.roundRobinInvalidDefaultWeight=The provided default weight value is not valid. Weight values must be an integer greater than or equal to the default weight value of 1.
196196
HostSelector.weightedRandomInvalidHostWeightPairs=The provided host weight pairs have not been configured correctly. Please ensure the provided host weight pairs is a comma separated list of pairs, each pair in the format of <host>:<weight>. Weight values must be an integer greater than or equal to the default weight value of 1.
197+
HostSelector.weightedRandomUnableToGetHost=Weighted Random strategy was unable to select a host.
197198

198199
IamAuthConnectionPlugin.unhandledException=Unhandled exception: ''{0}''
199200
IamAuthConnectionPlugin.connectException=Error occurred while opening a connection: ''{0}''

wrapper/src/test/java/software/amazon/jdbc/util/HostSelectorUtilsTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package software.amazon.jdbc.util;
218

319
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -34,7 +50,9 @@ void testSetHostWeightPairsProperty() {
3450
.build()
3551
);
3652
final Properties properties = new Properties();
37-
HostSelectorUtils.setHostWeightPairsProperty(RoundRobinHostSelector.ROUND_ROBIN_HOST_WEIGHT_PAIRS, properties, hosts);
53+
HostSelectorUtils.setHostWeightPairsProperty(
54+
RoundRobinHostSelector.ROUND_ROBIN_HOST_WEIGHT_PAIRS,
55+
properties, hosts);
3856

3957
final String actualPropertyValue = properties.getProperty(
4058
RoundRobinHostSelector.ROUND_ROBIN_HOST_WEIGHT_PAIRS.name);

0 commit comments

Comments
 (0)