use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.
the class AuroraTopologyServiceTest method testCachedEntryRetrieved.
@Test
public void testCachedEntryRetrieved() throws SQLException {
final JdbcConnection mockConn = Mockito.mock(ConnectionImpl.class);
final Statement mockStatement = Mockito.mock(StatementImpl.class);
final ResultSet mockResultSet = Mockito.mock(ResultSetImpl.class);
stubTopologyQuery(mockConn, mockStatement, mockResultSet);
final String url = "jdbc:mysql:aws://my-cluster-name.cluster-XYZ.us-east-2.rds.amazonaws.com:1234/test";
final ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
final HostInfo mainHost = conStr.getMainHost();
final HostInfo clusterInstanceInfo = new HostInfo(conStr, "?.XYZ.us-east-2.rds.amazonaws.com", mainHost.getPort(), mainHost.getUser(), mainHost.getPassword(), mainHost.isPasswordless(), mainHost.getHostProperties());
spyProvider.setClusterInstanceTemplate(clusterInstanceInfo);
spyProvider.getTopology(mockConn, false);
spyProvider.getTopology(mockConn, false);
verify(spyProvider, times(1)).queryForTopology(mockConn);
}
use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.
the class AuroraTopologyServiceTest method testQueryFailureReturnsStaleTopology.
@Test
public void testQueryFailureReturnsStaleTopology() throws SQLException, InterruptedException {
final JdbcConnection mockConn = Mockito.mock(ConnectionImpl.class);
final Statement mockStatement = Mockito.mock(StatementImpl.class);
final ResultSet mockResultSet = Mockito.mock(ResultSetImpl.class);
stubTopologyQuery(mockConn, mockStatement, mockResultSet);
final String url = "jdbc:mysql:aws://my-cluster-name.cluster-XYZ.us-east-2.rds.amazonaws.com:1234/test";
final ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
final HostInfo mainHost = conStr.getMainHost();
final HostInfo clusterInstanceInfo = new HostInfo(conStr, "?.XYZ.us-east-2.rds.amazonaws.com", mainHost.getPort(), mainHost.getUser(), mainHost.getPassword(), mainHost.isPasswordless(), mainHost.getHostProperties());
spyProvider.setClusterInstanceTemplate(clusterInstanceInfo);
spyProvider.setRefreshRate(1);
final List<HostInfo> hosts = spyProvider.getTopology(mockConn, false);
when(mockConn.createStatement()).thenThrow(SQLSyntaxErrorException.class);
Thread.sleep(5);
final List<HostInfo> staleHosts = spyProvider.getTopology(mockConn, false);
verify(spyProvider, times(2)).queryForTopology(mockConn);
assertEquals(3, staleHosts.size());
assertEquals(hosts, staleHosts);
}
use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.
the class AuroraTopologyServiceTest method testTopologyQuery.
@Test
public void testTopologyQuery() throws SQLException {
final JdbcConnection mockConn = Mockito.mock(ConnectionImpl.class);
final Statement mockStatement = Mockito.mock(StatementImpl.class);
final ResultSet mockResultSet = Mockito.mock(ResultSetImpl.class);
stubTopologyQuery(mockConn, mockStatement, mockResultSet);
final String url = "jdbc:mysql:aws://my-cluster-name.cluster-XYZ.us-east-2.rds.amazonaws.com:1234/test";
final Properties mainHostProps = new Properties();
final ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, mainHostProps);
final HostInfo mainHost = conStr.getMainHost();
final HostInfo clusterInstanceInfo = new HostInfo(conStr, "?.XYZ.us-east-2.rds.amazonaws.com", mainHost.getPort(), mainHost.getUser(), mainHost.getPassword(), mainHost.isPasswordless(), mainHost.getHostProperties());
spyProvider.setClusterInstanceTemplate(clusterInstanceInfo);
final List<HostInfo> topology = spyProvider.getTopology(mockConn, false);
final HostInfo master = topology.get(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX);
final List<HostInfo> slaves = topology.subList(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX + 1, topology.size());
assertEquals("writer-instance.XYZ.us-east-2.rds.amazonaws.com", master.getHost());
assertEquals(1234, master.getPort());
assertEquals("", master.getUser());
assertEquals("", master.getPassword());
assertTrue(master.isPasswordless());
final Map<String, String> props = master.getHostProperties();
assertEquals("writer-instance", props.get(TopologyServicePropertyKeys.INSTANCE_NAME));
assertEquals(AuroraTopologyService.WRITER_SESSION_ID, props.get(TopologyServicePropertyKeys.SESSION_ID));
assertEquals("2020-09-15 17:51:53.0", props.get(TopologyServicePropertyKeys.LAST_UPDATED));
assertEquals("13.5", props.get(TopologyServicePropertyKeys.REPLICA_LAG));
assertFalse(spyProvider.isMultiWriterCluster());
assertEquals(3, topology.size());
assertEquals(2, slaves.size());
}
use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.
the class ClusterAwareTestUtils method createBasicHostInfo.
protected static HostInfo createBasicHostInfo(String instanceName, String db, String user, String password) {
final Map<String, String> properties = new HashMap<>();
properties.put(TopologyServicePropertyKeys.INSTANCE_NAME, instanceName);
String url = "jdbc:mysql:aws://" + instanceName + ".com:1234/";
db = (db == null) ? "" : db;
properties.put(PropertyKey.DBNAME.getKeyName(), db);
url += db;
final ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
return new HostInfo(conStr, instanceName, 1234, user, password, properties);
}
use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.
the class FailoverConnectionPluginTest method testForWriterReconnectWhenInvalidInitialWriterConnection.
@Test
public void testForWriterReconnectWhenInvalidInitialWriterConnection() throws SQLException {
final ConnectionImpl mockCachedWriterConn = mockConnection;
final ConnectionImpl mockActualWriterConn = mockConnection;
final String url = "jdbc:mysql:aws://my-cluster-name.cluster-XYZ.us-east-2.rds.amazonaws.com";
final String host = url.split(PREFIX)[1];
when(mockHostInfo.getDatabaseUrl()).thenReturn(url);
when(mockHostInfo.getHost()).thenReturn(host);
final HostInfo cachedWriterHost = ClusterAwareTestUtils.createBasicHostInfo("cached-writer-host", "test");
final HostInfo readerA_Host = ClusterAwareTestUtils.createBasicHostInfo("reader-a-host", "test");
final HostInfo readerB_Host = ClusterAwareTestUtils.createBasicHostInfo("reader-b-host", "test");
final List<HostInfo> cachedTopology = new ArrayList<>();
cachedTopology.add(cachedWriterHost);
cachedTopology.add(readerA_Host);
cachedTopology.add(readerB_Host);
final HostInfo actualWriterHost = ClusterAwareTestUtils.createBasicHostInfo("actual-writer-host", "test");
final HostInfo obsoleteWriterHost = ClusterAwareTestUtils.createBasicHostInfo("obsolete-writer-host", "test");
final List<HostInfo> actualTopology = new ArrayList<>();
actualTopology.add(actualWriterHost);
actualTopology.add(readerA_Host);
actualTopology.add(obsoleteWriterHost);
when(mockCurrentConnectionProvider.getCurrentConnection()).thenReturn(mockCachedWriterConn);
when(mockTopologyService.getCachedTopology()).thenReturn(cachedTopology);
when(mockTopologyService.getTopology(mockCachedWriterConn, false)).thenReturn(actualTopology);
when(mockTopologyService.getHostByName(mockCachedWriterConn)).thenReturn(obsoleteWriterHost);
when(mockConnectionProvider.connect(refEq(cachedWriterHost))).thenReturn(mockCachedWriterConn);
when(mockConnectionProvider.connect(refEq(actualWriterHost))).thenReturn(mockActualWriterConn);
final FailoverConnectionPlugin failoverPlugin = initFailoverPlugin();
final ConnectionUrl connectionUrl = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
failoverPlugin.openInitialConnection(connectionUrl);
assertEquals(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX, failoverPlugin.currentHostIndex);
assertEquals(actualWriterHost, failoverPlugin.hosts.get(failoverPlugin.currentHostIndex));
assertFalse(failoverPlugin.explicitlyReadOnly);
assertFalse(failoverPlugin.isCurrentConnectionReadOnly());
}
Aggregations