Search in sources :

Example 26 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AuroraTopologyServiceTest method testProviderTopologyNotExpired.

@Test
public void testProviderTopologyNotExpired() 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);
    // 10 sec
    AuroraTopologyService.setExpireTime(10000);
    // 1 sec
    spyProvider.setRefreshRate(1000);
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(1)).queryForTopology(mockConn);
    Thread.sleep(2000);
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(2)).queryForTopology(mockConn);
    Thread.sleep(2000);
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(3)).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 27 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AuroraTopologyServiceTest method testClearProviderCache.

@Test
public void testClearProviderCache() 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.addToDownHostList(clusterInstanceInfo);
    assertEquals(1, AuroraTopologyService.topologyCache.size());
    spyProvider.clearAll();
    assertEquals(0, AuroraTopologyService.topologyCache.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 28 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AuroraTopologyServiceTest method testProviderTopologyExpires.

@Test
public void testProviderTopologyExpires() 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);
    // 1 sec
    AuroraTopologyService.setExpireTime(1000);
    spyProvider.setRefreshRate(// 10 sec; and cache expiration time is also (indirectly) changed to 10 sec
    10000);
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(1)).queryForTopology(mockConn);
    Thread.sleep(3000);
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(1)).queryForTopology(mockConn);
    Thread.sleep(3000);
    // internal cache has NOT expired yet
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(1)).queryForTopology(mockConn);
    Thread.sleep(5000);
    // internal cache has expired by now
    spyProvider.getTopology(mockConn, false);
    verify(spyProvider, times(2)).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 29 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AuroraTopologyServiceTest method testTopologyQuery_MultiWriter.

@Test
public void testTopologyQuery_MultiWriter() throws SQLException {
    final JdbcConnection mockConn = Mockito.mock(ConnectionImpl.class);
    final Statement mockStatement = Mockito.mock(StatementImpl.class);
    final ResultSet mockResultSet = Mockito.mock(ResultSetImpl.class);
    stubTopologyQueryMultiWriter(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);
    final List<HostInfo> topology = spyProvider.getTopology(mockConn, false);
    final List<HostInfo> readers = topology.subList(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX + 1, topology.size());
    assertTrue(spyProvider.isMultiWriterCluster());
    assertEquals(3, topology.size());
    assertEquals(2, readers.size());
    final HostInfo master1 = topology.get(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX);
    final HostInfo master2 = topology.get(1);
    final HostInfo master3 = topology.get(2);
    assertEquals("writer-instance-1.XYZ.us-east-2.rds.amazonaws.com", master1.getHost());
    assertEquals(1234, master1.getPort());
    assertEquals("", master1.getUser());
    assertEquals("", master1.getPassword());
    assertTrue(master1.isPasswordless());
    assertEquals("writer-instance-2.XYZ.us-east-2.rds.amazonaws.com", master2.getHost());
    assertEquals(1234, master2.getPort());
    assertEquals("", master2.getUser());
    assertEquals("", master2.getPassword());
    assertTrue(master2.isPasswordless());
    assertEquals("writer-instance-3.XYZ.us-east-2.rds.amazonaws.com", master3.getHost());
    assertEquals(1234, master3.getPort());
    assertEquals("", master3.getUser());
    assertEquals("", master3.getPassword());
    assertTrue(master3.isPasswordless());
    Map<String, String> props1 = master1.getHostProperties();
    assertEquals("writer-instance-1", props1.get(TopologyServicePropertyKeys.INSTANCE_NAME));
    assertEquals(AuroraTopologyService.WRITER_SESSION_ID, props1.get(TopologyServicePropertyKeys.SESSION_ID));
    assertEquals("2020-09-15 17:51:53.0", props1.get(TopologyServicePropertyKeys.LAST_UPDATED));
    assertEquals("13.5", props1.get(TopologyServicePropertyKeys.REPLICA_LAG));
    Map<String, String> props2 = master2.getHostProperties();
    assertEquals("writer-instance-2", props2.get(TopologyServicePropertyKeys.INSTANCE_NAME));
    assertEquals(AuroraTopologyService.WRITER_SESSION_ID, props2.get(TopologyServicePropertyKeys.SESSION_ID));
    assertEquals("2020-09-15 17:51:53.0", props2.get(TopologyServicePropertyKeys.LAST_UPDATED));
    assertEquals("13.5", props2.get(TopologyServicePropertyKeys.REPLICA_LAG));
    Map<String, String> props3 = master3.getHostProperties();
    assertEquals("writer-instance-3", props3.get(TopologyServicePropertyKeys.INSTANCE_NAME));
    assertEquals(AuroraTopologyService.WRITER_SESSION_ID, props3.get(TopologyServicePropertyKeys.SESSION_ID));
    assertEquals("2020-09-15 17:51:53.0", props3.get(TopologyServicePropertyKeys.LAST_UPDATED));
    assertEquals("13.5", props3.get(TopologyServicePropertyKeys.REPLICA_LAG));
}
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 30 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class FailoverConnectionPluginTest method testForWriterReconnectWhenDirectReaderConnectionFails.

@Test
public void testForWriterReconnectWhenDirectReaderConnectionFails() throws SQLException {
    // Although the user specified an instance that happened to be a reader, they have not
    // explicitly specified that they want a reader.
    // It is possible that they don't know the reader/writer status of this instance, so we cannot
    // assume they want a read-only connection.
    // As a result, if this direct connection fails, we should reconnect to the writer.
    final ConnectionImpl mockDirectReaderConn = mockConnection;
    final ConnectionImpl mockWriterConn = mockConnection;
    final String url = "jdbc:mysql:aws://reader-b-host.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 writerHost = ClusterAwareTestUtils.createBasicHostInfo("writer-host", null);
    final HostInfo readerAHost = ClusterAwareTestUtils.createBasicHostInfo("reader-a-host", null);
    final HostInfo readerBHost = ClusterAwareTestUtils.createBasicHostInfo("reader-b-host", null);
    final List<HostInfo> topology = new ArrayList<>();
    topology.add(writerHost);
    topology.add(readerAHost);
    topology.add(readerBHost);
    when(mockCurrentConnectionProvider.getCurrentConnection()).thenReturn(mockDirectReaderConn);
    when(mockTopologyService.getTopology(mockDirectReaderConn, false)).thenReturn(topology);
    when(mockTopologyService.getHostByName(mockDirectReaderConn)).thenReturn(null);
    when(mockConnectionProvider.connect(mockHostInfo)).thenReturn(mockDirectReaderConn);
    when(mockConnectionProvider.connect(refEq(writerHost))).thenReturn(mockWriterConn);
    final FailoverConnectionPlugin failoverPlugin = initFailoverPlugin();
    final ConnectionUrl connectionUrl = ConnectionUrl.getConnectionUrlInstance(url, new Properties());
    failoverPlugin.openInitialConnection(connectionUrl);
    assertEquals(FailoverConnectionPlugin.WRITER_CONNECTION_INDEX, failoverPlugin.currentHostIndex);
    assertNull(failoverPlugin.explicitlyReadOnly);
    assertFalse(failoverPlugin.isCurrentConnectionReadOnly());
    assertTrue(failoverPlugin.isFailoverEnabled());
}
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