Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with connection state transfer #418

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,7 @@ private boolean shouldReconnectToWriter(final Boolean readOnly) {
* @throws SQLException if an error occurs
*/
private void switchCurrentConnectionTo(final HostSpec host, final Connection connection) throws SQLException {
final Connection currentConnection = this.pluginService.getCurrentConnection();
if (currentConnection != connection) {
invalidateCurrentConnection();
}
Connection currentConnection = this.pluginService.getCurrentConnection();

final boolean readOnly;
if (isWriter(host)) {
Expand All @@ -517,7 +514,12 @@ private void switchCurrentConnectionTo(final HostSpec host, final Connection con
} else {
readOnly = false;
}
transferSessionState(currentConnection, connection, readOnly);

if (currentConnection != connection) {
transferSessionState(currentConnection, connection, readOnly);
invalidateCurrentConnection();
}

this.pluginService.setCurrentConnection(connection, host);

if (this.pluginManagerService != null) {
Expand Down Expand Up @@ -563,6 +565,8 @@ private <E extends Exception> void dealWithOriginalException(
if (this.lastExceptionDealtWith != originalException
&& shouldExceptionTriggerConnectionSwitch(originalException)) {
invalidateCurrentConnection();
this.pluginService.setAvailability(
this.pluginService.getCurrentHostSpec().getAliases(), HostAvailability.NOT_AVAILABLE);
try {
pickNewConnection();
} catch (final SQLException e) {
Expand Down Expand Up @@ -702,7 +706,6 @@ protected void invalidateCurrentConnection() {
return;
}

final HostSpec originalHost = this.pluginService.getCurrentHostSpec();
if (this.pluginService.isInTransaction()) {
isInTransaction = this.pluginService.isInTransaction();
try {
Expand All @@ -719,19 +722,6 @@ protected void invalidateCurrentConnection() {
} catch (final SQLException e) {
// swallow this exception, current connection should be useless anyway.
}

try {
this.pluginService.setCurrentConnection(
conn,
new HostSpec(
originalHost.getHost(),
originalHost.getPort(),
originalHost.getRole(),
HostAvailability.NOT_AVAILABLE));
this.pluginService.setAvailability(originalHost.getAliases(), HostAvailability.NOT_AVAILABLE);
} catch (final SQLException e) {
LOGGER.fine(() -> Messages.get("Failover.failedToUpdateCurrentHostspecAvailability"));
}
}

protected synchronized void pickNewConnection() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import software.amazon.jdbc.HostAvailability;
Expand Down Expand Up @@ -80,10 +78,9 @@ class FailoverConnectionPluginTest {
@Mock ClusterAwareWriterFailoverHandler mockWriterFailoverHandler;
@Mock ReaderFailoverResult mockReaderResult;
@Mock WriterFailoverResult mockWriterResult;
@Captor ArgumentCaptor<HostSpec> hostSpecArgumentCaptor;
@Mock JdbcCallable<ResultSet, SQLException> mockSqlFunction;

private Properties properties = new Properties();
private final Properties properties = new Properties();
private FailoverConnectionPlugin plugin;
private AutoCloseable closeable;

Expand Down Expand Up @@ -425,34 +422,26 @@ void test_invalidateCurrentConnection_inTransaction() throws SQLException {
when(mockHostSpec.getPort()).thenReturn(123);
when(mockHostSpec.getRole()).thenReturn(HostRole.READER);

final HostSpec expectedHostSpec = new HostSpec("host", 123, HostRole.READER, HostAvailability.NOT_AVAILABLE);

initializePlugin();
plugin.invalidateCurrentConnection();
verify(mockConnection).rollback();

// Assert SQL exceptions thrown during rollback do not get propagated.
doThrow(new SQLException()).when(mockConnection).rollback();
assertDoesNotThrow(() -> plugin.invalidateCurrentConnection());

verify(mockPluginService, times(2)).setCurrentConnection(eq(mockConnection), hostSpecArgumentCaptor.capture());
assertEquals(expectedHostSpec, hostSpecArgumentCaptor.getValue());
}

@Test
void test_invalidateCurrentConnection_notInTransaction() throws SQLException {
void test_invalidateCurrentConnection_notInTransaction() {
when(mockPluginService.isInTransaction()).thenReturn(false);
when(mockHostSpec.getHost()).thenReturn("host");
when(mockHostSpec.getPort()).thenReturn(123);
when(mockHostSpec.getRole()).thenReturn(HostRole.READER);
final HostSpec expectedHostSpec = new HostSpec("host", 123, HostRole.READER, HostAvailability.NOT_AVAILABLE);

initializePlugin();
plugin.invalidateCurrentConnection();

verify(mockPluginService).isInTransaction();
verify(mockPluginService).setCurrentConnection(eq(mockConnection), hostSpecArgumentCaptor.capture());
assertEquals(expectedHostSpec, hostSpecArgumentCaptor.getValue());
}

@Test
Expand All @@ -462,7 +451,6 @@ void test_invalidateCurrentConnection_withOpenConnection() throws SQLException {
when(mockHostSpec.getHost()).thenReturn("host");
when(mockHostSpec.getPort()).thenReturn(123);
when(mockHostSpec.getRole()).thenReturn(HostRole.READER);
final HostSpec expectedHostSpec = new HostSpec("host", 123, HostRole.READER, HostAvailability.NOT_AVAILABLE);

initializePlugin();
plugin.invalidateCurrentConnection();
Expand All @@ -472,8 +460,6 @@ void test_invalidateCurrentConnection_withOpenConnection() throws SQLException {

verify(mockConnection, times(2)).isClosed();
verify(mockConnection, times(2)).close();
verify(mockPluginService, times(2)).setCurrentConnection(eq(mockConnection), hostSpecArgumentCaptor.capture());
assertEquals(expectedHostSpec, hostSpecArgumentCaptor.getValue());
}

@Test
Expand Down