Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiyv-improving committed May 2, 2023
1 parent a6e57bb commit c2f12f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import software.amazon.jdbc.HostSpec;
import software.amazon.jdbc.JdbcCallable;
import software.amazon.jdbc.NodeChangeOptions;
Expand All @@ -36,9 +37,12 @@
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.SubscribedMethodHelper;
import software.amazon.jdbc.wrapper.ConnectionWrapper;

public class AuroraConnectionTrackerPlugin extends AbstractConnectionPlugin {

private static final Logger LOGGER = Logger.getLogger(AuroraConnectionTrackerPlugin.class.getName());

static final String METHOD_ABORT = "Connection.abort";
static final String METHOD_CLOSE = "Connection.close";
private static final Set<String> subscribedMethods =
Expand Down Expand Up @@ -143,7 +147,9 @@ public <T, E extends Exception> T execute(final Class<T> resultClass, final Clas
@Override
public void notifyNodeListChanged(final Map<String, EnumSet<NodeChangeOptions>> changes) {
for (final String node : changes.keySet()) {
LOGGER.finest(() -> "Node " + node + " has changed.");
if (isRoleChanged(changes.get(node))) {
LOGGER.finest(() -> "Node " + node + " has changed its role. Invalidating idle connections.");
tracker.invalidateAllConnections(node);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void invalidateConnections(final Queue<WeakReference<Connection>> connec
});
}

public void logOpenedConnections() {
public static void logOpenedConnections() {
LOGGER.finest(() -> {
final StringBuilder builder = new StringBuilder();
openedConnections.forEach((key, queue) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import software.amazon.jdbc.PropertyDefinition;
import software.amazon.jdbc.ds.AwsWrapperDataSource;
import software.amazon.jdbc.hostlistprovider.AuroraHostListProvider;
import software.amazon.jdbc.plugin.OpenedConnectionTracker;
import software.amazon.jdbc.plugin.failover.FailoverSQLException;
import software.amazon.jdbc.plugin.failover.FailoverSuccessSQLException;
import software.amazon.jdbc.util.SqlState;
Expand Down Expand Up @@ -324,12 +325,14 @@ public void testServerFailoverWithIdleConnections() throws SQLException, Interru

for (int i = 0; i < IDLE_CONNECTIONS_NUM; i++) {
// Keep references to 5 idle connections created using the cluster endpoints.
idleConnections.add(DriverManager.getConnection(
final Connection conn = DriverManager.getConnection(
ConnectionStringHelper.getWrapperUrl(
clusterEndpoint,
clusterEndpointPort,
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getDefaultDbName()),
props));
props);
LOGGER.finest(() -> "Idle connection: " + conn.toString());
idleConnections.add(conn);
}

// Connect to a writer instance.
Expand All @@ -340,6 +343,8 @@ public void testServerFailoverWithIdleConnections() throws SQLException, Interru
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getDefaultDbName()),
props)) {

LOGGER.finest(() -> "Connection: " + conn.toString());

final String instanceId = auroraUtil.queryInstanceId(
TestEnvironment.getCurrent().getInfo().getRequest().getDatabaseEngine(),
conn);
Expand All @@ -361,7 +366,9 @@ public void testServerFailoverWithIdleConnections() throws SQLException, Interru
conn));

// Sleep for a second to allow daemon threads to finish running.
Thread.sleep(1000);
Thread.sleep(10000);

OpenedConnectionTracker.logOpenedConnections();

// Ensure that all idle connections are closed.
for (Connection idleConnection : idleConnections) {
Expand Down

0 comments on commit c2f12f5

Please sign in to comment.