Search in sources :

Example 6 with ConnectionUrl

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);
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo) Test(org.junit.jupiter.api.Test)

Example 7 with ConnectionUrl

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);
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo) Test(org.junit.jupiter.api.Test)

Example 8 with ConnectionUrl

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());
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo) Test(org.junit.jupiter.api.Test)

Example 9 with ConnectionUrl

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);
}
Also used : HashMap(java.util.HashMap) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo)

Example 10 with ConnectionUrl

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());
}
Also used : ArrayList(java.util.ArrayList) ConnectionImpl(com.mysql.cj.jdbc.ConnectionImpl) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) HostInfo(com.mysql.cj.conf.HostInfo) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionUrl (com.mysql.cj.conf.ConnectionUrl)49 Test (org.junit.jupiter.api.Test)41 Properties (java.util.Properties)39 HostInfo (com.mysql.cj.conf.HostInfo)21 SQLException (java.sql.SQLException)10 ReplicationConnectionUrl (com.mysql.cj.conf.url.ReplicationConnectionUrl)9 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)9 ResultSet (java.sql.ResultSet)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 FailoverConnectionUrl (com.mysql.cj.conf.url.FailoverConnectionUrl)7 FailoverDnsSrvConnectionUrl (com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl)7 LoadBalanceConnectionUrl (com.mysql.cj.conf.url.LoadBalanceConnectionUrl)7 LoadBalanceDnsSrvConnectionUrl (com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl)7 ReplicationDnsSrvConnectionUrl (com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl)7 SingleConnectionUrl (com.mysql.cj.conf.url.SingleConnectionUrl)7 XDevApiConnectionUrl (com.mysql.cj.conf.url.XDevApiConnectionUrl)7 XDevApiDnsSrvConnectionUrl (com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl)7 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)6 Connection (java.sql.Connection)6