From 19c4d4bfed3c7991a95a2d98a30e9eebf7cc5110 Mon Sep 17 00:00:00 2001 From: sergiyvamz Date: Tue, 2 Jan 2024 16:44:10 -0800 Subject: [PATCH 1/2] transfer session state during failover --- .../java/software/amazon/jdbc/Driver.java | 4 +-- .../amazon/jdbc/DriverConnectionProvider.java | 3 +- .../failover/FailoverConnectionPlugin.java | 32 ++++++++++--------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/wrapper/src/main/java/software/amazon/jdbc/Driver.java b/wrapper/src/main/java/software/amazon/jdbc/Driver.java index 8b54fe1a2..76d1710ac 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/Driver.java +++ b/wrapper/src/main/java/software/amazon/jdbc/Driver.java @@ -29,8 +29,6 @@ import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Collectors; -import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import software.amazon.jdbc.profile.ConfigurationProfile; import software.amazon.jdbc.profile.DriverConfigurationProfiles; @@ -112,7 +110,7 @@ public Connection connect(final String url, final Properties info) throws SQLExc PropertyDefinition.DATABASE.set(props, databaseName); } - LOGGER.finest(() -> PropertyUtils.logProperties(props, "DEBUG: Connecting with properties: \n")); + LOGGER.finest(() -> PropertyUtils.logProperties(props, "Connecting with properties: \n")); final String profileName = PropertyDefinition.PROFILE_NAME.getString(props); ConfigurationProfile configurationProfile = null; diff --git a/wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java b/wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java index 52bcea141..063fe1cf5 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java +++ b/wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java @@ -113,8 +113,7 @@ public Connection connect( final @NonNull Properties props) throws SQLException { - LOGGER.finest(() -> "DEBUG: Connecting with properties:\n" - + PropertyUtils.logProperties(props, "")); + LOGGER.finest(() -> PropertyUtils.logProperties(props, "Connecting with properties: \n")); final Properties copy = PropertyUtils.copyProperties(props); dialect.prepareConnectProperties(copy, protocol, hostSpec); diff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java index 2461dc181..a92cdc496 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java +++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java @@ -482,13 +482,17 @@ private boolean canUpdateTopology(final String methodName) { /** * Connects this dynamic failover connection proxy to the host pointed out by the given host * index. + *

+ * The method assumes that current connection is not setup. If it's not true, a session state + * transfer from the current connection to a new one may be necessary. This should be handled by callee. * * @param host The host. * @throws SQLException if an error occurs */ private void connectTo(final HostSpec host) throws SQLException { try { - switchCurrentConnectionTo(host, createConnectionForHost(host)); + this.pluginService.setCurrentConnection(createConnectionForHost(host), host); + LOGGER.fine( () -> Messages.get( "Failover.establishedConnection", @@ -536,26 +540,20 @@ private boolean shouldAttemptReaderConnection() { } /** - * Replaces the previous underlying connection by the connection given. State from previous - * connection, if any, is synchronized with the new one. + * Synchronizes state from current connection, if any, is the new one. * - * @param host The host that matches the given connection. - * @param connection The connection instance to switch to. + * @param newHost The host that matches the given connection. + * @param newConnection The connection instance to switch to. * @throws SQLException if an error occurs */ - private void switchCurrentConnectionTo(final HostSpec host, final Connection connection) throws SQLException { + private void transferSessionStateToNewConnection(final HostSpec newHost, final Connection newConnection) + throws SQLException { + Connection currentConnection = this.pluginService.getCurrentConnection(); HostSpec currentHostSpec = this.pluginService.getCurrentHostSpec(); - if (currentConnection != connection) { - transferSessionState(currentConnection, currentHostSpec, connection, host); - invalidateCurrentConnection(); - } - - this.pluginService.setCurrentConnection(connection, host); - - if (this.pluginManagerService != null) { - this.pluginManagerService.setInTransaction(false); + if (currentConnection != newConnection) { + transferSessionState(currentConnection, currentHostSpec, newConnection, newHost); } } @@ -744,7 +742,9 @@ protected void failoverReader(final HostSpec failedHostSpec) throws SQLException if (keepSessionStateOnFailover) { restoreSessionState(result.getConnection()); + transferSessionStateToNewConnection(result.getHost(), result.getConnection()); } + this.invalidateCurrentConnection(); this.pluginService.setCurrentConnection(result.getConnection(), result.getHost()); this.pluginService.getCurrentHostSpec().removeAlias(oldAliases.toArray(new String[]{})); @@ -801,7 +801,9 @@ protected void failoverWriter() throws SQLException { final HostSpec writerHostSpec = getWriter(failoverResult.getTopology()); if (keepSessionStateOnFailover) { restoreSessionState(failoverResult.getNewConnection()); + transferSessionStateToNewConnection(writerHostSpec, failoverResult.getNewConnection()); } + this.invalidateCurrentConnection(); this.pluginService.setCurrentConnection(failoverResult.getNewConnection(), writerHostSpec); LOGGER.fine( From 22e60c800d64b4de170504b07163c92067b16198 Mon Sep 17 00:00:00 2001 From: sergiyvamz Date: Wed, 3 Jan 2024 12:05:09 -0800 Subject: [PATCH 2/2] code review; renaming --- .../jdbc/plugin/failover/FailoverConnectionPlugin.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java index a92cdc496..8d877de70 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java +++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/failover/FailoverConnectionPlugin.java @@ -542,18 +542,18 @@ private boolean shouldAttemptReaderConnection() { /** * Synchronizes state from current connection, if any, is the new one. * - * @param newHost The host that matches the given connection. + * @param newHostSpec The host that matches the given connection. * @param newConnection The connection instance to switch to. * @throws SQLException if an error occurs */ - private void transferSessionStateToNewConnection(final HostSpec newHost, final Connection newConnection) + private void transferSessionStateToNewConnection(final HostSpec newHostSpec, final Connection newConnection) throws SQLException { Connection currentConnection = this.pluginService.getCurrentConnection(); HostSpec currentHostSpec = this.pluginService.getCurrentHostSpec(); if (currentConnection != newConnection) { - transferSessionState(currentConnection, currentHostSpec, newConnection, newHost); + transferSessionState(currentConnection, currentHostSpec, newConnection, newHostSpec); } }