Skip to content

Commit a671d85

Browse files
authored
refactor: log topology and increase reconnect interval (#38)
* refactor: log topology and increase reconnect interval
1 parent 94dc4f2 commit a671d85

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

wrapper/src/main/java/com/amazon/awslabs/jdbc/hostlistprovider/AuroraHostListProvider.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,17 @@ public List<HostSpec> getTopology(final Connection conn, final boolean forceUpda
186186
|| refreshNeeded(clusterTopologyInfo)) {
187187

188188
final ClusterTopologyInfo latestTopologyInfo = queryForTopology(conn);
189-
189+
List<HostSpec> hosts;
190190
if (!latestTopologyInfo.hosts.isEmpty()) {
191191
clusterTopologyInfo = updateCache(clusterTopologyInfo, latestTopologyInfo);
192+
hosts = clusterTopologyInfo.hosts;
192193
} else {
193-
return (clusterTopologyInfo == null || forceUpdate)
194+
hosts = (clusterTopologyInfo == null || forceUpdate)
194195
? new ArrayList<>()
195196
: clusterTopologyInfo.hosts;
196197
}
198+
logTopology(hosts);
199+
return hosts;
197200
}
198201

199202
return clusterTopologyInfo.hosts;
@@ -536,6 +539,23 @@ private void validateHostPatternSetting(String hostPattern) {
536539
}
537540
}
538541

542+
private void logTopology(final List<HostSpec> topology) {
543+
if (!LOGGER.isLoggable(Level.FINER)) {
544+
return;
545+
}
546+
547+
StringBuilder msg = new StringBuilder();
548+
for (int i = 0; i < topology.size(); i++) {
549+
HostSpec hostInfo = topology.get(i);
550+
msg.append("\n [")
551+
.append(i)
552+
.append("]: ")
553+
.append(hostInfo == null ? "<null>" : hostInfo.getHost());
554+
}
555+
LOGGER.finer(
556+
Messages.get("Failover.topologyObtained", new Object[] {msg.toString()}));
557+
}
558+
539559
/**
540560
* Class that holds the topology and additional information about the topology.
541561
*/

wrapper/src/main/java/com/amazon/awslabs/jdbc/plugin/failover/ClusterAwareReaderFailoverHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private ReaderFailoverResult getConnectionFromHostGroup(List<HostTuple> hostGrou
294294
}
295295

