Skip to content

Commit ed3bbe7

Browse files
fix: limitless plugin - add null checks (#1152)
1 parent 7c5f391 commit ed3bbe7

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private Connection connectInternalWithDialect(
161161
try {
162162
conn = connectFunc.call();
163163
} catch (final SQLException e) {
164-
return retryConnectWithLeastLoadedRouters(limitlessRouters, props, conn, hostSpec);
164+
return retryConnectWithLeastLoadedRouters(limitlessRouters, props, null, hostSpec);
165165
}
166166
}
167167
return conn;
@@ -174,7 +174,7 @@ private Connection connectInternalWithDialect(
174174
HostRole.WRITER, RoundRobinHostSelector.STRATEGY_ROUND_ROBIN);
175175
LOGGER.fine(Messages.get(
176176
"LimitlessConnectionPlugin.selectedHost",
177-
new Object[] {selectedHostSpec.getHost()}));
177+
new Object[] {selectedHostSpec != null ? selectedHostSpec.getHost() : "null"}));
178178
} catch (SQLException e) {
179179
LOGGER.warning(Messages.get("LimitlessConnectionPlugin.errorSelectingRouter", new Object[] {e.getMessage()}));
180180
if (conn == null || conn.isClosed()) {
@@ -186,10 +186,12 @@ private Connection connectInternalWithDialect(
186186
try {
187187
return pluginService.connect(selectedHostSpec, props);
188188
} catch (SQLException e) {
189-
LOGGER.fine(Messages.get(
190-
"LimitlessConnectionPlugin.failedToConnectToHost",
191-
new Object[] {selectedHostSpec.getHost()}));
192-
selectedHostSpec.setAvailability(HostAvailability.NOT_AVAILABLE);
189+
if (selectedHostSpec != null) {
190+
LOGGER.fine(Messages.get(
191+
"LimitlessConnectionPlugin.failedToConnectToHost",
192+
new Object[] {selectedHostSpec.getHost()}));
193+
selectedHostSpec.setAvailability(HostAvailability.NOT_AVAILABLE);
194+
}
193195
if (conn == null || conn.isClosed()) {
194196
conn = connectFunc.call();
195197
}
@@ -238,21 +240,31 @@ private void initLimitlessRouterMonitorService() {
238240
}
239241
}
240242

241-
private Connection retryConnectWithLeastLoadedRouters(final List<HostSpec> limitlessRouters, final Properties props,
242-
final Connection conn, final HostSpec hostSpec) throws SQLException {
243+
private Connection retryConnectWithLeastLoadedRouters(
244+
final List<HostSpec> limitlessRouters,
245+
final Properties props,
246+
final Connection conn,
247+
final HostSpec hostSpec) throws SQLException {
243248

244249
List<HostSpec> currentRouters = limitlessRouters;
245250
int retryCount = 0;
246251
final int maxRetries = MAX_RETRIES.getInteger(props);
247252

248253
while (retryCount++ < maxRetries) {
249254
if (currentRouters.stream().noneMatch(h -> h.getAvailability().equals(HostAvailability.AVAILABLE))) {
250-
currentRouters = synchronouslyGetLimitlessRoutersWithRetry(conn, hostSpec.getPort(), props);
255+
if (conn != null && !conn.isClosed()) {
256+
currentRouters = synchronouslyGetLimitlessRoutersWithRetry(conn, hostSpec.getPort(), props);
257+
}
258+
251259
if (currentRouters == null
252260
|| currentRouters.isEmpty()
253261
|| currentRouters.stream().noneMatch(h -> h.getAvailability().equals(HostAvailability.AVAILABLE))) {
254262
LOGGER.warning(Messages.get("LimitlessConnectionPlugin.noRoutersAvailableForRetry"));
255-
return conn;
263+
if (conn != null && !conn.isClosed()) {
264+
return conn;
265+
} else {
266+
throw new SQLException(Messages.get("LimitlessConnectionPlugin.noRoutersAvailable"));
267+
}
256268
}
257269
}
258270

@@ -281,8 +293,12 @@ private Connection retryConnectWithLeastLoadedRouters(final List<HostSpec> limit
281293
new Object[] {selectedHostSpec.getHost()}));
282294
}
283295
}
284-
LOGGER.warning(Messages.get("LimitlessConnectionPlugin.maxRetriesExceeded"));
285-
return conn;
296+
if (conn != null && !conn.isClosed()) {
297+
LOGGER.warning(Messages.get("LimitlessConnectionPlugin.maxRetriesExceeded"));
298+
return conn;
299+
} else {
300+
throw new SQLException(Messages.get("LimitlessConnectionPlugin.maxRetriesExceeded"));
301+
}
286302
}
287303

288304
private List<HostSpec> synchronouslyGetLimitlessRoutersWithRetry(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void run() {
137137

138138
while (!this.stopped.get()) {
139139
TelemetryContext telemetryContext = telemetryFactory.openTelemetryContext(
140-
"node response time thread", TelemetryTraceLevel.TOP_LEVEL);
140+
"limitless router monitor thread", TelemetryTraceLevel.TOP_LEVEL);
141141
telemetryContext.setAttribute("url", hostSpec.getUrl());
142142
try {
143143
this.openConnection();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
@FunctionalInterface
2828
public interface LimitlessRouterMonitorInitializer {
2929
LimitlessRouterMonitor createLimitlessRouterMonitor(
30-
final PluginService pluginService,
3130
final HostSpec hostSpec,
3231
final SlidingExpirationCacheWithCleanupThread<String, List<HostSpec>> limitlessRouterCache,
3332
final String limitlessRouterCacheKey,

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ public class LimitlessRouterServiceImpl implements LimitlessRouterService {
6565
);
6666

6767
public LimitlessRouterServiceImpl(final @NonNull PluginService pluginService) {
68-
this(pluginService, LimitlessRouterMonitor::new, new LimitlessQueryHelper(pluginService));
68+
this(
69+
pluginService,
70+
(hostSpec,
71+
routerCache,
72+
routerCacheKey,
73+
props,
74+
intervalMs) ->
75+
new LimitlessRouterMonitor(
76+
pluginService,
77+
hostSpec,
78+
routerCache,
79+
routerCacheKey,
80+
props,
81+
intervalMs),
82+
new LimitlessQueryHelper(pluginService));
6983
}
7084

7185
public LimitlessRouterServiceImpl(
@@ -127,7 +141,6 @@ public void startMonitoring(final @NonNull HostSpec hostSpec,
127141
limitlessRouterMonitorKey,
128142
key -> this.limitlessRouterMonitorInitializer
129143
.createLimitlessRouterMonitor(
130-
pluginService,
131144
hostSpec,
132145
limitlessRouterCache,
133146
this.pluginService.getHostListProvider().getClusterId(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum RdsUrlType {
2323
RDS_CUSTOM_CLUSTER(true, true),
2424
RDS_PROXY(true, false),
2525
RDS_INSTANCE(true, false),
26-
RDS_AURORA_LIMITLESS_DB_SHARD_GROUP(true, true),
26+
RDS_AURORA_LIMITLESS_DB_SHARD_GROUP(true, false),
2727
OTHER(false, false);
2828

2929
private final boolean isRds;

wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ IamAuthConnectionPlugin.missingRequiredConfigParameter=Configuration parameter '
187187

188188
# Limitless Connection Plugin
189189
LimitlessConnectionPlugin.connectWithHost=Connecting to host {0}.
190-
LimitlessConnectionPlugin.usingProvidedConnectUrl=Connecting using provided connection URL.
190+
LimitlessConnectionPlugin.errorSelectingRouter=An error occurred while selecting Limitless Transaction Router: {0}
191191
LimitlessConnectionPlugin.failedToConnectToHost=Failed to connect to host {0}.
192192
LimitlessConnectionPlugin.incorrectConfiguration=Limitless Connection Plugin is unable to run. Please ensure the connection settings are correct.
193193
LimitlessConnectionPlugin.limitlessRouterCacheEmpty=Limitless Router cache is empty. This normal during application start up when the cache is not yet populated.
@@ -198,6 +198,7 @@ LimitlessConnectionPlugin.selectedHost=Host {0} has been selected.
198198
LimitlessConnectionPlugin.selectedHostForRetry=Host {0} has been selected for connection retry.
199199
LimitlessConnectionPlugin.synchronouslyGetLimitlessRouters=Fetching Limitless Routers synchronously.
200200
LimitlessConnectionPlugin.unsupportedDialectOrDatabase=Unsupported dialect ''{0}'' encountered. Please ensure JDBC connection parameters are correct, and refer to the documentation to ensure that the connecting database is compatible with the Limitless Connection Plugin.
201+
LimitlessConnectionPlugin.usingProvidedConnectUrl=Connecting using provided connection URL.
201202

202203
# Limitless Query Helper
203204
LimitlessQueryHelper.unsupportedDialectOrDatabase=Unsupported dialect ''{0}'' encountered. Please ensure JDBC connection parameters are correct, and refer to the documentation to ensure that the connecting database is compatible with the Limitless Connection Plugin.

wrapper/src/test/java/software/amazon/jdbc/plugin/limitless/LimitlessRouterServiceImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void testGetLimitlessRouters() throws SQLException {
8383

8484
final LimitlessRouterService limitlessRouterService = new LimitlessRouterServiceImpl(
8585
mockPluginService,
86-
(a, b, c, d, e, f) -> mockLimitlessRouterMonitor,
86+
(a, b, c, d, e) -> mockLimitlessRouterMonitor,
8787
mockLimitlessQueryHelper);
8888
limitlessRouterService.startMonitoring(hostSpec, props, intervalMs);
8989
final List<HostSpec> actualEndpointHostSpecList = limitlessRouterService.getLimitlessRouters(CLUSTER_ID, props);
@@ -112,7 +112,7 @@ void testForceGetLimitlessRoutersWithConn() throws SQLException {
112112

113113
final LimitlessRouterServiceImpl limitlessRouterService = new LimitlessRouterServiceImpl(
114114
mockPluginService,
115-
(a, b, c, d, e, f) -> mockLimitlessRouterMonitor,
115+
(a, b, c, d, e) -> mockLimitlessRouterMonitor,
116116
mockLimitlessQueryHelper);
117117

118118
final List<HostSpec> actualHostSpecList = limitlessRouterService

0 commit comments

Comments
 (0)