Skip to content

Commit

Permalink
fix: query timeout kill query directed to incorrect instance (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
crystall-bitquill authored Oct 31, 2023
1 parent 31e578d commit 7591def
Show file tree
Hide file tree
Showing 16 changed files with 403 additions and 17 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,12 @@ public class AWSSecretsManagerPluginSample {
}
```
## Reader Cluster Connection Plugin
When connecting to an Amazon Aurora database using the reader endpoint, the endpoint will load balance connections between all the available Aurora Replicas. In situations where the AWS JDBC Driver for MySQL needs to create a new connection internally, the new connection may or may not be to the same instance the original connection was made to. This means any processes that require the same instance will result in errors. For example, setting query timeouts may result in errors due to the kill query being sent to the incorrect instance. In these cases, the Reader Cluster Connection Plugin can be used to ensure all new connections are made to the same reader.
The Reader Cluster Connection Plugin is not enabled by default and can be enabled by using the [`connectionPluginFactories`](#connection-plugin-manager-parameters).
## Extra Additions
### XML Entity Injection Fix
Expand Down Expand Up @@ -814,7 +820,6 @@ public class AWSSecretsManagerPluginSample2 {
```
## Getting Help and Opening Issues
If you encounter a bug with the AWS JDBC Driver for MySQL, we would like to hear about it. Please search the [existing issues](https://github.com/awslabs/aws-mysql-jdbc/issues) and see if others are also experiencing the issue before opening a new issue. When opening a new issue, we will need the version of AWS JDBC Driver for MySQL, Java language version, OS you’re using, and the MySQL database version you're running against. Please include a reproduction case for the issue when appropriate.
Expand Down
2 changes: 1 addition & 1 deletion src/main/core-impl/java/com/mysql/cj/NativeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import com.mysql.cj.exceptions.MysqlErrorNumbers;
import com.mysql.cj.exceptions.OperationCancelledException;
import com.mysql.cj.interceptors.QueryInterceptor;
import com.mysql.cj.jdbc.ha.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.log.Log;
import com.mysql.cj.protocol.ColumnDefinition;
import com.mysql.cj.protocol.NetworkResources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

package com.mysql.cj.jdbc.ha;

import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.mysql.cj.conf.HostInfo;
import com.mysql.cj.conf.PropertySet;
import com.mysql.cj.exceptions.CJException;
import com.mysql.cj.jdbc.ha.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.log.Log;
import com.mysql.cj.util.LRUCache;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public void openInitialConnection(ConnectionUrl connectionUrl) throws SQLExcepti
return;
}

HostInfo mainHostInfo = connectionUrl.getMainHost();
final HostInfo mainHostInfo = connectionUrl.getMainHost();
JdbcConnection connection = this.connectionProvider.connect(mainHostInfo);

this.currentConnectionProvider.setCurrentConnection(connection, mainHostInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0
* (GPLv2), as published by the Free Software Foundation, with the
* following additional permissions:
*
* This program is distributed with certain software that is licensed
* under separate terms, as designated in a particular file or component
* or in the license documentation. Without limiting your rights under
* the GPLv2, the authors of this program hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have included with the program.
*
* Without limiting the foregoing grant of rights under the GPLv2 and
* additional permission as to separately licensed software, this
* program is also subject to the Universal FOSS Exception, version 1.0,
* a copy of which can be found along with its FAQ at
* http://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* http://www.gnu.org/licenses/gpl-2.0.html.
*/

package com.mysql.cj.jdbc.ha.plugins;

import com.mysql.cj.conf.ConnectionUrl;
import com.mysql.cj.conf.HostInfo;
import com.mysql.cj.conf.PropertyKey;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.RdsUtils;
import com.mysql.cj.util.StringUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.Callable;

/**
* This connection plugin is used when connecting through reader cluster endpoints, where all new
* connections should be directed to the same reader instance rather than a random reader instance
* for each new connection.
*/
public class ReaderClusterConnectionPlugin implements IConnectionPlugin {
private static final String GET_INSTANCE_QUERY = "SELECT @@aurora_server_id";
protected IConnectionProvider connectionProvider;
private final ICurrentConnectionProvider currentConnectionProvider;
private final IConnectionPlugin nextPlugin;

public ReaderClusterConnectionPlugin(
ICurrentConnectionProvider currentConnectionProvider,
IConnectionPlugin nextPlugin) {

this(currentConnectionProvider,
nextPlugin,
new BasicConnectionProvider());
}

public ReaderClusterConnectionPlugin(
ICurrentConnectionProvider currentConnectionProvider,
IConnectionPlugin nextPlugin,
IConnectionProvider connectionProvider) {
if (connectionProvider == null) {
throw new IllegalArgumentException(NullArgumentMessage.getMessage("connectionProvider"));
}
if (currentConnectionProvider == null) {
throw new IllegalArgumentException(NullArgumentMessage.getMessage("currentConnectionProvider"));
}

this.currentConnectionProvider = currentConnectionProvider;
this.nextPlugin = nextPlugin;
this.connectionProvider = connectionProvider;
}

@Override
public Object execute(
Class<?> methodInvokeOn,
String methodName,
Callable<?> executeSqlFunc,
Object[] args)
throws Exception {
return this.nextPlugin.execute(methodInvokeOn, methodName, executeSqlFunc, args);
}

@Override
public void openInitialConnection(ConnectionUrl connectionUrl) throws SQLException {
this.nextPlugin.openInitialConnection(connectionUrl);

final JdbcConnection currentConnection = this.currentConnectionProvider.getCurrentConnection();
if (currentConnection != null) {
final HostInfo mainHostInfo = connectionUrl.getMainHost();
final RdsUtils rdsUtils = new RdsUtils();

if (rdsUtils.isReaderClusterDns(mainHostInfo.getHost())) {
final String connectedHostName = getCurrentlyConnectedInstance(currentConnection);
if (!StringUtils.isNullOrEmpty(connectedHostName)) {
final String pattern =
mainHostInfo.getHostProperties().get(PropertyKey.clusterInstanceHostPattern.getKeyName());
final String instanceEndpoint = !StringUtils.isNullOrEmpty(pattern) ? pattern :
rdsUtils.getRdsInstanceHostPattern(mainHostInfo.getHost());
final HostInfo instanceHostInfo =
ConnectionUtils.createInstanceHostWithProperties(
instanceEndpoint.replace("?", connectedHostName),
mainHostInfo);
final JdbcConnection hostConnection = this.connectionProvider.connect(instanceHostInfo);
this.currentConnectionProvider.setCurrentConnection(hostConnection, instanceHostInfo);
}
}
}
}

@Override
public void releaseResources() {
this.nextPlugin.releaseResources();
}

@Override
public void transactionBegun() {
this.nextPlugin.transactionBegun();
}

@Override
public void transactionCompleted() {
this.nextPlugin.transactionCompleted();
}

private String getCurrentlyConnectedInstance(JdbcConnection connection) throws SQLException {
try (final Statement statement = connection.createStatement()) {
final ResultSet rs = statement.executeQuery(GET_INSTANCE_QUERY);
if (rs.next()) {
return rs.getString(1);
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2.0
* (GPLv2), as published by the Free Software Foundation, with the
* following additional permissions:
*
* This program is distributed with certain software that is licensed
* under separate terms, as designated in a particular file or component
* or in the license documentation. Without limiting your rights under
* the GPLv2, the authors of this program hereby grant you an additional
* permission to link the program and your derivative works with the
* separately licensed software that they have included with the program.
*
* Without limiting the foregoing grant of rights under the GPLv2 and
* additional permission as to separately licensed software, this
* program is also subject to the Universal FOSS Exception, version 1.0,
* a copy of which can be found along with its FAQ at
* http://oss.oracle.com/licenses/universal-foss-exception.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License, version 2.0, for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* http://www.gnu.org/licenses/gpl-2.0.html.
*/

package com.mysql.cj.jdbc.ha.plugins;

import com.mysql.cj.conf.PropertySet;
import com.mysql.cj.log.Log;
import java.sql.SQLException;

public class ReaderClusterConnectionPluginFactory implements IConnectionPluginFactory {
@Override
public IConnectionPlugin getInstance(
ICurrentConnectionProvider currentConnectionProvider,
PropertySet propertySet,
IConnectionPlugin nextPlugin,
Log logger) throws SQLException {
return new ReaderClusterConnectionPlugin(currentConnectionProvider, nextPlugin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.mysql.cj.Messages;
import com.mysql.cj.conf.HostInfo;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.jdbc.ha.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.jdbc.ha.plugins.IConnectionProvider;
import com.mysql.cj.log.Log;
import com.mysql.cj.log.NullLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.mysql.cj.conf.HostInfo;
import com.mysql.cj.exceptions.CJCommunicationsException;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.jdbc.ha.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.jdbc.ha.plugins.IConnectionProvider;
import com.mysql.cj.log.Log;
import com.mysql.cj.log.NullLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import com.mysql.cj.jdbc.exceptions.SQLError;
import com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping;
import com.mysql.cj.jdbc.ha.ConnectionUtils;
import com.mysql.cj.jdbc.ha.util.ConnectionUtils;
import com.mysql.cj.jdbc.ha.plugins.BasicConnectionProvider;
import com.mysql.cj.jdbc.ha.plugins.IConnectionPlugin;
import com.mysql.cj.jdbc.ha.plugins.IConnectionProvider;
Expand All @@ -65,16 +65,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A {@link IConnectionPlugin} implementation that provides cluster-aware failover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* http://www.gnu.org/licenses/gpl-2.0.html.
*/

package com.mysql.cj.jdbc.ha;
package com.mysql.cj.jdbc.ha.util;

import com.mysql.cj.Messages;
import com.mysql.cj.conf.ConnectionUrl;
Expand Down Expand Up @@ -157,6 +157,38 @@ public static HostInfo createHostWithProperties(HostInfo baseHost, Map<String, S
propertiesCopy);
}

/**
* Create a copy of {@link HostInfo} object where all properties are the same but the hostInfo points to the provided
* instance.
*
* @param instance The instance name to replace the previous host.
* @param previousHostInfo The {@link HostInfo} object that all other information to add to the new {@link HostInfo}.
*
* @return A copy of {@link HostInfo} object where only the host has been changed.
*/
public static HostInfo createInstanceHostWithProperties(String instance, HostInfo previousHostInfo)
throws SQLException {
final Properties propertiesCopy = new Properties();
propertiesCopy.putAll(previousHostInfo.getHostProperties());
propertiesCopy.put(PropertyKey.USER.getKeyName(), previousHostInfo.getUser());
propertiesCopy.put(PropertyKey.PASSWORD.getKeyName(), previousHostInfo.getPassword());

final ConnectionUrl hostUrl = ConnectionUrl.getConnectionUrlInstance(
getUrlFromEndpoint(
instance,
previousHostInfo.getPort(),
propertiesCopy),
propertiesCopy);

return new HostInfo(
hostUrl,
instance,
previousHostInfo.getPort(),
previousHostInfo.getUser(),
previousHostInfo.getPassword(),
previousHostInfo.getHostProperties());
}

public static String getUrlFromEndpoint(String endpoint, int port, Properties props) throws SQLException {
final Properties propsCopy = new Properties();
propsCopy.putAll(props);
Expand All @@ -172,8 +204,10 @@ public static String getUrlFromEndpoint(String endpoint, int port, Properties pr
);

final String dbName = propsCopy.getProperty(PropertyKey.DBNAME.getKeyName());
boolean containsDbName = false;
if (!StringUtils.isNullOrEmpty(dbName)) {
urlBuilder.append("/").append(dbName);
containsDbName = true;
}
propsCopy.remove(PropertyKey.DBNAME.getKeyName());

Expand Down Expand Up @@ -202,7 +236,7 @@ public static String getUrlFromEndpoint(String endpoint, int port, Properties pr

if (queryBuilder.length() != 0) {
urlBuilder.append("?").append(queryBuilder);
} else {
} else if (!containsDbName) {
urlBuilder.append("/");
}

Expand Down
Loading

0 comments on commit 7591def

Please sign in to comment.