296296
try {
297-
TimeUnit.MILLISECONDS.sleep(1);
297+
TimeUnit.SECONDS.sleep(1);
298298
} catch (InterruptedException e) {
299299
Thread.currentThread().interrupt();
300300
throw new SQLException(Messages.get("ClusterAwareReaderFailoverHandler.1"), "70100", e);

wrapper/src/main/java/com/amazon/awslabs/jdbc/plugin/failover/ClusterAwareWriterFailoverHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.concurrent.Executors;
3636
import java.util.concurrent.Future;
3737
import java.util.concurrent.TimeUnit;
38+
import java.util.logging.Level;
3839
import java.util.logging.Logger;
3940

4041
/**
@@ -343,7 +344,7 @@ private void connectToReader() throws InterruptedException {
343344
// ignore
344345
}
345346
LOGGER.fine(Messages.get("ClusterAwareWriterFailoverHandler.12"));
346-
TimeUnit.MILLISECONDS.sleep(1);
347+
TimeUnit.SECONDS.sleep(1);
347348
}
348349
}
349350

@@ -448,6 +449,10 @@ private void performFinalCleanup() {
448449
}
449450

450451
private void logTopology() {
452+
if (!LOGGER.isLoggable(Level.FINER)) {
453+
return;
454+
}
455+
451456
StringBuilder msg = new StringBuilder();
452457
for (int i = 0; i < this.currentTopology.size(); i++) {
453458
HostSpec hostInfo = this.currentTopology.get(i);

wrapper/src/main/java/com/amazon/awslabs/jdbc/plugin/failover/FailoverConnectionPlugin.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,6 @@ public <T, E extends Exception> T execute(
170170
return jdbcMethodFunc.call();
171171
}
172172

173-
try {
174-
final RdsUrlType type = this.getHostListProvider().getRdsUrlType();
175-
if (type.isRdsCluster()) {
176-
this.explicitlyReadOnly = (type == RdsUrlType.RDS_READER_CLUSTER);
177-
LOGGER.finer(
178-
Messages.get(
179-
"Failover.parameterValue",
180-
new Object[] {"explicitlyReadOnly", this.explicitlyReadOnly}));
181-
}
182-
} catch (SQLException ex) {
183-
throw wrapExceptionIfNeeded(exceptionClass, ex);
184-
}
185-
186173
if (this.isClosed && !allowedOnClosedConnection(methodName)) {
187174
try {
188175
invalidInvocationOnClosedConnection();
@@ -267,6 +254,15 @@ void initHostProvider(
267254
this.writerFailoverHandler = writerFailoverHandlerSupplier.apply(hostListProvider);
268255

269256
initHostProviderFunc.call();
257+
258+
final RdsUrlType type = this.getHostListProvider().getRdsUrlType();
259+
if (type.isRdsCluster()) {
260+
this.explicitlyReadOnly = (type == RdsUrlType.RDS_READER_CLUSTER);
261+
LOGGER.finer(
262+
Messages.get(
263+
"Failover.parameterValue",
264+
new Object[] {"explicitlyReadOnly", this.explicitlyReadOnly}));
265+
}
270266
}
271267

272268
private AuroraHostListProvider getHostListProvider() throws SQLException {
@@ -345,7 +341,7 @@ private void invalidInvocationOnClosedConnection() throws SQLException {
345341
pickNewConnection();
346342

347343
// "The active SQL connection has changed. Please re-configure session state if required."
348-
LOGGER.severe(Messages.get("Failover.connectionChangedError"));
344+
LOGGER.info(Messages.get("Failover.connectionChangedError"));
349345
throw new SQLException(
350346
Messages.get("Failover.connectionChangedError"),
351347
SqlState.COMMUNICATION_LINK_CHANGED.getState());
@@ -652,7 +648,7 @@ protected synchronized void failover(final HostSpec failedHost) throws SQLExcept
652648
// "Transaction resolution unknown. Please re-configure session state if required and try
653649
// restarting transaction."
654650
final String errorMessage = Messages.get("Failover.transactionResolutionUnknownError");
655-
LOGGER.severe(errorMessage);
651+
LOGGER.info(errorMessage);
656652
throw new SQLException(errorMessage, SqlState.CONNECTION_FAILURE_DURING_TRANSACTION.getState());
657653
} else {
658654
// "The active SQL connection has changed due to a connection failure. Please re-configure

wrapper/src/test/java/com/amazon/awslabs/jdbc/plugin/failover/FailoverConnectionPluginTest.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void init() throws SQLException {
9898
closeable = MockitoAnnotations.openMocks(this);
9999

100100
when(mockPluginService.getHostListProvider()).thenReturn(mockHostListProvider);
101+
when(mockHostListProvider.getRdsUrlType()).thenReturn(RdsUrlType.RDS_WRITER_CLUSTER);
101102
when(mockPluginService.getCurrentConnection()).thenReturn(mockConnection);
102103
when(mockPluginService.getCurrentHostSpec()).thenReturn(mockHostSpec);
103104
when(mockPluginService.connect(any(HostSpec.class), eq(properties))).thenReturn(mockConnection);
@@ -523,20 +524,6 @@ void test_execute_withDirectExecute() throws SQLException {
523524
verify(mockHostListProvider, never()).getRdsUrlType();
524525
}
525526

526-
@Test
527-
void test_execute_withInvalidHostListProvider() {
528-
when(mockPluginService.getHostListProvider()).thenReturn(new FooHostListProvider());
529-
530-
initializePlugin();
531-
assertThrows(SQLException.class, () -> plugin.execute(
532-
ResultSet.class,
533-
SQLException.class,
534-
MONITOR_METHOD_INVOKE_ON,
535-
MONITOR_METHOD_NAME,
536-
mockSqlFunction,
537-
EMPTY_ARGS));
538-
}
539-
540527
private void initializePlugin() {
541528
plugin = new FailoverConnectionPlugin(mockPluginService, properties);
542529
}

0 commit comments

Comments
 (0)