Skip to content

Commit

Permalink
add topology tests and change access to private topology queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonli-improving committed Feb 27, 2024
1 parent a449b4d commit 6bc6c44
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class AuroraMysqlDialect extends MysqlDialect {

private static final String TOPOLOGY_QUERY =
public static final String TOPOLOGY_QUERY =
"SELECT SERVER_ID, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END, "
+ "CPU, REPLICA_LAG_IN_MILLISECONDS, LAST_UPDATE_TIMESTAMP "
+ "FROM information_schema.replica_host_status "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class AuroraPgDialect extends PgDialect {

private static final String topologySql = "SELECT 1 FROM aurora_replica_status() LIMIT 1";

private static final String TOPOLOGY_QUERY =
public static final String TOPOLOGY_QUERY =
"SELECT SERVER_ID, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END, "
+ "CPU, COALESCE(REPLICA_LAG_IN_MSEC, 0), LAST_UPDATE_TIMESTAMP "
+ "FROM aurora_replica_status() "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class RdsMultiAzDbClusterMysqlDialect extends MysqlDialect {

private static final String TOPOLOGY_QUERY = "SELECT id, endpoint, port FROM mysql.rds_topology";
public static final String TOPOLOGY_QUERY = "SELECT id, endpoint, port FROM mysql.rds_topology";

private static final String TOPOLOGY_TABLE_EXIST_QUERY =
"SELECT 1 AS tmp FROM information_schema.tables WHERE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RdsMultiAzDbClusterPgDialect extends PgDialect {

private static MultiAzDbClusterPgExceptionHandler exceptionHandler;

private static final String TOPOLOGY_QUERY =
public static final String TOPOLOGY_QUERY =
"SELECT id, endpoint, port FROM rds_tools.show_topology('aws_jdbc_driver-" + DriverInfo.DRIVER_VERSION + "')";

private static final String WRITER_NODE_FUNC_EXIST_QUERY =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,39 @@
import integration.container.TestDriverProvider;
import integration.container.TestEnvironment;
import integration.container.condition.DisableOnTestFeature;
import integration.util.AuroraTestUtility;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.MethodOrderer.MethodName;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import software.amazon.awssdk.regions.Region;
import software.amazon.jdbc.dialect.AuroraMysqlDialect;
import software.amazon.jdbc.dialect.AuroraPgDialect;
import software.amazon.jdbc.dialect.RdsMultiAzDbClusterMysqlDialect;
import software.amazon.jdbc.dialect.RdsMultiAzDbClusterPgDialect;

@TestMethodOrder(MethodOrderer.MethodName.class)
@TestMethodOrder(MethodName.class)
@DisableOnTestFeature({
TestEnvironmentFeatures.PERFORMANCE,
TestEnvironmentFeatures.RUN_HIBERNATE_TESTS_ONLY,
TestEnvironmentFeatures.RUN_AUTOSCALING_TESTS_ONLY})
public class TopologyQueryTests {
private static final Logger LOGGER = Logger.getLogger(BasicConnectivityTests.class.getName());
private static final String CONNECTION_STRING = "jdbc:aws-wrapper:postgresql://atlas-postgres-2-instance-4.czygpppufgy4.us-east-2.rds.amazonaws.com:5432/test";
protected static final AuroraTestUtility auroraUtil =
new AuroraTestUtility(TestEnvironment.getCurrent().getInfo().getAuroraRegion());

@TestTemplate
@ExtendWith(TestDriverProvider.class)
public void testConnection(TestDriver testDriver) throws SQLException {
public void auroraTestTypes(TestDriver testDriver) throws SQLException {
LOGGER.info(testDriver.toString());

final Properties props = ConnectionStringHelper.getDefaultPropertiesWithNoPlugins();
Expand All @@ -80,15 +83,134 @@ public void testConnection(TestDriver testDriver) throws SQLException {
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getDefaultDbName());
LOGGER.finest("Connecting to " + url);

String query = null;
if (TestEnvironment.getCurrent().getCurrentDriver() == TestDriver.PG) {
query = AuroraPgDialect.TOPOLOGY_QUERY;
} else {
query = AuroraMysqlDialect.TOPOLOGY_QUERY;
}

final Connection conn = DriverManager.getConnection(url, props);
assertTrue(conn.isValid(5));
Statement stmt = conn.createStatement();
stmt.executeQuery(query);
ResultSet rs = stmt.getResultSet();
int cols = rs.getMetaData().getColumnCount();
List<String> columnTypes = new ArrayList<>();
List<String> expectedTypes = Arrays.asList(
"VARCHAR",
"BIGINT",
"DOUBLE",
"DOUBLE",
"DATETIME"
);
for (int i = 1; i <= cols; i++) {
columnTypes.add(rs.getMetaData().getColumnTypeName(i));
}
assertEquals(columnTypes, expectedTypes);
conn.close();
}

@TestTemplate
@ExtendWith(TestDriverProvider.class)
public void auroraTestTimestamp(TestDriver testDriver) throws SQLException, ParseException {
LOGGER.info(testDriver.toString());

final Properties props = ConnectionStringHelper.getDefaultPropertiesWithNoPlugins();
DriverHelper.setConnectTimeout(testDriver, props, 10, TimeUnit.SECONDS);
DriverHelper.setSocketTimeout(testDriver, props, 10, TimeUnit.SECONDS);

String url =
ConnectionStringHelper.getWrapperUrl(
testDriver,
TestEnvironment.getCurrent()
.getInfo()
.getDatabaseInfo()
.getInstances()
.get(0)
.getHost(),
TestEnvironment.getCurrent()
.getInfo()
.getDatabaseInfo()
.getInstances()
.get(0)
.getPort(),
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getDefaultDbName());
LOGGER.finest("Connecting to " + url);

String query = null;
if (TestEnvironment.getCurrent().getCurrentDriver() == TestDriver.PG) {
query = AuroraPgDialect.TOPOLOGY_QUERY;
} else {
query = AuroraMysqlDialect.TOPOLOGY_QUERY;
}

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");

final Connection conn = DriverManager.getConnection(url, props);
assertTrue(conn.isValid(5));
// List<String> res = auroraUtil.getAuroraInstanceIds();
Statement stmt = conn.createStatement();
stmt.executeQuery("select 1");
stmt.executeQuery(query);
ResultSet rs = stmt.getResultSet();
rs.next();
assertEquals(1, rs.getInt(1));

while (rs.next()) {
format.parse(rs.getString("LAST_UPDATE_TIMESTAMP"));
}

conn.close();
}

@TestTemplate
@ExtendWith(TestDriverProvider.class)
public void rdsTestTypes(TestDriver testDriver) throws SQLException {
LOGGER.info(testDriver.toString());

final Properties props = ConnectionStringHelper.getDefaultPropertiesWithNoPlugins();
DriverHelper.setConnectTimeout(testDriver, props, 10, TimeUnit.SECONDS);
DriverHelper.setSocketTimeout(testDriver, props, 10, TimeUnit.SECONDS);

String url =
ConnectionStringHelper.getWrapperUrl(
testDriver,
TestEnvironment.getCurrent()
.getInfo()
.getDatabaseInfo()
.getInstances()
.get(0)
.getHost(),
TestEnvironment.getCurrent()
.getInfo()
.getDatabaseInfo()
.getInstances()
.get(0)
.getPort(),
TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getDefaultDbName());
LOGGER.finest("Connecting to " + url);

String query = null;
if (TestEnvironment.getCurrent().getCurrentDriver() == TestDriver.PG) {
query = RdsMultiAzDbClusterPgDialect.TOPOLOGY_QUERY;
} else {
query = RdsMultiAzDbClusterMysqlDialect.TOPOLOGY_QUERY;
}

final Connection conn = DriverManager.getConnection(url, props);
assertTrue(conn.isValid(5));
Statement stmt = conn.createStatement();
stmt.executeQuery(query);
ResultSet rs = stmt.getResultSet();
int cols = rs.getMetaData().getColumnCount();
List<String> columnTypes = new ArrayList<>();
List<String> expectedTypes = Arrays.asList(
"VARCHAR",
"VARCHAR",
"BIGINT"
);
for (int i = 1; i <= cols; i++) {
columnTypes.add(rs.getMetaData().getColumnTypeName(i));
LOGGER.fine(rs.getMetaData().getColumnTypeName(i));
}
assertEquals(columnTypes, expectedTypes);
conn.close();
}
}

0 comments on commit 6bc6c44

Please sign in to comment.